(t *testing.T)
| 1487 | } |
| 1488 | |
| 1489 | func TestDefaultPresets(t *testing.T) { |
| 1490 | t.Parallel() |
| 1491 | |
| 1492 | // nolint:dogsled |
| 1493 | _, filename, _, _ := runtime.Caller(0) |
| 1494 | dir := filepath.Join(filepath.Dir(filename), "testdata", "resources") |
| 1495 | |
| 1496 | cases := map[string]struct { |
| 1497 | fixtureFile string |
| 1498 | expectError bool |
| 1499 | errorMsg string |
| 1500 | validate func(t *testing.T, state *terraform.State) |
| 1501 | }{ |
| 1502 | "multiple defaults should fail": { |
| 1503 | fixtureFile: "presets-multiple-defaults", |
| 1504 | expectError: true, |
| 1505 | errorMsg: "a maximum of 1 coder_workspace_preset can be marked as default, but 2 are set", |
| 1506 | }, |
| 1507 | "single default should succeed": { |
| 1508 | fixtureFile: "presets-single-default", |
| 1509 | expectError: false, |
| 1510 | validate: func(t *testing.T, state *terraform.State) { |
| 1511 | require.Len(t, state.Presets, 2) |
| 1512 | var defaultCount int |
| 1513 | for _, preset := range state.Presets { |
| 1514 | if preset.Default { |
| 1515 | defaultCount++ |
| 1516 | require.Equal(t, "development", preset.Name) |
| 1517 | } |
| 1518 | } |
| 1519 | require.Equal(t, 1, defaultCount) |
| 1520 | }, |
| 1521 | }, |
| 1522 | } |
| 1523 | |
| 1524 | for name, tc := range cases { |
| 1525 | t.Run(name, func(t *testing.T) { |
| 1526 | t.Parallel() |
| 1527 | ctx, logger := ctxAndLogger(t) |
| 1528 | |
| 1529 | tfPlanRaw, err := os.ReadFile(filepath.Join(dir, tc.fixtureFile, tc.fixtureFile+".tfplan.json")) |
| 1530 | require.NoError(t, err) |
| 1531 | var tfPlan tfjson.Plan |
| 1532 | err = json.Unmarshal(tfPlanRaw, &tfPlan) |
| 1533 | require.NoError(t, err) |
| 1534 | tfPlanGraph, err := os.ReadFile(filepath.Join(dir, tc.fixtureFile, tc.fixtureFile+".tfplan.dot")) |
| 1535 | require.NoError(t, err) |
| 1536 | |
| 1537 | modules := []*tfjson.StateModule{tfPlan.PlannedValues.RootModule} |
| 1538 | if tfPlan.PriorState != nil { |
| 1539 | modules = append(modules, tfPlan.PriorState.Values.RootModule) |
| 1540 | } else { |
| 1541 | modules = append(modules, tfPlan.PlannedValues.RootModule) |
| 1542 | } |
| 1543 | state, err := terraform.ConvertState(ctx, modules, string(tfPlanGraph), logger) |
| 1544 | |
| 1545 | if tc.expectError { |
| 1546 | require.Error(t, err) |
nothing calls this directly
no test coverage detected