Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: Read params from file for template/workspace creation #1541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 20, 2022
Merged
Prev Previous commit
Next Next commit
Fix unit tests for Windows
  • Loading branch information
AbhineetJain committed May 20, 2022
commit 149162029a1347831c6a2c392c798c6ca1504ddc
8 changes: 6 additions & 2 deletions cli/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ func TestCreate(t *testing.T) {

coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
parameterFile, _ := os.CreateTemp(t.TempDir(), "testParameterFile*.yaml")
tempDir := t.TempDir()
parameterFile, _ := os.CreateTemp(tempDir, "testParameterFile*.yaml")
_, _ = parameterFile.WriteString("region: \"bingo\"\nusername: \"boingo\"")
cmd, root := clitest.New(t, "create", "", "--parameter-file", parameterFile.Name())
clitest.SetupConfig(t, client, root)
Expand All @@ -187,6 +188,7 @@ func TestCreate(t *testing.T) {
pty.WriteLine(value)
}
<-doneChan
removeTmpDirUntilSuccess(t, tempDir)
})
t.Run("WithParameterFileNotContainingTheValue", func(t *testing.T) {
t.Parallel()
Expand All @@ -201,7 +203,8 @@ func TestCreate(t *testing.T) {
})
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
parameterFile, _ := os.CreateTemp(t.TempDir(), "testParameterFile*.yaml")
tempDir := t.TempDir()
parameterFile, _ := os.CreateTemp(tempDir, "testParameterFile*.yaml")
_, _ = parameterFile.WriteString("zone: \"bananas\"")
cmd, root := clitest.New(t, "create", "my-workspace", "--template", template.Name, "--parameter-file", parameterFile.Name())
clitest.SetupConfig(t, client, root)
Expand All @@ -215,6 +218,7 @@ func TestCreate(t *testing.T) {
require.EqualError(t, err, "Parameter value absent in parameter file for \"region\"!")
}()
<-doneChan
removeTmpDirUntilSuccess(t, tempDir)
})
}

Expand Down
17 changes: 10 additions & 7 deletions cli/parameter_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ func TestCreateParameterMapFromFile(t *testing.T) {
t.Parallel()
t.Run("CreateParameterMapFromFile", func(t *testing.T) {
t.Parallel()
parameterFile, _ := os.CreateTemp(t.TempDir(), "testParameterFile*.yaml")
tempDir := t.TempDir()
parameterFile, _ := os.CreateTemp(tempDir, "testParameterFile*.yaml")
_, _ = parameterFile.WriteString("region: \"bananas\"\ndisk: \"20\"\n")

parameterMapFromFile, err := createParameterMapFromFile(parameterFile.Name())
Expand All @@ -25,7 +26,7 @@ func TestCreateParameterMapFromFile(t *testing.T) {
assert.Equal(t, expectedMap, parameterMapFromFile)
assert.Nil(t, err)

removeTmpDirUntilSuccess(t)
removeTmpDirUntilSuccess(t, tempDir)
})
t.Run("WithEmptyFilename", func(t *testing.T) {
t.Parallel()
Expand All @@ -52,23 +53,25 @@ func TestCreateParameterMapFromFile(t *testing.T) {
})
t.Run("WithInvalidYAML", func(t *testing.T) {
t.Parallel()
parameterFile, _ := os.CreateTemp(t.TempDir(), "testParameterFile*.yaml")
tempDir := t.TempDir()
parameterFile, _ := os.CreateTemp(tempDir, "testParameterFile*.yaml")
_, _ = parameterFile.WriteString("region = \"bananas\"\ndisk = \"20\"\n")

parameterMapFromFile, err := createParameterMapFromFile(parameterFile.Name())

assert.Nil(t, parameterMapFromFile)
assert.EqualError(t, err, "yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `region ...` into map[string]string")

removeTmpDirUntilSuccess(t)
removeTmpDirUntilSuccess(t, tempDir)
})
}

func removeTmpDirUntilSuccess(t *testing.T) {
func removeTmpDirUntilSuccess(t *testing.T, tempDir string) {
t.Helper()
t.Cleanup(func() {
err := os.RemoveAll(t.TempDir())
err := os.RemoveAll(tempDir)
for err != nil {
err = os.RemoveAll(t.TempDir())
err = os.RemoveAll(tempDir)
}
})
}
18 changes: 16 additions & 2 deletions cli/templatecreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ func TestTemplateCreate(t *testing.T) {
Provision: echo.ProvisionComplete,
ProvisionDryRun: echo.ProvisionComplete,
})
parameterFile, _ := os.CreateTemp(t.TempDir(), "testParameterFile*.yaml")
tempDir := t.TempDir()
parameterFile, _ := os.CreateTemp(tempDir, "testParameterFile*.yaml")
_, _ = parameterFile.WriteString("region: \"bananas\"")
cmd, root := clitest.New(t, "templates", "create", "my-template", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--parameter-file", parameterFile.Name())
clitest.SetupConfig(t, client, root)
Expand All @@ -121,6 +122,7 @@ func TestTemplateCreate(t *testing.T) {
}

require.NoError(t, <-execDone)
removeTmpDirUntilSuccess(t, tempDir)
})

t.Run("WithParameterFileNotContainingTheValue", func(t *testing.T) {
Expand All @@ -132,7 +134,8 @@ func TestTemplateCreate(t *testing.T) {
Provision: echo.ProvisionComplete,
ProvisionDryRun: echo.ProvisionComplete,
})
parameterFile, _ := os.CreateTemp(t.TempDir(), "testParameterFile*.yaml")
tempDir := t.TempDir()
parameterFile, _ := os.CreateTemp(tempDir, "testParameterFile*.yaml")
_, _ = parameterFile.WriteString("zone: \"bananas\"")
cmd, root := clitest.New(t, "templates", "create", "my-template", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--parameter-file", parameterFile.Name())
clitest.SetupConfig(t, client, root)
Expand All @@ -157,6 +160,7 @@ func TestTemplateCreate(t *testing.T) {
}

require.EqualError(t, <-execDone, "Parameter value absent in parameter file for \"region\"!")
removeTmpDirUntilSuccess(t, tempDir)
})
}

Expand All @@ -176,3 +180,13 @@ func createTestParseResponse() []*proto.Parse_Response {
},
}}
}

func removeTmpDirUntilSuccess(t *testing.T, tempDir string) {
t.Helper()
t.Cleanup(func() {
err := os.RemoveAll(tempDir)
for err != nil {
err = os.RemoveAll(tempDir)
}
})
}