forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplatepull_test.go
More file actions
151 lines (115 loc) · 4.21 KB
/
templatepull_test.go
File metadata and controls
151 lines (115 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package cli_test
import (
"bytes"
"os"
"path/filepath"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
"github.com/coder/coder/pty/ptytest"
)
func TestTemplatePull(t *testing.T) {
t.Parallel()
t.Run("NoName", func(t *testing.T) {
t.Parallel()
cmd, _ := clitest.New(t, "templates", "pull")
err := cmd.Execute()
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) {
t.Parallel()
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)
cmd, root := clitest.New(t, "templates", "pull", template.Name)
clitest.SetupConfig(t, client, root)
var buf bytes.Buffer
cmd.SetOut(&buf)
err = cmd.Execute()
require.NoError(t, err)
require.True(t, bytes.Equal(expected, buf.Bytes()), "tar files differ")
})
// ToFile tests that 'templates pull' pulls down the latest template
// and writes it to the correct directory.
t.Run("ToFile", func(t *testing.T) {
t.Parallel()
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)
dir := t.TempDir()
dest := filepath.Join(dir, "actual.tar")
// Create the file so that we can test that the command
// warns the user before overwriting a preexisting file.
fi, err := os.OpenFile(dest, os.O_CREATE|os.O_RDONLY, 0600)
require.NoError(t, err)
_ = fi.Close()
cmd, root := clitest.New(t, "templates", "pull", template.Name, dest)
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
errChan := make(chan error)
go func() {
defer close(errChan)
errChan <- cmd.Execute()
}()
// We expect to be prompted that a file already exists.
pty.ExpectMatch("already exists")
pty.WriteLine("yes")
require.NoError(t, <-errChan)
actual, err := os.ReadFile(dest)
require.NoError(t, err)
require.True(t, bytes.Equal(actual, expected), "tar files differ")
})
}
// genTemplateVersionSource returns a unique bundle that can be used to create
// a template version source.
func genTemplateVersionSource() *echo.Responses {
return &echo.Responses{
Parse: []*proto.Parse_Response{
{
Type: &proto.Parse_Response_Log{
Log: &proto.Log{
Output: uuid.NewString(),
},
},
},
{
Type: &proto.Parse_Response_Complete{
Complete: &proto.Parse_Complete{},
},
},
},
ProvisionApply: echo.ProvisionComplete,
}
}