@@ -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+ }
0 commit comments