|
1 | 1 | package ovh |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "os" |
| 7 | + "reflect" |
6 | 8 | "testing" |
7 | 9 |
|
8 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
9 | 12 | ) |
10 | 13 |
|
11 | 14 | const testAccDataCloudProjectUserS3PolicyConfig_basic = ` |
@@ -54,9 +57,28 @@ func TestAccDataCloudProjectUserS3Policy_basic(t *testing.T) { |
54 | 57 | Config: config, |
55 | 58 | Check: resource.ComposeTestCheckFunc( |
56 | 59 | resource.TestCheckResourceAttrSet(resourceName, "policy"), |
57 | | - resource.TestCheckResourceAttr(resourceName, "policy", normalizedPolicyRWBucket), |
| 60 | + testCheckResourceAttrJson(resourceName, "policy", normalizedPolicyRWBucket), |
58 | 61 | ), |
59 | 62 | }, |
60 | 63 | }, |
61 | 64 | }) |
62 | 65 | } |
| 66 | + |
| 67 | +func testCheckResourceAttrJson(resourceName string, attributeName string, attributeValue string) resource.TestCheckFunc { |
| 68 | + return func(s *terraform.State) error { |
| 69 | + // retrieve the resource by name from state |
| 70 | + rs, ok := s.RootModule().Resources[resourceName] |
| 71 | + if !ok { |
| 72 | + return fmt.Errorf("Not found: %s", resourceName) |
| 73 | + } |
| 74 | + |
| 75 | + stateValue := rs.Primary.Attributes[attributeName] |
| 76 | + var v1, v2 interface{} |
| 77 | + json.Unmarshal([]byte(attributeValue), &v1) |
| 78 | + json.Unmarshal([]byte(stateValue), &v2) |
| 79 | + if !reflect.DeepEqual(v1, v2) { |
| 80 | + return fmt.Errorf("for attribute %s.%s got %s expected %s", resourceName, attributeName, stateValue, attributeValue) |
| 81 | + } |
| 82 | + return nil |
| 83 | + } |
| 84 | +} |
0 commit comments