|
| 1 | +/* |
| 2 | +Copyright 2021 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 hooks |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" |
| 23 | + "github.com/GoogleContainerTools/skaffold/testutil" |
| 24 | +) |
| 25 | + |
| 26 | +func TestSetupStaticEnvOptions(t *testing.T) { |
| 27 | + defer func() { |
| 28 | + staticEnvOpts = StaticEnvOpts{} |
| 29 | + }() |
| 30 | + |
| 31 | + cfg := mockCfg{ |
| 32 | + defaultRepo: util.StringPtr("gcr.io/foo"), |
| 33 | + workDir: ".", |
| 34 | + rpcPort: 8080, |
| 35 | + httpPort: 8081, |
| 36 | + } |
| 37 | + SetupStaticEnvOptions(cfg) |
| 38 | + testutil.CheckDeepEqual(t, cfg.defaultRepo, staticEnvOpts.DefaultRepo) |
| 39 | + testutil.CheckDeepEqual(t, cfg.workDir, staticEnvOpts.WorkDir) |
| 40 | + testutil.CheckDeepEqual(t, cfg.rpcPort, staticEnvOpts.RPCPort) |
| 41 | + testutil.CheckDeepEqual(t, cfg.httpPort, staticEnvOpts.HTTPPort) |
| 42 | +} |
| 43 | + |
| 44 | +func TestGetEnv(t *testing.T) { |
| 45 | + tests := []struct { |
| 46 | + description string |
| 47 | + input interface{} |
| 48 | + expected []string |
| 49 | + }{ |
| 50 | + { |
| 51 | + description: "static env opts, all defined", |
| 52 | + input: StaticEnvOpts{ |
| 53 | + DefaultRepo: util.StringPtr("gcr.io/foo"), |
| 54 | + RPCPort: 8080, |
| 55 | + HTTPPort: 8081, |
| 56 | + WorkDir: "./foo", |
| 57 | + }, |
| 58 | + expected: []string{ |
| 59 | + "SKAFFOLD_DEFAULT_REPO=gcr.io/foo", |
| 60 | + "SKAFFOLD_RPC_PORT=8080", |
| 61 | + "SKAFFOLD_HTTP_PORT=8081", |
| 62 | + "SKAFFOLD_WORK_DIR=./foo", |
| 63 | + }, |
| 64 | + }, |
| 65 | + { |
| 66 | + description: "static env opts, some missing", |
| 67 | + input: StaticEnvOpts{ |
| 68 | + RPCPort: 8080, |
| 69 | + HTTPPort: 8081, |
| 70 | + WorkDir: "./foo", |
| 71 | + }, |
| 72 | + expected: []string{ |
| 73 | + "SKAFFOLD_RPC_PORT=8080", |
| 74 | + "SKAFFOLD_HTTP_PORT=8081", |
| 75 | + "SKAFFOLD_WORK_DIR=./foo", |
| 76 | + }, |
| 77 | + }, |
| 78 | + { |
| 79 | + description: "build env opts", |
| 80 | + input: BuildEnvOpts{ |
| 81 | + Image: "foo", |
| 82 | + PushImage: true, |
| 83 | + ImageRepo: "gcr.io/foo", |
| 84 | + ImageTag: "latest", |
| 85 | + BuildContext: "./foo", |
| 86 | + }, |
| 87 | + expected: []string{ |
| 88 | + "SKAFFOLD_IMAGE=foo", |
| 89 | + "SKAFFOLD_PUSH_IMAGE=true", |
| 90 | + "SKAFFOLD_IMAGE_REPO=gcr.io/foo", |
| 91 | + "SKAFFOLD_IMAGE_TAG=latest", |
| 92 | + "SKAFFOLD_BUILD_CONTEXT=./foo", |
| 93 | + }, |
| 94 | + }, |
| 95 | + { |
| 96 | + description: "sync env opts, all defined", |
| 97 | + input: SyncEnvOpts{ |
| 98 | + Image: "foo", |
| 99 | + FilesAddedOrModified: util.StringPtr("./foo/1;./foo/2"), |
| 100 | + FilesDeleted: util.StringPtr("./foo/3;./foo/4"), |
| 101 | + KubeContext: "minikube", |
| 102 | + Namespaces: "np1,np2,np3", |
| 103 | + BuildContext: "./foo", |
| 104 | + }, |
| 105 | + expected: []string{ |
| 106 | + "SKAFFOLD_IMAGE=foo", |
| 107 | + "SKAFFOLD_FILES_ADDED_OR_MODIFIED=./foo/1;./foo/2", |
| 108 | + "SKAFFOLD_FILES_DELETED=./foo/3;./foo/4", |
| 109 | + "SKAFFOLD_KUBE_CONTEXT=minikube", |
| 110 | + "SKAFFOLD_NAMESPACES=np1,np2,np3", |
| 111 | + "SKAFFOLD_BUILD_CONTEXT=./foo", |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + description: "sync env opts, some missing", |
| 116 | + input: SyncEnvOpts{ |
| 117 | + Image: "foo", |
| 118 | + KubeContext: "minikube", |
| 119 | + Namespaces: "np1,np2,np3", |
| 120 | + BuildContext: "./foo", |
| 121 | + }, |
| 122 | + expected: []string{ |
| 123 | + "SKAFFOLD_IMAGE=foo", |
| 124 | + "SKAFFOLD_KUBE_CONTEXT=minikube", |
| 125 | + "SKAFFOLD_NAMESPACES=np1,np2,np3", |
| 126 | + "SKAFFOLD_BUILD_CONTEXT=./foo", |
| 127 | + }, |
| 128 | + }, |
| 129 | + { |
| 130 | + description: "deploy env opts", |
| 131 | + input: DeployEnvOpts{ |
| 132 | + RunID: "1234", |
| 133 | + KubeContext: "minikube", |
| 134 | + Namespaces: "np1,np2,np3", |
| 135 | + }, |
| 136 | + expected: []string{ |
| 137 | + "SKAFFOLD_RUN_ID=1234", |
| 138 | + "SKAFFOLD_KUBE_CONTEXT=minikube", |
| 139 | + "SKAFFOLD_NAMESPACES=np1,np2,np3", |
| 140 | + }, |
| 141 | + }, |
| 142 | + } |
| 143 | + |
| 144 | + for _, test := range tests { |
| 145 | + testutil.Run(t, test.description, func(t *testutil.T) { |
| 146 | + actual := getEnv(test.input) |
| 147 | + t.CheckElementsMatch(test.expected, actual) |
| 148 | + }) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +type mockCfg struct { |
| 153 | + defaultRepo *string |
| 154 | + workDir string |
| 155 | + rpcPort int |
| 156 | + httpPort int |
| 157 | +} |
| 158 | + |
| 159 | +func (m mockCfg) DefaultRepo() *string { return m.defaultRepo } |
| 160 | +func (m mockCfg) GetWorkingDir() string { return m.workDir } |
| 161 | +func (m mockCfg) RPCPort() int { return m.rpcPort } |
| 162 | +func (m mockCfg) RPCHTTPPort() int { return m.httpPort } |
0 commit comments