Skip to content

Commit

Permalink
fix(container): incorrect parsing of image ID with port in hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Jun 13, 2019
1 parent b49ecc3 commit 78e03b7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
10 changes: 8 additions & 2 deletions garden-service/src/plugins/container/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { pathExists } from "fs-extra"
import { join } from "path"
import * as semver from "semver"
import { ConfigurationError, RuntimeError } from "../../exceptions"
import { splitFirst, spawn } from "../../util/util"
import { splitFirst, spawn, splitLast } from "../../util/util"
import { ModuleConfig } from "../../config/module"
import { ContainerModule, ContainerRegistryConfig, defaultTag, defaultNamespace, ContainerModuleConfig } from "./config"

Expand Down Expand Up @@ -127,7 +127,13 @@ const helpers = {
},

parseImageId(imageId: string): ParsedImageId {
let [name, tag] = imageId.split(":")
let [name, tag] = splitLast(imageId, ":")

if (name === "") {
name = tag
tag = defaultTag
}

const parts = name.length > 0 ? name.split("/") : []

if (!tag) {
Expand Down
9 changes: 9 additions & 0 deletions garden-service/test/unit/src/plugins/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,15 @@ describe("plugins.container", () => {
})
})

it("should correctly parse an id with a host with a port, and namespace", () => {
expect(helpers.parseImageId("localhost:5000/namespace/image:tag")).to.eql({
host: "localhost:5000",
namespace: "namespace",
repository: "image",
tag: "tag",
})
})

it("should correctly parse an id with a host and multi-level namespace", () => {
expect(helpers.parseImageId("my-host.com/a/b/c/d/image:tag")).to.eql({
host: "my-host.com",
Expand Down
22 changes: 22 additions & 0 deletions garden-service/test/unit/src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {
getEnvVarName,
deepOmitUndefined,
deepFilter,
splitLast,
} from "../../../../src/util/util"
import { expectError } from "../../../helpers"
import { splitFirst } from "../../../../src/util/util"

describe("util", () => {
describe("getEnvVarName", () => {
Expand Down Expand Up @@ -118,4 +120,24 @@ describe("util", () => {
expect(deepOmitUndefined(obj)).to.eql({ a: 1, b: 2, c: [{ d: 3 }] })
})
})

describe("splitFirst", () => {
it("should split string on first occurrence of given delimiter", () => {
expect(splitFirst("foo:bar:boo", ":")).to.eql(["foo", "bar:boo"])
})

it("should return the whole string as first element when no delimiter is found in string", () => {
expect(splitFirst("foo", ":")).to.eql(["foo", ""])
})
})

describe("splitLast", () => {
it("should split string on last occurrence of given delimiter", () => {
expect(splitLast("foo:bar:boo", ":")).to.eql(["foo:bar", "boo"])
})

it("should return the whole string as last element when no delimiter is found in string", () => {
expect(splitLast("foo", ":")).to.eql(["", "foo"])
})
})
})

0 comments on commit 78e03b7

Please sign in to comment.