Skip to content

Commit 7e8942d

Browse files
authored
Merge pull request #2149 from dgageot/remove-duplicate-test-helper
Use testutil.NewTempDir() instead
2 parents 7cf0058 + a4475a0 commit 7e8942d

File tree

3 files changed

+62
-80
lines changed

3 files changed

+62
-80
lines changed

pkg/skaffold/initializer/kubectl/kubectl_test.go

+39-40
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ limitations under the License.
1717
package kubectl
1818

1919
import (
20-
"io/ioutil"
21-
"os"
2220
"testing"
2321

2422
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
2523
"github.com/GoogleContainerTools/skaffold/testutil"
2624
)
2725

2826
func TestGenerateKubeCtlPipeline(t *testing.T) {
29-
content := []byte(`apiVersion: v1
27+
tmpDir, delete := testutil.NewTempDir(t)
28+
defer delete()
29+
30+
tmpDir.Write("deployment.yaml", `apiVersion: v1
3031
kind: Pod
3132
metadata:
3233
name: getting-started
@@ -35,8 +36,12 @@ spec:
3536
- name: getting-started
3637
image: gcr.io/k8s-skaffold/skaffold-example
3738
`)
38-
filename := testutil.CreateTempFileWithContents(t, "", "deployment.yaml", content)
39-
defer os.Remove(filename) // clean up
39+
filename := tmpDir.Path("deployment.yaml")
40+
41+
k, err := New([]string{filename})
42+
if err != nil {
43+
t.Fatal("failed to create a pipeline")
44+
}
4045

4146
expectedConfig := latest.DeployConfig{
4247
DeployType: latest.DeployType{
@@ -45,54 +50,48 @@ spec:
4550
},
4651
},
4752
}
48-
actual := latest.DeployConfig{}
49-
k, err := New([]string{filename})
50-
if k != nil {
51-
actual = k.GenerateDeployConfig()
52-
}
53-
testutil.CheckErrorAndDeepEqual(t, false, err, expectedConfig, actual)
53+
testutil.CheckDeepEqual(t, expectedConfig, k.GenerateDeployConfig())
5454
}
5555

5656
func TestParseImagesFromKubernetesYaml(t *testing.T) {
57-
validContent := []byte(`apiVersion: v1
57+
tests := []struct {
58+
description string
59+
contents string
60+
images []string
61+
shouldErr bool
62+
}{
63+
{
64+
description: "incorrect k8 yaml",
65+
contents: `no apiVersion: t
66+
kind: Pod`,
67+
images: nil,
68+
shouldErr: true,
69+
},
70+
{
71+
description: "correct k8 yaml",
72+
contents: `apiVersion: v1
5873
kind: Pod
5974
metadata:
6075
name: getting-started
6176
spec:
6277
containers:
6378
- name: getting-started
64-
image: gcr.io/k8s-skaffold/skaffold-example`)
65-
tests := []struct {
66-
name string
67-
contents []byte
68-
images []string
69-
err bool
70-
}{
71-
{
72-
name: "incorrect k8 yaml",
73-
contents: []byte(`no apiVersion: t
74-
kind: Pod`),
75-
images: nil,
76-
err: true,
77-
},
78-
{
79-
name: "correct k8 yaml",
80-
contents: validContent,
81-
images: []string{"gcr.io/k8s-skaffold/skaffold-example"},
82-
err: false,
79+
image: gcr.io/k8s-skaffold/skaffold-example`,
80+
images: []string{"gcr.io/k8s-skaffold/skaffold-example"},
81+
shouldErr: false,
8382
},
8483
}
8584

86-
tmpDir, err := ioutil.TempDir("", "test")
87-
if err != nil {
88-
t.Fatal(err)
89-
}
90-
defer os.Remove(tmpDir) // clean up
9185
for _, test := range tests {
92-
t.Run(test.name, func(t *testing.T) {
93-
tmpFile := testutil.CreateTempFileWithContents(t, tmpDir, "deployment.yaml", test.contents)
94-
images, err := parseImagesFromKubernetesYaml(tmpFile)
95-
testutil.CheckErrorAndDeepEqual(t, test.err, err, test.images, images)
86+
t.Run(test.description, func(t *testing.T) {
87+
tmpDir, delete := testutil.NewTempDir(t)
88+
defer delete()
89+
90+
tmpDir.Write("deployment.yaml", test.contents)
91+
92+
images, err := parseImagesFromKubernetesYaml(tmpDir.Path("deployment.yaml"))
93+
94+
testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.images, images)
9695
})
9796
}
9897
}

pkg/skaffold/initializer/util_test.go

+23-22
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package initializer
1818

1919
import (
20-
"os"
2120
"testing"
2221

2322
"github.com/GoogleContainerTools/skaffold/testutil"
@@ -62,40 +61,42 @@ func TestIsSupportedKubernetesFileExtension(t *testing.T) {
6261

6362
func TestIsSkaffoldConfig(t *testing.T) {
6463
tests := []struct {
65-
name string
66-
contents []byte
67-
expected bool
64+
description string
65+
contents string
66+
isValid bool
6867
}{
6968
{
70-
name: "valid skaffold config",
71-
contents: []byte(`apiVersion: skaffold/v1beta6
69+
description: "valid skaffold config",
70+
contents: `apiVersion: skaffold/v1beta6
7271
kind: Config
7372
deploy:
74-
kustomize: {}`),
75-
expected: true,
73+
kustomize: {}`,
74+
isValid: true,
7675
},
7776
{
78-
name: "not a valid format",
79-
contents: []byte("test"),
80-
expected: false,
77+
description: "not a valid format",
78+
contents: "test",
79+
isValid: false,
8180
},
8281
{
83-
name: "invalid skaffold config version",
84-
contents: []byte(`apiVersion: skaffold/v2beta1
82+
description: "invalid skaffold config version",
83+
contents: `apiVersion: skaffold/v2beta1
8584
kind: Config
8685
deploy:
87-
kustomize: {}`),
88-
expected: false,
86+
kustomize: {}`,
87+
isValid: false,
8988
},
9089
}
9190
for _, test := range tests {
92-
t.Run(test.name, func(t *testing.T) {
93-
filename := testutil.CreateTempFileWithContents(t, "", "skaffold.yaml", test.contents)
94-
defer os.Remove(filename) // clean up
95-
if IsSkaffoldConfig(filename) != test.expected {
96-
t.Errorf("expected to see %t for\n%s. but instead got %t", test.expected,
97-
test.contents, !test.expected)
98-
}
91+
t.Run(test.description, func(t *testing.T) {
92+
tmpDir, delete := testutil.NewTempDir(t)
93+
defer delete()
94+
95+
tmpDir.Write("skaffold.yaml", test.contents)
96+
97+
isValid := IsSkaffoldConfig(tmpDir.Path("skaffold.yaml"))
98+
99+
testutil.CheckDeepEqual(t, test.isValid, isValid)
99100
})
100101
}
101102
}

testutil/util.go

-18
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package testutil
1919
import (
2020
"errors"
2121
"fmt"
22-
"io/ioutil"
2322
"net/http"
2423
"net/http/httptest"
2524
"os"
@@ -156,20 +155,3 @@ func ServeFile(t *testing.T, content []byte) (url string, tearDown func()) {
156155

157156
return ts.URL, ts.Close
158157
}
159-
160-
// CreateTempFileWithContents creates a temporary file in the dir specified or
161-
// os.TempDir by default with contents mentioned.
162-
func CreateTempFileWithContents(t *testing.T, dir string, name string, content []byte) string {
163-
t.Helper()
164-
tmpfile, err := ioutil.TempFile(dir, name)
165-
if err != nil {
166-
t.Fatal(err)
167-
}
168-
if _, err := tmpfile.Write(content); err != nil {
169-
t.Fatal(err)
170-
}
171-
if err := tmpfile.Close(); err != nil {
172-
t.Fatal(err)
173-
}
174-
return tmpfile.Name()
175-
}

0 commit comments

Comments
 (0)