-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdummy_data_test.go
More file actions
162 lines (142 loc) · 4.75 KB
/
dummy_data_test.go
File metadata and controls
162 lines (142 loc) · 4.75 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package data_dummy_test
import (
"fmt"
"strconv"
"testing"
"time"
"github.com/Nciso/low_level_provider_example/internal/acctest"
"github.com/Nciso/low_level_provider_example/internal/data_dummy"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func TestAccBlueprintConfig_basic(t *testing.T) {
var stateInGoStruct data_dummy.DataDummyState
resourceName := "data.provider_dummy.test"
experimental := true
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV5ProviderFactories: acctest.CreateMuxFactories(experimental),
Steps: []resource.TestStep{
{
Config: testAccDummyData(),
Check: resource.ComposeTestCheckFunc(
testAccCheckDummyConfigExist(resourceName, &stateInGoStruct),
resource.TestCheckResourceAttrSet(
resourceName, "regular_block.0.%"),
resource.TestCheckResourceAttrSet(
resourceName, "dynamic_attribute"),
resource.TestCheckResourceAttrSet(
resourceName, "regular_attribute"),
resource.TestCheckResourceAttrSet(
resourceName, "dynamic_block.0.%"),
),
},
},
})
}
func testAccDummyData() string {
return `
data "provider_dummy" "test" {
dynamic_attribute = "hello"
regular_attribute = "bye"
regular_block {
bar = "bar"
foo = 3
}
dynamic_block {
bar = "bar"
foo = 4
}
}
`
}
func testAccCheckDummyConfigExist(resourceName string, dummyConfig *data_dummy.DataDummyState) resource.TestCheckFunc {
// this is just to explore the state
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}
rawConf := rs.Primary.Attributes
fmt.Println(rawConf)
// err := json.Unmarshal([]byte(rawConf), dummyConfig)
// if err != nil {
// return fmt.Errorf("not a valid data config: %s", rawConf)
// }
return nil
}
}
func TestDecodeMSg(t *testing.T) {
type testCase struct {
decPack []uint8
}
testCases := map[string]testCase{
"decoded_with_dpt_block": {
decPack: []uint8("\x85\xb1dynamic_attribute\x92\xc4\b\"string\"\xa5hello\xaddynamic_block\x92\xc46[\"tuple\",[[\"object\",{\"bar\":\"string\",\"foo\":\"number\"}]]]\x91\x82\xa3bar\xa3bar\xa3foo\x04\xa2id\xc0\xb1regular_attribute\xa3bye\xadregular_block\x91\x82\xa3bar\xa3bar\xa3foo\x03"),
},
"decoded_without_dpt_block": {
decPack: []uint8("\x85\xb1dynamic_attribute\x92\xc4\b\"string\"\xa5hello\xaddynamic_block\x91\x82\xa3bar\xa3bar\xa3foo\x04\xa2id\xc0\xb1regular_attribute\xa3bye\xadregular_block\x91\x82\xa3bar\xa3bar\xa3foo\x03"),
},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
objTypeDef, ok := data_dummy.GetDataDummyLowLevelSchema().ValueType().(tftypes.Object)
if !ok {
t.Fatalf("cant get schema")
}
typ := tftypes.Object{
AttributeTypes: objTypeDef.AttributeTypes,
}
// internal function used
val, err := tftypes.ValueFromMsgPack(test.decPack, typ)
if err != nil {
t.Fatalf("fail decoding, testName: %s error -> %s", name, err.Error())
}
fmt.Println(val.String())
})
}
}
func TestDecodeValue(t *testing.T) {
objTypeDef, ok := data_dummy.GetDataDummyLowLevelSchema().ValueType().(tftypes.Object)
if !ok {
t.Fatalf("cant get schema")
}
outputObjectType := map[string]tftypes.Type{
"bar": tftypes.String,
"foo": tftypes.Number,
}
tfval := tftypes.NewValue(tftypes.Object{
AttributeTypes: objTypeDef.AttributeTypes,
}, map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, strconv.FormatInt(time.Now().Unix(), 10)),
"dynamic_block": tftypes.NewValue(tftypes.List{ElementType: tftypes.Object{AttributeTypes: outputObjectType}},
[]tftypes.Value{
tftypes.NewValue(tftypes.Object{AttributeTypes: outputObjectType},
map[string]tftypes.Value{
"bar": tftypes.NewValue(tftypes.String, "hello world regular"),
"foo": tftypes.NewValue(tftypes.Number, 101),
},
),
},
),
"regular_block": tftypes.NewValue(tftypes.List{ElementType: tftypes.Object{AttributeTypes: outputObjectType}},
[]tftypes.Value{
tftypes.NewValue(tftypes.Object{AttributeTypes: outputObjectType},
map[string]tftypes.Value{
"bar": tftypes.NewValue(tftypes.String, "hello world regular"),
"foo": tftypes.NewValue(tftypes.Number, 101),
},
),
},
),
"regular_attribute": tftypes.NewValue(tftypes.String, "this is regular string"),
"dynamic_attribute": tftypes.NewValue(tftypes.String, "this is dynamic string"),
})
dynamicValue, err := tfprotov5.NewDynamicValue(objTypeDef, tfval)
if err != nil {
t.Fatalf(err.Error())
}
fmt.Println(dynamicValue)
}