Skip to content

Commit

Permalink
Add test for passing through env variables to container-structure-test
Browse files Browse the repository at this point in the history
Signed-off-by: Cornelius Weig <[email protected]>
  • Loading branch information
corneliusweig committed Aug 14, 2019
1 parent 2ad7e3e commit 1075fdc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
44 changes: 44 additions & 0 deletions pkg/skaffold/test/structure/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2019 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package structure

import (
"context"
"io/ioutil"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestNewRunner(t *testing.T) {
const (
imageName = "foo.io/baz"
structureTestName = "foo.testcase"
)

testutil.Run(t, "", func(t *testutil.T) {
extraEnv := []string{"SOME=env_var", "OTHER=env_value"}
fakeCmd := t.NewFakeCmd().
WithRunEnv("container-structure-test test -v warn --image "+imageName+" --config "+structureTestName, extraEnv)
t.Override(&util.DefaultExecCommand, fakeCmd)

testRunner := NewRunner([]string{structureTestName}, extraEnv)
err := testRunner.Test(context.Background(), ioutil.Discard, imageName)
t.CheckNoError(err)
})
}
32 changes: 32 additions & 0 deletions testutil/cmd_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type run struct {
command string
input []byte
output []byte
env []string
err error
}

Expand Down Expand Up @@ -117,6 +118,14 @@ func (c *FakeCmd) WithRunOutErr(command string, output string, err error) *FakeC
})
}

// WithRunEnv registers a command that requires the given env variables to be set.
func (c *FakeCmd) WithRunEnv(command string, env []string) *FakeCmd {
return c.addRun(run{
command: command,
env: env,
})
}

func (c *FakeCmd) RunCmdOut(cmd *exec.Cmd) ([]byte, error) {
command := strings.Join(cmd.Args, " ")

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

c.assertCmdEnv(r.env, cmd.Env)

if r.output == nil {
c.t.Errorf("expected RunCmd(%s) to be called. Got RunCmdOut(%s)", r.command, command)
}
Expand All @@ -152,6 +163,8 @@ func (c *FakeCmd) RunCmd(cmd *exec.Cmd) error {
c.t.Errorf("expected RunCmdOut(%s) to be called. Got RunCmd(%s)", r.command, command)
}

c.assertCmdEnv(r.env, cmd.Env)

if r.input != nil {
if cmd.Stdin == nil {
c.t.Error("expected to run the command with a custom stdin", command)
Expand All @@ -171,3 +184,22 @@ func (c *FakeCmd) RunCmd(cmd *exec.Cmd) error {

return r.err
}

// assertCmdEnv ensures that actualEnv contains all values from requiredEnv
func (c *FakeCmd) assertCmdEnv(requiredEnv, actualEnv []string) {
if requiredEnv == nil {
return
}
c.t.Helper()

envs := make(map[string]struct{}, len(actualEnv))
for _, e := range actualEnv {
envs[e] = struct{}{}
}

for _, e := range requiredEnv {
if _, ok := envs[e]; !ok {
c.t.Errorf("expected env variable with value %q", e)
}
}
}

0 comments on commit 1075fdc

Please sign in to comment.