Skip to content

Commit 56c62e1

Browse files
authored
feat: Refactor tests with plan caching (PSKD-1513) (#462)
* feat: Refactor tests with plan caching (PSKD-1513) Signed-off-by: [email protected] <[email protected]>
1 parent 05e3df9 commit 56c62e1

23 files changed

+1302
-1321
lines changed

docs/user/TestingPhilosophy.md

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,49 @@ The unit tests in this project are designed to quickly and efficiently verify th
1313

1414
### Unit Testing Structure
1515

16-
The unit tests are written as [table-driven tests](https://go.dev/wiki/TableDrivenTests) so that they are easier to read, understand, and expand. The tests are divided into two files, [default_unit_test.go](../../test/default_unit_test.go) and [non_default_unit_test.go](../../test/non_default_unit_test.go).
16+
The unit tests are written as [table-driven tests](https://go.dev/wiki/TableDrivenTests) so that they are easier to read, understand, and expand. The tests are divided into two packages, [defaultplan](../../test/defaultplan) and [nondefaultplan](../../test/nondefaultplan).
1717

18-
The test file named default_unit_test.go validates the default values of a Terraform plan. This testing ensures that there are no regressions in the default behavior of the code base. The test file named non_default_unit_test.go modifies the input values before running the Terraform plan. After generating the plan file, the test verifies that it contains the expected values. Both files are written as table-driven tests.
18+
The test package defaultplan validates the default values of a Terraform plan. This testing ensures that there are no regressions in the default behavior of the code base. The test package nondefaultplan modifies the input values before running the Terraform plan. After generating the plan file, the test verifies that it contains the expected values. Both sets of tests are written to be table-driven.
1919

20-
To see an example, look at the `TestPlanStorageDefaults` function in the default_unit_test.go file that is shown below.
20+
To see an example, look at the `TestPlanStorage` function in the defaultplan/storage_test.go file that is shown below.
2121

22-
With the Table-Driven approach, each entry in the `storageTests` map is a test. These tests verify that the expected value matches the actual value of the "module.nfs[0].azurerm_linux_virtual_machine.vm" resource.  We use the [k8s.io JsonPath](https://pkg.go.dev/k8s.io/[email protected]/util/jsonpath) library to parse the Terraform output and extract the desired attribute.  The runTest call is a helper function that runs through each test in the map and perform assertions. See the [helpers.go](../../test/helpers.go) file for more information on the common helper functions.
22+
With the Table-Driven approach, each entry in the `tests` map is a test. These tests verify that the expected value matches the actual value of the "module.nfs[0].azurerm_linux_virtual_machine.vm" resource.  We use the [k8s.io JsonPath](https://pkg.go.dev/k8s.io/[email protected]/util/jsonpath) library to parse the Terraform output and extract the desired attribute.  The RunTests call is a helper function that runs through each test in the map and perform the supplied assertions. See the [helpers](../../test/helpers) package for more information on the common helper functions.
2323

2424
```go
25-
// Function containing all unit tests for the Storage type
26-
// and its default values.
27-
func TestPlanStorageDefaults(t *testing.T) {
28-
    // Map containing the different tests. Each entry is 
29-
    // a separate test.
30-
    storageTests := map[string]testCase{
31-
        // Verify that the default user is 'nfsuser'.
25+
func TestPlanStorage(t *testing.T) {
26+
    t.Parallel()
27+
28+
29+
    tests := map[string]helpers.TestCase{
3230
        "userTest": {
33-
            expected:          "nfsuser",
34-
            resourceMapName:   "module.nfs[0].azurerm_linux_virtual_machine.vm",
35-
            attributeJsonPath: "{$.admin_username}",
31+
            Expected:          "nfsuser",
32+
            ResourceMapName:   "module.nfs[0].azurerm_linux_virtual_machine.vm",
33+
            AttributeJsonPath: "{$.admin_username}",
3634
        },
37-
// Verify that the default size is 'Standard_D4s_v5'.
3835
        "sizeTest": {
39-
            expected:          "Standard_D4s_v5",
40-
            resourceMapName:   "module.nfs[0].azurerm_linux_virtual_machine.vm",
41-
            attributeJsonPath: "{$.size}",
36+
            Expected:          "Standard_D4s_v5",
37+
            ResourceMapName:   "module.nfs[0].azurerm_linux_virtual_machine.vm",
38+
            AttributeJsonPath: "{$.size}",
39+
        },
40+
        "vmNotNilTest": {
41+
            Expected:          "<nil>",
42+
            ResourceMapName:   "module.nfs[0].azurerm_linux_virtual_machine.vm",
43+
            AttributeJsonPath: "{$}",
44+
            AssertFunction: assert.NotEqual,
4245
        },
43-
    }
44-
45-
// Generate a Plan file using the default input variables.
46-
    variables := getDefaultPlanVars(t)
47-
    plan, err := initPlanWithVariables(t, variables)
48-
    require.NotNil(t, plan)
49-
    require.NoError(t, err)
50-
51-
// For each test in the Test Table, run the test helper function
52-
for name, tc := range storageTests {
53-
        t.Run(name, func(t *testing.T) {
54-
            runTest(t, tc, plan)
55-
        })
56-
}
46+
        "vmZoneEmptyStrTest": {
47+
            Expected:          "",
48+
            ResourceMapName:   "module.nfs[0].azurerm_linux_virtual_machine.vm",
49+
            AttributeJsonPath: "{$.vm_zone}",
50+
        },
51+
52+
// Run the tests using the default input variables.
53+
    helpers.RunTests(t, tests, helpers.GetDefaultPlan(t))
5754
}
5855
```
5956
### Adding Unit Tests
6057
61-
To create a unit test, you can add an entry to an existing test table in the [default_unit_test.go](../../test/default_unit_test.go) file or the [non_default_unit_test.go](../../test/non_default_unit_test.go) file, depending on the test type. If you don't see an existing test table that fits your needs, you are welcome to create a new function in a similar table-driven test format.
58+
To create a unit test, you can add an entry to an existing test table if it's related to the resources being validated. If you don't see an existing test table that fits your needs, you are welcome to create a new file in a similar table-driven test format and drop it in the appropriate package.
6259
6360
### Integration Testing
6461

0 commit comments

Comments
 (0)