Skip to content

Commit

Permalink
fix update
Browse files Browse the repository at this point in the history
  • Loading branch information
ValyaB committed Mar 27, 2024
1 parent b71f2ed commit eea9986
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 26 deletions.
4 changes: 2 additions & 2 deletions castai/resource_node_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
Expand Down
114 changes: 90 additions & 24 deletions castai/resource_node_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,14 @@ 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"

"github.com/castai/terraform-provider-castai/castai/sdk"
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)
Expand Down Expand Up @@ -91,8 +73,6 @@ func Test_resourceNodeConfigurationCreate(t *testing.T) {
"default": false,
"diskCpuRatio": 0,
"subnets": [
"subnet-0ede99883f8d65813",
"subnet-0f7cb7d2702533af0",
"subnet-0beea7ca69ceb165b"
],
"tags": {
Expand All @@ -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",
Expand Down Expand Up @@ -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{}

Check failure on line 155 in castai/resource_node_configuration_test.go

View workflow job for this annotation

GitHub Actions / lint

field `dataSetInitial` is unused (unused)
dataSetUpdate map[string]interface{}

Check failure on line 156 in castai/resource_node_configuration_test.go

View workflow job for this annotation

GitHub Actions / lint

field `dataSetUpdate` is unused (unused)
}
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())
})
}
}

0 comments on commit eea9986

Please sign in to comment.