@@ -25,13 +25,15 @@ import (
25
25
"gotest.tools/assert"
26
26
)
27
27
28
- var tempDir , _ = ioutil .TempDir ("" , "kpt-fn-export-test" )
28
+ // Use file path as key, and content as value.
29
+ type files map [string ]string
29
30
30
31
type TestCase struct {
31
32
description string
32
33
params []string
33
34
expected string
34
35
err string
36
+ files files
35
37
}
36
38
37
39
var testCases = []TestCase {
70
72
"github-actions" ,
71
73
"." ,
72
74
"--output" ,
73
- filepath . Join ( tempDir , "main.yaml" ) ,
75
+ "main.yaml" ,
74
76
},
75
77
expected : `
76
78
name: kpt
90
92
},
91
93
{
92
94
description : "exports a GitHub Actions pipeline with --fn-path" ,
93
- params : []string {"github-actions" , "." , "--fn-path" , "functions/" },
95
+ files : map [string ]string {
96
+ "function.yaml" : "" ,
97
+ },
98
+ params : []string {"github-actions" , "." , "--fn-path" , "function.yaml" },
94
99
expected : `
95
100
name: kpt
96
101
on:
@@ -104,18 +109,21 @@ jobs:
104
109
- name: Run all kpt functions
105
110
uses: docker://gongpu/kpt:latest
106
111
with:
107
- args: fn run . --fn-path functions/
112
+ args: fn run . --fn-path function.yaml
108
113
` ,
109
114
},
110
115
{
111
- description : "exports a Cloud Build pipeline" ,
116
+ description : "exports a Cloud Build pipeline with --fn-path" ,
117
+ files : map [string ]string {
118
+ "functions/function.yaml" : "" ,
119
+ },
112
120
params : []string {
113
121
"cloud-build" ,
114
122
"." ,
115
123
"--fn-path" ,
116
124
"functions/" ,
117
125
"--output" ,
118
- filepath . Join ( tempDir , "cloudbuild.yaml" ) ,
126
+ "cloudbuild.yaml" ,
119
127
},
120
128
expected : `
121
129
steps:
@@ -128,11 +136,69 @@ steps:
128
136
- functions/
129
137
` ,
130
138
},
139
+ {
140
+ description : "fails to export a Cloud Build pipeline with non-exist function path" ,
141
+ params : []string {
142
+ "cloud-build" ,
143
+ "." ,
144
+ "--fn-path" ,
145
+ "functions.yaml" ,
146
+ "--output" ,
147
+ "cloudbuild.yaml" ,
148
+ },
149
+ err : "function path (functions.yaml) does not exist" ,
150
+ },
151
+ {
152
+ description : "fails to export a Cloud Build pipeline with outside function path" ,
153
+ params : []string {
154
+ "cloud-build" ,
155
+ "." ,
156
+ "--fn-path" ,
157
+ "../functions/functions.yaml" ,
158
+ "--output" ,
159
+ "cloudbuild.yaml" ,
160
+ },
161
+ err : "function path (../functions/functions.yaml) is not within the current working directory" ,
162
+ },
163
+ }
164
+
165
+ func setupTempDir (files files ) (dir string , err error ) {
166
+ tempDir , err := ioutil .TempDir ("" , "kpt-fn-export-test" )
167
+ if err != nil {
168
+ return "" , err
169
+ }
170
+
171
+ for p , content := range files {
172
+ p = filepath .Join (tempDir , p )
173
+
174
+ err = os .MkdirAll (
175
+ filepath .Dir (p ),
176
+ 0755 , // drwxr-xr-x
177
+ )
178
+ if err != nil {
179
+ return "" , nil
180
+ }
181
+
182
+ err = ioutil .WriteFile (
183
+ p ,
184
+ []byte (content ),
185
+ 0644 , // -rw-r--r--
186
+ )
187
+ if err != nil {
188
+ return "" , err
189
+ }
190
+ }
191
+
192
+ return tempDir , nil
131
193
}
132
194
133
195
func TestCmdExport (t * testing.T ) {
134
196
for i := range testCases {
135
197
testCase := testCases [i ]
198
+ tempDir , err := setupTempDir (testCase .files )
199
+ assert .NilError (t , err )
200
+ err = os .Chdir (tempDir )
201
+ assert .NilError (t , err )
136
202
137
203
t .Run (testCase .description , func (t * testing.T ) {
138
204
r := GetExportRunner ()
@@ -162,7 +228,7 @@ func TestCmdExport(t *testing.T) {
162
228
assert .Equal (t , expected , actual )
163
229
}
164
230
})
165
- }
166
231
167
- _ = os .RemoveAll (tempDir )
232
+ _ = os .RemoveAll (tempDir )
233
+ }
168
234
}
0 commit comments