Skip to content

Commit e200923

Browse files
authored
feat: add acceptance tests for DBO resources (#609)
1 parent 30034dc commit e200923

6 files changed

Lines changed: 328 additions & 2 deletions

castai/data_source_cache_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func dataSourceCacheGroup() *schema.Resource {
6363
FieldCacheGroupEndpointConnectionString: {
6464
Type: schema.TypeString,
6565
Computed: true,
66-
Description: "Connection string for the endpoint.",
66+
Description: "Connection string for the endpoint. Only available once DBO is deployed and running on the Kubernetes cluster.",
6767
},
6868
},
6969
},

castai/data_source_cache_group_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"fmt"
78
"io"
89
"net/http"
910
"testing"
1011

1112
"github.com/golang/mock/gomock"
1213
"github.com/hashicorp/go-cty/cty"
14+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1315
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
16+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1417
"github.com/stretchr/testify/require"
1518

1619
"github.com/castai/terraform-provider-castai/castai/sdk"
@@ -158,3 +161,105 @@ func TestCacheGroupDataSourceReadNotFound(t *testing.T) {
158161
result := resource.ReadContext(ctx, data, provider)
159162
r.True(result.HasError())
160163
}
164+
165+
func TestAccCloudAgnostic_DataSourceCacheGroup(t *testing.T) {
166+
rName := fmt.Sprintf("%v-cache-group-%v", ResourcePrefix, acctest.RandString(8))
167+
resourceName := "castai_cache_group.test"
168+
dataSourceName := "data.castai_cache_group.test"
169+
170+
resource.ParallelTest(t, resource.TestCase{
171+
PreCheck: func() { testAccPreCheck(t) },
172+
ProviderFactories: providerFactories,
173+
CheckDestroy: testAccCacheGroupDestroy,
174+
Steps: []resource.TestStep{
175+
{
176+
Config: testAccDataSourceCacheGroupConfig(rName),
177+
Check: resource.ComposeTestCheckFunc(
178+
resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"),
179+
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
180+
resource.TestCheckResourceAttrPair(dataSourceName, "protocol_type", resourceName, "protocol_type"),
181+
resource.TestCheckResourceAttr(dataSourceName, "name", rName),
182+
resource.TestCheckResourceAttr(dataSourceName, "protocol_type", "PostgreSQL"),
183+
),
184+
},
185+
},
186+
})
187+
}
188+
189+
func TestAccCloudAgnostic_DataSourceCacheGroupWithEndpoints(t *testing.T) {
190+
rName := fmt.Sprintf("%v-cache-group-%v", ResourcePrefix, acctest.RandString(8))
191+
resourceName := "castai_cache_group.test"
192+
dataSourceName := "data.castai_cache_group.test"
193+
194+
resource.ParallelTest(t, resource.TestCase{
195+
PreCheck: func() { testAccPreCheck(t) },
196+
ProviderFactories: providerFactories,
197+
CheckDestroy: testAccCacheGroupDestroy,
198+
Steps: []resource.TestStep{
199+
{
200+
Config: testAccDataSourceCacheGroupWithEndpointsConfig(rName),
201+
Check: resource.ComposeTestCheckFunc(
202+
resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"),
203+
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
204+
resource.TestCheckResourceAttrPair(dataSourceName, "protocol_type", resourceName, "protocol_type"),
205+
resource.TestCheckResourceAttrPair(dataSourceName, "direct_mode", resourceName, "direct_mode"),
206+
resource.TestCheckResourceAttr(dataSourceName, "name", rName),
207+
resource.TestCheckResourceAttr(dataSourceName, "protocol_type", "PostgreSQL"),
208+
resource.TestCheckResourceAttr(dataSourceName, "direct_mode", "true"),
209+
// Verify endpoints
210+
resource.TestCheckResourceAttr(dataSourceName, "endpoints.#", "2"),
211+
resource.TestCheckResourceAttrPair(dataSourceName, "endpoints.0.hostname", resourceName, "endpoints.0.hostname"),
212+
resource.TestCheckResourceAttrPair(dataSourceName, "endpoints.0.port", resourceName, "endpoints.0.port"),
213+
resource.TestCheckResourceAttrPair(dataSourceName, "endpoints.0.name", resourceName, "endpoints.0.name"),
214+
resource.TestCheckResourceAttr(dataSourceName, "endpoints.0.hostname", "primary.db.example.com"),
215+
resource.TestCheckResourceAttr(dataSourceName, "endpoints.0.port", "5432"),
216+
resource.TestCheckResourceAttr(dataSourceName, "endpoints.0.name", "primary"),
217+
resource.TestCheckResourceAttrPair(dataSourceName, "endpoints.1.hostname", resourceName, "endpoints.1.hostname"),
218+
resource.TestCheckResourceAttrPair(dataSourceName, "endpoints.1.port", resourceName, "endpoints.1.port"),
219+
resource.TestCheckResourceAttrPair(dataSourceName, "endpoints.1.name", resourceName, "endpoints.1.name"),
220+
resource.TestCheckResourceAttr(dataSourceName, "endpoints.1.hostname", "replica.db.example.com"),
221+
resource.TestCheckResourceAttr(dataSourceName, "endpoints.1.port", "5433"),
222+
resource.TestCheckResourceAttr(dataSourceName, "endpoints.1.name", "replica"),
223+
// Note: connection_string is only available once DBO is deployed and running on the cluster
224+
),
225+
},
226+
},
227+
})
228+
}
229+
230+
func testAccDataSourceCacheGroupConfig(name string) string {
231+
return fmt.Sprintf(`
232+
resource "castai_cache_group" "test" {
233+
name = %[1]q
234+
protocol_type = "PostgreSQL"
235+
}
236+
237+
data "castai_cache_group" "test" {
238+
id = castai_cache_group.test.id
239+
}`, name)
240+
}
241+
242+
func testAccDataSourceCacheGroupWithEndpointsConfig(name string) string {
243+
return fmt.Sprintf(`
244+
resource "castai_cache_group" "test" {
245+
name = %[1]q
246+
protocol_type = "PostgreSQL"
247+
direct_mode = true
248+
249+
endpoints {
250+
hostname = "primary.db.example.com"
251+
port = 5432
252+
name = "primary"
253+
}
254+
255+
endpoints {
256+
hostname = "replica.db.example.com"
257+
port = 5433
258+
name = "replica"
259+
}
260+
}
261+
262+
data "castai_cache_group" "test" {
263+
id = castai_cache_group.test.id
264+
}`, name)
265+
}

