Skip to content

Commit cfff1d4

Browse files
committed
Use test helpers
Signed-off-by: David Gageot <[email protected]>
1 parent 7a420f1 commit cfff1d4

File tree

13 files changed

+128
-171
lines changed

13 files changed

+128
-171
lines changed

pkg/skaffold/build/cluster/sources/sources_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,10 @@ func TestSetProxy(t *testing.T) {
283283
}
284284

285285
for _, test := range tests {
286-
t.Run(test.description, func(t *testing.T) {
286+
testutil.Run(t, test.description, func(t *testutil.T) {
287287
actual := setProxy(test.clusterDetails, test.env)
288-
testutil.CheckErrorAndDeepEqual(t, false, nil, test.expectedArgs, actual)
288+
289+
t.CheckDeepEqual(test.expectedArgs, actual)
289290
})
290291
}
291292
}

pkg/skaffold/build/custom/dependencies_test.go

+16-14
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,24 @@ func TestGetDependenciesDockerfile(t *testing.T) {
5757
}
5858

5959
func TestGetDependenciesCommand(t *testing.T) {
60-
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.FakeRunOut(t,
61-
"echo [\"file1\",\"file2\",\"file3\"]",
62-
"[\"file1\",\"file2\",\"file3\"]",
63-
))
64-
defer reset()
65-
66-
customArtifact := &latest.CustomArtifact{
67-
Dependencies: &latest.CustomDependencies{
68-
Command: "echo [\"file1\",\"file2\",\"file3\"]",
69-
},
70-
}
60+
testutil.Run(t, "", func(t *testutil.T) {
61+
t.Override(&util.DefaultExecCommand, t.FakeRunOut(
62+
"echo [\"file1\",\"file2\",\"file3\"]",
63+
"[\"file1\",\"file2\",\"file3\"]",
64+
))
65+
66+
customArtifact := &latest.CustomArtifact{
67+
Dependencies: &latest.CustomDependencies{
68+
Command: "echo [\"file1\",\"file2\",\"file3\"]",
69+
},
70+
}
7171

72-
expected := []string{"file1", "file2", "file3"}
73-
deps, err := GetDependencies(context.Background(), "", customArtifact, nil)
72+
expected := []string{"file1", "file2", "file3"}
73+
deps, err := GetDependencies(context.Background(), "", customArtifact, nil)
7474

75-
testutil.CheckErrorAndDeepEqual(t, false, err, expected, deps)
75+
t.CheckNoError(err)
76+
t.CheckDeepEqual(expected, deps)
77+
})
7678
}
7779

