@@ -36,20 +36,114 @@ import (
36
36
)
37
37
38
38
func TestKpt_Deploy (t * testing.T ) {
39
+ output := `apiVersion: v1
40
+ kind: Pod
41
+ metadata:
42
+ namespace: default
43
+ spec:
44
+ containers:
45
+ - image: gcr.io/project/image1
46
+ name: image1
47
+ `
39
48
tests := []struct {
40
- description string
41
- expected []string
42
- shouldErr bool
49
+ description string
50
+ builds []build.Artifact
51
+ cfg * latest.KptDeploy
52
+ kustomizations map [string ]string
53
+ commands util.Command
54
+ expected []string
55
+ shouldErr bool
43
56
}{
44
57
{
45
- description : "nil" ,
58
+ description : "no manifest" ,
59
+ cfg : & latest.KptDeploy {
60
+ Dir : "." ,
61
+ },
62
+ commands : testutil .
63
+ CmdRunOut ("kpt fn source ." , `` ).
64
+ AndRunOut ("kpt fn sink .pipeline" , `` ).
65
+ AndRunOut ("kpt fn run .pipeline --dry-run" , `` ),
66
+ },
67
+ {
68
+ description : "invalid manifest" ,
69
+ cfg : & latest.KptDeploy {
70
+ Dir : "." ,
71
+ },
72
+ commands : testutil .
73
+ CmdRunOut ("kpt fn source ." , `` ).
74
+ AndRunOut ("kpt fn sink .pipeline" , `` ).
75
+ AndRunOut ("kpt fn run .pipeline --dry-run" , `foo` ),
76
+ shouldErr : true ,
77
+ },
78
+ {
79
+ description : "invalid user specified applyDir" ,
80
+ cfg : & latest.KptDeploy {
81
+ Dir : "." ,
82
+ ApplyDir : "invalid_path" ,
83
+ },
84
+ commands : testutil .
85
+ CmdRunOut ("kpt fn source ." , `` ).
86
+ AndRunOut ("kpt fn sink .pipeline" , `` ).
87
+ AndRunOut ("kpt fn run .pipeline --dry-run" , output ),
88
+ shouldErr : true ,
89
+ },
90
+ {
91
+ description : "kustomization and specified kpt fn" ,
92
+ cfg : & latest.KptDeploy {
93
+ Dir : "." ,
94
+ Fn : latest.KptFn {FnPath : "kpt-func.yaml" },
95
+ ApplyDir : "valid_path" ,
96
+ },
97
+ kustomizations : map [string ]string {"Kustomization" : `resources:
98
+ - foo.yaml` },
99
+ commands : testutil .
100
+ CmdRunOut ("kpt fn source ." , `` ).
101
+ AndRunOut ("kpt fn sink .pipeline" , `` ).
102
+ AndRunOut ("kustomize build -o .pipeline ." , `` ).
103
+ AndRunOut ("kpt fn run .pipeline --dry-run --fn-path kpt-func.yaml" , output ).
104
+ AndRun ("kpt live apply valid_path" ),
105
+ expected : []string {"default" },
106
+ },
107
+ {
108
+ description : "kpt live apply fails" ,
109
+ cfg : & latest.KptDeploy {
110
+ Dir : "." ,
111
+ },
112
+ commands : testutil .
113
+ CmdRunOut ("kpt fn source ." , `` ).
114
+ AndRunOut ("kpt fn sink .pipeline" , `` ).
115
+ AndRunOut ("kpt fn run .pipeline --dry-run" , output ).
116
+ AndRunOut ("kpt live init .kpt-hydrated" , `` ).
117
+ AndRunErr ("kpt live apply .kpt-hydrated" , errors .New ("BUG" )),
118
+ shouldErr : true ,
46
119
},
47
120
}
48
121
for _ , test := range tests {
49
122
testutil .Run (t , test .description , func (t * testutil.T ) {
50
- k := NewKptDeployer (& runcontext.RunContext {}, nil )
51
- res , err := k .Deploy (context .Background (), nil , nil )
52
- t .CheckErrorAndDeepEqual (test .shouldErr , err , test .expected , res )
123
+ t .Override (& util .DefaultExecCommand , test .commands )
124
+ tmpDir := t .NewTempDir ().Chdir ()
125
+
126
+ tmpDir .WriteFiles (test .kustomizations )
127
+
128
+ k := NewKptDeployer (& runcontext.RunContext {
129
+ Cfg : latest.Pipeline {
130
+ Deploy : latest.DeployConfig {
131
+ DeployType : latest.DeployType {
132
+ KptDeploy : test .cfg ,
133
+ },
134
+ },
135
+ },
136
+ }, nil )
137
+
138
+ if k .ApplyDir == "valid_path" {
139
+ // 0755 is a permission setting where the owner can read, write, and execute.
140
+ // Others can read and execute but not modify the directory.
141
+ os .Mkdir (k .ApplyDir , 0755 )
142
+ }
143
+
144
+ _ , err := k .Deploy (context .Background (), ioutil .Discard , test .builds )
145
+
146
+ t .CheckError (test .shouldErr , err )
53
147
})
54
148
}
55
149
}
@@ -194,19 +288,19 @@ func TestKpt_Cleanup(t *testing.T) {
194
288
{
195
289
description : "valid user specified applyDir w/o template resource" ,
196
290
applyDir : "valid_path" ,
197
- commands : testutil .CmdRunOutErr ("kpt live destroy valid_path" , " " , errors .New ("BUG" )),
291
+ commands : testutil .CmdRunErr ("kpt live destroy valid_path" , errors .New ("BUG" )),
198
292
shouldErr : true ,
199
293
},
200
294
{
201
295
description : "valid user specified applyDir w/ template resource (emulated)" ,
202
296
applyDir : "valid_path" ,
203
- commands : testutil .CmdRunOut ("kpt live destroy valid_path" , " " ),
297
+ commands : testutil .CmdRun ("kpt live destroy valid_path" ),
204
298
},
205
299
{
206
300
description : "unspecified applyDir" ,
207
301
commands : testutil .
208
302
CmdRunOut ("kpt live init .kpt-hydrated" , "" ).
209
- AndRunOut ("kpt live destroy .kpt-hydrated" , " " ),
303
+ AndRun ("kpt live destroy .kpt-hydrated" ),
210
304
},
211
305
}
212
306
for _ , test := range tests {
0 commit comments