|
| 1 | +/* |
| 2 | +Copyright 2019 The Skaffold Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1" |
| 23 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" |
| 24 | + "github.com/GoogleContainerTools/skaffold/testutil" |
| 25 | +) |
| 26 | + |
| 27 | +func TestCollectHelmReleasesNamespaces(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + description string |
| 30 | + helmReleases []latestV1.HelmRelease |
| 31 | + env []string |
| 32 | + expected []string |
| 33 | + shouldErr bool |
| 34 | + }{ |
| 35 | + { |
| 36 | + description: "namspaces are collected correctly", |
| 37 | + helmReleases: []latestV1.HelmRelease{ |
| 38 | + { |
| 39 | + Namespace: "foo", |
| 40 | + }, |
| 41 | + { |
| 42 | + Namespace: "bar", |
| 43 | + }, |
| 44 | + { |
| 45 | + Namespace: "baz", |
| 46 | + }, |
| 47 | + }, |
| 48 | + expected: []string{"foo", "bar", "baz"}, |
| 49 | + }, |
| 50 | + { |
| 51 | + description: "namespaces are collected correctly with env expansion", |
| 52 | + helmReleases: []latestV1.HelmRelease{ |
| 53 | + { |
| 54 | + Namespace: "{{.FOO}}", |
| 55 | + }, |
| 56 | + { |
| 57 | + Namespace: "bar", |
| 58 | + }, |
| 59 | + { |
| 60 | + Namespace: "baz", |
| 61 | + }, |
| 62 | + }, |
| 63 | + env: []string{"FOO=foo"}, |
| 64 | + expected: []string{"foo", "bar", "baz"}, |
| 65 | + }, |
| 66 | + { |
| 67 | + description: "should error when template expansion fails", |
| 68 | + helmReleases: []latestV1.HelmRelease{ |
| 69 | + { |
| 70 | + Namespace: "{{.DOESNT_EXIST_AND_SHOULD_ERROR_AS_SUCH}}", |
| 71 | + }, |
| 72 | + }, |
| 73 | + shouldErr: true, |
| 74 | + }, |
| 75 | + } |
| 76 | + for _, test := range tests { |
| 77 | + testutil.Run(t, test.description, func(t *testutil.T) { |
| 78 | + t.Override(&util.OSEnviron, func() []string { return test.env }) |
| 79 | + ns, err := collectHelmReleasesNamespaces([]latestV1.Pipeline{ |
| 80 | + { |
| 81 | + Deploy: latestV1.DeployConfig{ |
| 82 | + DeployType: latestV1.DeployType{ |
| 83 | + HelmDeploy: &latestV1.HelmDeploy{ |
| 84 | + Releases: test.helmReleases, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }) |
| 90 | + t.CheckError(test.shouldErr, err) |
| 91 | + if !test.shouldErr { |
| 92 | + t.CheckDeepEqual(test.expected, ns) |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
0 commit comments