mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
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:
@ -83,7 +83,7 @@ func (r *RootCmd) templatePull() *clibase.Cmd {
|
||||
}
|
||||
|
||||
if dest == "" {
|
||||
dest = templateName + "/"
|
||||
dest = templateName
|
||||
}
|
||||
|
||||
err = os.MkdirAll(dest, 0o750)
|
||||
|
@ -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) {
|
||||
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) {
|
||||
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),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// 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
|
||||
t.Run("FolderConflict", func(t *testing.T) {
|
||||
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
|
||||
|
Reference in New Issue
Block a user