|
2 | 2 | package test |
3 | 3 |
|
4 | 4 | import ( |
| 5 | + "fmt" |
| 6 | + "reflect" |
5 | 7 | "testing" |
6 | 8 |
|
| 9 | + "github.com/gruntwork-io/terratest/modules/terraform" |
7 | 10 | "github.com/stretchr/testify/assert" |
8 | 11 | "github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper" |
9 | 12 | ) |
@@ -61,13 +64,69 @@ func setupOptions(t *testing.T, prefix string, dir string) *testhelper.TestOptio |
61 | 64 | return options |
62 | 65 | } |
63 | 66 |
|
| 67 | +// ValidateOutputMapSliceContent takes a map of Terraform output keys and values and it expects that the |
| 68 | +// map contains a set of key => Slices couples |
| 69 | +// It checks that the input map has length > 0 and for each key the related Slice is not empty |
| 70 | +// If the value related to a key is not a slice it returns an error message |
| 71 | +// The function returns a list of the output keys whose Slice has length 0 |
| 72 | +// and an error message that includes details about which keys were missing. |
| 73 | +// If the input map is empty it returns only the related error message |
| 74 | +func ValidateOutputMapOfSlicesContent(inputMap map[string]interface{}) ([]string, error) { |
| 75 | + var failedKeys []string |
| 76 | + var err error |
| 77 | + // Set up ANSI escape codes for blue and bold text |
| 78 | + blueBold := "\033[1;34m" |
| 79 | + reset := "\033[0m" |
| 80 | + |
| 81 | + // mapLen := len(inputMap) |
| 82 | + // fmt.Println("Len of inputMap is ", mapLen) |
| 83 | + if len(inputMap) == 0 { |
| 84 | + err = fmt.Errorf("Output: %s'The input map has zero elements'%s\n", blueBold, reset) |
| 85 | + } else { |
| 86 | + // going through the inputMap keys |
| 87 | + for k, v := range inputMap { |
| 88 | + if reflect.TypeOf(v).String() == "[]interface {}" { |
| 89 | + vArray := v.([]interface{}) |
| 90 | + if len(vArray) == 0 { |
| 91 | + failedKeys = append(failedKeys, k) |
| 92 | + err = fmt.Errorf("Output: The keys %s'%s'%s have empty slices\n", blueBold, failedKeys, reset) |
| 93 | + } |
| 94 | + } else { |
| 95 | + failedKeys = append(failedKeys, k) |
| 96 | + err = fmt.Errorf("Output: The key %s'%s'%s value is not a slice\n", blueBold, k, reset) |
| 97 | + break |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return failedKeys, err |
| 103 | +} |
| 104 | + |
64 | 105 | func TestRunDefaultExample(t *testing.T) { |
65 | 106 | t.Parallel() |
66 | 107 |
|
67 | 108 | options := setupOptions(t, "vpe-default", defaultExampleTerraformDir) |
| 109 | + options.SkipTestTearDown = true |
68 | 110 | output, err := options.RunTestConsistency() |
69 | 111 | assert.Nil(t, err, "This should not have errored") |
70 | 112 | assert.NotNil(t, output, "Expected some output") |
| 113 | + // checking vpe_ips to exist |
| 114 | + outputs := terraform.OutputAll(options.Testing, options.TerraformOptions) |
| 115 | + expectedOutputs := []string{"vpe_ips"} |
| 116 | + _, outputErr := testhelper.ValidateTerraformOutputs(outputs, expectedOutputs...) |
| 117 | + assert.NoErrorf(t, outputErr, "Some outputs not found or nil") |
| 118 | + // checking vpe_ips to contain a set on not empty slices as expected |
| 119 | + mapToValidate, ok := outputs["vpe_ips"].(map[string]interface{}) |
| 120 | + var outputErrMap error |
| 121 | + if !ok { |
| 122 | + outputErrMap = fmt.Errorf("Output: Failed to read value of key %s\n", "vpe_ips") |
| 123 | + } else { |
| 124 | + _, outputErrMap = ValidateOutputMapOfSlicesContent(mapToValidate) |
| 125 | + } |
| 126 | + |
| 127 | + assert.NoErrorf(t, outputErr, "Some outputs not found or nil") |
| 128 | + assert.NoErrorf(t, outputErrMap, "Some outputs not having the expected structure") |
| 129 | + options.TestTearDown() |
71 | 130 | } |
72 | 131 |
|
73 | 132 | func TestRunUpgradeExample(t *testing.T) { |
|
0 commit comments