Skip to content

Commit 821605b

Browse files
committed
Use test wrapper in more tests
Signed-off-by: David Gageot <[email protected]>
1 parent d5d676b commit 821605b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+719
-735
lines changed

cmd/skaffold/app/cmd/config/config_test.go

+37-30
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,43 @@ var baseConfig = &Config{
3939
var emptyConfig = &Config{}
4040

4141
func TestReadConfig(t *testing.T) {
42-
c, _ := yaml.Marshal(*baseConfig)
43-
cfg, teardown := testutil.TempFile(t, "config", c)
44-
defer teardown()
45-
4642
var tests = []struct {
43+
description string
4744
filename string
4845
expectedCfg *Config
4946
shouldErr bool
5047
}{
5148
{
52-
filename: "",
53-
shouldErr: true,
54-
},
55-
{
56-
filename: cfg,
49+
description: "valid config",
50+
filename: "config",
5751
expectedCfg: baseConfig,
5852
shouldErr: false,
5953
},
54+
{
55+
description: "missing config",
56+
filename: "",
57+
shouldErr: true,
58+
},
6059
}
6160
for _, test := range tests {
62-
cfg, err := ReadConfigForFile(test.filename)
61+
testutil.Run(t, test.description, func(t *testutil.T) {
62+
tmpDir := t.NewTempDir()
63+
t.Chdir(tmpDir.Root())
64+
65+
if test.filename != "" {
66+
c, _ := yaml.Marshal(*baseConfig)
67+
tmpDir.Write(test.filename, string(c))
68+
}
6369

64-
testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expectedCfg, cfg)
70+
cfg, err := ReadConfigForFile(test.filename)
71+
72+
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expectedCfg, cfg)
73+
})
6574
}
6675
}
6776

