| 1 |
package hcl |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/stretchr/testify/assert" |
| 7 |
) |
| 8 |
|
| 9 |
func TestHCL_Unmarshal(t *testing.T) { |
| 10 |
|
| 11 |
hclParserWithFlatten := Parser(true) |
| 12 |
hclParserWithoutFlatten := Parser(false) |
| 13 |
|
| 14 |
testCases := []struct { |
| 15 |
name string |
| 16 |
input []byte |
| 17 |
output map[string]any |
| 18 |
isErr bool |
| 19 |
function HCL |
| 20 |
}{ |
| 21 |
{ |
| 22 |
name: "Empty HCL - With flatten", |
| 23 |
input: []byte(`{}`), |
| 24 |
function: *hclParserWithFlatten, |
| 25 |
output: map[string]any{}, |
| 26 |
}, |
| 27 |
{ |
| 28 |
name: "Empty HCL - Without flatten", |
| 29 |
input: []byte(`{}`), |
| 30 |
function: *hclParserWithoutFlatten, |
| 31 |
output: map[string]any{}, |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "Valid HCL - With flatten", |
| 35 |
input: []byte(`resource "aws_instance" "example" { |
| 36 |
count = 2 # meta-argument first |
| 37 |
ami = "abc123" |
| 38 |
instance_type = "t2.micro" |
| 39 |
lifecycle { # meta-argument block last |
| 40 |
create_before_destroy = true |
| 41 |
} |
| 42 |
}`), |
| 43 |
function: *hclParserWithFlatten, |
| 44 |
output: map[string]any{ |
| 45 |
"resource": map[string]any{ |
| 46 |
"aws_instance": map[string]any{ |
| 47 |
"example": map[string]any{ |
| 48 |
"ami": "abc123", |
| 49 |
"count": 2, |
| 50 |
"instance_type": "t2.micro", |
| 51 |
"lifecycle": map[string]any{ |
| 52 |
"create_before_destroy": true, |
| 53 |
}, |
| 54 |
}, |
| 55 |
}, |
| 56 |
}, |
| 57 |
}, |
| 58 |
}, |
| 59 |
{ |
| 60 |
name: "Valid HCL - Without flatten", |
| 61 |
input: []byte(`resource "aws_instance" "example" { |
| 62 |
count = 2 # meta-argument first |
| 63 |
ami = "abc123" |
| 64 |
instance_type = "t2.micro" |
| 65 |
lifecycle { # meta-argument block last |
| 66 |
create_before_destroy = true |
| 67 |
} |
| 68 |
}`), |
| 69 |
function: *hclParserWithoutFlatten, |
| 70 |
output: map[string]any{ |
| 71 |
"resource": []map[string]any{{ |
| 72 |
"aws_instance": []map[string]any{{ |
| 73 |
"example": []map[string]any{{ |
| 74 |
"ami": "abc123", |
| 75 |
"count": 2, |
| 76 |
"instance_type": "t2.micro", |
| 77 |
"lifecycle": []map[string]any{{ |
| 78 |
"create_before_destroy": true}, |
| 79 |
}, |
| 80 |
}}, |
| 81 |
}}, |
| 82 |
}}, |
| 83 |
}, |
| 84 |
}, |
| 85 |
{ |
| 86 |
name: "Invalid HCL - With missing parenthesis", |
| 87 |
input: []byte(`resource "aws_instance" "example" { |
| 88 |
ami = "abc123" |
| 89 |
`), |
| 90 |
function: *hclParserWithFlatten, |
| 91 |
isErr: true, |
| 92 |
}, |
| 93 |
} |
| 94 |
|
| 95 |
for _, tc := range testCases { |
| 96 |
t.Run(tc.name, func(t *testing.T) { |
| 97 |
out, err := tc.function.Unmarshal(tc.input) |
| 98 |
if tc.isErr { |
| 99 |
assert.NotNil(t, err) |
| 100 |
} else { |
| 101 |
assert.Nil(t, err) |
| 102 |
assert.Equal(t, tc.output, out) |
| 103 |
} |
| 104 |
}) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func TestHCL_Marshal(t *testing.T) { |
| 109 |
|
| 110 |
hclParserWithFlatten := Parser(true) |
| 111 |
|
| 112 |
testCases := []struct { |
| 113 |
name string |
| 114 |
input map[string]any |
| 115 |
isErr bool |
| 116 |
function HCL |
| 117 |
}{ |
| 118 |
{ |
| 119 |
name: "Empty HCL", |
| 120 |
input: map[string]any{}, |
| 121 |
isErr: true, |
| 122 |
function: *hclParserWithFlatten, |
| 123 |
}, |
| 124 |
{ |
| 125 |
name: "Complex HCL", |
| 126 |
input: map[string]any{ |
| 127 |
"resource": []map[string]any{{ |
| 128 |
"aws_instance": []map[string]any{{ |
| 129 |
"example": []map[string]any{{ |
| 130 |
"ami": "abc123", |
| 131 |
"count": 2, |
| 132 |
"instance_type": "t2.micro", |
| 133 |
"lifecycle": []map[string]any{{ |
| 134 |
"create_before_destroy": true}, |
| 135 |
}, |
| 136 |
}}, |
| 137 |
}}, |
| 138 |
}}, |
| 139 |
}, |
| 140 |
isErr: true, |
| 141 |
}, |
| 142 |
} |
| 143 |
|
| 144 |
for _, tc := range testCases { |
| 145 |
t.Run(tc.name, func(t *testing.T) { |
| 146 |
_, err := tc.function.Marshal(tc.input) |
| 147 |
if tc.isErr { |
| 148 |
assert.EqualError(t, err, "HCL marshalling is not supported") |
| 149 |
} else { |
| 150 |
assert.Nil(t, err) |
| 151 |
} |
| 152 |
}) |
| 153 |
} |
| 154 |
} |