Skip to content

Commit 0919499

Browse files
committed
add tests
1 parent 0d6f66b commit 0919499

File tree

40 files changed

+919
-194
lines changed

40 files changed

+919
-194
lines changed

coderd/database/dbfake/dbfake.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4428,6 +4428,7 @@ func (q *FakeQuerier) InsertWorkspaceAgent(_ context.Context, arg database.Inser
44284428
MOTDFile: arg.MOTDFile,
44294429
LifecycleState: database.WorkspaceAgentLifecycleStateCreated,
44304430
ShutdownScript: arg.ShutdownScript,
4431+
DefaultApps: arg.DefaultApps,
44314432
}
44324433

44334434
q.workspaceAgents = append(q.workspaceAgents, agent)

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
12171217
Valid: prAgent.ShutdownScript != "",
12181218
},
12191219
ShutdownScriptTimeoutSeconds: prAgent.GetShutdownScriptTimeoutSeconds(),
1220-
DefaultApps: prAgent.DefaultApps,
1220+
DefaultApps: prAgent.GetDefaultApps(),
12211221
})
12221222
if err != nil {
12231223
return xerrors.Errorf("insert agent: %w", err)

coderd/provisionerdserver/provisionerdserver_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/google/uuid"
14+
"github.com/lib/pq"
1415
"github.com/stretchr/testify/assert"
1516
"github.com/stretchr/testify/require"
1617
"go.opentelemetry.io/otel/trace"
@@ -28,6 +29,7 @@ import (
2829
"github.com/coder/coder/v2/coderd/schedule"
2930
"github.com/coder/coder/v2/coderd/telemetry"
3031
"github.com/coder/coder/v2/codersdk"
32+
"github.com/coder/coder/v2/provisioner/terraform"
3133
"github.com/coder/coder/v2/provisionerd/proto"
3234
"github.com/coder/coder/v2/provisionersdk"
3335
sdkproto "github.com/coder/coder/v2/provisionersdk/proto"
@@ -1475,6 +1477,7 @@ func TestInsertWorkspaceResource(t *testing.T) {
14751477
t.Parallel()
14761478
db := dbfake.New()
14771479
job := uuid.New()
1480+
defaultApps := []string{"a", "b", "c"}
14781481
err := insert(db, job, &sdkproto.Resource{
14791482
Name: "something",
14801483
Type: "aws_instance",
@@ -1494,6 +1497,7 @@ func TestInsertWorkspaceResource(t *testing.T) {
14941497
Slug: "a",
14951498
}},
14961499
ShutdownScript: "shutdown",
1500+
DefaultApps: defaultApps,
14971501
}},
14981502
})
14991503
require.NoError(t, err)
@@ -1516,7 +1520,57 @@ func TestInsertWorkspaceResource(t *testing.T) {
15161520
got, err := agent.EnvironmentVariables.RawMessage.MarshalJSON()
15171521
require.NoError(t, err)
15181522
require.Equal(t, want, got)
1523+
require.Equal(t, defaultApps, []string(agent.DefaultApps))
15191524
})
1525+
1526+
t.Run("AllDefaultApps", func(t *testing.T) {
1527+
t.Parallel()
1528+
db := dbfake.New()
1529+
job := uuid.New()
1530+
err := insert(db, job, &sdkproto.Resource{
1531+
Name: "something",
1532+
Type: "aws_instance",
1533+
Agents: []*sdkproto.Agent{{
1534+
DefaultApps: []string{terraform.AllDefaultApps},
1535+
}},
1536+
})
1537+
require.NoError(t, err)
1538+
resources, err := db.GetWorkspaceResourcesByJobID(ctx, job)
1539+
require.NoError(t, err)
1540+
require.Len(t, resources, 1)
1541+
agents, err := db.GetWorkspaceAgentsByResourceIDs(ctx, []uuid.UUID{resources[0].ID})
1542+
require.NoError(t, err)
1543+
require.Len(t, agents, 1)
1544+
agent := agents[0]
1545+
// We should be setting the default apps slice to nil to indicate
1546+
// that a user did not set the field in the template.
1547+
require.Equal(t, pq.StringArray(nil), agent.DefaultApps)
1548+
})
1549+
1550+
t.Run("DisableDefaultApps", func(t *testing.T) {
1551+
t.Parallel()
1552+
db := dbfake.New()
1553+
job := uuid.New()
1554+
err := insert(db, job, &sdkproto.Resource{
1555+
Name: "something",
1556+
Type: "aws_instance",
1557+
Agents: []*sdkproto.Agent{{
1558+
DefaultApps: []string{},
1559+
}},
1560+
})
1561+
require.NoError(t, err)
1562+
resources, err := db.GetWorkspaceResourcesByJobID(ctx, job)
1563+
require.NoError(t, err)
1564+
require.Len(t, resources, 1)
1565+
agents, err := db.GetWorkspaceAgentsByResourceIDs(ctx, []uuid.UUID{resources[0].ID})
1566+
require.NoError(t, err)
1567+
require.Len(t, agents, 1)
1568+
agent := agents[0]
1569+
// An empty array (as opposed to nil) should be returned to indicate
1570+
// that all apps are disabled.
1571+
require.Equal(t, pq.StringArray([]string{}), agent.DefaultApps)
1572+
})
1573+
15201574
}
15211575

