-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathschemas_test.go
More file actions
171 lines (152 loc) · 5.06 KB
/
schemas_test.go
File metadata and controls
171 lines (152 loc) · 5.06 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
163
164
165
166
167
168
169
170
171
// Copyright IBM Corp. 2019, 2026
// SPDX-License-Identifier: MPL-2.0
package tfjson
import (
"encoding/json"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/zclconf/go-cty/cty"
)
func TestProviderSchemasValidate(t *testing.T) {
cases := map[string]struct {
testDataPath string
}{
"a basic provider schema is validated": {
testDataPath: "testdata/basic/schemas.json",
},
"a provider schema including functions is validated": {
testDataPath: "testdata/functions/schemas.json",
},
"a provider schema including ephemeral resources is validated": {
testDataPath: "testdata/ephemeral_resources/schemas.json",
},
"a provider schema including a resource with write-only attribute(s) is validated": {
testDataPath: "testdata/write_only_attribute_on_resource/schemas.json",
},
"a provider schema including resource identity schemas is validated": {
testDataPath: "testdata/identity/schemas.json",
},
"a provider schema including list resource schemas is validated": {
testDataPath: "testdata/list_resources/schemas.json",
},
}
for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
f, err := os.Open(tc.testDataPath)
if err != nil {
t.Fatal(err)
}
defer f.Close()
var schemas *ProviderSchemas
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
t.Fatal(err)
}
if err := schemas.Validate(); err != nil {
t.Fatal(err)
}
})
}
}
// TestProviderSchemas_writeOnlyAttribute asserts that write-only attributes in a resource in a
// provider schema JSON file are marked as WriteOnly once decoded into a ProviderSchemas struct
func TestProviderSchemas_writeOnlyAttribute(t *testing.T) {
f, err := os.Open("testdata/write_only_attribute_on_resource/schemas.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var schemas *ProviderSchemas
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
t.Fatal(err)
}
resourceSchema := schemas.Schemas["terraform.io/builtin/terraform"].ResourceSchemas["terraform_example"]
if resourceSchema.Block.Attributes["wo_attr"].WriteOnly != true {
t.Fatal("expected terraform_example.wo_attr to be marked as write-only")
}
if resourceSchema.Block.Attributes["foo"].WriteOnly != false {
t.Fatal("expected terraform_example.foo to not be marked as write-only")
}
}
func TestProviderSchemas_action(t *testing.T) {
expectedAction := &ActionSchema{
Block: &SchemaBlock{
DescriptionKind: SchemaDescriptionKindPlain,
Attributes: map[string]*SchemaAttribute{
"program": {
AttributeType: cty.List(cty.String),
Description: "A list of strings, whose first element is the program to run and whose subsequent elements are optional command line arguments to the program.",
DescriptionKind: SchemaDescriptionKindPlain,
Required: true,
},
"query": {
AttributeType: cty.Map(cty.String),
Description: "A map of string values to pass to the external program as the query arguments. If not supplied, the program will receive an empty object as its input.",
DescriptionKind: SchemaDescriptionKindPlain,
Optional: true,
},
"working_dir": {
AttributeType: cty.String,
Description: "Working directory of the program. If not supplied, the program will run in the current directory.",
DescriptionKind: SchemaDescriptionKindPlain,
Optional: true,
},
},
},
}
f, err := os.Open("testdata/actions/schemas.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var schemas *ProviderSchemas
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
t.Fatal(err)
}
gotAction := schemas.Schemas["registry.terraform.io/hashicorp/external"].ActionSchemas["external"]
if diff := cmp.Diff(gotAction, expectedAction, cmpopts.EquateComparable(cty.Type{})); diff != "" {
t.Errorf("Unexpected diff (+wanted, -got): %s", diff)
return
}
}
// Providers can include one or more state store implementations
func TestProviderSchemas_stateStores(t *testing.T) {
expectedStoreSchema := &Schema{
Version: 0,
Block: &SchemaBlock{
DescriptionKind: SchemaDescriptionKindPlain,
Attributes: map[string]*SchemaAttribute{
"value": {
AttributeType: cty.String,
DescriptionKind: "plain",
Required: true,
},
},
},
}
f, err := os.Open("testdata/state_stores/single_store.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var schemas *ProviderSchemas
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
t.Fatal(err)
}
storeSchemas := schemas.Schemas["registry.terraform.io/hashicorp/test"].StateStoreSchemas
if len(storeSchemas) != 1 {
t.Fatalf("expected schema to include 1 state store, but got %v", len(storeSchemas))
}
storeName := "test_store"
gotStoreSchema, ok := storeSchemas[storeName]
if !ok {
t.Fatalf("expected schema to include a state store called %q, but it's missing.",
storeName,
)
}
if diff := cmp.Diff(gotStoreSchema, expectedStoreSchema, cmpopts.EquateComparable(cty.Type{})); diff != "" {
t.Errorf("Unexpected diff (+wanted, -got): %s", diff)
return
}
}