Skip to content

Commit 9e8a1f9

Browse files
committed
Add test for passing through env variables to container-structure-test
Signed-off-by: Cornelius Weig <[email protected]>
1 parent 2ad7e3e commit 9e8a1f9

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 structure
18+
19+
import (
20+
"context"
21+
"io/ioutil"
22+
"testing"
23+
24+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
25+
"github.com/GoogleContainerTools/skaffold/testutil"
26+
)
27+
28+
func TestNewRunner(t *testing.T) {
29+
const (
30+
imageName = "foo.io/baz"
31+
structureTestName = "foo.testcase"
32+
)
33+
34+
testutil.Run(t, "", func(t *testutil.T) {
35+
extraEnv := []string{"SOME=env_var", "OTHER=env_value"}
36+
fakeCmd := t.NewFakeCmd().
37+
WithRunEnv("container-structure-test test -v warn --image "+imageName+" --config "+structureTestName, extraEnv)
38+
t.Override(&util.DefaultExecCommand, fakeCmd)
39+
40+
testRunner := NewRunner([]string{structureTestName}, extraEnv)
41+
err := testRunner.Test(context.Background(), ioutil.Discard, imageName)
42+
t.CheckNoError(err)
43+
})
44+
}

testutil/cmd_helper.go

+32
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type run struct {
3434
command string
3535
input []byte
3636
output []byte
37+
env []string
3738
err error
3839
}
3940

@@ -117,6 +118,14 @@ func (c *FakeCmd) WithRunOutErr(command string, output string, err error) *FakeC
117118
})
118119
}
119120

121+
// WithRunEnv registers a command that requires the given env variables to be set.
122+
func (c *FakeCmd) WithRunEnv(command string, env []string) *FakeCmd {
123+
return c.addRun(run{
124+
command: command,
125+
env: env,
126+
})
127+
}
128+
120129
func (c *FakeCmd) RunCmdOut(cmd *exec.Cmd) ([]byte, error) {
121130
command := strings.Join(cmd.Args, " ")
122131

@@ -129,6 +138,8 @@ func (c *FakeCmd) RunCmdOut(cmd *exec.Cmd) ([]byte, error) {
129138
c.t.Errorf("expected: %s. Got: %s", r.command, command)
130139
}
131140

141+
c.assertCmdEnv(r.env, cmd.Env)
142+
132143
if r.output == nil {
133144
c.t.Errorf("expected RunCmd(%s) to be called. Got RunCmdOut(%s)", r.command, command)
134145
}
@@ -152,6 +163,8 @@ func (c *FakeCmd) RunCmd(cmd *exec.Cmd) error {
152163
c.t.Errorf("expected RunCmdOut(%s) to be called. Got RunCmd(%s)", r.command, command)
153164
}
154165

166+
c.assertCmdEnv(r.env, cmd.Env)
167+
155168
if r.input != nil {
156169
if cmd.Stdin == nil {
157170
c.t.Error("expected to run the command with a custom stdin", command)
@@ -171,3 +184,22 @@ func (c *FakeCmd) RunCmd(cmd *exec.Cmd) error {
171184

172185
return r.err
173186
}
187+
188+
// assertCmdEnv ensures that actualEnv contains all values from requiredEnv
189+
func (c *FakeCmd) assertCmdEnv(requiredEnv, actualEnv []string) {
190+
if requiredEnv == nil {
191+
return
192+
}
193+
c.t.Helper()
194+
195+
envs := make(map[string]struct{}, len(actualEnv))
196+
for _, e := range actualEnv {
197+
envs[e] = struct{}{}
198+
}
199+
200+
for _, e := range requiredEnv {
201+
if _, ok := envs[e]; !ok {
202+
c.t.Errorf("expected env variable with value %q", e)
203+
}
204+
}
205+
}

0 commit comments

Comments
 (0)