7880
func TestGetDependenciesPaths(t *testing.T) {

pkg/skaffold/build/local/bazel_test.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ import (
2626
)
2727

2828
func TestBazelBin(t *testing.T) {
29-
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.FakeRunOut(t,
30-
"bazel info bazel-bin --arg1 --arg2",
31-
"/absolute/path/bin\n",
32-
))
33-
defer reset()
34-
35-
bazelBin, err := bazelBin(context.Background(), ".", &latest.BazelArtifact{
36-
BuildArgs: []string{"--arg1", "--arg2"},
29+
testutil.Run(t, "", func(t *testutil.T) {
30+
t.Override(&util.DefaultExecCommand, t.FakeRunOut(
31+
"bazel info bazel-bin --arg1 --arg2",
32+
"/absolute/path/bin\n",
33+
))
34+
35+
bazelBin, err := bazelBin(context.Background(), ".", &latest.BazelArtifact{
36+
BuildArgs: []string{"--arg1", "--arg2"},
37+
})
38+
39+
t.CheckNoError(err)
40+
t.CheckDeepEqual("/absolute/path/bin", bazelBin)
3741
})
38-
39-
testutil.CheckErrorAndDeepEqual(t, false, err, "/absolute/path/bin", bazelBin)
4042
}
4143

4244
func TestBuildTarPath(t *testing.T) {

pkg/skaffold/debug/transform_python_test.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ import (
2020
"strings"
2121
"testing"
2222

23+
"github.com/GoogleContainerTools/skaffold/testutil"
24+
"github.com/google/go-cmp/cmp"
2325
appsv1 "k8s.io/api/apps/v1"
2426
batchv1 "k8s.io/api/batch/v1"
2527
v1 "k8s.io/api/core/v1"
2628
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2729
"k8s.io/apimachinery/pkg/runtime"
28-
29-
"github.com/GoogleContainerTools/skaffold/testutil"
30-
"github.com/google/go-cmp/cmp"
3130
)
3231

3332
func TestExtractPtvsdArg(t *testing.T) {
@@ -47,11 +46,11 @@ func TestExtractPtvsdArg(t *testing.T) {
4746
{[]string{"-m", "ptvsd", "--wait", "--port", "9329", "--host", "foo"}, &ptvsdSpec{host: "foo", port: 9329, wait: true}},
4847
}
4948
for _, test := range tests {
50-
t.Run(strings.Join(test.in, " "), func(t *testing.T) {
49+
testutil.Run(t, strings.Join(test.in, " "), func(t *testutil.T) {
5150
if test.result == nil {
52-
testutil.CheckDeepEqual(t, test.result, extractPtvsdArg(test.in))
51+
t.CheckDeepEqual(test.result, extractPtvsdArg(test.in))
5352
} else {
54-
testutil.CheckDeepEqual(t, *test.result, *extractPtvsdArg(test.in), cmp.AllowUnexported(ptvsdSpec{}))
53+
t.CheckDeepEqual(*test.result, *extractPtvsdArg(test.in), cmp.AllowUnexported(ptvsdSpec{}))
5554
}
5655
})
5756
}
@@ -121,9 +120,10 @@ func TestPythonTransformer_IsApplicable(t *testing.T) {
121120
}
122121

123122
for _, test := range tests {
124-
t.Run(test.description, func(t *testing.T) {
123+
testutil.Run(t, test.description, func(t *testutil.T) {
125124
result := pythonTransformer{}.IsApplicable(test.source)
126-
testutil.CheckDeepEqual(t, test.result, result)
125+
126+
t.CheckDeepEqual(test.result, result)
127127
})
128128
}
129129
}
@@ -182,9 +182,10 @@ func TestPythonTransformerApply(t *testing.T) {
182182
return port
183183
}
184184
for _, test := range tests {
185-
t.Run(test.description, func(t *testing.T) {
185+
testutil.Run(t, test.description, func(t *testutil.T) {
186186
pythonTransformer{}.Apply(&test.containerSpec, test.configuration, identity)
187-
testutil.CheckDeepEqual(t, test.result, test.containerSpec)
187+
188+
t.CheckDeepEqual(test.result, test.containerSpec)
188189
})
189190
}
190191
}
@@ -551,15 +552,16 @@ func TestTransformManifestPython(t *testing.T) {
551552
},
552553
}
553554
for _, test := range tests {
554-
t.Run(test.description, func(t *testing.T) {
555+
testutil.Run(t, test.description, func(t *testutil.T) {
555556
value := test.in.DeepCopyObject()
556557

557558
retriever := func(image string) (imageConfiguration, error) {
558559
return imageConfiguration{}, nil
559560
}
560561
result := transformManifest(value, retriever)
561-
testutil.CheckDeepEqual(t, test.transformed, result)
562-
testutil.CheckDeepEqual(t, test.out, value)
562+
563+
t.CheckDeepEqual(test.transformed, result)
564+
t.CheckDeepEqual(test.out, value)
563565
})
564566
}
565567
}

pkg/skaffold/deploy/kubectl/images_test.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,20 @@ spec:
8686
- image: in valid
8787
`)}
8888

89-
fakeWarner := &warnings.Collect{}
90-
reset := testutil.Override(t, &warnings.Printf, fakeWarner.Warnf)
91-
defer reset()
92-
93-
resultManifest, err := manifests.ReplaceImages(builds, "")
94-
95-
testutil.CheckErrorAndDeepEqual(t, false, err, expected.String(), resultManifest.String())
96-
testutil.CheckErrorAndDeepEqual(t, false, err, []string{
97-
"Couldn't parse image: in valid",
98-
"image [skaffold/unused] is not used by the deployment",
99-
"image [skaffold/usedwrongfqn] is not used by the deployment",
100-
}, fakeWarner.Warnings)
89+
testutil.Run(t, "", func(t *testutil.T) {
90+
fakeWarner := &warnings.Collect{}
91+
t.Override(&warnings.Printf, fakeWarner.Warnf)
92+
93+
resultManifest, err := manifests.ReplaceImages(builds, "")
94+
95+
t.CheckNoError(err)
96+
t.CheckDeepEqual(expected.String(), resultManifest.String())
97+
t.CheckDeepEqual([]string{
98+
"Couldn't parse image: in valid",
99+
"image [skaffold/unused] is not used by the deployment",
100+
"image [skaffold/usedwrongfqn] is not used by the deployment",
101+
}, fakeWarner.Warnings)
102+
})
101103
}
102104

103105
func TestReplaceEmptyManifest(t *testing.T) {

pkg/skaffold/deploy/kubectl_test.go

+47-47
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,15 @@ func TestKubectlCleanup(t *testing.T) {
271271
}
272272

273273
func TestKubectlRedeploy(t *testing.T) {
274-
tmpDir, cleanup := testutil.NewTempDir(t)
275-
defer cleanup()
276-
tmpDir.Write("deployment-web.yaml", deploymentWebYAML)
277-
tmpDir.Write("deployment-app.yaml", deploymentAppYAML)
274+
testutil.Run(t, "", func(t *testutil.T) {
275+
tmpDir := t.NewTempDir().
276+
Write("deployment-web.yaml", deploymentWebYAML).
277+
Write("deployment-app.yaml", deploymentAppYAML)
278278

279-
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.NewFakeCmd(t).
280-
WithRunOut("kubectl version --client -ojson", kubectlVersion).
281-
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment-app.yaml")+" -f "+tmpDir.Path("deployment-web.yaml"), deploymentAppYAML+"\n"+deploymentWebYAML).
282-
WithRunInput("kubectl --context kubecontext --namespace testNamespace apply -f -", `apiVersion: v1
279+
t.Override(&util.DefaultExecCommand, t.
280+
FakeRunOut("kubectl version --client -ojson", kubectlVersion).
281+
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment-app.yaml")+" -f "+tmpDir.Path("deployment-web.yaml"), deploymentAppYAML+"\n"+deploymentWebYAML).
282+
WithRunInput("kubectl --context kubecontext --namespace testNamespace apply -f -", `apiVersion: v1
283283
kind: Pod
284284
metadata:
285285
labels:
@@ -300,8 +300,8 @@ spec:
300300
containers:
301301
- image: leeroy-web:v1
302302
name: leeroy-web`).
303-
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment-app.yaml")+" -f "+tmpDir.Path("deployment-web.yaml"), deploymentAppYAML+"\n"+deploymentWebYAML).
304-
WithRunInput("kubectl --context kubecontext --namespace testNamespace apply -f -", `apiVersion: v1
303+
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment-app.yaml")+" -f "+tmpDir.Path("deployment-web.yaml"), deploymentAppYAML+"\n"+deploymentWebYAML).
304+
WithRunInput("kubectl --context kubecontext --namespace testNamespace apply -f -", `apiVersion: v1
305305
kind: Pod
306306
metadata:
307307
labels:
@@ -311,47 +311,47 @@ spec:
311311
containers:
312312
- image: leeroy-app:v2
313313
name: leeroy-app`).
314-
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment-app.yaml")+" -f "+tmpDir.Path("deployment-web.yaml"), deploymentAppYAML+"\n"+deploymentWebYAML),
315-
)
316-
defer reset()
314+
WithRunOut("kubectl --context kubecontext --namespace testNamespace create --dry-run -oyaml -f "+tmpDir.Path("deployment-app.yaml")+" -f "+tmpDir.Path("deployment-web.yaml"), deploymentAppYAML+"\n"+deploymentWebYAML),
315+
)
317316

318-
cfg := &latest.KubectlDeploy{
319-
Manifests: []string{tmpDir.Path("deployment-app.yaml"), "deployment-web.yaml"},
320-
}
321-
deployer := NewKubectlDeployer(&runcontext.RunContext{
322-
WorkingDir: tmpDir.Root(),
323-
Cfg: &latest.Pipeline{
324-
Deploy: latest.DeployConfig{
325-
DeployType: latest.DeployType{
326-
KubectlDeploy: cfg,
317+
cfg := &latest.KubectlDeploy{
318+
Manifests: []string{tmpDir.Path("deployment-app.yaml"), "deployment-web.yaml"},
319+
}
320+
deployer := NewKubectlDeployer(&runcontext.RunContext{
321+
WorkingDir: tmpDir.Root(),
322+
Cfg: &latest.Pipeline{
323+
Deploy: latest.DeployConfig{
324+
DeployType: latest.DeployType{
325+
KubectlDeploy: cfg,
326+
},
327327
},
328328
},
329-
},
330-
KubeContext: testKubeContext,
331-
Opts: &config.SkaffoldOptions{
332-
Namespace: testNamespace,
333-
},
334-
})
335-
labellers := []Labeller{deployer}
329+
KubeContext: testKubeContext,
330+
Opts: &config.SkaffoldOptions{
331+
Namespace: testNamespace,
332+
},
333+
})
334+
labellers := []Labeller{deployer}
336335

337-
// Deploy one manifest
338-
err := deployer.Deploy(context.Background(), ioutil.Discard, []build.Artifact{
339-
{ImageName: "leeroy-web", Tag: "leeroy-web:v1"},
340-
{ImageName: "leeroy-app", Tag: "leeroy-app:v1"},
341-
}, labellers)
342-
testutil.CheckError(t, false, err)
336+
// Deploy one manifest
337+
err := deployer.Deploy(context.Background(), ioutil.Discard, []build.Artifact{
338+
{ImageName: "leeroy-web", Tag: "leeroy-web:v1"},
339+
{ImageName: "leeroy-app", Tag: "leeroy-app:v1"},
340+
}, labellers)
341+
t.CheckNoError(err)
343342

344-
// Deploy one manifest since only one image is updated
345-
err = deployer.Deploy(context.Background(), ioutil.Discard, []build.Artifact{
346-
{ImageName: "leeroy-web", Tag: "leeroy-web:v1"},
347-
{ImageName: "leeroy-app", Tag: "leeroy-app:v2"},
348-
}, labellers)
349-
testutil.CheckError(t, false, err)
343+
// Deploy one manifest since only one image is updated
344+
err = deployer.Deploy(context.Background(), ioutil.Discard, []build.Artifact{
345+
{ImageName: "leeroy-web", Tag: "leeroy-web:v1"},
346+
{ImageName: "leeroy-app", Tag: "leeroy-app:v2"},
347+
}, labellers)
348+
t.CheckNoError(err)
350349

351-
// Deploy zero manifest since no image is updated
352-
err = deployer.Deploy(context.Background(), ioutil.Discard, []build.Artifact{
353-
{ImageName: "leeroy-web", Tag: "leeroy-web:v1"},
354-
{ImageName: "leeroy-app", Tag: "leeroy-app:v2"},
355-
}, labellers)
356-
testutil.CheckError(t, false, err)
350+
// Deploy zero manifest since no image is updated
351+
err = deployer.Deploy(context.Background(), ioutil.Discard, []build.Artifact{
352+
{ImageName: "leeroy-web", Tag: "leeroy-web:v1"},
353+
{ImageName: "leeroy-app", Tag: "leeroy-app:v2"},
354+
}, labellers)
355+
t.CheckNoError(err)
356+
})
357357
}

pkg/skaffold/kubernetes/portforward/pod_forwarder_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -495,14 +495,14 @@ func TestStartPodForwarder(t *testing.T) {
495495
}
496496

497497
for _, test := range tests {
498-
t.Run(test.description, func(t *testing.T) {
498+
testutil.Run(t, test.description, func(t *testutil.T) {
499499
event.InitializeState(latest.BuildConfig{})
500500
client := fakekubeclientset.NewSimpleClientset(&v1.Pod{})
501501
fakeWatcher := watch.NewRaceFreeFake()
502502
client.PrependWatchReactor("*", testutil.SetupFakeWatcher(fakeWatcher))
503503

504504
waitForWatcher := make(chan bool)
505-
testutil.Override(t, &aggregatePodWatcher, func(_ []string, aggregate chan<- watch.Event) (func(), error) {
505+
t.Override(&aggregatePodWatcher, func(_ []string, aggregate chan<- watch.Event) (func(), error) {
506506
go func() {
507507
waitForWatcher <- true
508508
for msg := range fakeWatcher.ResultChan() {

pkg/skaffold/kubernetes/portforward/port_forward_entry_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
24+
"github.com/GoogleContainerTools/skaffold/testutil"
2425
)
2526

2627
func TestPortForwardEntryKey(t *testing.T) {
@@ -55,7 +56,7 @@ func TestPortForwardEntryKey(t *testing.T) {
5556
}
5657

5758
for _, test := range tests {
58-
t.Run(test.description, func(t *testing.T) {
59+
testutil.Run(t, test.description, func(t *testutil.T) {
5960
acutalKey := test.pfe.key()
6061

6162
if acutalKey != test.expected {
@@ -93,7 +94,7 @@ func TestAutomaticPodForwardingKey(t *testing.T) {
9394
}
9495

9596
for _, test := range tests {
96-
t.Run(test.description, func(t *testing.T) {
97+
testutil.Run(t, test.description, func(t *testutil.T) {
9798
acutalKey := test.pfe.key()
9899

99100
if acutalKey != test.expected {

pkg/skaffold/kubernetes/wait_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestWaitForPodSucceeded(t *testing.T) {
5050
}
5151

5252
for _, test := range tests {
53-
t.Run(test.description, func(t *testing.T) {
53+
testutil.Run(t, test.description, func(t *testutil.T) {
5454
pod := &v1.Pod{}
5555
client := fakekubeclientset.NewSimpleClientset(pod)
5656

@@ -75,7 +75,8 @@ func TestWaitForPodSucceeded(t *testing.T) {
7575
time.Sleep(time.Second)
7676
}
7777
err := <-errChan
78-
testutil.CheckError(t, test.shouldErr, err)
78+
79+
t.CheckError(test.shouldErr, err)
7980
})
8081
}
8182
}
@@ -116,7 +117,7 @@ func TestIsPodSucceeded(t *testing.T) {
116117
}
117118

118119
for _, test := range tests {
119-
t.Run(test.description, func(t *testing.T) {
120+
testutil.Run(t, test.description, func(t *testutil.T) {
120121
pod := &v1.Pod{
121122
Status: v1.PodStatus{
122123
Phase: test.phase,
@@ -126,8 +127,10 @@ func TestIsPodSucceeded(t *testing.T) {
126127
Type: "dummyEvent",
127128
Object: pod,
128129
}
130+
129131
actual, err := isPodSucceeded(test.podName)(dummyEvent)
130-
testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, actual, test.expected)
132+
133+
t.CheckErrorAndDeepEqual(test.shouldErr, err, actual, test.expected)
131134
})
132135
}
133136
}

0 commit comments

Comments
 (0)