|
| 1 | +package cli_test |
| 2 | + |
| 3 | +import ( |
| 4 | +"bytes" |
| 5 | +"os" |
| 6 | +"path/filepath" |
| 7 | +"testing" |
| 8 | + |
| 9 | +"github.com/google/uuid" |
| 10 | +"github.com/stretchr/testify/require" |
| 11 | + |
| 12 | +"github.com/coder/coder/cli/clitest" |
| 13 | +"github.com/coder/coder/coderd/coderdtest" |
| 14 | +"github.com/coder/coder/provisioner/echo" |
| 15 | +"github.com/coder/coder/provisionersdk/proto" |
| 16 | +"github.com/coder/coder/pty/ptytest" |
| 17 | +) |
| 18 | + |
| 19 | +funcTestTemplatePull(t*testing.T) { |
| 20 | +t.Parallel() |
| 21 | + |
| 22 | +// Stdout tests that 'templates pull' pulls down the latest template |
| 23 | +// and writes it to stdout. |
| 24 | +t.Run("Stdout",func(t*testing.T) { |
| 25 | +t.Parallel() |
| 26 | + |
| 27 | +client:=coderdtest.New(t,&coderdtest.Options{IncludeProvisionerD:true}) |
| 28 | +user:=coderdtest.CreateFirstUser(t,client) |
| 29 | + |
| 30 | +// Create an initial template bundle. |
| 31 | +source1:=genTemplateVersionSource() |
| 32 | +// Create an updated template bundle. This will be used to ensure |
| 33 | +// that templates are correctly returned in order from latest to oldest. |
| 34 | +source2:=genTemplateVersionSource() |
| 35 | + |
| 36 | +expected,err:=echo.Tar(source2) |
| 37 | +require.NoError(t,err) |
| 38 | + |
| 39 | +version1:=coderdtest.CreateTemplateVersion(t,client,user.OrganizationID,source1) |
| 40 | +_=coderdtest.AwaitTemplateVersionJob(t,client,version1.ID) |
| 41 | + |
| 42 | +template:=coderdtest.CreateTemplate(t,client,user.OrganizationID,version1.ID) |
| 43 | + |
| 44 | +// Update the template version so that we can assert that templates |
| 45 | +// are being sorted correctly. |
| 46 | +_=coderdtest.UpdateTemplateVersion(t,client,user.OrganizationID,source2,template.ID) |
| 47 | + |
| 48 | +cmd,root:=clitest.New(t,"templates","pull",template.Name) |
| 49 | +clitest.SetupConfig(t,client,root) |
| 50 | + |
| 51 | +varbuf bytes.Buffer |
| 52 | +cmd.SetOut(&buf) |
| 53 | + |
| 54 | +err=cmd.Execute() |
| 55 | +require.NoError(t,err) |
| 56 | + |
| 57 | +require.True(t,bytes.Equal(expected,buf.Bytes()),"tar files differ") |
| 58 | +}) |
| 59 | + |
| 60 | +// ToFile tests that 'templates pull' pulls down the latest template |
| 61 | +// and writes it to the correct directory. |
| 62 | +t.Run("ToFile",func(t*testing.T) { |
| 63 | +t.Parallel() |
| 64 | + |
| 65 | +client:=coderdtest.New(t,&coderdtest.Options{IncludeProvisionerD:true}) |
| 66 | +user:=coderdtest.CreateFirstUser(t,client) |
| 67 | + |
| 68 | +// Create an initial template bundle. |
| 69 | +source1:=genTemplateVersionSource() |
| 70 | +// Create an updated template bundle. This will be used to ensure |
| 71 | +// that templates are correctly returned in order from latest to oldest. |
| 72 | +source2:=genTemplateVersionSource() |
| 73 | + |
| 74 | +expected,err:=echo.Tar(source2) |
| 75 | +require.NoError(t,err) |
| 76 | + |
| 77 | +version1:=coderdtest.CreateTemplateVersion(t,client,user.OrganizationID,source1) |
| 78 | +_=coderdtest.AwaitTemplateVersionJob(t,client,version1.ID) |
| 79 | + |
| 80 | +template:=coderdtest.CreateTemplate(t,client,user.OrganizationID,version1.ID) |
| 81 | + |
| 82 | +// Update the template version so that we can assert that templates |
| 83 | +// are being sorted correctly. |
| 84 | +_=coderdtest.UpdateTemplateVersion(t,client,user.OrganizationID,source2,template.ID) |
| 85 | + |
| 86 | +dir:=t.TempDir() |
| 87 | + |
| 88 | +dest:=filepath.Join(dir,"actual.tar") |
| 89 | + |
| 90 | +// Create the file so that we can test that the command |
| 91 | +// warns the user before overwriting a preexisting file. |
| 92 | +fi,err:=os.OpenFile(dest,os.O_CREATE|os.O_RDONLY,0600) |
| 93 | +require.NoError(t,err) |
| 94 | +_=fi.Close() |
| 95 | + |
| 96 | +cmd,root:=clitest.New(t,"templates","pull",template.Name,dest) |
| 97 | +clitest.SetupConfig(t,client,root) |
| 98 | + |
| 99 | +pty:=ptytest.New(t) |
| 100 | +cmd.SetIn(pty.Input()) |
| 101 | +cmd.SetOut(pty.Output()) |
| 102 | + |
| 103 | +errChan:=make(chanerror) |
| 104 | +gofunc() { |
| 105 | +deferclose(errChan) |
| 106 | +errChan<-cmd.Execute() |
| 107 | +}() |
| 108 | + |
| 109 | +// We expect to be prompted that a file already exists. |
| 110 | +pty.ExpectMatch("already exists") |
| 111 | +pty.WriteLine("yes") |
| 112 | + |
| 113 | +require.NoError(t,<-errChan) |
| 114 | + |
| 115 | +actual,err:=os.ReadFile(dest) |
| 116 | +require.NoError(t,err) |
| 117 | + |
| 118 | +require.True(t,bytes.Equal(actual,expected),"tar files differ") |
| 119 | +}) |
| 120 | +} |
| 121 | + |
| 122 | +// genTemplateVersionSource returns a unique bundle that can be used to create |
| 123 | +// a template version source. |
| 124 | +funcgenTemplateVersionSource()*echo.Responses { |
| 125 | +return&echo.Responses{ |
| 126 | +Parse: []*proto.Parse_Response{ |
| 127 | +{ |
| 128 | +Type:&proto.Parse_Response_Log{ |
| 129 | +Log:&proto.Log{ |
| 130 | +Output:uuid.NewString(), |
| 131 | +}, |
| 132 | +}, |
| 133 | +}, |
| 134 | + |
| 135 | +{ |
| 136 | +Type:&proto.Parse_Response_Complete{ |
| 137 | +Complete:&proto.Parse_Complete{}, |
| 138 | +}, |
| 139 | +}, |
| 140 | +}, |
| 141 | +Provision:echo.ProvisionComplete, |
| 142 | +} |
| 143 | +} |