forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplatelist_test.go
More file actions
109 lines (86 loc) · 3.37 KB
/
templatelist_test.go
File metadata and controls
109 lines (86 loc) · 3.37 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
package cli_test
import (
"bytes"
"context"
"encoding/json"
"sort"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)
func TestTemplateList(t *testing.T) {
t.Parallel()
t.Run("ListTemplates", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)
firstVersion := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, firstVersion.ID)
firstTemplate := coderdtest.CreateTemplate(t, client, user.OrganizationID, firstVersion.ID)
secondVersion := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, secondVersion.ID)
secondTemplate := coderdtest.CreateTemplate(t, client, user.OrganizationID, secondVersion.ID)
inv, root := clitest.New(t, "templates", "list")
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t).Attach(inv)
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancelFunc()
errC := make(chan error)
go func() {
errC <- inv.WithContext(ctx).Run()
}()
// expect that templates are listed alphabetically
templatesList := []string{firstTemplate.Name, secondTemplate.Name}
sort.Strings(templatesList)
require.NoError(t, <-errC)
for _, name := range templatesList {
pty.ExpectMatch(name)
}
})
t.Run("ListTemplatesJSON", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)
firstVersion := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, firstVersion.ID)
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, firstVersion.ID)
secondVersion := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, secondVersion.ID)
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, secondVersion.ID)
inv, root := clitest.New(t, "templates", "list", "--output=json")
clitest.SetupConfig(t, client, root)
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancelFunc()
out := bytes.NewBuffer(nil)
inv.Stdout = out
err := inv.WithContext(ctx).Run()
require.NoError(t, err)
var templates []codersdk.Template
require.NoError(t, json.Unmarshal(out.Bytes(), &templates))
require.Len(t, templates, 2)
})
t.Run("NoTemplates", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{})
coderdtest.CreateFirstUser(t, client)
inv, root := clitest.New(t, "templates", "list")
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
inv.Stdin = pty.Input()
inv.Stderr = pty.Output()
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancelFunc()
errC := make(chan error)
go func() {
errC <- inv.WithContext(ctx).Run()
}()
require.NoError(t, <-errC)
pty.ExpectMatch("No templates found in")
pty.ExpectMatch(coderdtest.FirstUserParams.Username)
pty.ExpectMatch("Create one:")
})
}