7
7
"encoding/hex"
8
8
"os"
9
9
"path/filepath"
10
+ "strings"
10
11
"testing"
11
12
12
13
"github.com/codeclysm/extract/v3"
@@ -49,7 +50,7 @@ func TestTemplatePull_NoName(t *testing.T) {
49
50
require .Error (t , err )
50
51
}
51
52
52
- // Stdout tests that 'templates pull' pulls down the latest template
53
+ // Stdout tests that 'templates pull' pulls down the active template
53
54
// and writes it to stdout.
54
55
func TestTemplatePull_Stdout (t * testing.T ) {
55
56
t .Parallel ()
@@ -78,6 +79,7 @@ func TestTemplatePull_Stdout(t *testing.T) {
78
79
// are being sorted correctly.
79
80
updatedVersion := coderdtest .UpdateTemplateVersion (t , client , owner .OrganizationID , source2 , template .ID )
80
81
_ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , updatedVersion .ID )
82
+ coderdtest .UpdateActiveTemplateVersion (t , client , template .ID , updatedVersion .ID )
81
83
82
84
inv , root := clitest .New (t , "templates" , "pull" , "--tar" , template .Name )
83
85
clitest .SetupConfig (t , templateAdmin , root )
@@ -91,7 +93,123 @@ func TestTemplatePull_Stdout(t *testing.T) {
91
93
require .True (t , bytes .Equal (expected , buf .Bytes ()), "tar files differ" )
92
94
}
93
95
94
- // ToDir tests that 'templates pull' pulls down the latest template
96
+ // Stdout tests that 'templates pull' pulls down the non-latest active template
97
+ // and writes it to stdout.
98
+ func TestTemplatePull_ActiveOldStdout (t * testing.T ) {
99
+ t .Parallel ()
100
+
101
+ client := coderdtest .New (t , & coderdtest.Options {
102
+ IncludeProvisionerDaemon : true ,
103
+ })
104
+ user := coderdtest .CreateFirstUser (t , client )
105
+
106
+ source1 := genTemplateVersionSource ()
107
+ source2 := genTemplateVersionSource ()
108
+
109
+ expected , err := echo .Tar (source1 )
110
+ require .NoError (t , err )
111
+
112
+ version1 := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , source1 )
113
+ _ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , version1 .ID )
114
+
115
+ template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version1 .ID )
116
+
117
+ updatedVersion := coderdtest .UpdateTemplateVersion (t , client , user .OrganizationID , source2 , template .ID )
118
+ _ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , updatedVersion .ID )
119
+
120
+ inv , root := clitest .New (t , "templates" , "pull" , "--tar" , template .Name )
121
+ clitest .SetupConfig (t , client , root )
122
+
123
+ var buf bytes.Buffer
124
+ inv .Stdout = & buf
125
+ var stderr strings.Builder
126
+ inv .Stderr = & stderr
127
+
128
+ err = inv .Run ()
129
+ require .NoError (t , err )
130
+
131
+ require .True (t , bytes .Equal (expected , buf .Bytes ()), "tar files differ" )
132
+ require .Contains (t , stderr .String (), "A newer template version than the active version exists." )
133
+ }
134
+
135
+ // Stdout tests that 'templates pull' pulls down the specified template and
136
+ // writes it to stdout.
137
+ func TestTemplatePull_SpecifiedStdout (t * testing.T ) {
138
+ t .Parallel ()
139
+
140
+ client := coderdtest .New (t , & coderdtest.Options {
141
+ IncludeProvisionerDaemon : true ,
142
+ })
143
+ user := coderdtest .CreateFirstUser (t , client )
144
+
145
+ source1 := genTemplateVersionSource ()
146
+ source2 := genTemplateVersionSource ()
147
+ source3 := genTemplateVersionSource ()
148
+
149
+ expected , err := echo .Tar (source1 )
150
+ require .NoError (t , err )
151
+
152
+ version1 := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , source1 )
153
+ _ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , version1 .ID )
154
+
155
+ template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version1 .ID )
156
+
157
+ updatedVersion := coderdtest .UpdateTemplateVersion (t , client , user .OrganizationID , source2 , template .ID )
158
+ _ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , updatedVersion .ID )
159
+
160
+ updatedVersion2 := coderdtest .UpdateTemplateVersion (t , client , user .OrganizationID , source3 , template .ID )
161
+ _ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , updatedVersion2 .ID )
162
+ coderdtest .UpdateActiveTemplateVersion (t , client , template .ID , updatedVersion2 .ID )
163
+
164
+ inv , root := clitest .New (t , "templates" , "pull" , "--tar" , template .Name , "--version" , version1 .Name )
165
+ clitest .SetupConfig (t , client , root )
166
+
167
+ var buf bytes.Buffer
168
+ inv .Stdout = & buf
169
+
170
+ err = inv .Run ()
171
+ require .NoError (t , err )
172
+
173
+ require .True (t , bytes .Equal (expected , buf .Bytes ()), "tar files differ" )
174
+ }
175
+
176
+ // Stdout tests that 'templates pull' pulls down the latest template
177
+ // and writes it to stdout.
178
+ func TestTemplatePull_LatestStdout (t * testing.T ) {
179
+ t .Parallel ()
180
+
181
+ client := coderdtest .New (t , & coderdtest.Options {
182
+ IncludeProvisionerDaemon : true ,
183
+ })
184
+ user := coderdtest .CreateFirstUser (t , client )
185
+
186
+ source1 := genTemplateVersionSource ()
187
+ source2 := genTemplateVersionSource ()
188
+
189
+ expected , err := echo .Tar (source1 )
190
+ require .NoError (t , err )
191
+
192
+ version1 := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , source1 )
193
+ _ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , version1 .ID )
194
+
195
+ template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version1 .ID )
196
+
197
+ updatedVersion := coderdtest .UpdateTemplateVersion (t , client , user .OrganizationID , source2 , template .ID )
198
+ _ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , updatedVersion .ID )
199
+
200
+ inv , root := clitest .New (t , "templates" , "pull" , "--tar" , template .Name , "latest" )
201
+ clitest .SetupConfig (t , client , root )
202
+
203
+ var buf bytes.Buffer
204
+ inv .Stdout = & buf
205
+
206
+ err = inv .Run ()
207
+ require .NoError (t , err )
208
+
209
+ require .True (t , bytes .Equal (expected , buf .Bytes ()), "tar files differ" )
210
+ }
211
+
212
+ // ToDir tests that 'templates pull' pulls down the active template
95
213
// and writes it to the correct directory.
96
214
func TestTemplatePull_ToDir (t * testing.T ) {
97
215
t .Parallel ()
@@ -120,6 +238,7 @@ func TestTemplatePull_ToDir(t *testing.T) {
120
238
// are being sorted correctly.
121
239
updatedVersion := coderdtest .UpdateTemplateVersion (t , client , owner .OrganizationID , source2 , template .ID )
122
240
_ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , updatedVersion .ID )
241
+ coderdtest .UpdateActiveTemplateVersion (t , client , template .ID , updatedVersion .ID )
123
242
124
243
dir := t .TempDir ()
125
244
@@ -143,8 +262,9 @@ func TestTemplatePull_ToDir(t *testing.T) {
143
262
)
144
263
}
145
264
146
- // ToDir tests that 'templates pull' pulls down the latest template
147
- // and writes it to a directory with the name of the template if the path is not implicitly supplied.
265
+ // ToDir tests that 'templates pull' pulls down the active template and writes
266
+ // it to a directory with the name of the template if the path is not implicitly
267
+ // supplied.
148
268
// nolint: paralleltest
149
269
func TestTemplatePull_ToImplicit (t * testing.T ) {
150
270
client := coderdtest .New (t , & coderdtest.Options {
@@ -171,6 +291,7 @@ func TestTemplatePull_ToImplicit(t *testing.T) {
171
291
// are being sorted correctly.
172
292
updatedVersion := coderdtest .UpdateTemplateVersion (t , client , owner .OrganizationID , source2 , template .ID )
173
293
_ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , updatedVersion .ID )
294
+ coderdtest .UpdateActiveTemplateVersion (t , client , template .ID , updatedVersion .ID )
174
295
175
296
// create a tempdir and change the working directory to it for the duration of the test (cannot run in parallel)
176
297
dir := t .TempDir ()
@@ -233,6 +354,7 @@ func TestTemplatePull_FolderConflict(t *testing.T) {
233
354
// are being sorted correctly.
234
355
updatedVersion := coderdtest .UpdateTemplateVersion (t , client , owner .OrganizationID , source2 , template .ID )
235
356
_ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , updatedVersion .ID )
357
+ coderdtest .UpdateActiveTemplateVersion (t , client , template .ID , updatedVersion .ID )
236
358
237
359
dir := t .TempDir ()
238
360
0 commit comments