|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccSumologicApp_basic(t *testing.T) { |
| 14 | + // uuid of Varnish - OpenTelemetry app |
| 15 | + uuid := "d2ef33c3-67f2-4438-9124-14a30ec2ecf3" |
| 16 | + version := "1.0.3" |
| 17 | + parameterKey := "key" |
| 18 | + parameterValue := "value" |
| 19 | + |
| 20 | + tfResourceName := "tf_app_test" |
| 21 | + resource.Test(t, resource.TestCase{ |
| 22 | + PreCheck: func() { testAccPreCheck(t) }, |
| 23 | + Providers: testAccProviders, |
| 24 | + CheckDestroy: testAccCheckAppDestroy(), |
| 25 | + Steps: []resource.TestStep{ |
| 26 | + { |
| 27 | + Config: testAccSumologicApp(tfResourceName, uuid, version, parameterKey, parameterValue), |
| 28 | + }, |
| 29 | + { |
| 30 | + ResourceName: fmt.Sprintf("sumologic_app.%s", tfResourceName), |
| 31 | + ImportState: true, |
| 32 | + ImportStateVerify: true, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +func TestAccSumologicApp_create(t *testing.T) { |
| 39 | + // create config |
| 40 | + // uuid of Varnish - OpenTelemetry app |
| 41 | + uuid := "d2ef33c3-67f2-4438-9124-14a30ec2ecf3" |
| 42 | + version := "1.0.4" |
| 43 | + parameterKey := "key" |
| 44 | + parameterValue := "value" |
| 45 | + |
| 46 | + tfResourceName := "tf_app_test" |
| 47 | + tfAppResource := fmt.Sprintf("sumologic_app.%s", tfResourceName) |
| 48 | + |
| 49 | + resource.Test(t, resource.TestCase{ |
| 50 | + PreCheck: func() { testAccPreCheck(t) }, |
| 51 | + Providers: testAccProviders, |
| 52 | + CheckDestroy: testAccCheckAppDestroy(), |
| 53 | + Steps: []resource.TestStep{ |
| 54 | + { |
| 55 | + Config: testAccSumologicApp(tfResourceName, uuid, version, parameterKey, parameterValue), |
| 56 | + Check: resource.ComposeTestCheckFunc( |
| 57 | + testAccCheckAppInstanceExists(tfAppResource, t), |
| 58 | + resource.TestCheckResourceAttr(tfAppResource, "uuid", uuid), |
| 59 | + resource.TestCheckResourceAttr(tfAppResource, "version", version), |
| 60 | + resource.TestCheckResourceAttr(tfAppResource, "parameters.%", "1"), |
| 61 | + resource.TestCheckResourceAttr(tfAppResource, "parameters.key", "value"), |
| 62 | + ), |
| 63 | + }, |
| 64 | + }, |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func TestAccSumologicApp_update(t *testing.T) { |
| 69 | + |
| 70 | + // create config |
| 71 | + // uuid of Varnish - OpenTelemetry app |
| 72 | + uuid := "d2ef33c3-67f2-4438-9124-14a30ec2ecf3" |
| 73 | + version := "1.0.3" |
| 74 | + parameterKey := "key" |
| 75 | + parameterValue := "value" |
| 76 | + |
| 77 | + tfResourceName := "tf_app_test" |
| 78 | + tfAppResource := fmt.Sprintf("sumologic_app.%s", tfResourceName) |
| 79 | + |
| 80 | + resource.Test(t, resource.TestCase{ |
| 81 | + PreCheck: func() { testAccPreCheck(t) }, |
| 82 | + Providers: testAccProviders, |
| 83 | + CheckDestroy: testAccCheckAppDestroy(), |
| 84 | + Steps: []resource.TestStep{ |
| 85 | + { |
| 86 | + Config: testAccSumologicApp(tfResourceName, uuid, version, parameterKey, parameterValue), |
| 87 | + Check: resource.ComposeTestCheckFunc( |
| 88 | + testAccCheckAppInstanceExists(tfAppResource, t), |
| 89 | + resource.TestCheckResourceAttr(tfAppResource, "uuid", uuid), |
| 90 | + resource.TestCheckResourceAttr(tfAppResource, "version", version), |
| 91 | + resource.TestCheckResourceAttr(tfAppResource, "parameters.%", "1"), |
| 92 | + resource.TestCheckResourceAttr(tfAppResource, "parameters.key", "value"), |
| 93 | + ), |
| 94 | + }, |
| 95 | + { |
| 96 | + Config: testAccSumologicApp(tfResourceName, uuid, "1.0.4", parameterKey, parameterValue), |
| 97 | + Check: resource.ComposeTestCheckFunc( |
| 98 | + testAccCheckAppInstanceExists(tfAppResource, t), |
| 99 | + resource.TestCheckResourceAttr(tfAppResource, "uuid", uuid), |
| 100 | + resource.TestCheckResourceAttr(tfAppResource, "version", "1.0.4"), |
| 101 | + resource.TestCheckResourceAttr(tfAppResource, "parameters.%", "1"), |
| 102 | + resource.TestCheckResourceAttr(tfAppResource, "parameters.key", "value"), |
| 103 | + ), |
| 104 | + }, |
| 105 | + }, |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +func testAccCheckAppDestroy() resource.TestCheckFunc { |
| 110 | + return func(s *terraform.State) error { |
| 111 | + client := testAccProvider.Meta().(*Client) |
| 112 | + for _, r := range s.RootModule().Resources { |
| 113 | + id := r.Primary.ID |
| 114 | + appInstance, _ := client.GetAppInstance(id) |
| 115 | + if appInstance != nil { |
| 116 | + return fmt.Errorf("AppInstance %s still exists", id) |
| 117 | + } |
| 118 | + } |
| 119 | + return nil |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func testAccCheckAppInstanceExists(name string, t *testing.T) resource.TestCheckFunc { |
| 124 | + return func(s *terraform.State) error { |
| 125 | + rs, ok := s.RootModule().Resources[name] |
| 126 | + if !ok { |
| 127 | + return fmt.Errorf("Error = %s. App Instance not found: %s", strconv.FormatBool(ok), name) |
| 128 | + } |
| 129 | + |
| 130 | + if strings.EqualFold(rs.Primary.ID, "") { |
| 131 | + return fmt.Errorf("App Instance ID is not set") |
| 132 | + } |
| 133 | + |
| 134 | + id := rs.Primary.ID |
| 135 | + client := testAccProvider.Meta().(*Client) |
| 136 | + _, err := client.GetAppInstance(id) |
| 137 | + if err != nil { |
| 138 | + return fmt.Errorf("App instance (id=%s) not found", id) |
| 139 | + } |
| 140 | + return nil |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func testAccSumologicApp(tfResourceName string, uuid string, version string, parameterKey string, parameterValue string) string { |
| 145 | + |
| 146 | + return fmt.Sprintf(` |
| 147 | +
|
| 148 | + resource "sumologic_app" "%s" { |
| 149 | + uuid = "%s" |
| 150 | + version = "%s" |
| 151 | + parameters = { |
| 152 | + "%s": "%s" |
| 153 | + } |
| 154 | + } |
| 155 | + `, tfResourceName, uuid, version, parameterKey, parameterValue) |
| 156 | +} |
0 commit comments