fix: fix coder template pull on Windows (#9327)

* fix: fix coder template pull on Windows

Signed-off-by: Spike Curtis <spike@coder.com>

* appease linter

Signed-off-by: Spike Curtis <spike@coder.com>

* improvements from code review

Signed-off-by: Spike Curtis <spike@coder.com>

---------

Signed-off-by: Spike Curtis <spike@coder.com>
This commit is contained in:
Spike Curtis
2023-08-25 14:58:13 +04:00
committed by GitHub
parent e7a231e44f
commit aed891b4ff
2 changed files with 165 additions and 112 deletions

View File

@ -83,7 +83,7 @@ func (r *RootCmd) templatePull() *clibase.Cmd {
}
if dest == "" {
dest = templateName + "/"
dest = templateName
}
err = os.MkdirAll(dest, 0o750)

View File

@ -40,20 +40,17 @@ func dirSum(t *testing.T, dir string) string {
return hex.EncodeToString(sum.Sum(nil))
}
func TestTemplatePull(t *testing.T) {
t.Parallel()
t.Run("NoName", func(t *testing.T) {
func TestTemplatePull_NoName(t *testing.T) {
t.Parallel()
inv, _ := clitest.New(t, "templates", "pull")
err := inv.Run()
require.Error(t, err)
})
}
// Stdout tests that 'templates pull' pulls down the latest template
// and writes it to stdout.
t.Run("Stdout", func(t *testing.T) {
// Stdout tests that 'templates pull' pulls down the latest template
// and writes it to stdout.
func TestTemplatePull_Stdout(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
@ -87,11 +84,11 @@ func TestTemplatePull(t *testing.T) {
require.NoError(t, err)
require.True(t, bytes.Equal(expected, buf.Bytes()), "tar files differ")
})
}
// ToDir tests that 'templates pull' pulls down the latest template
// and writes it to the correct directory.
t.Run("ToDir", func(t *testing.T) {
// ToDir tests that 'templates pull' pulls down the latest template
// and writes it to the correct directory.
func TestTemplatePull_ToDir(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
@ -135,11 +132,68 @@ func TestTemplatePull(t *testing.T) {
dirSum(t, expectedDest),
dirSum(t, actualDest),
)
})
}
// FolderConflict tests that 'templates pull' fails when a folder with has
// existing
t.Run("FolderConflict", func(t *testing.T) {
// ToDir tests that 'templates pull' pulls down the latest template
// and writes it to a directory with the name of the template if the path is not implicitly supplied.
// nolint: paralleltest
func TestTemplatePull_ToImplicit(t *testing.T) {
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)
// Create an initial template bundle.
source1 := genTemplateVersionSource()
// Create an updated template bundle. This will be used to ensure
// that templates are correctly returned in order from latest to oldest.
source2 := genTemplateVersionSource()
expected, err := echo.Tar(source2)
require.NoError(t, err)
version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, source1)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version1.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version1.ID)
// Update the template version so that we can assert that templates
// are being sorted correctly.
_ = coderdtest.UpdateTemplateVersion(t, client, user.OrganizationID, source2, template.ID)
// create a tempdir and change the working directory to it for the duration of the test (cannot run in parallel)
dir := t.TempDir()
wd, err := os.Getwd()
require.NoError(t, err)
err = os.Chdir(dir)
require.NoError(t, err)
defer func() {
err := os.Chdir(wd)
require.NoError(t, err, "if this fails, it can break other subsequent tests due to wrong working directory")
}()
expectedDest := filepath.Join(dir, "expected")
actualDest := filepath.Join(dir, template.Name)
ctx := context.Background()
err = extract.Tar(ctx, bytes.NewReader(expected), expectedDest, nil)
require.NoError(t, err)
inv, root := clitest.New(t, "templates", "pull", template.Name)
clitest.SetupConfig(t, client, root)
ptytest.New(t).Attach(inv)
require.NoError(t, inv.Run())
require.Equal(t,
dirSum(t, expectedDest),
dirSum(t, actualDest),
)
}
// FolderConflict tests that 'templates pull' fails when a folder with has
// existing
func TestTemplatePull_FolderConflict(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
@ -198,7 +252,6 @@ func TestTemplatePull(t *testing.T) {
require.NoError(t, err)
require.Len(t, ents, 1, "conflict folder should have single conflict file")
})
}
// genTemplateVersionSource returns a unique bundle that can be used to create