Skip to content

Commit

Permalink
Merge #13325
Browse files Browse the repository at this point in the history
13325: test: Attempt to fix TestCreatingProjectWithEmptyConfig r=abhinav a=abhinav

TestCreatingProjectWithEmptyConfig appears to be broken on master:

```
=== Failed
=== FAIL: cmd/pulumi TestCreatingProjectWithEmptyConfig (0.00s)
    new_test.go:330:
        	Error Trace:	/home/runner/work/pulumi/pulumi/pkg/cmd/pulumi/new_test.go:330
        	Error:      	Received unexpected error:
        	            	A project with this name already exists
        	Test:       	TestCreatingProjectWithEmptyConfig

=== FAIL: cmd/pulumi TestCreatingProjectWithEmptyConfig (re-run 1) (0.28s)
    new_test.go:330:
        	Error Trace:	/home/runner/work/pulumi/pulumi/pkg/cmd/pulumi/new_test.go:330
        	Error:      	Received unexpected error:
        	            	A project with this name already exists
        	Test:       	TestCreatingProjectWithEmptyConfig
```

As seen in #13323 and the attempts to land #13322.

To reduce likelihood of "name already exists" errors,
generate a random name for all tests where the name is currently based
on the name of the temporary directory.

Also, change all manually managed temporary directories
(with an unchecked error) to `t.TempDir()` -- making the testing package
handle cleanup of the temporary directories.

Note: The randomly generated directory name is used
only in cases where the name of the directory was used as input
in the rest of the test.

Co-authored-by: Abhinav Gupta <abhinav@pulumi.com>
  • Loading branch information
