7
7
8
8
"github.com/google/uuid"
9
9
"github.com/stretchr/testify/assert"
10
+ "github.com/stretchr/testify/require"
10
11
11
12
"github.com/coder/coder/v2/cli/clitest"
12
13
"github.com/coder/coder/v2/coderd/coderdtest"
@@ -32,11 +33,13 @@ func TestEnterpriseCreate(t *testing.T) {
32
33
secondTemplates []string
33
34
}
34
35
36
+ // setupMultipleOrganizations creates an extra organization, assigns a member
37
+ // both organizations, and optionally creates templates in each organization.
35
38
setupMultipleOrganizations := func (t * testing.T , args setupArgs ) setupData {
36
39
ownerClient , first := coderdenttest .New (t , & coderdenttest.Options {
37
40
Options : & coderdtest.Options {
38
41
// This only affects the first org.
39
- IncludeProvisionerDaemon : false ,
42
+ IncludeProvisionerDaemon : true ,
40
43
},
41
44
LicenseOptions : & coderdenttest.LicenseOptions {
42
45
Features : license.Features {
@@ -83,8 +86,9 @@ func TestEnterpriseCreate(t *testing.T) {
83
86
}
84
87
}
85
88
89
+ // Test creating a workspace in the second organization with a template
90
+ // name.
86
91
t .Run ("CreateMultipleOrganization" , func (t * testing.T ) {
87
- // Creates a workspace in another organization
88
92
t .Parallel ()
89
93
90
94
const templateName = "secondtemplate"
@@ -96,37 +100,101 @@ func TestEnterpriseCreate(t *testing.T) {
96
100
args := []string {
97
101
"create" ,
98
102
"my-workspace" ,
103
+ "-y" ,
99
104
"--template" , templateName ,
100
105
}
101
106
inv , root := clitest .New (t , args ... )
102
107
clitest .SetupConfig (t , member , root )
103
- doneChan := make (chan struct {})
104
- pty := ptytest .New (t ).Attach (inv )
105
- go func () {
106
- defer close (doneChan )
107
- err := inv .Run ()
108
- assert .NoError (t , err )
109
- }()
110
- matches := []struct {
111
- match string
112
- write string
113
- }{
114
- {match : "compute.main" },
115
- {match : "smith (linux, i386)" },
116
- {match : "Confirm create" , write : "yes" },
108
+ _ = ptytest .New (t ).Attach (inv )
109
+ err := inv .Run ()
110
+ require .NoError (t , err )
111
+
112
+ ws , err := member .WorkspaceByOwnerAndName (context .Background (), codersdk .Me , "my-workspace" , codersdk.WorkspaceOptions {})
113
+ if assert .NoError (t , err , "expected workspace to be created" ) {
114
+ assert .Equal (t , ws .TemplateName , templateName )
115
+ assert .Equal (t , ws .OrganizationName , setup .second .Name , "workspace in second organization" )
117
116
}
118
- for _ , m := range matches {
119
- pty .ExpectMatch (m .match )
120
- if len (m .write ) > 0 {
121
- pty .WriteLine (m .write )
122
- }
117
+ })
118
+
119
+ // If a template name exists in two organizations, the workspace create will
120
+ // fail.
121
+ t .Run ("AmbiguousTemplateName" , func (t * testing.T ) {
122
+ t .Parallel ()
123
+
124
+ const templateName = "ambiguous"
125
+ setup := setupMultipleOrganizations (t , setupArgs {
126
+ firstTemplates : []string {templateName },
127
+ secondTemplates : []string {templateName },
128
+ })
129
+ member := setup .member
130
+
131
+ args := []string {
132
+ "create" ,
133
+ "my-workspace" ,
134
+ "-y" ,
135
+ "--template" , templateName ,
123
136
}
124
- <- doneChan
137
+ inv , root := clitest .New (t , args ... )
138
+ clitest .SetupConfig (t , member , root )
139
+ _ = ptytest .New (t ).Attach (inv )
140
+ err := inv .Run ()
141
+ require .Error (t , err , "expected error due to ambiguous template name" )
142
+ require .ErrorContains (t , err , "multiple templates found" )
143
+ })
144
+
145
+ // Ambiguous template names are allowed if the organization is specified.
146
+ t .Run ("WorkingAmbiguousTemplateName" , func (t * testing.T ) {
147
+ t .Parallel ()
148
+
149
+ const templateName = "ambiguous"
150
+ setup := setupMultipleOrganizations (t , setupArgs {
151
+ firstTemplates : []string {templateName },
152
+ secondTemplates : []string {templateName },
153
+ })
154
+ member := setup .member
155
+
156
+ args := []string {
157
+ "create" ,
158
+ "my-workspace" ,
159
+ "-y" ,
160
+ "--template" , templateName ,
161
+ "--org" , setup .second .Name ,
162
+ }
163
+ inv , root := clitest .New (t , args ... )
164
+ clitest .SetupConfig (t , member , root )
165
+ _ = ptytest .New (t ).Attach (inv )
166
+ err := inv .Run ()
167
+ require .NoError (t , err )
125
168
126
169
ws , err := member .WorkspaceByOwnerAndName (context .Background (), codersdk .Me , "my-workspace" , codersdk.WorkspaceOptions {})
127
170
if assert .NoError (t , err , "expected workspace to be created" ) {
128
171
assert .Equal (t , ws .TemplateName , templateName )
129
- assert .Equal (t , ws .OrganizationName , setup .second .ID , "workspace in second organization" )
172
+ assert .Equal (t , ws .OrganizationName , setup .second .Name , "workspace in second organization" )
130
173
}
131
174
})
175
+
176
+ // If an organization is specified, but the template is not in that
177
+ // organization, an error is thrown.
178
+ t .Run ("CreateIncorrectOrg" , func (t * testing.T ) {
179
+ t .Parallel ()
180
+
181
+ const templateName = "secondtemplate"
182
+ setup := setupMultipleOrganizations (t , setupArgs {
183
+ firstTemplates : []string {templateName },
184
+ })
185
+ member := setup .member
186
+
187
+ args := []string {
188
+ "create" ,
189
+ "my-workspace" ,
190
+ "-y" ,
191
+ "--org" , setup .second .Name ,
192
+ "--template" , templateName ,
193
+ }
194
+ inv , root := clitest .New (t , args ... )
195
+ clitest .SetupConfig (t , member , root )
196
+ _ = ptytest .New (t ).Attach (inv )
197
+ err := inv .Run ()
198
+ require .Error (t , err )
199
+ })
132
200
}
0 commit comments