castai/resource_cache_configuration_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"fmt"
78
"io"
89
"net/http"
910
"testing"
11+
"time"
1012

1113
"github.com/golang/mock/gomock"
1214
"github.com/hashicorp/go-cty/cty"
15+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1316
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
17+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
18+
tfterraform "github.com/hashicorp/terraform-plugin-testing/terraform"
1419
"github.com/stretchr/testify/require"
1520

1621
"github.com/castai/terraform-provider-castai/castai/sdk"
@@ -393,3 +398,74 @@ func TestCacheConfigurationResource_ModeValidation(t *testing.T) {
393398
})
394399
}
395400
}
401+
402+
func TestAccCloudAgnostic_ResourceCacheConfiguration(t *testing.T) {
403+
rName := fmt.Sprintf("%v-cache-config-%v", ResourcePrefix, acctest.RandString(8))
404+
dbName := fmt.Sprintf("testdb_%v", acctest.RandString(8))
405+
resourceName := "castai_cache_configuration.test"
406+
407+
resource.ParallelTest(t, resource.TestCase{
408+
PreCheck: func() { testAccPreCheck(t) },
409+
ProviderFactories: providerFactories,
410+
CheckDestroy: testAccCacheConfigurationDestroy,
411+
Steps: []resource.TestStep{
412+
{
413+
Config: testAccCreateCacheConfigurationConfig(rName, dbName),
414+
Check: resource.ComposeTestCheckFunc(
415+
resource.TestCheckResourceAttr(resourceName, "database_name", dbName),
416+
resource.TestCheckResourceAttr(resourceName, "mode", "Auto"),
417+
resource.TestCheckResourceAttrSet(resourceName, "id"),
418+
resource.TestCheckResourceAttrSet(resourceName, "cache_group_id"),
419+
),
420+
},
421+
},
422+
})
423+
}
424+
425+
func testAccCreateCacheConfigurationConfig(groupName, dbName string) string {
426+
return fmt.Sprintf(`
427+
resource "castai_cache_group" "test" {
428+
name = %[1]q
429+
protocol_type = "PostgreSQL"
430+
}
431+
432+
resource "castai_cache_configuration" "test" {
433+
cache_group_id = castai_cache_group.test.id
434+
database_name = %[2]q
435+
mode = "Auto"
436+
}`, groupName, dbName)
437+
}
438+
439+
func testAccCacheConfigurationDestroy(s *tfterraform.State) error {
440+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
441+
defer cancel()
442+
443+
client := testAccProvider.Meta().(*ProviderConfig).api
444+
for _, rs := range s.RootModule().Resources {
445+
if rs.Type != "castai_cache_configuration" {
446+
continue
447+
}
448+
449+
cacheGroupID := rs.Primary.Attributes["cache_group_id"]
450+
configID := rs.Primary.ID
451+
452+
response, err := client.DboAPIListCacheConfigurationsWithResponse(ctx, cacheGroupID, &sdk.DboAPIListCacheConfigurationsParams{})
453+
if err != nil {
454+
return err
455+
}
456+
457+
if response.StatusCode() == http.StatusNotFound {
458+
return nil
459+
}
460+
461+
if response.JSON200 != nil && response.JSON200.Items != nil {
462+
for _, cfg := range *response.JSON200.Items {
463+
if cfg.Id != nil && *cfg.Id == configID {
464+
return fmt.Errorf("cache configuration %s still exists", configID)
465+
}
466+
}
467+
}
468+
}
469+
470+
return nil
471+
}