bors[bot] and abhinav committed Jun 29, 2023
2 parents af1cbee + eed0bd5 commit d0aa3c8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
18 changes: 8 additions & 10 deletions pkg/cmd/pulumi/new_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func chdir(t *testing.T, dir string) {
func TestCreatingStackWithArgsSpecifiedName(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := t.TempDir()
chdir(t, tempdir)

args := newArgs{
Expand Down Expand Up @@ -106,8 +105,8 @@ func TestCreatingStackWithNumericName(t *testing.T) {
func TestCreatingStackWithPromptedName(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
chdir(t, tempdir)
uniqueProjectName := filepath.Base(tempdir)

Expand All @@ -129,8 +128,8 @@ func TestCreatingStackWithPromptedName(t *testing.T) {
func TestCreatingProjectWithDefaultName(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
chdir(t, tempdir)
defaultProjectName := filepath.Base(tempdir)

Expand Down Expand Up @@ -161,17 +160,16 @@ func TestCreatingProjectWithPulumiBackendURL(t *testing.T) {
require.NoError(t, err)
assert.True(t, strings.HasPrefix(b.URL(), "https://app.pulumi.com"))

fileStateDir, _ := os.MkdirTemp("", "local-state-dir")
defer os.RemoveAll(fileStateDir)
fileStateDir := t.TempDir()

// Now override to local filesystem backend
backendURL := "file://" + filepath.ToSlash(fileStateDir)
t.Setenv("PULUMI_CONFIG_PASSPHRASE", "how now brown cow")
t.Setenv(workspace.PulumiBackendURLEnvVar, backendURL)

backendInstance = nil
tempdir, _ := os.MkdirTemp("", "test-env-local")
defer os.RemoveAll(tempdir)
tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
chdir(t, tempdir)
defaultProjectName := filepath.Base(tempdir)

Expand Down
61 changes: 31 additions & 30 deletions pkg/cmd/pulumi/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package main

import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"path/filepath"
Expand All @@ -34,8 +36,7 @@ import (
func TestFailInInteractiveWithoutYes(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := t.TempDir()
chdir(t, tempdir)

args := newArgs{
Expand All @@ -55,8 +56,7 @@ func TestFailInInteractiveWithoutYes(t *testing.T) {
func TestCreatingStackWithArgsSpecifiedOrgName(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := t.TempDir()
chdir(t, tempdir)

orgStackName := fmt.Sprintf("%s/%s", currentUser(t), stackName)
Expand All @@ -81,8 +81,8 @@ func TestCreatingStackWithArgsSpecifiedOrgName(t *testing.T) {
func TestCreatingStackWithPromptedOrgName(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
chdir(t, tempdir)

uniqueProjectName := filepath.Base(tempdir)
Expand All @@ -106,8 +106,8 @@ func TestCreatingStackWithPromptedOrgName(t *testing.T) {
func TestCreatingStackWithArgsSpecifiedFullNameSucceeds(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
chdir(t, tempdir)

// the project name and the project name in the stack name must match
Expand All @@ -134,8 +134,8 @@ func TestCreatingStackWithArgsSpecifiedFullNameSucceeds(t *testing.T) {
func TestCreatingProjectWithArgsSpecifiedName(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
chdir(t, tempdir)
uniqueProjectName := filepath.Base(tempdir) + "test"

Expand All @@ -162,8 +162,8 @@ func TestCreatingProjectWithArgsSpecifiedName(t *testing.T) {
func TestCreatingProjectWithPromptedName(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
chdir(t, tempdir)
uniqueProjectName := filepath.Base(tempdir) + "test"

Expand All @@ -187,8 +187,7 @@ func TestCreatingProjectWithPromptedName(t *testing.T) {
func TestCreatingProjectWithExistingArgsSpecifiedNameFails(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := t.TempDir()
chdir(t, tempdir)

backendInstance = &backend.MockBackend{
Expand All @@ -215,8 +214,7 @@ func TestCreatingProjectWithExistingArgsSpecifiedNameFails(t *testing.T) {
func TestCreatingProjectWithExistingPromptedNameFails(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := t.TempDir()
chdir(t, tempdir)

backendInstance = &backend.MockBackend{
Expand All @@ -241,8 +239,7 @@ func TestCreatingProjectWithExistingPromptedNameFails(t *testing.T) {
func TestGeneratingProjectWithExistingArgsSpecifiedNameSucceeds(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := t.TempDir()
chdir(t, tempdir)

backendInstance = &backend.MockBackend{
Expand Down Expand Up @@ -273,8 +270,7 @@ func TestGeneratingProjectWithExistingArgsSpecifiedNameSucceeds(t *testing.T) {
func TestGeneratingProjectWithExistingPromptedNameSucceeds(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := t.TempDir()
chdir(t, tempdir)

backendInstance = &backend.MockBackend{
Expand Down Expand Up @@ -342,8 +338,7 @@ func TestCreatingProjectWithEmptyConfig(t *testing.T) {
func TestGeneratingProjectWithInvalidArgsSpecifiedNameFails(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := t.TempDir()
chdir(t, tempdir)

backendInstance = &backend.MockBackend{
Expand Down Expand Up @@ -372,8 +367,7 @@ func TestGeneratingProjectWithInvalidArgsSpecifiedNameFails(t *testing.T) {
func TestGeneratingProjectWithInvalidPromptedNameFails(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := t.TempDir()
chdir(t, tempdir)

backendInstance = &backend.MockBackend{
Expand Down Expand Up @@ -409,8 +403,7 @@ func TestInvalidTemplateName(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

t.Run("NoTemplateSpecified", func(t *testing.T) {
tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := t.TempDir()
chdir(t, tempdir)

args := newArgs{
Expand All @@ -427,8 +420,7 @@ func TestInvalidTemplateName(t *testing.T) {
})

t.Run("RemoteTemplateNotFound", func(t *testing.T) {
tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := t.TempDir()
chdir(t, tempdir)

// A template that will never exist.
Expand All @@ -448,8 +440,7 @@ func TestInvalidTemplateName(t *testing.T) {
})

t.Run("LocalTemplateNotFound", func(t *testing.T) {
tempdir, _ := os.MkdirTemp("", "test-env")
defer os.RemoveAll(tempdir)
tempdir := t.TempDir()
chdir(t, tempdir)

// A template that will never exist remotely.
Expand Down Expand Up @@ -818,3 +809,13 @@ func TestErrorIfNotEmptyDirectory(t *testing.T) {
})
}
}

func genUniqueName(t *testing.T) string {
t.Helper()

var bs [8]byte
_, err := rand.Read(bs[:])
require.NoError(t, err)

return hex.EncodeToString(bs[:])
}

0 comments on commit d0aa3c8

Please sign in to comment.