MCPcopy Create free account
hub / github.com/coder/coder / TestAppSlugValidation

Function TestAppSlugValidation

provisioner/terraform/resources_test.go:1254–1301  ·  view source on GitHub ↗

nolint:tparallel

(t *testing.T)

Source from the content-addressed store, hash-verified

1252
1253//nolint:tparallel
1254func TestAppSlugValidation(t *testing.T) {
1255 t.Parallel()
1256 ctx, logger := ctxAndLogger(t)
1257
1258 // nolint:dogsled
1259 _, filename, _, _ := runtime.Caller(0)
1260
1261 // Load the multiple-apps state file and edit it.
1262 dir := filepath.Join(filepath.Dir(filename), "testdata", "resources", "multiple-apps")
1263 tfPlanRaw, err := os.ReadFile(filepath.Join(dir, "multiple-apps.tfplan.json"))
1264 require.NoError(t, err)
1265 var tfPlan tfjson.Plan
1266 err = json.Unmarshal(tfPlanRaw, &tfPlan)
1267 require.NoError(t, err)
1268 tfPlanGraph, err := os.ReadFile(filepath.Join(dir, "multiple-apps.tfplan.dot"))
1269 require.NoError(t, err)
1270
1271 cases := []struct {
1272 slug string
1273 errContains string
1274 }{
1275 {slug: "$$$ invalid slug $$$", errContains: "does not match regex"},
1276 {slug: "invalid--slug", errContains: "does not match regex"},
1277 {slug: "invalid_slug", errContains: "does not match regex"},
1278 {slug: "Invalid-slug", errContains: "does not match regex"},
1279 {slug: "valid", errContains: ""},
1280 }
1281
1282 //nolint:paralleltest
1283 for i, c := range cases {
1284 t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
1285 // Change the first app slug to match the current case.
1286 for _, resource := range tfPlan.PlannedValues.RootModule.Resources {
1287 if resource.Type == "coder_app" {
1288 resource.AttributeValues["slug"] = c.slug
1289 break
1290 }
1291 }
1292
1293 _, err := terraform.ConvertState(ctx, []*tfjson.StateModule{tfPlan.PlannedValues.RootModule}, string(tfPlanGraph), logger)
1294 if c.errContains != "" {
1295 require.ErrorContains(t, err, c.errContains)
1296 } else {
1297 require.NoError(t, err)
1298 }
1299 })
1300 }
1301}
1302
1303func TestAppSlugDuplicate(t *testing.T) {
1304 t.Parallel()

Callers

nothing calls this directly

Calls 5

ConvertStateFunction · 0.92
ctxAndLoggerFunction · 0.85
ReadFileMethod · 0.65
RunMethod · 0.65
UnmarshalMethod · 0.45

Tested by

no test coverage detected