Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test regexp usage in profiles activation #2417

Merged
merged 2 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pkg/skaffold/schema/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,14 @@ func satisfies(expected, actual string) bool {
}

func matches(expected, actual string) bool {
matcher, err := re.Compile(expected)
if actual == expected {
return true
}

matcher, err := re.Compile(expected)
if err != nil {
logrus.Infof("profile activation criteria '%s' is not a valid regexp, falling back to string", expected)
return actual == expected
return false
}

return matcher.MatchString(actual)
Expand Down
60 changes: 33 additions & 27 deletions pkg/skaffold/schema/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,23 @@ profiles:
dockerfile: Dockerfile.second
`

tmp, cleanup := testutil.NewTempDir(t)
defer cleanup()
testutil.Run(t, "", func(t *testutil.T) {
tmpDir := t.NewTempDir().
Write("skaffold.yaml", addVersion(config))

yaml := fmt.Sprintf("apiVersion: %s\nkind: Config\n%s", latest.Version, config)
tmp.Write("skaffold.yaml", yaml)
parsed, err := ParseConfig(tmpDir.Path("skaffold.yaml"), false)
t.CheckNoError(err)

parsed, err := ParseConfig(tmp.Path("skaffold.yaml"), false)
testutil.CheckError(t, false, err)
skaffoldConfig := parsed.(*latest.SkaffoldConfig)
err = ApplyProfiles(skaffoldConfig, &cfg.SkaffoldOptions{
Profiles: []string{"patches"},
})

skaffoldConfig := parsed.(*latest.SkaffoldConfig)
err = ApplyProfiles(skaffoldConfig, &cfg.SkaffoldOptions{
Profiles: []string{"patches"},
t.CheckNoError(err)
t.CheckDeepEqual("replacement", skaffoldConfig.Build.Artifacts[0].ImageName)
t.CheckDeepEqual("Dockerfile.DEV", skaffoldConfig.Build.Artifacts[0].DockerArtifact.DockerfilePath)
t.CheckDeepEqual("Dockerfile.second", skaffoldConfig.Build.Artifacts[1].DockerArtifact.DockerfilePath)
})
testutil.CheckError(t, false, err)

testutil.CheckDeepEqual(t, "replacement", skaffoldConfig.Build.Artifacts[0].ImageName)
testutil.CheckDeepEqual(t, "Dockerfile.DEV", skaffoldConfig.Build.Artifacts[0].DockerArtifact.DockerfilePath)
testutil.CheckDeepEqual(t, "Dockerfile.second", skaffoldConfig.Build.Artifacts[1].DockerArtifact.DockerfilePath)
}

func TestApplyInvalidPatch(t *testing.T) {
Expand All @@ -80,21 +79,20 @@ profiles:
value: replacement
`

tmp, cleanup := testutil.NewTempDir(t)
defer cleanup()
testutil.Run(t, "", func(t *testutil.T) {
tmp := t.NewTempDir().
Write("skaffold.yaml", addVersion(config))

yaml := fmt.Sprintf("apiVersion: %s\nkind: Config\n%s", latest.Version, config)
tmp.Write("skaffold.yaml", yaml)
parsed, err := ParseConfig(tmp.Path("skaffold.yaml"), false)
t.CheckNoError(err)

parsed, err := ParseConfig(tmp.Path("skaffold.yaml"), false)
testutil.CheckError(t, false, err)
skaffoldConfig := parsed.(*latest.SkaffoldConfig)
err = ApplyProfiles(skaffoldConfig, &cfg.SkaffoldOptions{
Profiles: []string{"patches"},
})

skaffoldConfig := parsed.(*latest.SkaffoldConfig)
err = ApplyProfiles(skaffoldConfig, &cfg.SkaffoldOptions{
Profiles: []string{"patches"},
t.CheckErrorAndDeepEqual(true, err, "applying profile patches: invalid path: /build/artifacts/0/image/", err.Error())
})

testutil.CheckErrorAndDeepEqual(t, true, err, "applying profile patches: invalid path: /build/artifacts/0/image/", err.Error())
}

func TestApplyProfiles(t *testing.T) {
Expand Down Expand Up @@ -350,17 +348,20 @@ func TestActivatedProfiles(t *testing.T) {
{Name: "run-profile", Activation: []latest.Activation{{Command: "run"}}},
{Name: "dev-profile", Activation: []latest.Activation{{Command: "dev"}}},
{Name: "non-run-profile", Activation: []latest.Activation{{Command: "!run"}}},
{Name: "run-or-dev-profile", Activation: []latest.Activation{{Command: "(run)|(dev)"}}},
{Name: "other-profile", Activation: []latest.Activation{{Command: "!(run)|(dev)"}}},
},
expected: []string{"dev-profile", "non-run-profile"},
expected: []string{"dev-profile", "non-run-profile", "run-or-dev-profile"},
}, {
description: "Auto-activated by env variable",
opts: &cfg.SkaffoldOptions{},
profiles: []latest.Profile{
{Name: "activated", Activation: []latest.Activation{{Env: "KEY=VALUE"}}},
{Name: "not-activated", Activation: []latest.Activation{{Env: "KEY=OTHER"}}},
{Name: "also-activated", Activation: []latest.Activation{{Env: "KEY=!OTHER"}}},
{Name: "regex-activated", Activation: []latest.Activation{{Env: "KEY=V.*E"}}},
},
expected: []string{"activated", "also-activated"},
expected: []string{"activated", "also-activated", "regex-activated"},
}, {
description: "Invalid env variable",
opts: &cfg.SkaffoldOptions{},
Expand All @@ -377,6 +378,7 @@ func TestActivatedProfiles(t *testing.T) {
{Name: "also-activated", Activation: []latest.Activation{{KubeContext: "!dev-context"}}},
{Name: "activated-regexp", Activation: []latest.Activation{{KubeContext: "prod-.*"}}},
{Name: "not-activated-regexp", Activation: []latest.Activation{{KubeContext: "dev-.*"}}},
{Name: "invalid-regexp", Activation: []latest.Activation{{KubeContext: `\`}}},
},
expected: []string{"activated", "also-activated", "activated-regexp"},
}, {
Expand Down Expand Up @@ -435,3 +437,7 @@ func str(value string) *interface{} {
var v interface{} = value
return &v
}

func addVersion(yaml string) string {
return fmt.Sprintf("apiVersion: %s\nkind: Config\n%s", latest.Version, yaml)
}