Skip to content

Commit

Permalink
Merge #13330
Browse files Browse the repository at this point in the history
13330: test(cmd/pulumi): Always use a valid project directory name r=abhinav a=abhinav

Using `t.TempDir()` or a hex-encoded string isn't enough
because the name may start with a number.

This switches all such temporary directories to use:

    $tempDir/test-$random

Where `$tempDir` is managed by the current test.

This will address flaky errors like:

```
=== FAIL: cmd/pulumi TestCreatingStackWithArgsSpecifiedOrgName (0.69s)
    new_test.go:74:
        	Error Trace:	/home/runner/work/pulumi/pulumi/pkg/cmd/pulumi/new_test.go:74
        	Error:      	Received unexpected error:
        	            	'001' is not a valid project name. a project with this name already exists: 001
        	Test:       	TestCreatingStackWithArgsSpecifiedOrgName
```

Because the temporary directory was:

    /tmp/TestCreatingStackWithArgsSpecifiedOrgName963299567/001


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

tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

args := newArgs{
Expand All @@ -69,7 +69,7 @@ func TestCreatingStackWithArgsSpecifiedName(t *testing.T) {
func TestCreatingStackWithNumericName(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

// This test requires a numeric project name.
Expand Down Expand Up @@ -105,8 +105,7 @@ func TestCreatingStackWithNumericName(t *testing.T) {
func TestCreatingStackWithPromptedName(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
tempdir := tempProjectDir(t)
chdir(t, tempdir)
uniqueProjectName := filepath.Base(tempdir)

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

tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
tempdir := tempProjectDir(t)
chdir(t, tempdir)
defaultProjectName := filepath.Base(tempdir)

Expand Down Expand Up @@ -168,8 +166,7 @@ func TestCreatingProjectWithPulumiBackendURL(t *testing.T) {
t.Setenv(workspace.PulumiBackendURLEnvVar, backendURL)

backendInstance = nil
tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
tempdir := tempProjectDir(t)
chdir(t, tempdir)
defaultProjectName := filepath.Base(tempdir)

Expand Down Expand Up @@ -235,7 +232,7 @@ func currentUser(t *testing.T) string {

func loadStackName(t *testing.T) string {
w, err := workspace.New()
assert.NoError(t, err)
require.NoError(t, err)
return w.Settings().Stack
}

Expand Down
47 changes: 25 additions & 22 deletions pkg/cmd/pulumi/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
func TestFailInInteractiveWithoutYes(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

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

tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

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

tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
tempdir := tempProjectDir(t)
chdir(t, tempdir)

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

tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
tempdir := tempProjectDir(t)
chdir(t, tempdir)

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

tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
tempdir := tempProjectDir(t)
chdir(t, tempdir)
uniqueProjectName := filepath.Base(tempdir) + "test"

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

tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
tempdir := tempProjectDir(t)
chdir(t, tempdir)
uniqueProjectName := filepath.Base(tempdir) + "test"

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

tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

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

tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

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

tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

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

tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

backendInstance = &backend.MockBackend{
Expand Down Expand Up @@ -300,8 +296,7 @@ func TestCreatingProjectWithEmptyConfig(t *testing.T) {
// Regression test for https://github.com/pulumi/pulumi/issues/4081
skipIfShortOrNoPulumiAccessToken(t)

tempdir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(tempdir, 0o700))
tempdir := tempProjectDir(t)
chdir(t, tempdir)
uniqueProjectName := filepath.Base(tempdir) + "test"

Expand Down Expand Up @@ -339,7 +334,7 @@ func TestCreatingProjectWithEmptyConfig(t *testing.T) {
func TestGeneratingProjectWithInvalidArgsSpecifiedNameFails(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

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

tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

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

t.Run("NoTemplateSpecified", func(t *testing.T) {
tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

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

t.Run("RemoteTemplateNotFound", func(t *testing.T) {
tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

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

t.Run("LocalTemplateNotFound", func(t *testing.T) {
tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

// A template that will never exist remotely.
Expand Down Expand Up @@ -811,12 +806,20 @@ func TestErrorIfNotEmptyDirectory(t *testing.T) {
}
}

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

dir := filepath.Join(t.TempDir(), genUniqueName(t))
require.NoError(t, os.MkdirAll(dir, 0o700))
return dir
}

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

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

return hex.EncodeToString(bs[:])
return "test-" + hex.EncodeToString(bs[:])
}
2 changes: 1 addition & 1 deletion pkg/cmd/pulumi/policy_new_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
func TestCreatingPolicyPackWithArgsSpecifiedName(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

args := newPolicyArgs{
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/pulumi/policy_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
func TestCreatingPolicyPackWithPromptedName(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)

tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

args := newPolicyArgs{
Expand All @@ -51,7 +51,7 @@ func TestInvalidPolicyPackTemplateName(t *testing.T) {
const nonExistantTemplate = "this-is-not-the-template-youre-looking-for"

t.Run("RemoteTemplateNotFound", func(t *testing.T) {
tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

args := newPolicyArgs{
Expand All @@ -66,7 +66,7 @@ func TestInvalidPolicyPackTemplateName(t *testing.T) {
})

t.Run("LocalTemplateNotFound", func(t *testing.T) {
tempdir := t.TempDir()
tempdir := tempProjectDir(t)
chdir(t, tempdir)

args := newPolicyArgs{
Expand Down

0 comments on commit c0c14fc

Please sign in to comment.