@@ -17,16 +17,17 @@ limitations under the License.
17
17
package kubectl
18
18
19
19
import (
20
- "io/ioutil"
21
- "os"
22
20
"testing"
23
21
24
22
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
25
23
"github.com/GoogleContainerTools/skaffold/testutil"
26
24
)
27
25
28
26
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
30
31
kind: Pod
31
32
metadata:
32
33
name: getting-started
35
36
- name: getting-started
36
37
image: gcr.io/k8s-skaffold/skaffold-example
37
38
` )
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
+ }
40
45
41
46
expectedConfig := latest.DeployConfig {
42
47
DeployType : latest.DeployType {
@@ -45,54 +50,48 @@ spec:
45
50
},
46
51
},
47
52
}
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 ())
54
54
}
55
55
56
56
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
58
73
kind: Pod
59
74
metadata:
60
75
name: getting-started
61
76
spec:
62
77
containers:
63
78
- 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 ,
83
82
},
84
83
}
85
84
86
- tmpDir , err := ioutil .TempDir ("" , "test" )
87
- if err != nil {
88
- t .Fatal (err )
89
- }
90
- defer os .Remove (tmpDir ) // clean up
91
85
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 )
96
95
})
97
96
}
98
97
}
0 commit comments