Skip to content

Commit

Permalink
Tests for EKS. Added nil check in update handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsonov committed Nov 15, 2024
1 parent 59f26b5 commit 662d30c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 24 deletions.
4 changes: 3 additions & 1 deletion castai/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ func resourceCastaiClusterUpdate(
return fmt.Errorf("error in update cluster response: %w", err)
}

credentialsID = *response.JSON200.CredentialsId
if response.JSON200.CredentialsId != nil {
credentialsID = *response.JSON200.CredentialsId
}
return nil
}, b, func(err error, _ time.Duration) {
// Only store non-context errors so we can surface the last "real" error to the user at the end
Expand Down
121 changes: 98 additions & 23 deletions castai/resource_eks_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package castai
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
Expand Down Expand Up @@ -145,34 +146,108 @@ func TestEKSClusterResourceReadContextArchived(t *testing.T) {
}

func TestEKSClusterResourceUpdateError(t *testing.T) {
r := require.New(t)
mockctrl := gomock.NewController(t)
mockClient := mock_sdk.NewMockClientInterface(mockctrl)

clusterID := "b6bfc074-a267-400f-b8f1-db0850c36gk3d"
ctx := context.Background()
provider := &ProviderConfig{
api: &sdk.ClientWithResponses{
ClientInterface: mockClient,
},
}

clusterId := "b6bfc074-a267-400f-b8f1-db0850c36gk3d"
mockClient.EXPECT().
ExternalClusterAPIUpdateCluster(gomock.Any(), clusterId, gomock.Any(), gomock.Any()).
Return(&http.Response{StatusCode: 400, Body: io.NopCloser(bytes.NewBufferString(`{"message":"Bad Request", "fieldViolations":[{"field":"credentials","description":"error"}]}`)), Header: map[string][]string{"Content-Type": {"json"}}}, nil)
t.Run("resource update error generic propagated", func(t *testing.T) {
r := require.New(t)
mockctrl := gomock.NewController(t)
mockClient := mock_sdk.NewMockClientInterface(mockctrl)

resource := resourceEKSCluster()
provider := &ProviderConfig{
api: &sdk.ClientWithResponses{
ClientInterface: mockClient,
},
}

raw := make(map[string]interface{})
raw[FieldEKSClusterAssumeRoleArn] = "something"
mockClient.EXPECT().
ExternalClusterAPIUpdateCluster(gomock.Any(), clusterID, gomock.Any(), gomock.Any()).
Return(&http.Response{StatusCode: 400, Body: io.NopCloser(bytes.NewBufferString(`{"message":"Bad Request", "fieldViolations":[{"field":"credentials","description":"error"}]}`)), Header: map[string][]string{"Content-Type": {"json"}}}, nil)

resource := resourceEKSCluster()

raw := make(map[string]interface{})
raw[FieldEKSClusterAssumeRoleArn] = "something"

data := schema.TestResourceDataRaw(t, resource.Schema, raw)
_ = data.Set(FieldEKSClusterAssumeRoleArn, "creds")
data.SetId(clusterID)
result := resource.UpdateContext(ctx, data, provider)
r.NotNil(result)
r.True(result.HasError())
r.Equal("updating cluster configuration: expected status code 200, received: status=400 body={\"message\":\"Bad Request\", \"fieldViolations\":[{\"field\":\"credentials\",\"description\":\"error\"}]}", result[0].Summary)
})

t.Run("credentials_id special handling", func(t *testing.T) {
t.Run("on successful update, should avoid drift on the read", func(t *testing.T) {
r := require.New(t)
mockctrl := gomock.NewController(t)
mockClient := mock_sdk.NewMockClientInterface(mockctrl)
provider := &ProviderConfig{
api: &sdk.ClientWithResponses{
ClientInterface: mockClient,
},
}

credentialsIDAfterUpdate := "after-update-credentialsid"
roleARN := "aws-role"
updateResponse := io.NopCloser(bytes.NewReader([]byte(fmt.Sprintf(`{"credentialsId": "%s"}`, credentialsIDAfterUpdate))))
readResponse := io.NopCloser(bytes.NewReader([]byte(fmt.Sprintf(`{"credentialsId": "%s"}`, credentialsIDAfterUpdate))))
mockClient.EXPECT().
ExternalClusterAPIGetCluster(gomock.Any(), clusterID).
Return(&http.Response{StatusCode: 200, Body: readResponse, Header: map[string][]string{"Content-Type": {"json"}}}, nil)
mockClient.EXPECT().
ExternalClusterAPIUpdateCluster(gomock.Any(), clusterID, gomock.Any()).
Return(&http.Response{StatusCode: 200, Body: updateResponse, Header: map[string][]string{"Content-Type": {"json"}}}, nil)

awsResource := resourceEKSCluster()

diff := map[string]any{
FieldEKSClusterAssumeRoleArn: roleARN,
FieldClusterCredentialsId: "before-update-credentialsid",
}
data := schema.TestResourceDataRaw(t, awsResource.Schema, diff)
data.SetId(clusterID)
diagnostics := awsResource.UpdateContext(ctx, data, provider)

r.Empty(diagnostics)

r.Equal(credentialsIDAfterUpdate, data.Get(FieldClusterCredentialsId))
r.Equal(roleARN, data.Get(FieldEKSClusterAssumeRoleArn))
})

t.Run("on failed update, should overwrite credentialsID", func(t *testing.T) {
r := require.New(t)
mockctrl := gomock.NewController(t)
mockClient := mock_sdk.NewMockClientInterface(mockctrl)
provider := &ProviderConfig{
api: &sdk.ClientWithResponses{
ClientInterface: mockClient,
},
}

mockClient.EXPECT().
ExternalClusterAPIUpdateCluster(gomock.Any(), clusterID, gomock.Any()).
Return(&http.Response{StatusCode: 400, Body: http.NoBody}, nil)

awsResource := resourceEKSCluster()

credentialsID := "credentialsID-before-updates"
diff := map[string]any{
FieldClusterCredentialsId: credentialsID,
}
data := schema.TestResourceDataRaw(t, awsResource.Schema, diff)
data.SetId(clusterID)
diagnostics := awsResource.UpdateContext(ctx, data, provider)

r.NotEmpty(diagnostics)

valueAfter := data.Get(FieldClusterCredentialsId)
r.NotEqual(credentialsID, valueAfter)
r.Contains(valueAfter, "drift")
})
})

data := schema.TestResourceDataRaw(t, resource.Schema, raw)
_ = data.Set(FieldEKSClusterAssumeRoleArn, "creds")
data.SetId(clusterId)
result := resource.UpdateContext(ctx, data, provider)
r.NotNil(result)
r.True(result.HasError())
r.Equal("updating cluster configuration: expected status code 200, received: status=400 body={\"message\":\"Bad Request\", \"fieldViolations\":[{\"field\":\"credentials\",\"description\":\"error\"}]}", result[0].Summary)
}

func TestEKSClusterResourceUpdateRetry(t *testing.T) {
Expand Down

0 comments on commit 662d30c

Please sign in to comment.