|
4 | 4 | package query_test |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "regexp" |
7 | 8 | "testing" |
8 | 9 |
|
9 | 10 | "github.com/hashicorp/terraform-plugin-go/tfprotov6" |
| 11 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 12 | + |
10 | 13 | r "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
11 | 14 | "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" |
| 15 | + "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/list" |
12 | 16 | "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" |
13 | 17 | "github.com/hashicorp/terraform-plugin-testing/knownvalue" |
14 | 18 | "github.com/hashicorp/terraform-plugin-testing/querycheck" |
@@ -100,3 +104,88 @@ func TestQuery(t *testing.T) { |
100 | 104 | }, |
101 | 105 | }) |
102 | 106 | } |
| 107 | + |
| 108 | +func TestQuery_ExpectError_ValidationError(t *testing.T) { |
| 109 | + t.Parallel() |
| 110 | + |
| 111 | + r.UnitTest(t, r.TestCase{ |
| 112 | + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ |
| 113 | + tfversion.SkipBelow(tfversion.Version1_14_0), |
| 114 | + }, |
| 115 | + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ |
| 116 | + "examplecloud": providerserver.NewProviderServer(testprovider.Provider{ |
| 117 | + ListResources: map[string]testprovider.ListResource{ |
| 118 | + "examplecloud_containerette": { |
| 119 | + IncludeResource: true, |
| 120 | + SchemaResponse: &list.SchemaResponse{ |
| 121 | + Schema: &tfprotov6.Schema{ |
| 122 | + Block: &tfprotov6.SchemaBlock{ |
| 123 | + Attributes: []*tfprotov6.SchemaAttribute{ |
| 124 | + { |
| 125 | + Name: "resource_group_name", |
| 126 | + Type: tftypes.String, |
| 127 | + Required: true, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + ValidateListConfigResponse: &list.ValidateListConfigResponse{ |
| 134 | + Diagnostics: []*tfprotov6.Diagnostic{ |
| 135 | + { |
| 136 | + Severity: tfprotov6.DiagnosticSeverityError, |
| 137 | + Summary: "Diagnostic summary", |
| 138 | + Detail: "Diagnostic details", |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + Resources: map[string]testprovider.Resource{ |
| 145 | + "examplecloud_containerette": examplecloudResource(), |
| 146 | + }, |
| 147 | + }), |
| 148 | + }, |
| 149 | + Steps: []r.TestStep{ |
| 150 | + { // config mode step 1 needs tf file with terraform providers block |
| 151 | + // this step should provision all the resources that the query is support to list |
| 152 | + // for simplicity we're only "provisioning" one here |
| 153 | + Config: ` |
| 154 | + resource "examplecloud_containerette" "primary" { |
| 155 | + name = "banana" |
| 156 | + resource_group_name = "foo" |
| 157 | + location = "westeurope" |
| 158 | + |
| 159 | + instances = 5 |
| 160 | + }`, |
| 161 | + }, |
| 162 | + { // Query mode step 2, operates on .tfquery.hcl files (needs tf file with terraform providers block) |
| 163 | + // ```provider "examplecloud" {}``` has a slightly different syntax for a .tfquery.hcl file |
| 164 | + // provider bock simulates a real providers workflow |
| 165 | + // "config" in this case means configuration of the list resource/filters |
| 166 | + |
| 167 | + Query: true, |
| 168 | + Config: ` |
| 169 | + provider "examplecloud" {} |
| 170 | +
|
| 171 | + list "examplecloud_containerette" "test" { |
| 172 | + provider = examplecloud |
| 173 | +
|
| 174 | + config { |
| 175 | + resource_group_name = "foo" |
| 176 | + } |
| 177 | + } |
| 178 | +
|
| 179 | + list "examplecloud_containerette" "test2" { |
| 180 | + provider = examplecloud |
| 181 | +
|
| 182 | + config { |
| 183 | + resource_group_name = "bar" |
| 184 | + } |
| 185 | + } |
| 186 | + `, |
| 187 | + ExpectError: regexp.MustCompile(`Diagnostic summary`), |
| 188 | + }, |
| 189 | + }, |
| 190 | + }) |
| 191 | +} |
0 commit comments