File: expression_test.go

package info (click to toggle)
golang-github-hashicorp-terraform-json 0.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,572 kB
  • sloc: makefile: 31
file content (132 lines) | stat: -rw-r--r-- 2,701 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package tfjson

import (
	"encoding/json"
	"reflect"
	"testing"

	"github.com/davecgh/go-spew/spew"
)

func TestUnmarshalExpressions(t *testing.T) {
	cases := []struct {
		name     string
		in       string
		expected *ConfigResource
	}{
		{
			name: "basic",
			in: `
{
  "address": "aws_instance.foo",
  "mode": "managed",
  "type": "aws_instance",
  "name": "foo",
  "provider_config_key": "aws",
  "expressions": {
    "ami": {
      "constant_value": "ami-foobar"
    },
    "ebs_block_device": [
      {
        "device_name": {
          "references": [
            "var.foo"
          ]
        }
      }
    ],
    "instance_type": {
      "constant_value": "t2.micro"
    }
  },
  "schema_version": 1
}
`,
			expected: &ConfigResource{
				Address:           "aws_instance.foo",
				Mode:              ManagedResourceMode,
				Type:              "aws_instance",
				Name:              "foo",
				ProviderConfigKey: "aws",
				Expressions: map[string]*Expression{
					"ami": {
						ExpressionData: &ExpressionData{
							ConstantValue: "ami-foobar",
						},
					},
					"ebs_block_device": {
						ExpressionData: &ExpressionData{
							NestedBlocks: []map[string]*Expression{
								{
									"device_name": {
										ExpressionData: &ExpressionData{
											ConstantValue: UnknownConstantValue,
											References:    []string{"var.foo"},
										},
									},
								},
							},
						},
					},
					"instance_type": {
						ExpressionData: &ExpressionData{
							ConstantValue: "t2.micro",
						},
					},
				},
				SchemaVersion: 1,
			},
		},
		{
			name: "explicit null in contstant value",
			in: `
{
  "address": "null_resource.foo",
  "mode": "managed",
  "type": "null_resource",
  "name": "foo",
  "provider_config_key": "null",
  "expressions": {
    "triggers": {
      "constant_value": {
        "foo": null
      }
    }
  },
  "schema_version": 0
}
`,
			expected: &ConfigResource{
				Address:           "null_resource.foo",
				Mode:              ManagedResourceMode,
				Type:              "null_resource",
				Name:              "foo",
				ProviderConfigKey: "null",
				Expressions: map[string]*Expression{
					"triggers": {
						ExpressionData: &ExpressionData{
							ConstantValue: map[string]interface{}{
								"foo": nil,
							},
						},
					},
				},
				SchemaVersion: 0,
			},
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			var actual *ConfigResource
			if err := json.Unmarshal([]byte(tc.in), &actual); err != nil {
				t.Fatal(err)
			}

			if !reflect.DeepEqual(tc.expected, actual) {
				t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s\n\n", spew.Sdump(tc.expected), spew.Sdump(actual))
			}
		})
	}
}