castai/resource_cache_group_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"fmt"
78
"io"
89
"net/http"
910
"testing"
11+
"time"
1012

1113
"github.com/golang/mock/gomock"
1214
"github.com/hashicorp/go-cty/cty"
15+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1316
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
17+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
18+
tfterraform "github.com/hashicorp/terraform-plugin-testing/terraform"
1419
"github.com/stretchr/testify/require"
1520

1621
"github.com/castai/terraform-provider-castai/castai/sdk"
@@ -421,3 +426,57 @@ func TestFlattenEndpoints(t *testing.T) {
421426
})
422427
}
423428
}
429+
430+
func TestAccCloudAgnostic_ResourceCacheGroup(t *testing.T) {
431+
rName := fmt.Sprintf("%v-cache-group-%v", ResourcePrefix, acctest.RandString(8))
432+
resourceName := "castai_cache_group.test"
433+
434+
resource.ParallelTest(t, resource.TestCase{
435+
PreCheck: func() { testAccPreCheck(t) },
436+
ProviderFactories: providerFactories,
437+
CheckDestroy: testAccCacheGroupDestroy,
438+
Steps: []resource.TestStep{
439+
{
440+
Config: testAccCreateCacheGroupConfig(rName),
441+
Check: resource.ComposeTestCheckFunc(
442+
resource.TestCheckResourceAttr(resourceName, "name", rName),
443+
resource.TestCheckResourceAttr(resourceName, "protocol_type", "PostgreSQL"),
444+
resource.TestCheckResourceAttrSet(resourceName, "id"),
445+
),
446+
},
447+
},
448+
})
449+
}
450+
451+
func testAccCreateCacheGroupConfig(name string) string {
452+
return fmt.Sprintf(`
453+
resource "castai_cache_group" "test" {
454+
name = %[1]q
455+
protocol_type = "PostgreSQL"
456+
}`, name)
457+
}
458+
459+
func testAccCacheGroupDestroy(s *tfterraform.State) error {
460+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
461+
defer cancel()
462+
463+
client := testAccProvider.Meta().(*ProviderConfig).api
464+
for _, rs := range s.RootModule().Resources {
465+
if rs.Type != "castai_cache_group" {
466+
continue
467+
}
468+
469+
response, err := client.DboAPIGetCacheGroupWithResponse(ctx, rs.Primary.ID, &sdk.DboAPIGetCacheGroupParams{})
470+
if err != nil {
471+
return err
472+
}
473+
474+
if response.StatusCode() == http.StatusNotFound {
475+
return nil
476+
}
477+
478+
return fmt.Errorf("cache group %s still exists", rs.Primary.ID)
479+
}
480+
481+
return nil
482+
}

0 commit comments

Comments
 (0)