15221576
func setup(t *testing.T, ignoreLogErrors bool) *provisionerdserver.Server {

provisioner/terraform/resources_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func TestConvertResources(t *testing.T) {
4545
Auth: &proto.Agent_Token{},
4646
StartupScriptBehavior: "non-blocking",
4747
ConnectionTimeoutSeconds: 120,
48+
DefaultApps: []string{terraform.AllDefaultApps},
4849
}},
4950
}},
5051
},
@@ -62,6 +63,7 @@ func TestConvertResources(t *testing.T) {
6263
Auth: &proto.Agent_Token{},
6364
StartupScriptBehavior: "non-blocking",
6465
ConnectionTimeoutSeconds: 120,
66+
DefaultApps: []string{terraform.AllDefaultApps},
6567
}},
6668
}, {
6769
Name: "second",
@@ -80,6 +82,7 @@ func TestConvertResources(t *testing.T) {
8082
Auth: &proto.Agent_InstanceId{},
8183
StartupScriptBehavior: "non-blocking",
8284
ConnectionTimeoutSeconds: 120,
85+
DefaultApps: []string{terraform.AllDefaultApps},
8386
}},
8487
}},
8588
},
@@ -96,6 +99,7 @@ func TestConvertResources(t *testing.T) {
9699
Auth: &proto.Agent_Token{},
97100
StartupScriptBehavior: "non-blocking",
98101
ConnectionTimeoutSeconds: 120,
102+
DefaultApps: []string{terraform.AllDefaultApps},
99103
}},
100104
}},
101105
},
@@ -114,6 +118,7 @@ func TestConvertResources(t *testing.T) {
114118
StartupScriptBehavior: "non-blocking",
115119
StartupScriptTimeoutSeconds: 300,
116120
ShutdownScriptTimeoutSeconds: 300,
121+
DefaultApps: []string{terraform.AllDefaultApps},
117122
}, {
118123
Name: "dev2",
119124
OperatingSystem: "darwin",
@@ -125,6 +130,7 @@ func TestConvertResources(t *testing.T) {
125130
StartupScriptTimeoutSeconds: 30,
126131
ShutdownScript: "echo bye bye",
127132
ShutdownScriptTimeoutSeconds: 30,
133+
DefaultApps: []string{terraform.AllDefaultApps},
128134
}, {
129135
Name: "dev3",
130136
OperatingSystem: "windows",
@@ -135,6 +141,7 @@ func TestConvertResources(t *testing.T) {
135141
StartupScriptBehavior: "blocking",
136142
StartupScriptTimeoutSeconds: 300,
137143
ShutdownScriptTimeoutSeconds: 300,
144+
DefaultApps: []string{terraform.AllDefaultApps},
138145
}, {
139146
Name: "dev4",
140147
OperatingSystem: "linux",
@@ -144,6 +151,7 @@ func TestConvertResources(t *testing.T) {
144151
StartupScriptBehavior: "blocking",
145152
StartupScriptTimeoutSeconds: 300,
146153
ShutdownScriptTimeoutSeconds: 300,
154+
DefaultApps: []string{terraform.AllDefaultApps},
147155
}},
148156
}},
149157
},
@@ -182,6 +190,7 @@ func TestConvertResources(t *testing.T) {
182190
Auth: &proto.Agent_Token{},
183191
StartupScriptBehavior: "non-blocking",
184192
ConnectionTimeoutSeconds: 120,
193+
DefaultApps: []string{terraform.AllDefaultApps},
185194
}},
186195
}},
187196
},
@@ -206,6 +215,7 @@ func TestConvertResources(t *testing.T) {
206215
Auth: &proto.Agent_Token{},
207216
StartupScriptBehavior: "non-blocking",
208217
ConnectionTimeoutSeconds: 120,
218+
DefaultApps: []string{terraform.AllDefaultApps},
209219
}},
210220
}},
211221
},
@@ -246,6 +256,7 @@ func TestConvertResources(t *testing.T) {
246256
StartupScriptTimeoutSeconds: 300,
247257
StartupScriptBehavior: "non-blocking",
248258
ConnectionTimeoutSeconds: 120,
259+
DefaultApps: []string{terraform.AllDefaultApps},
249260
}},
250261
}},
251262
},
@@ -297,6 +308,7 @@ func TestConvertResources(t *testing.T) {
297308
Auth: &proto.Agent_Token{},
298309
StartupScriptBehavior: "non-blocking",
299310
ConnectionTimeoutSeconds: 120,
311+
DefaultApps: []string{terraform.AllDefaultApps},
300312
}},
301313
},
302314
},
@@ -314,6 +326,7 @@ func TestConvertResources(t *testing.T) {
314326
Auth: &proto.Agent_Token{},
315327
StartupScriptBehavior: "non-blocking",
316328
ConnectionTimeoutSeconds: 120,
329+
DefaultApps: []string{terraform.AllDefaultApps},
317330
}},
318331
}},
319332
parameters: []*proto.RichParameter{{
@@ -395,6 +408,7 @@ func TestConvertResources(t *testing.T) {
395408
Auth: &proto.Agent_Token{},
396409
StartupScriptBehavior: "non-blocking",
397410
ConnectionTimeoutSeconds: 120,
411+
DefaultApps: []string{terraform.AllDefaultApps},
398412
}},
399413
}},
400414
parameters: []*proto.RichParameter{{
@@ -423,6 +437,7 @@ func TestConvertResources(t *testing.T) {
423437
Auth: &proto.Agent_Token{},
424438
StartupScriptBehavior: "non-blocking",
425439
ConnectionTimeoutSeconds: 120,
440+
DefaultApps: []string{terraform.AllDefaultApps},
426441
}},
427442
}},
428443
parameters: []*proto.RichParameter{{
@@ -478,10 +493,45 @@ func TestConvertResources(t *testing.T) {
478493
ConnectionTimeoutSeconds: 120,
479494
StartupScriptTimeoutSeconds: 300,
480495
ShutdownScriptTimeoutSeconds: 300,
496+
DefaultApps: []string{terraform.AllDefaultApps},
481497
}},
482498
}},
483499
gitAuthProviders: []string{"github", "gitlab"},
484500
},
501+
"default-apps": {
502+
resources: []*proto.Resource{{
503+
Name: "dev",
504+
Type: "null_resource",
505+
Agents: []*proto.Agent{{
506+
Name: "main",
507+
OperatingSystem: "linux",
508+
Architecture: "amd64",
509+
Auth: &proto.Agent_Token{},
510+
StartupScriptBehavior: "non-blocking",
511+
ConnectionTimeoutSeconds: 120,
512+
StartupScriptTimeoutSeconds: 300,
513+
ShutdownScriptTimeoutSeconds: 300,
514+
DefaultApps: []string{"vscode-desktop", "web-terminal"},
515+
}},
516+
}},
517+
},
518+
"default-apps-disabled": {
519+
resources: []*proto.Resource{{
520+
Name: "dev",
521+
Type: "null_resource",
522+
Agents: []*proto.Agent{{
523+
Name: "main",
524+
OperatingSystem: "linux",
525+
Architecture: "amd64",
526+
Auth: &proto.Agent_Token{},
527+
StartupScriptBehavior: "non-blocking",
528+
ConnectionTimeoutSeconds: 120,
529+
StartupScriptTimeoutSeconds: 300,
530+
ShutdownScriptTimeoutSeconds: 300,
531+
DefaultApps: []string{},
532+
}},
533+
}},
534+
},
485535
} {
486536
folderName := folderName
487537
expected := expected

provisioner/terraform/testdata/calling-module/calling-module.tfplan.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

provisioner/terraform/testdata/calling-module/calling-module.tfstate.json

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

provisioner/terraform/testdata/chaining-resources/chaining-resources.tfplan.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

provisioner/terraform/testdata/chaining-resources/chaining-resources.tfstate.json

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)