|
| 1 | +/** |
| 2 | +* @license |
| 3 | +* Copyright 2020 Dynatrace LLC |
| 4 | +* |
| 5 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +* you may not use this file except in compliance with the License. |
| 7 | +* You may obtain a copy of the License at |
| 8 | +* |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +* |
| 11 | +* Unless required by applicable law or agreed to in writing, software |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +* See the License for the specific language governing permissions and |
| 15 | +* limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package main_test |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "io/ioutil" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/dynatrace-oss/terraform-provider-dynatrace/config" |
| 27 | + |
| 28 | + "github.com/dtcookie/dynatrace/api/config/autotags" |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 31 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 32 | +) |
| 33 | + |
| 34 | +type AutoTagTest struct { |
| 35 | + resourceKey string |
| 36 | + testDataFld string |
| 37 | + reqPath string |
| 38 | + display string |
| 39 | +} |
| 40 | + |
| 41 | +func NewAutoTagTest() *AutoTagTest { |
| 42 | + return &AutoTagTest{ |
| 43 | + resourceKey: "dynatrace_autotag", |
| 44 | + testDataFld: "auto_tags", |
| 45 | + reqPath: "autoTags", |
| 46 | + display: "Auto Tag", |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func (test *AutoTagTest) Anonymize(m map[string]interface{}) { |
| 51 | + delete(m, "id") |
| 52 | + delete(m, "name") |
| 53 | + delete(m, "metadata") |
| 54 | +} |
| 55 | + |
| 56 | +func (test *AutoTagTest) ResourceKey() string { |
| 57 | + return test.resourceKey |
| 58 | +} |
| 59 | + |
| 60 | +func (test *AutoTagTest) CreateTestCase(file string, localJSONFile string, t *testing.T) (*resource.TestCase, error) { |
| 61 | + var content []byte |
| 62 | + var err error |
| 63 | + if content, err = ioutil.ReadFile(file); err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + config := string(content) |
| 67 | + name := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) |
| 68 | + resourceName := test.ResourceKey() + "." + name |
| 69 | + config = strings.ReplaceAll(config, "#name#", name) |
| 70 | + return &resource.TestCase{ |
| 71 | + PreCheck: func() { testAccPreCheck(t) }, |
| 72 | + IDRefreshName: resourceName, |
| 73 | + ProviderFactories: testAccProviderFactories, |
| 74 | + CheckDestroy: test.CheckDestroy, |
| 75 | + Steps: []resource.TestStep{ |
| 76 | + { |
| 77 | + Config: config, |
| 78 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 79 | + test.CheckExists(resourceName, t), |
| 80 | + compareLocalRemote(test, resourceName, localJSONFile, t), |
| 81 | + ), |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, nil |
| 85 | +} |
| 86 | + |
| 87 | +func TestAccAutoTagExampleA(t *testing.T) { |
| 88 | + test := NewAutoTagTest() |
| 89 | + var err error |
| 90 | + var testCase *resource.TestCase |
| 91 | + if testCase, err = test.CreateTestCase( |
| 92 | + fmt.Sprintf("test_data/%s/example_a.tf", test.testDataFld), |
| 93 | + fmt.Sprintf("test_data/%s/example_a.json", test.testDataFld), |
| 94 | + t, |
| 95 | + ); err != nil { |
| 96 | + t.Fatal(err) |
| 97 | + return |
| 98 | + } |
| 99 | + resource.Test(t, *testCase) |
| 100 | +} |
| 101 | + |
| 102 | +func (test *AutoTagTest) URL(id string) string { |
| 103 | + envURL := testAccProvider.Meta().(*config.ProviderConfiguration).DTenvURL |
| 104 | + reqPath := "%s/" + test.reqPath + "/%s" |
| 105 | + return fmt.Sprintf(reqPath, envURL, id) |
| 106 | +} |
| 107 | + |
| 108 | +func (test *AutoTagTest) CheckDestroy(s *terraform.State) error { |
| 109 | + providerConf := testAccProvider.Meta().(*config.ProviderConfiguration) |
| 110 | + restClient := autotags.NewService(providerConf.DTenvURL, providerConf.APIToken) |
| 111 | + |
| 112 | + for _, rs := range s.RootModule().Resources { |
| 113 | + if rs.Type != test.resourceKey { |
| 114 | + continue |
| 115 | + } |
| 116 | + |
| 117 | + id := rs.Primary.ID |
| 118 | + |
| 119 | + if _, err := restClient.Get(id); err != nil { |
| 120 | + // HTTP Response "404 Not Found" signals a success |
| 121 | + if strings.Contains(err.Error(), `"code": 404`) { |
| 122 | + return nil |
| 123 | + } |
| 124 | + // any other error should fail the test |
| 125 | + return err |
| 126 | + } |
| 127 | + return fmt.Errorf("%s still exists: %s", test.display, rs.Primary.ID) |
| 128 | + } |
| 129 | + |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | +func (test *AutoTagTest) CheckExists(n string, t *testing.T) resource.TestCheckFunc { |
| 134 | + return func(s *terraform.State) error { |
| 135 | + providerConf := testAccProvider.Meta().(*config.ProviderConfiguration) |
| 136 | + restClient := autotags.NewService(providerConf.DTenvURL, providerConf.APIToken) |
| 137 | + |
| 138 | + if rs, ok := s.RootModule().Resources[n]; ok { |
| 139 | + if _, err := restClient.Get(rs.Primary.ID); err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + return nil |
| 143 | + } |
| 144 | + |
| 145 | + return fmt.Errorf("Not found: %s", n) |
| 146 | + } |
| 147 | +} |
0 commit comments