From eea9986eba13618cae7e0d67995a8bf314f39c3b Mon Sep 17 00:00:00 2001 From: Valentyna Bukhalova Date: Wed, 27 Mar 2024 13:57:48 +0100 Subject: [PATCH] fix update --- castai/resource_node_configuration.go | 4 +- castai/resource_node_configuration_test.go | 114 ++++++++++++++++----- 2 files changed, 92 insertions(+), 26 deletions(-) diff --git a/castai/resource_node_configuration.go b/castai/resource_node_configuration.go index d278cc2b..439d321f 100644 --- a/castai/resource_node_configuration.go +++ b/castai/resource_node_configuration.go @@ -623,8 +623,8 @@ func toEKSConfig(obj map[string]interface{}) *sdk.NodeconfigV1EKSConfig { if arn, ok := e["arn"].(string); ok && arn != "" { out.TargetGroup.Arn = toPtr(arn) } - if port, ok := e["port"].(int32); ok && port > 0 && port < 65536 { - out.TargetGroup.Port = toPtr(port) + if port, ok := e["port"].(int); ok && port > 0 && port < 65536 { + out.TargetGroup.Port = toPtr(int32(port)) } } } diff --git a/castai/resource_node_configuration_test.go b/castai/resource_node_configuration_test.go index ea180dae..676c34f2 100644 --- a/castai/resource_node_configuration_test.go +++ b/castai/resource_node_configuration_test.go @@ -9,6 +9,7 @@ import ( "github.com/golang/mock/gomock" "github.com/hashicorp/go-cty/cty" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/stretchr/testify/require" @@ -16,25 +17,6 @@ import ( mock_sdk "github.com/castai/terraform-provider-castai/castai/sdk/mock" ) -func Test_resourceNodeConfigurationRead(t *testing.T) { - t.Run("flattern to node configs", func(t *testing.T) { - result := flattenEKSConfig(&sdk.NodeconfigV1EKSConfig{ - TargetGroup: &sdk.NodeconfigV1TargetGroup{ - Arn: toPtr("arn:aws:iam::aws:policy/AdministratorAccess"), - Port: toPtr(int32(80)), - }, - }) - - got := toEKSConfig(result[0]) - require.Equal(t, &sdk.NodeconfigV1EKSConfig{ - TargetGroup: &sdk.NodeconfigV1TargetGroup{ - Arn: toPtr("arn:aws:iam::aws:policy/AdministratorAccess"), - Port: toPtr(int32(80)), - }, - }, got) - }) -} - func Test_resourceNodeConfigurationCreate(t *testing.T) { type args struct { tuneMock func(m *mock_sdk.MockClientInterface) @@ -91,8 +73,6 @@ func Test_resourceNodeConfigurationCreate(t *testing.T) { "default": false, "diskCpuRatio": 0, "subnets": [ - "subnet-0ede99883f8d65813", - "subnet-0f7cb7d2702533af0", "subnet-0beea7ca69ceb165b" ], "tags": { @@ -101,9 +81,6 @@ func Test_resourceNodeConfigurationCreate(t *testing.T) { "eks": { "securityGroups": [ "sg-04dffb0bec1821a92", - "sg-008614a8aad956a53", - "sg-031d074b817c04773", - "sg-053e9a1980987985a", "sg-084e6537aff751bd6" ], "instanceProfileArn": "arn:aws:iam::028075177508:instance-profile/cast-valentyna-0326-1-eks-0626f5c2", @@ -171,3 +148,92 @@ func Test_resourceNodeConfigurationCreate(t *testing.T) { }) } } + +func Test_NodeConfiguration_UpdateContext(t *testing.T) { + type args struct { + tuneMock func(m *mock_sdk.MockClientInterface) + dataSetInitial map[string]interface{} + dataSetUpdate map[string]interface{} + } + tests := []struct { + name string + args args + }{ + { + name: "success", + args: args{ + tuneMock: func(m *mock_sdk.MockClientInterface) { + m.EXPECT().NodeConfigurationAPIUpdateConfiguration(gomock.Any(), "", + "765fdc7b-2577-4ae8-a6b8-e3b60afbc33a", + sdk.NodeconfigV1NodeConfigurationUpdate{ + Eks: &sdk.NodeconfigV1EKSConfig{ + TargetGroup: &sdk.NodeconfigV1TargetGroup{ + Arn: toPtr("test2"), + Port: toPtr(int32(80)), + }, + ImdsHopLimit: toPtr(int32(0)), + ImdsV1: toPtr(false), + }, + DiskCpuRatio: toPtr(int32(0)), + MinDiskSize: toPtr(int32(100)), + }). + Return( + &http.Response{ + StatusCode: 200, + Header: map[string][]string{"Content-Type": {"json"}}, + Body: io.NopCloser(bytes.NewReader([]byte(`{ + "id": "765fdc7b-2577-4ae8-a6b8-e3b60afbc33a", + "name": "test4" +} +`))), + }, nil) + m.EXPECT().NodeConfigurationAPIGetConfiguration(gomock.Any(), gomock.Any(), gomock.Any()). + Return( + &http.Response{ + StatusCode: 200, + Header: map[string][]string{"Content-Type": {"json"}}, + Body: io.NopCloser(bytes.NewReader([]byte(`{ + "id": "765fdc7b-2577-4ae8-a6b8-e3b60afbc33a", + "name": "test4", + "tags": {}, + "eks": { + "targetGroup": { + "arn": "test2", + "port": 80 + } + } +} +`))), + }, nil) + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockClient := mock_sdk.NewMockClientInterface(gomock.NewController(t)) + if tt.args.tuneMock != nil { + tt.args.tuneMock(mockClient) + } + provider := &ProviderConfig{ + api: &sdk.ClientWithResponses{ + ClientInterface: mockClient, + }, + } + + resource := resourceNodeConfiguration() + raw := make(map[string]interface{}) + data := schema.TestResourceDataRaw(t, resource.Schema, raw) + require.NoError(t, data.Set("eks", flattenEKSConfig(&sdk.NodeconfigV1EKSConfig{ + TargetGroup: &sdk.NodeconfigV1TargetGroup{ + Arn: toPtr("test2"), + Port: toPtr(int32(80)), + }, + }))) + data.SetId("765fdc7b-2577-4ae8-a6b8-e3b60afbc33a") + updateResult := resource.UpdateContext(context.Background(), data, provider) + require.Nil(t, updateResult) + require.False(t, updateResult.HasError()) + }) + } +}