6877
func TestSetAndUnsetConfig(t *testing.T) {
6978
dummyContext := "dummy_context"
70-
reset := testutil.Override(t, &kubecontext, dummyContext)
71-
defer reset()
7279

7380
var tests = []struct {
7481
expectedSetCfg *Config
@@ -78,7 +85,7 @@ func TestSetAndUnsetConfig(t *testing.T) {
7885
value string
7986
kubecontext string
8087
global bool
81-
shouldErrSet bool
88+
shouldErr bool
8289
}{
8390
{
8491
description: "set default repo",
@@ -123,10 +130,10 @@ func TestSetAndUnsetConfig(t *testing.T) {
123130
},
124131
},
125132
{
126-
description: "set invalid local cluster",
127-
key: "local-cluster",
128-
shouldErrSet: true,
129-
value: "not-a-bool",
133+
description: "set invalid local cluster",
134+
key: "local-cluster",
135+
shouldErr: true,
136+
value: "not-a-bool",
130137
expectedSetCfg: &Config{
131138
ContextConfigs: []*ContextConfig{
132139
{
@@ -136,9 +143,9 @@ func TestSetAndUnsetConfig(t *testing.T) {
136143
},
137144
},
138145
{
139-
description: "set fake value",
140-
key: "not_a_real_value",
141-
shouldErrSet: true,
146+
description: "set fake value",
147+
key: "not_a_real_value",
148+
shouldErr: true,
142149
expectedSetCfg: &Config{
143150
ContextConfigs: []*ContextConfig{
144151
{
@@ -203,6 +210,8 @@ func TestSetAndUnsetConfig(t *testing.T) {
203210
}
204211
for _, test := range tests {
205212
testutil.Run(t, test.description, func(t *testutil.T) {
213+
t.Override(&kubecontext, dummyContext)
214+
206215
// create new config file
207216
c, _ := yaml.Marshal(*emptyConfig)
208217

@@ -217,23 +226,21 @@ func TestSetAndUnsetConfig(t *testing.T) {
217226
// set specified value
218227
err := setConfigValue(test.key, test.value)
219228
actualConfig, cfgErr := readConfig()
220-
if cfgErr != nil {
221-
t.Error(cfgErr)
222-
}
223-
t.CheckErrorAndDeepEqual(test.shouldErrSet, err, test.expectedSetCfg, actualConfig)
229+
t.CheckNoError(cfgErr)
230+
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expectedSetCfg, actualConfig)
224231

225-
if test.shouldErrSet {
232+
if test.shouldErr {
226233
// if we expect an error when setting, don't try and unset
227234
return
228235
}
229236

230237
// unset the value
231238
err = unsetConfigValue(test.key)
232239
newConfig, cfgErr := readConfig()
233-
if cfgErr != nil {
234-
t.Error(cfgErr)
235-
}
236-
t.CheckErrorAndDeepEqual(false, err, test.expectedUnsetCfg, newConfig)
240+
t.CheckNoError(cfgErr)
241+
242+
t.CheckNoError(err)
243+
t.CheckDeepEqual(test.expectedUnsetCfg, newConfig)
237244
})
238245
}
239246
}

cmd/skaffold/app/cmd/flags_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ func TestHasCmdAnnotation(t *testing.T) {
5050
}
5151
for _, test := range tests {
5252
testutil.Run(t, test.description, func(t *testutil.T) {
53-
if test.expected != hasCmdAnnotation(test.cmd, test.definedOn) {
54-
t.Errorf("expected %t but found %t", test.expected, !test.expected)
55-
}
53+
hasAnnotation := hasCmdAnnotation(test.cmd, test.definedOn)
54+
55+
t.CheckDeepEqual(test.expected, hasAnnotation)
5656
})
5757
}
5858
}

cmd/skaffold/app/cmd/runner_test.go

+66-49
Original file line numberDiff line numberDiff line change
@@ -18,62 +18,79 @@ package cmd
1818

1919
import (
2020
"fmt"
21-
"os"
2221
"testing"
2322

2423
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
2524
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
2625
"github.com/GoogleContainerTools/skaffold/testutil"
27-
"github.com/pkg/errors"
2826
)
2927

30-
func writeSkaffoldConfig(t *testing.T, content string) (string, func()) {
31-
yaml := fmt.Sprintf("apiVersion: %s\nkind: Config\n%s", latest.Version, content)
32-
return testutil.TempFile(t, "skaffold.yaml", []byte(yaml))
33-
}
34-
3528
func TestNewRunner(t *testing.T) {
36-
cfg, delete := writeSkaffoldConfig(t, "")
37-
defer delete()
38-
39-
_, _, err := newRunner(&config.SkaffoldOptions{
40-
ConfigurationFile: cfg,
41-
Trigger: "polling",
42-
})
43-
44-
testutil.CheckError(t, false, err)
45-
}
46-
47-
func TestNewRunnerMissingConfig(t *testing.T) {
48-
_, _, err := newRunner(&config.SkaffoldOptions{
49-
ConfigurationFile: "missing-skaffold.yaml",
50-
})
51-
52-
testutil.CheckError(t, true, err)
53-
if !os.IsNotExist(errors.Cause(err)) {
54-
t.Error("error should say that file is missing")
29+
tests := []struct {
30+
description string
31+
config string
32+
options *config.SkaffoldOptions
33+
shouldErr bool
34+
expectedError string
35+
}{
36+
{
37+
description: "valid config",
38+
config: "",
39+
options: &config.SkaffoldOptions{
40+
ConfigurationFile: "skaffold.yaml",
41+
Trigger: "polling",
42+
},
43+
shouldErr: false,
44+
},
45+
{
46+
description: "invalid config",
47+
config: "invalid",
48+
options: &config.SkaffoldOptions{
49+
ConfigurationFile: "skaffold.yaml",
50+
},
51+
shouldErr: true,
52+
},
53+
{
54+
description: "missing config",
55+
config: "",
56+
options: &config.SkaffoldOptions{
57+
ConfigurationFile: "missing-skaffold.yaml",
58+
},
59+
shouldErr: true,
60+
},
61+
{
62+
description: "unknown profile",
63+
config: "",
64+
options: &config.SkaffoldOptions{
65+
ConfigurationFile: "skaffold.yaml",
66+
Profiles: []string{"unknown-profile"},
67+
},
68+
shouldErr: true,
69+
expectedError: "applying profiles",
70+
},
71+
{
72+
description: "unsupported trigger",
73+
config: "",
74+
options: &config.SkaffoldOptions{
75+
ConfigurationFile: "skaffold.yaml",
76+
Trigger: "unknown trigger",
77+
},
78+
shouldErr: true,
79+
expectedError: "unsupported trigger",
80+
},
81+
}
82+
for _, test := range tests {
83+
testutil.Run(t, test.description, func(t *testutil.T) {
84+
tmpDir := t.NewTempDir().
85+
Write("skaffold.yaml", fmt.Sprintf("apiVersion: %s\nkind: Config\n%s", latest.Version, test.config))
86+
t.Chdir(tmpDir.Root())
87+
88+
_, _, err := newRunner(test.options)
89+
90+
t.CheckError(test.shouldErr, err)
91+
if test.expectedError != "" {
92+
t.CheckErrorContains(test.expectedError, err)
93+
}
94+
})
5595
}
56-
}
57-
58-
func TestNewRunnerInvalidConfig(t *testing.T) {
59-
cfg, delete := writeSkaffoldConfig(t, "invalid")
60-
defer delete()
61-
62-
_, _, err := newRunner(&config.SkaffoldOptions{
63-
ConfigurationFile: cfg,
64-
})
65-
66-
testutil.CheckErrorContains(t, "parsing skaffold config", err)
67-
}
68-
69-
func TestNewRunnerUnknownProfile(t *testing.T) {
70-
cfg, delete := writeSkaffoldConfig(t, "")
71-
defer delete()
72-
73-
_, _, err := newRunner(&config.SkaffoldOptions{
74-
ConfigurationFile: cfg,
75-
Profiles: []string{"unknown-profile"},
76-
})
77-
78-
testutil.CheckErrorContains(t, "applying profiles", err)
7996
}

cmd/skaffold/app/flags/build_output_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ func TestNewBuildOutputFlag(t *testing.T) {
3232
}
3333

3434
func TestBuildOutputSet(t *testing.T) {
35-
dir, cleanUp := testutil.NewTempDir(t)
36-
defer cleanUp()
37-
3835
var tests = []struct {
3936
description string
4037
buildOutputBytes []byte
@@ -79,6 +76,8 @@ func TestBuildOutputSet(t *testing.T) {
7976
}
8077
for _, test := range tests {
8178
testutil.Run(t, test.description, func(t *testutil.T) {
79+
dir := t.NewTempDir()
80+
8281
flag := NewBuildOutputFileFlag("")
8382
if test.buildOutputBytes != nil {
8483
dir.Write(test.setValue, string(test.buildOutputBytes))

cmd/skaffold/app/skaffold_test.go

+16-14
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,28 @@ import (
2626
)
2727

2828
func TestMainHelp(t *testing.T) {
29-
var (
30-
output bytes.Buffer
31-
errOutput bytes.Buffer
32-
)
29+
testutil.Run(t, "", func(t *testutil.T) {
30+
var (
31+
output bytes.Buffer
32+
errOutput bytes.Buffer
33+
)
3334

34-
restore := testutil.Override(t, &os.Args, []string{"skaffold", "help"})
35-
defer restore()
35+
t.Override(&os.Args, []string{"skaffold", "help"})
3636

37-
err := Run(&output, &errOutput)
37+
err := Run(&output, &errOutput)
3838

39-
testutil.CheckError(t, false, err)
40-
testutil.CheckContains(t, "Available Commands", output.String())
41-
testutil.CheckDeepEqual(t, "", errOutput.String())
39+
t.CheckNoError(err)
40+
t.CheckContains("Available Commands", output.String())
41+
t.CheckDeepEqual("", errOutput.String())
42+
})
4243
}
4344

4445
func TestMainUnknownCommand(t *testing.T) {
45-
restore := testutil.Override(t, &os.Args, []string{"skaffold", "unknown"})
46-
defer restore()
46+
testutil.Run(t, "", func(t *testutil.T) {
47+
t.Override(&os.Args, []string{"skaffold", "unknown"})
4748

48-
err := Run(ioutil.Discard, ioutil.Discard)
49+
err := Run(ioutil.Discard, ioutil.Discard)
4950

50-
testutil.CheckError(t, true, err)
51+
t.CheckError(true, err)
52+
})
5153
}

0 commit comments

Comments
 (0)