-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
backend_test.go
623 lines (524 loc) 路 19.6 KB
/
backend_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package filestate
import (
"context"
"encoding/json"
"os"
"path"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
user "github.com/tweekmonster/luser"
"github.com/pulumi/pulumi/pkg/v3/backend"
"github.com/pulumi/pulumi/pkg/v3/operations"
"github.com/pulumi/pulumi/pkg/v3/resource/deploy"
"github.com/pulumi/pulumi/pkg/v3/resource/stack"
"github.com/pulumi/pulumi/pkg/v3/secrets/b64"
"github.com/pulumi/pulumi/pkg/v3/secrets/passphrase"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/pulumi/pulumi/sdk/v3/go/common/encoding"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
)
func TestMassageBlobPath(t *testing.T) {
t.Parallel()
testMassagePath := func(t *testing.T, s string, want string) {
massaged, err := massageBlobPath(s)
assert.NoError(t, err)
assert.Equal(t, want, massaged,
"massageBlobPath(%s) didn't return expected result.\nWant: %q\nGot: %q", s, want, massaged)
}
// URLs not prefixed with "file://" are kept as-is. Also why we add FilePathPrefix as a prefix for other tests.
t.Run("NonFilePrefixed", func(t *testing.T) {
t.Parallel()
testMassagePath(t, "asdf-123", "asdf-123")
})
// The home directory is converted into the user's actual home directory.
// Which requires even more tweaks to work on Windows.
t.Run("PrefixedWithTilde", func(t *testing.T) {
t.Parallel()
usr, err := user.Current()
if err != nil {
t.Fatalf("Unable to get current user: %v", err)
}
homeDir := usr.HomeDir
// When running on Windows, the "home directory" takes on a different meaning.
if runtime.GOOS == "windows" {
t.Logf("Running on %v", runtime.GOOS)
t.Run("NormalizeDirSeparator", func(t *testing.T) {
t.Parallel()
testMassagePath(t, FilePathPrefix+`C:\Users\steve\`, FilePathPrefix+"/C:/Users/steve")
})
newHomeDir := "/" + filepath.ToSlash(homeDir)
t.Logf("Changed homeDir to expect from %q to %q", homeDir, newHomeDir)
homeDir = newHomeDir
}
testMassagePath(t, FilePathPrefix+"~", FilePathPrefix+homeDir)
testMassagePath(t, FilePathPrefix+"~/alpha/beta", FilePathPrefix+homeDir+"/alpha/beta")
})
t.Run("MakeAbsolute", func(t *testing.T) {
t.Parallel()
// Run the expected result through filepath.Abs, since on Windows we expect "C:\1\2".
expected := "/1/2"
abs, err := filepath.Abs(expected)
assert.NoError(t, err)
expected = filepath.ToSlash(abs)
if expected[0] != '/' {
expected = "/" + expected // A leading slash is added on Windows.
}
testMassagePath(t, FilePathPrefix+"/1/2/3/../4/..", FilePathPrefix+expected)
})
}
func TestGetLogsForTargetWithNoSnapshot(t *testing.T) {
t.Parallel()
target := &deploy.Target{
Name: "test",
Config: config.Map{},
Decrypter: config.NopDecrypter,
Snapshot: nil,
}
query := operations.LogQuery{}
res, err := GetLogsForTarget(target, query)
assert.NoError(t, err)
assert.Nil(t, res)
}
func makeUntypedDeployment(name tokens.QName, phrase, state string) (*apitype.UntypedDeployment, error) {
sm, err := passphrase.NewPassphraseSecretsManager(phrase, state)
if err != nil {
return nil, err
}
resources := []*resource.State{
{
URN: resource.NewURN("a", "proj", "d:e:f", "a:b:c", name),
Type: "a:b:c",
Inputs: resource.PropertyMap{
resource.PropertyKey("secret"): resource.MakeSecret(resource.NewStringProperty("s3cr3t")),
},
},
}
snap := deploy.NewSnapshot(deploy.Manifest{}, sm, resources, nil)
sdep, err := stack.SerializeDeployment(snap, snap.SecretsManager, false /* showSecrsts */)
if err != nil {
return nil, err
}
data, err := json.Marshal(sdep)
if err != nil {
return nil, err
}
return &apitype.UntypedDeployment{
Version: 3,
Deployment: json.RawMessage(data),
}, nil
}
//nolint:paralleltest // mutates environment variables
func TestListStacksWithMultiplePassphrases(t *testing.T) {
// Login to a temp dir filestate backend
tmpDir := t.TempDir()
b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir))
assert.NoError(t, err)
ctx := context.Background()
// Create stack "a" and import a checkpoint with a secret
aStackRef, err := b.ParseStackReference("a")
assert.NoError(t, err)
aStack, err := b.CreateStack(ctx, aStackRef, nil)
assert.NoError(t, err)
assert.NotNil(t, aStack)
defer func() {
t.Setenv("PULUMI_CONFIG_PASSPHRASE", "abc123")
_, err := b.RemoveStack(ctx, aStack, true)
assert.NoError(t, err)
}()
deployment, err := makeUntypedDeployment("a", "abc123",
"v1:4iF78gb0nF0=:v1:Co6IbTWYs/UdrjgY:FSrAWOFZnj9ealCUDdJL7LrUKXX9BA==")
assert.NoError(t, err)
t.Setenv("PULUMI_CONFIG_PASSPHRASE", "abc123")
err = b.ImportDeployment(ctx, aStack, deployment)
assert.NoError(t, err)
// Create stack "b" and import a checkpoint with a secret
bStackRef, err := b.ParseStackReference("b")
assert.NoError(t, err)
bStack, err := b.CreateStack(ctx, bStackRef, nil)
assert.NoError(t, err)
assert.NotNil(t, bStack)
defer func() {
t.Setenv("PULUMI_CONFIG_PASSPHRASE", "123abc")
_, err := b.RemoveStack(ctx, bStack, true)
assert.NoError(t, err)
}()
deployment, err = makeUntypedDeployment("b", "123abc",
"v1:C7H2a7/Ietk=:v1:yfAd1zOi6iY9DRIB:dumdsr+H89VpHIQWdB01XEFqYaYjAg==")
assert.NoError(t, err)
t.Setenv("PULUMI_CONFIG_PASSPHRASE", "123abc")
err = b.ImportDeployment(ctx, bStack, deployment)
assert.NoError(t, err)
// Remove the config passphrase so that we can no longer deserialize the checkpoints
err = os.Unsetenv("PULUMI_CONFIG_PASSPHRASE")
assert.NoError(t, err)
// Ensure that we can list the stacks we created even without a passphrase
stacks, outContToken, err := b.ListStacks(ctx, backend.ListStacksFilter{}, nil /* inContToken */)
assert.NoError(t, err)
assert.Nil(t, outContToken)
assert.Len(t, stacks, 2)
for _, stack := range stacks {
assert.NotNil(t, stack.ResourceCount())
assert.Equal(t, 1, *stack.ResourceCount())
}
}
func TestDrillError(t *testing.T) {
t.Parallel()
// Login to a temp dir filestate backend
tmpDir := t.TempDir()
b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir))
assert.NoError(t, err)
ctx := context.Background()
// Get a non-existent stack and expect a nil error because it won't be found.
stackRef, err := b.ParseStackReference("dev")
if err != nil {
t.Fatalf("unexpected error %v when parsing stack reference", err)
}
_, err = b.GetStack(ctx, stackRef)
assert.Nil(t, err)
}
func TestCancel(t *testing.T) {
t.Parallel()
// Login to a temp dir filestate backend
tmpDir := t.TempDir()
b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir))
assert.NoError(t, err)
ctx := context.Background()
// Check that trying to cancel a stack that isn't created yet doesn't error
aStackRef, err := b.ParseStackReference("a")
assert.NoError(t, err)
err = b.CancelCurrentUpdate(ctx, aStackRef)
assert.NoError(t, err)
// Check that trying to cancel a stack that isn't locked doesn't error
aStack, err := b.CreateStack(ctx, aStackRef, nil)
assert.NoError(t, err)
assert.NotNil(t, aStack)
err = b.CancelCurrentUpdate(ctx, aStackRef)
assert.NoError(t, err)
// Locking and lock checks are only part of the internal interface
lb, ok := b.(*localBackend)
assert.True(t, ok)
assert.NotNil(t, lb)
// Lock the stack and check CancelCurrentUpdate deletes the lock file
err = lb.Lock(ctx, aStackRef)
assert.NoError(t, err)
// check the lock file exists
lockExists, err := lb.bucket.Exists(ctx, lb.lockPath(aStackRef.(localBackendReference).FullyQualifiedName()))
assert.NoError(t, err)
assert.True(t, lockExists)
// Call CancelCurrentUpdate
err = lb.CancelCurrentUpdate(ctx, aStackRef)
assert.NoError(t, err)
// Now check the lock file no longer exists
lockExists, err = lb.bucket.Exists(ctx, lb.lockPath(aStackRef.(localBackendReference).FullyQualifiedName()))
assert.NoError(t, err)
assert.False(t, lockExists)
// Make another filestate backend which will have a different lockId
ob, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir))
assert.NoError(t, err)
otherBackend, ok := ob.(*localBackend)
assert.True(t, ok)
assert.NotNil(t, lb)
// Lock the stack with this new backend, then check that checkForLocks on the first backend now errors
err = otherBackend.Lock(ctx, aStackRef)
assert.NoError(t, err)
err = lb.checkForLock(ctx, aStackRef)
assert.Error(t, err)
// Now call CancelCurrentUpdate and check that checkForLocks no longer errors
err = lb.CancelCurrentUpdate(ctx, aStackRef)
assert.NoError(t, err)
err = lb.checkForLock(ctx, aStackRef)
assert.NoError(t, err)
}
func TestRemoveMakesBackups(t *testing.T) {
t.Parallel()
// Login to a temp dir filestate backend
tmpDir := t.TempDir()
b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir))
assert.NoError(t, err)
ctx := context.Background()
// Grab the bucket interface to test with
lb, ok := b.(*localBackend)
assert.True(t, ok)
assert.NotNil(t, lb)
// Check that creating a new stack doesn't make a backup file
aStackRef, err := b.ParseStackReference("a")
assert.NoError(t, err)
aStack, err := b.CreateStack(ctx, aStackRef, nil)
assert.NoError(t, err)
assert.NotNil(t, aStack)
// Check the stack file now exists, but the backup file doesn't
stackFileExists, err := lb.bucket.Exists(ctx, lb.stackPath(aStackRef.FullyQualifiedName()))
assert.NoError(t, err)
assert.True(t, stackFileExists)
backupFileExists, err := lb.bucket.Exists(ctx, lb.stackPath(aStackRef.FullyQualifiedName())+".bak")
assert.NoError(t, err)
assert.False(t, backupFileExists)
// Now remove the stack
removed, err := b.RemoveStack(ctx, aStack, false)
assert.NoError(t, err)
assert.False(t, removed)
// Check the stack file is now gone, but the backup file exists
stackFileExists, err = lb.bucket.Exists(ctx, lb.stackPath(aStackRef.FullyQualifiedName()))
assert.NoError(t, err)
assert.False(t, stackFileExists)
backupFileExists, err = lb.bucket.Exists(ctx, lb.stackPath(aStackRef.FullyQualifiedName())+".bak")
assert.NoError(t, err)
assert.True(t, backupFileExists)
}
func TestRenameWorks(t *testing.T) {
t.Parallel()
// Login to a temp dir filestate backend
tmpDir := t.TempDir()
b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir))
assert.NoError(t, err)
ctx := context.Background()
// Grab the bucket interface to test with
lb, ok := b.(*localBackend)
assert.True(t, ok)
assert.NotNil(t, lb)
// Create a new stack
aStackRef, err := b.ParseStackReference("a")
assert.NoError(t, err)
aStack, err := b.CreateStack(ctx, aStackRef, nil)
assert.NoError(t, err)
assert.NotNil(t, aStack)
// Check the stack file now exists
stackFileExists, err := lb.bucket.Exists(ctx, lb.stackPath(aStackRef.FullyQualifiedName()))
assert.NoError(t, err)
assert.True(t, stackFileExists)
// Fake up some history
err = lb.addToHistory("a", backend.UpdateInfo{Kind: apitype.DestroyUpdate})
assert.NoError(t, err)
// And pollute the history folder
err = lb.bucket.WriteAll(ctx, path.Join(lb.historyDirectory("a"), "randomfile.txt"), []byte{0, 13}, nil)
assert.NoError(t, err)
// Rename the stack
bStackRef, err := b.RenameStack(ctx, aStack, "b")
assert.NoError(t, err)
assert.Equal(t, "b", bStackRef.String())
// Check the new stack file now exists and the old one is gone
stackFileExists, err = lb.bucket.Exists(ctx, lb.stackPath(bStackRef.FullyQualifiedName()))
assert.NoError(t, err)
assert.True(t, stackFileExists)
stackFileExists, err = lb.bucket.Exists(ctx, lb.stackPath(aStackRef.FullyQualifiedName()))
assert.NoError(t, err)
assert.False(t, stackFileExists)
// Rename again
bStack, err := b.GetStack(ctx, bStackRef)
assert.NoError(t, err)
cStackRef, err := b.RenameStack(ctx, bStack, "c")
assert.NoError(t, err)
assert.Equal(t, "c", cStackRef.String())
// Check the new stack file now exists and the old one is gone
stackFileExists, err = lb.bucket.Exists(ctx, lb.stackPath(cStackRef.FullyQualifiedName()))
assert.NoError(t, err)
assert.True(t, stackFileExists)
stackFileExists, err = lb.bucket.Exists(ctx, lb.stackPath(bStackRef.FullyQualifiedName()))
assert.NoError(t, err)
assert.False(t, stackFileExists)
// Check we can still get the history
history, err := b.GetHistory(ctx, cStackRef, 10, 0)
assert.NoError(t, err)
assert.Len(t, history, 1)
assert.Equal(t, apitype.DestroyUpdate, history[0].Kind)
}
func TestLoginToNonExistingFolderFails(t *testing.T) {
t.Parallel()
fakeDir := "file://" + filepath.ToSlash(os.TempDir()) + "/non-existing"
b, err := New(cmdutil.Diag(), fakeDir)
assert.Error(t, err)
assert.Nil(t, b)
}
// TestParseEmptyStackFails demonstrates that ParseStackReference returns
// an error when the stack name is the empty string.TestParseEmptyStackFails
func TestParseEmptyStackFails(t *testing.T) {
t.Parallel()
// ParseStackReference does use the method receiver
// (it is a total function disguised as a method.)
var b *localBackend
var stackName = ""
var _, err = b.ParseStackReference(stackName)
assert.Error(t, err)
}
// Regression test for https://github.com/pulumi/pulumi/issues/10439
func TestHtmlEscaping(t *testing.T) {
t.Parallel()
sm := b64.NewBase64SecretsManager()
resources := []*resource.State{
{
URN: resource.NewURN("a", "proj", "d:e:f", "a:b:c", "name"),
Type: "a:b:c",
Inputs: resource.PropertyMap{
resource.PropertyKey("html"): resource.NewStringProperty("<html@tags>"),
},
},
}
snap := deploy.NewSnapshot(deploy.Manifest{}, sm, resources, nil)
sdep, err := stack.SerializeDeployment(snap, snap.SecretsManager, false /* showSecrsts */)
assert.NoError(t, err)
data, err := encoding.JSON.Marshal(sdep)
assert.NoError(t, err)
// Ensure data has the string contents "<html@tags>"", not "\u003chtml\u0026tags\u003e"
// ImportDeployment below should not modify the data
assert.Contains(t, string(data), "<html@tags>")
udep := &apitype.UntypedDeployment{
Version: 3,
Deployment: json.RawMessage(data),
}
// Login to a temp dir filestate backend
tmpDir := t.TempDir()
b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir))
assert.NoError(t, err)
ctx := context.Background()
// Create stack "a" and import a checkpoint with a secret
aStackRef, err := b.ParseStackReference("a")
assert.NoError(t, err)
aStack, err := b.CreateStack(ctx, aStackRef, nil)
assert.NoError(t, err)
assert.NotNil(t, aStack)
err = b.ImportDeployment(ctx, aStack, udep)
assert.NoError(t, err)
// Ensure the file has the string contents "<html@tags>"", not "\u003chtml\u0026tags\u003e"
// Grab the bucket interface to read the file with
lb, ok := b.(*localBackend)
assert.True(t, ok)
assert.NotNil(t, lb)
chkpath := lb.stackPath("a")
bytes, err := lb.bucket.ReadAll(context.Background(), chkpath)
assert.NoError(t, err)
state := string(bytes)
assert.Contains(t, state, "<html@tags>")
}
func TestLegacyFolderStructure(t *testing.T) {
t.Parallel()
// Login to a temp dir filestate backend
tmpDir := t.TempDir()
b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir))
assert.NoError(t, err)
ctx := context.Background()
// Make a dummy stack file in the legacy location
err = os.MkdirAll(path.Join(tmpDir, ".pulumi", "stacks"), os.ModePerm)
assert.NoError(t, err)
err = os.WriteFile(path.Join(tmpDir, ".pulumi", "stacks", "a.json"), []byte("{}"), os.ModePerm)
assert.NoError(t, err)
// Check that list stack shows that stack
stacks, token, err := b.ListStacks(ctx, backend.ListStacksFilter{}, nil /* inContToken */)
assert.NoError(t, err)
assert.Nil(t, token)
assert.Len(t, stacks, 1)
assert.Equal(t, "a", stacks[0].Name().String())
// Create a new non-project stack
bRef, err := b.ParseStackReference("b")
assert.NoError(t, err)
assert.Equal(t, "b", bRef.String())
bStack, err := b.CreateStack(ctx, bRef, nil)
assert.NoError(t, err)
assert.Equal(t, "b", bStack.Ref().String())
assert.FileExists(t, path.Join(tmpDir, ".pulumi", "stacks", "b.json"))
}
func TestProjectFolderStructure(t *testing.T) {
t.Parallel()
// Login to a temp dir filestate backend
tmpDir := t.TempDir()
b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir))
assert.NoError(t, err)
ctx := context.Background()
// Make a dummy stack file in the legacy location
err = os.MkdirAll(path.Join(tmpDir, ".pulumi", "stacks", "testproj"), os.ModePerm)
assert.NoError(t, err)
err = os.WriteFile(path.Join(tmpDir, ".pulumi", "stacks", "testproj", "a.json"), []byte("{}"), os.ModePerm)
assert.NoError(t, err)
// Check that testproj is reported as existing
exists, err := b.DoesProjectExist(ctx, "testproj")
assert.NoError(t, err)
assert.True(t, exists)
// Check that list stack shows that stack
stacks, token, err := b.ListStacks(ctx, backend.ListStacksFilter{}, nil /* inContToken */)
assert.NoError(t, err)
assert.Nil(t, token)
assert.Len(t, stacks, 1)
assert.Equal(t, "testproj/a", stacks[0].Name().String())
// Create a new project stack
bRef, err := b.ParseStackReference("testproj/b")
assert.NoError(t, err)
assert.Equal(t, "testproj/b", bRef.String())
bStack, err := b.CreateStack(ctx, bRef, nil)
assert.NoError(t, err)
assert.Equal(t, "testproj/b", bStack.Ref().String())
assert.FileExists(t, path.Join(tmpDir, ".pulumi", "stacks", "testproj", "b.json"))
}
func TestCanRenameStack(t *testing.T) {
t.Parallel()
// Login to a temp dir filestate backend
tmpDir := t.TempDir()
b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir))
assert.NoError(t, err)
ctx := context.Background()
// Create a new non-project stack
bRef, err := b.ParseStackReference("b")
assert.NoError(t, err)
assert.Equal(t, "b", bRef.String())
bStack, err := b.CreateStack(ctx, bRef, nil)
assert.NoError(t, err)
assert.Equal(t, "b", bStack.Ref().String())
assert.FileExists(t, path.Join(tmpDir, ".pulumi", "stacks", "b.json"))
// Rename it
newBStack, err := b.RenameStack(ctx, bStack, "testproj/b")
assert.NoError(t, err)
assert.Equal(t, "testproj/b", newBStack.String())
assert.FileExists(t, path.Join(tmpDir, ".pulumi", "stacks", "testproj", "b.json"))
}
func chdir(t *testing.T, dir string) {
cwd, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(dir)) // Set directory
t.Cleanup(func() {
require.NoError(t, os.Chdir(cwd)) // Restore directory
restoredDir, err := os.Getwd()
require.NoError(t, err)
require.Equal(t, cwd, restoredDir)
})
}
//nolint:paralleltest // mutates cwd
func TestProjectNameMustMatch(t *testing.T) {
// Login to a temp dir filestate backend
tmpDir := t.TempDir()
b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir))
require.NoError(t, err)
ctx := context.Background()
// Create a new project
projectDir := t.TempDir()
pyaml := filepath.Join(projectDir, "Pulumi.yaml")
err = os.WriteFile(pyaml, []byte("name: my-project\nruntime: test"), 0600)
require.NoError(t, err)
chdir(t, projectDir)
// Create a new non-project stack
aRef, err := b.ParseStackReference("a")
assert.NoError(t, err)
assert.Equal(t, "a", aRef.String())
aStack, err := b.CreateStack(ctx, aRef, nil)
assert.NoError(t, err)
assert.Equal(t, "a", aStack.Ref().String())
assert.FileExists(t, path.Join(tmpDir, ".pulumi", "stacks", "a.json"))
// Create a new project stack with the wrong project name
bRef, err := b.ParseStackReference("not-my-project/b")
assert.NoError(t, err)
assert.Equal(t, "not-my-project/b", bRef.String())
bStack, err := b.CreateStack(ctx, bRef, nil)
assert.Error(t, err)
assert.Nil(t, bStack)
// Create a new project stack with the right project name
cRef, err := b.ParseStackReference("my-project/c")
assert.NoError(t, err)
assert.Equal(t, "my-project/c", cRef.String())
cStack, err := b.CreateStack(ctx, cRef, nil)
assert.NoError(t, err)
assert.Equal(t, "my-project/c", cStack.Ref().String())
assert.FileExists(t, path.Join(tmpDir, ".pulumi", "stacks", "my-project", "c.json"))
}