@@ -2,6 +2,7 @@ package cli_test
2
2
3
3
import (
4
4
"fmt"
5
+ "os"
5
6
"testing"
6
7
7
8
"github.com/stretchr/testify/require"
@@ -113,39 +114,7 @@ func TestCreate(t *testing.T) {
113
114
114
115
defaultValue := "something"
115
116
version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , & echo.Responses {
116
- Parse : []* proto.Parse_Response {{
117
- Type : & proto.Parse_Response_Complete {
118
- Complete : & proto.Parse_Complete {
119
- ParameterSchemas : []* proto.ParameterSchema {
120
- {
121
- AllowOverrideSource : true ,
122
- Name : "region" ,
123
- Description : "description 1" ,
124
- DefaultSource : & proto.ParameterSource {
125
- Scheme : proto .ParameterSource_DATA ,
126
- Value : defaultValue ,
127
- },
128
- DefaultDestination : & proto.ParameterDestination {
129
- Scheme : proto .ParameterDestination_PROVISIONER_VARIABLE ,
130
- },
131
- },
132
- {
133
- AllowOverrideSource : true ,
134
- Name : "username" ,
135
- Description : "description 2" ,
136
- DefaultSource : & proto.ParameterSource {
137
- Scheme : proto .ParameterSource_DATA ,
138
- // No default value
139
- Value : "" ,
140
- },
141
- DefaultDestination : & proto.ParameterDestination {
142
- Scheme : proto .ParameterDestination_PROVISIONER_VARIABLE ,
143
- },
144
- },
145
- },
146
- },
147
- },
148
- }},
117
+ Parse : createTestParseResponseWithDefault (defaultValue ),
149
118
Provision : echo .ProvisionComplete ,
150
119
ProvisionDryRun : echo .ProvisionComplete ,
151
120
})
@@ -178,4 +147,113 @@ func TestCreate(t *testing.T) {
178
147
}
179
148
<- doneChan
180
149
})
150
+
151
+ t .Run ("WithParameterFileContainingTheValue" , func (t * testing.T ) {
152
+ t .Parallel ()
153
+ client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerD : true })
154
+ user := coderdtest .CreateFirstUser (t , client )
155
+
156
+ defaultValue := "something"
157
+ version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , & echo.Responses {
158
+ Parse : createTestParseResponseWithDefault (defaultValue ),
159
+ Provision : echo .ProvisionComplete ,
160
+ ProvisionDryRun : echo .ProvisionComplete ,
161
+ })
162
+
163
+ coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
164
+ _ = coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
165
+ tempDir := t .TempDir ()
166
+ parameterFile , _ := os .CreateTemp (tempDir , "testParameterFile*.yaml" )
167
+ _ , _ = parameterFile .WriteString ("region: \" bingo\" \n username: \" boingo\" " )
168
+ cmd , root := clitest .New (t , "create" , "" , "--parameter-file" , parameterFile .Name ())
169
+ clitest .SetupConfig (t , client , root )
170
+ doneChan := make (chan struct {})
171
+ pty := ptytest .New (t )
172
+ cmd .SetIn (pty .Input ())
173
+ cmd .SetOut (pty .Output ())
174
+ go func () {
175
+ defer close (doneChan )
176
+ err := cmd .Execute ()
177
+ require .NoError (t , err )
178
+ }()
179
+
180
+ matches := []string {
181
+ "Specify a name" , "my-workspace" ,
182
+ "Confirm create?" , "yes" ,
183
+ }
184
+ for i := 0 ; i < len (matches ); i += 2 {
185
+ match := matches [i ]
186
+ value := matches [i + 1 ]
187
+ pty .ExpectMatch (match )
188
+ pty .WriteLine (value )
189
+ }
190
+ <- doneChan
191
+ removeTmpDirUntilSuccess (t , tempDir )
192
+ })
193
+ t .Run ("WithParameterFileNotContainingTheValue" , func (t * testing.T ) {
194
+ t .Parallel ()
195
+ client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerD : true })
196
+ user := coderdtest .CreateFirstUser (t , client )
197
+
198
+ defaultValue := "something"
199
+ version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , & echo.Responses {
200
+ Parse : createTestParseResponseWithDefault (defaultValue ),
201
+ Provision : echo .ProvisionComplete ,
202
+ ProvisionDryRun : echo .ProvisionComplete ,
203
+ })
204
+ coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
205
+ template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
206
+ tempDir := t .TempDir ()
207
+ parameterFile , _ := os .CreateTemp (tempDir , "testParameterFile*.yaml" )
208
+ _ , _ = parameterFile .WriteString ("zone: \" bananas\" " )
209
+ cmd , root := clitest .New (t , "create" , "my-workspace" , "--template" , template .Name , "--parameter-file" , parameterFile .Name ())
210
+ clitest .SetupConfig (t , client , root )
211
+ doneChan := make (chan struct {})
212
+ pty := ptytest .New (t )
213
+ cmd .SetIn (pty .Input ())
214
+ cmd .SetOut (pty .Output ())
215
+ go func () {
216
+ defer close (doneChan )
217
+ err := cmd .Execute ()
218
+ require .EqualError (t , err , "Parameter value absent in parameter file for \" region\" !" )
219
+ }()
220
+ <- doneChan
221
+ removeTmpDirUntilSuccess (t , tempDir )
222
+ })
223
+ }
224
+
225
+ func createTestParseResponseWithDefault (defaultValue string ) []* proto.Parse_Response {
226
+ return []* proto.Parse_Response {{
227
+ Type : & proto.Parse_Response_Complete {
228
+ Complete : & proto.Parse_Complete {
229
+ ParameterSchemas : []* proto.ParameterSchema {
230
+ {
231
+ AllowOverrideSource : true ,
232
+ Name : "region" ,
233
+ Description : "description 1" ,
234
+ DefaultSource : & proto.ParameterSource {
235
+ Scheme : proto .ParameterSource_DATA ,
236
+ Value : defaultValue ,
237
+ },
238
+ DefaultDestination : & proto.ParameterDestination {
239
+ Scheme : proto .ParameterDestination_PROVISIONER_VARIABLE ,
240
+ },
241
+ },
242
+ {
243
+ AllowOverrideSource : true ,
244
+ Name : "username" ,
245
+ Description : "description 2" ,
246
+ DefaultSource : & proto.ParameterSource {
247
+ Scheme : proto .ParameterSource_DATA ,
248
+ // No default value
249
+ Value : "" ,
250
+ },
251
+ DefaultDestination : & proto.ParameterDestination {
252
+ Scheme : proto .ParameterDestination_PROVISIONER_VARIABLE ,
253
+ },
254
+ },
255
+ },
256
+ },
257
+ },
258
+ }}
181
259
}
0 commit comments