|
| 1 | +package castai |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/golang/mock/gomock" |
| 12 | + "github.com/hashicorp/go-cty/cty" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + |
| 16 | + "github.com/castai/terraform-provider-castai/castai/sdk" |
| 17 | + mock_sdk "github.com/castai/terraform-provider-castai/castai/sdk/mock" |
| 18 | +) |
| 19 | + |
| 20 | +func TestCacheGroupDataSourceRead(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + |
| 23 | + r := require.New(t) |
| 24 | + mockClient := mock_sdk.NewMockClientInterface(gomock.NewController(t)) |
| 25 | + |
| 26 | + ctx := context.Background() |
| 27 | + provider := &ProviderConfig{ |
| 28 | + api: &sdk.ClientWithResponses{ |
| 29 | + ClientInterface: mockClient, |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + cacheGroupID := "cache-group-123" |
| 34 | + name := "test-cache-group" |
| 35 | + protocolType := "MySQL" |
| 36 | + directMode := true |
| 37 | + suffix := "primary" |
| 38 | + connectionString := "mysql://db.example.com:3306" |
| 39 | + |
| 40 | + readResponse := sdk.DboV1CacheGroup{ |
| 41 | + Id: &cacheGroupID, |
| 42 | + ProtocolType: sdk.DboV1CacheGroupProtocolType(protocolType), |
| 43 | + Name: &name, |
| 44 | + DirectMode: &directMode, |
| 45 | + Endpoints: &[]sdk.DboV1Endpoint{ |
| 46 | + { |
| 47 | + Hostname: "db.example.com", |
| 48 | + Port: 3306, |
| 49 | + Suffix: &suffix, |
| 50 | + ConnectionString: &connectionString, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + readBody, _ := json.Marshal(readResponse) |
| 56 | + mockClient.EXPECT(). |
| 57 | + DboAPIGetCacheGroup(ctx, cacheGroupID, gomock.Any()). |
| 58 | + Return(&http.Response{ |
| 59 | + StatusCode: http.StatusOK, |
| 60 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 61 | + Body: io.NopCloser(bytes.NewReader(readBody)), |
| 62 | + }, nil) |
| 63 | + |
| 64 | + state := terraform.NewInstanceStateShimmedFromValue(cty.ObjectVal(map[string]cty.Value{}), 0) |
| 65 | + |
| 66 | + resource := dataSourceCacheGroup() |
| 67 | + data := resource.Data(state) |
| 68 | + r.NoError(data.Set("id", cacheGroupID)) |
| 69 | + |
| 70 | + result := resource.ReadContext(ctx, data, provider) |
| 71 | + r.Nil(result) |
| 72 | + r.False(result.HasError()) |
| 73 | + r.Equal(`ID = cache-group-123 |
| 74 | +direct_mode = true |
| 75 | +endpoints.# = 1 |
| 76 | +endpoints.0.connection_string = mysql://db.example.com:3306 |
| 77 | +endpoints.0.hostname = db.example.com |
| 78 | +endpoints.0.name = primary |
| 79 | +endpoints.0.port = 3306 |
| 80 | +name = test-cache-group |
| 81 | +protocol_type = MySQL |
| 82 | +Tainted = false |
| 83 | +`, data.State().String()) |
| 84 | +} |
| 85 | + |
| 86 | +func TestCacheGroupDataSourceReadEmptyName(t *testing.T) { |
| 87 | + t.Parallel() |
| 88 | + |
| 89 | + r := require.New(t) |
| 90 | + mockClient := mock_sdk.NewMockClientInterface(gomock.NewController(t)) |
| 91 | + |
| 92 | + ctx := context.Background() |
| 93 | + provider := &ProviderConfig{ |
| 94 | + api: &sdk.ClientWithResponses{ |
| 95 | + ClientInterface: mockClient, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + cacheGroupID := "cache-group-123" |
| 100 | + protocolType := "PostgreSQL" |
| 101 | + |
| 102 | + readResponse := sdk.DboV1CacheGroup{ |
| 103 | + Id: &cacheGroupID, |
| 104 | + ProtocolType: sdk.DboV1CacheGroupProtocolType(protocolType), |
| 105 | + Name: nil, |
| 106 | + } |
| 107 | + |
| 108 | + readBody, _ := json.Marshal(readResponse) |
| 109 | + mockClient.EXPECT(). |
| 110 | + DboAPIGetCacheGroup(ctx, cacheGroupID, gomock.Any()). |
| 111 | + Return(&http.Response{ |
| 112 | + StatusCode: http.StatusOK, |
| 113 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 114 | + Body: io.NopCloser(bytes.NewReader(readBody)), |
| 115 | + }, nil) |
| 116 | + |
| 117 | + state := terraform.NewInstanceStateShimmedFromValue(cty.ObjectVal(map[string]cty.Value{}), 0) |
| 118 | + |
| 119 | + resource := dataSourceCacheGroup() |
| 120 | + data := resource.Data(state) |
| 121 | + r.NoError(data.Set("id", cacheGroupID)) |
| 122 | + |
| 123 | + result := resource.ReadContext(ctx, data, provider) |
| 124 | + r.Nil(result) |
| 125 | + r.False(result.HasError()) |
| 126 | + r.Equal("ID = cache-group-123\nname = \nprotocol_type = PostgreSQL\nTainted = false\n", data.State().String()) |
| 127 | +} |
| 128 | + |
| 129 | +func TestCacheGroupDataSourceReadNotFound(t *testing.T) { |
| 130 | + t.Parallel() |
| 131 | + |
| 132 | + r := require.New(t) |
| 133 | + mockClient := mock_sdk.NewMockClientInterface(gomock.NewController(t)) |
| 134 | + |
| 135 | + ctx := context.Background() |
| 136 | + provider := &ProviderConfig{ |
| 137 | + api: &sdk.ClientWithResponses{ |
| 138 | + ClientInterface: mockClient, |
| 139 | + }, |
| 140 | + } |
| 141 | + |
| 142 | + cacheGroupID := "non-existent-id" |
| 143 | + |
| 144 | + mockClient.EXPECT(). |
| 145 | + DboAPIGetCacheGroup(ctx, cacheGroupID, gomock.Any()). |
| 146 | + Return(&http.Response{ |
| 147 | + StatusCode: http.StatusNotFound, |
| 148 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 149 | + Body: io.NopCloser(bytes.NewReader([]byte(`{"message": "not found"}`))), |
| 150 | + }, nil) |
| 151 | + |
| 152 | + state := terraform.NewInstanceStateShimmedFromValue(cty.ObjectVal(map[string]cty.Value{}), 0) |
| 153 | + |
| 154 | + resource := dataSourceCacheGroup() |
| 155 | + data := resource.Data(state) |
| 156 | + r.NoError(data.Set("id", cacheGroupID)) |
| 157 | + |
| 158 | + result := resource.ReadContext(ctx, data, provider) |
| 159 | + r.True(result.HasError()) |
| 160 | +} |
0 commit comments