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

Function TestParameterValidation

provisioner/terraform/resources_test.go:1432–1487  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1430}
1431
1432func TestParameterValidation(t *testing.T) {
1433 t.Parallel()
1434 ctx, logger := ctxAndLogger(t)
1435
1436 // nolint:dogsled
1437 _, filename, _, _ := runtime.Caller(0)
1438
1439 // Load the rich-parameters state file and edit it.
1440 dir := filepath.Join(filepath.Dir(filename), "testdata", "resources", "rich-parameters")
1441 tfPlanRaw, err := os.ReadFile(filepath.Join(dir, "rich-parameters.tfplan.json"))
1442 require.NoError(t, err)
1443 var tfPlan tfjson.Plan
1444 err = json.Unmarshal(tfPlanRaw, &tfPlan)
1445 require.NoError(t, err)
1446 tfPlanGraph, err := os.ReadFile(filepath.Join(dir, "rich-parameters.tfplan.dot"))
1447 require.NoError(t, err)
1448
1449 for _, resource := range tfPlan.PriorState.Values.RootModule.Resources {
1450 if resource.Type == "coder_parameter" {
1451 resource.AttributeValues["name"] = "identical"
1452 }
1453 }
1454
1455 state, err := terraform.ConvertState(ctx, []*tfjson.StateModule{tfPlan.PriorState.Values.RootModule}, string(tfPlanGraph), logger)
1456 require.Nil(t, state)
1457 require.Error(t, err)
1458 require.ErrorContains(t, err, "coder_parameter names must be unique but \"identical\" appears multiple times")
1459
1460 // Make two sets of identical names.
1461 count := 0
1462 for _, resource := range tfPlan.PriorState.Values.RootModule.Resources {
1463 if resource.Type == "coder_parameter" {
1464 resource.AttributeValues["name"] = fmt.Sprintf("identical-%d", count%2)
1465 count++
1466 }
1467 }
1468
1469 state, err = terraform.ConvertState(ctx, []*tfjson.StateModule{tfPlan.PriorState.Values.RootModule}, string(tfPlanGraph), logger)
1470 require.Nil(t, state)
1471 require.Error(t, err)
1472 require.ErrorContains(t, err, "coder_parameter names must be unique but \"identical-0\" and \"identical-1\" appear multiple times")
1473
1474 // Once more with three sets.
1475 count = 0
1476 for _, resource := range tfPlan.PriorState.Values.RootModule.Resources {
1477 if resource.Type == "coder_parameter" {
1478 resource.AttributeValues["name"] = fmt.Sprintf("identical-%d", count%3)
1479 count++
1480 }
1481 }
1482
1483 state, err = terraform.ConvertState(ctx, []*tfjson.StateModule{tfPlan.PriorState.Values.RootModule}, string(tfPlanGraph), logger)
1484 require.Nil(t, state)
1485 require.Error(t, err)
1486 require.ErrorContains(t, err, "coder_parameter names must be unique but \"identical-0\", \"identical-1\" and \"identical-2\" appear multiple times")
1487}
1488
1489func TestDefaultPresets(t *testing.T) {

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected