8000 refactor test to allow multiple files · coder/coder@a587ceb · GitHub
[go: up one dir, main page]

Skip to content

Commit a587ceb

Browse files
committed
refactor test to allow multiple files
1 parent bc220e7 commit a587ceb

File tree

1 file changed

+125
-94
lines changed

1 file changed

+125
-94
lines changed

enterprise/coderd/workspaces_test.go

Lines changed: 125 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"database/sql"
7-
"fmt"
87
"net/http"
98
"runtime"
109
"sync/atomic"
@@ -1406,160 +1405,193 @@ func TestWorkspaceTagsTerraform(t *testing.T) {
14061405
t.Parallel()
14071406
}
14081407

1409-
mainTfTemplate := `
1408+
providersTf := `
14101409
terraform {
14111410
required_providers {
14121411
coder = {
14131412
source = "coder/coder"
14141413
}
14151414
}
14161415
}
1417-
provider "coder" {}
1416+
provider "coder" {}`
1417+
dataTf := `
14181418
data "coder_workspace" "me" {}
1419-
data "coder_workspace_owner" "me" {}
1420-
data "coder_parameter" "unrelated" {
1421-
name = "unrelated"
1422-
type = "list(string)"
1423-
default = jsonencode(["a", "b"])
1424-
}
1425-
%s
1426-
`
1427-
1419+
data "coder_workspace_owner" "me" {}`
14281420
for _, tc := range []struct {
14291421
name string
14301422
// tags to apply to the external provisioner
14311423
provisionerTags map[string]string
14321424
// tags to apply to the create template version request
14331425
createTemplateVersionRequestTags map[string]string
1434-
// the coder_workspace_tags bit of main.tf.
1435-
// you can add more stuff here if you need
1436-
tfWorkspaceTags string
1426+
// files to add to the template
1427+
files map[string]string
14371428
}{
14381429
{
1439-
name: "no tags",
1440-
tfWorkspaceTags: ``,
1430+
name: "no tags",
1431+
files: map[string]string{
1432+
"main.tf": ``,
1433+
"providers.tf": providersTf,
1434+
"data.tf": dataTf,
1435+
},
14411436
},
14421437
{
14431438
name: "empty tags",
1444-
tfWorkspaceTags: `
1445-
data "coder_workspace_tags" "tags" {
1446-
tags = {}
1447-
}
1448-
`,
1439+
files: map[string]string{
1440+
"main.tf": `
1441+
data "coder_workspace_tags" "tags" {
1442+
tags = {}
1443+
}`,
1444+
"providers.tf": providersTf,
1445+
"data.tf": dataTf,
1446+
},
14491447
},
14501448
{
14511449
name: "static tag",
14521450
provisionerTags: map[string]string{"foo": "bar"},
1453-
tfWorkspaceTags: `
1454-
data "coder_workspace_tags" "tags" {
1455-
tags = {
1456-
"foo" = "bar"
1457-
}
1458-
}`,
1451+
files: map[string]string{
1452+
"main.tf": `
1453+
data "coder_workspace_tags" "tags" {
1454+
tags = {
1455+
"foo" = "bar"
1456+
}
1457+
}`,
1458+
"providers.tf": providersTf,
1459+
"data.tf": dataTf,
1460+
},
14591461
},
14601462
{
14611463
name: "tag variable",
14621464
provisionerTags: map[string]string{"foo": "bar"},
1463-
tfWorkspaceTags: `
1464-
variable "foo" {
1465-
default = "bar"
1466-
}
1467-
data "coder_workspace_tags" "tags" {
1468-
tags = {
1469-
"foo" = var.foo
1465+
files: map[string]string{
1466+
"main.tf": `
1467+
variable "foo" {
1468+
default = "bar"
14701469
}
1471-
}`,
1470+
data "coder_workspace_tags" "tags" {
1471+
tags = {
1472+
"foo" = var.foo
1473+
}
1474+
}`,
1475+
"providers.tf": providersTf,
1476+
"data.tf": dataTf,
1477+
},
14721478
},
14731479
{
14741480
name: "tag param",
14751481
provisionerTags: map[string]string{"foo": "bar"},
1476-
tfWorkspaceTags: `
1477-
data "coder_parameter" "foo" {
1478-
name = "foo"
1479-
type = "string"
1480-
default = "bar"
1481-
}
1482-
data "coder_workspace_tags" "tags" {
1483-
tags = {
1484-
"foo" = data.coder_parameter.foo.value
1482+
files: map[string]string{
1483+
"main.tf": `
1484+
data "coder_parameter" "foo" {
1485+
name = "foo"
1486+
type = "string"
1487+
default = "bar"
14851488
}
1486-
}`,
1489+
data "coder_workspace_tags" "tags" {
1490+
tags = {
1491+
"foo" = data.coder_parameter.foo.value
1492+
}
1493+
}`,
1494+
"providers.tf": providersTf,
1495+
"data.tf": dataTf,
1496+
},
14871497
},
14881498
{
14891499
name: "tag param with default from var",
14901500
provisionerTags: map[string]string{"foo": "bar"},
1491-
tfWorkspaceTags: `
1492-
variable "foo" {
1493-
type = string
1494-
default = "bar"
1495-
}
1496-
data "coder_parameter" "foo" {
1497-
name = "foo"
1498-
type = "string"
1499-
default = var.foo
1500-
}
1501-
data "coder_workspace_tags" "tags" {
1502-
tags = {
1503-
"foo" = data.coder_parameter.foo.value
1501+
files: map[string]string{
1502+
"main.tf": `
1503+
variable "foo" {
1504+
type = string
1505+
default = "bar"
1506+
}
1507+
data "coder_parameter" "foo" {
1508+
name = "foo"
1509+
type = "string"
1510+
default = var.foo
15041511
}
1505-
}`,
1512+
data "coder_workspace_tags" "tags" {
1513+
tags = {
1514+
"foo" = data.coder_parameter.foo.value
1515+
}
1516+
}`,
1517+
"providers.tf": providersTf,
1518+
"data.tf": dataTf,
1519+
},
15061520
},
15071521
{
15081522
name: "override no tags",
15091523
provisionerTags: map[string]string{"foo": "baz"},
15101524
createTemplateVersionRequestTags: map[string]string{"foo": "baz"},
1511-
tfWorkspaceTags: ``,
1525+
files: map[string]string{
1526+
"main.tf": ``,
1527+
"providers.tf": providersTf,
1528+
"data.tf": dataTf,
1529+
},
15121530
},
15131531
{
15141532
name: "override empty tags",
15151533
provisionerTags: map[string]string{"foo": "baz"},
15161534
createTemplateVersionRequestTags: map[string]string{"foo": "baz"},
1517-
tfWorkspaceTags: `
1518-
data "coder_workspace_tags" "tags" {
1519-
tags = {}
1520-
}`,
1535+
files: map[string]string{
1536+
"main.tf": `
1537+
data "coder_workspace_tags" "tags" {
1538+
tags = {}
1539+
}`,
1540+
"providers.tf": providersTf,
1541+
"data.tf": dataTf,
1542+
},
15211543
},
15221544
{
15231545
name: "does not override static tag",
15241546
provisionerTags: map[string]string{"foo": "bar"},
15251547
createTemplateVersionRequestTags: map[string]string{"foo": "baz"},
1526-
tfWorkspaceTags: `
1527-
data "coder_workspace_tags" "tags" {
1528-
tags = {
1529-
"foo" = "bar"
1530-
}
1531-
}`,
1548+
files: map[string]string{
1549+
"main.tf": `
1550+
data "coder_workspace_tags" "tags" {
1551+
tags = {
1552+
"foo" = "bar"
1553+
}
1554+
}`,
1555+
"providers.tf": providersTf,
1556+
"data.tf": dataTf,
1557+
},
15321558
},
15331559
{
15341560
name: "locals",
15351561
provisionerTags: map[string]string{"foo": "bar"},
1536-
tfWorkspaceTags: `
1537-
locals {
1538-
tagFoo = "bar"
1539-
}
1540-
data "coder_workspace_tags" "tags" {
1541-
tags = {
1542-
"foo": local.tagFoo
1562+
files: map[string]string{
1563+
"main.tf": `
1564+
locals {
1565+
tagFoo = "bar"
15431566
}
1544-
}
1545-
`,
1567+
data "coder_workspace_tags" "tags" {
1568+
tags = {
1569+
"foo": local.tagFoo
1570+
}
1571+
}`,
1572+
"providers.tf": providersTf,
1573+
"data.tf": dataTf,
1574+
},
15461575
},
15471576
{
15481577
name: "var default from local",
15491578
provisionerTags: map[string]string{"foo": "bar"},
1550-
tfWorkspaceTags: `
1551-
locals {
1552-
tagFoo = "bar"
1553-
}
1554-
variable "foo" {
1555-
default = local.tagFoo
1556-
}
1557-
data "coder_workspace_tags" "tags" {
1558-
tags = {
1559-
foo: var.foo
1579+
files: map[string]string{
1580+
"main.tf": `
1581+
locals {
1582+
tagFoo = "bar"
15601583
}
1561-
}
1562-
`,
1584+
variable "foo" {
1585+
default = local.tagFoo
1586+
}
1587+
data "coder_workspace_tags" "tags" {
1588+
tags = {
1589+
foo: var.foo
1590+
}
1591+
}`,
1592+
"providers.tf": providersTf,
1593+
"data.tf": dataTf,
1594+
},
15631595
},
15641596
} {
15651597
tc := tc
@@ -1586,8 +1618,7 @@ func TestWorkspaceTagsTerraform(t *testing.T) {
15861618
_ = coderdenttest.NewExternalProvisionerDaemonTerraform(t, client, owner.OrganizationID, tc.provisionerTags)
15871619

15881620
// Creating a template as a template admin must succeed
1589-
templateFiles := map[string]string{"main.tf": fmt.Sprintf(mainTfTemplate, tc.tfWorkspaceTags)}
1590-
tarBytes := testutil.CreateTar(t, templateFiles)
1621+
tarBytes := testutil.CreateTar(t, tc.files)
15911622
fi, err := templateAdmin.Upload(ctx, "application/x-tar", bytes.NewReader(tarBytes))
15921623
require.NoError(t, err, "failed to upload file")
15931624
tv, err := templateAdmin.CreateTemplateVersion(ctx, owner.OrganizationID, codersdk.CreateTemplateVersionRequest{

0 commit comments

Comments
 (0)
0