|
| 1 | +package castai |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/golang/mock/gomock" |
| 11 | + "github.com/hashicorp/go-cty/cty" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 13 | + "github.com/samber/lo" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + |
| 16 | + "github.com/castai/terraform-provider-castai/castai/sdk" |
| 17 | + "github.com/castai/terraform-provider-castai/castai/sdk/cluster_autoscaler" |
| 18 | + mock_cluster_autoscaler "github.com/castai/terraform-provider-castai/castai/sdk/cluster_autoscaler/mock" |
| 19 | + mock_sdk "github.com/castai/terraform-provider-castai/castai/sdk/mock" |
| 20 | +) |
| 21 | + |
| 22 | +func TestHibernationScheduleDataSourceRead(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + ctrl := gomock.NewController(t) |
| 25 | + defer ctrl.Finish() |
| 26 | + |
| 27 | + r := require.New(t) |
| 28 | + mockClient := mock_sdk.NewMockClientWithResponsesInterface(ctrl) |
| 29 | + clusterAutoscalerClient := mock_cluster_autoscaler.NewMockClientInterface(ctrl) |
| 30 | + |
| 31 | + ctx := context.Background() |
| 32 | + provider := &ProviderConfig{ |
| 33 | + api: mockClient, |
| 34 | + clusterAutoscalerClient: &cluster_autoscaler.ClientWithResponses{ |
| 35 | + ClientInterface: clusterAutoscalerClient, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + organizationID := "0d0111f9-e5a4-4acc-85b2-4c3a8318dfc2" |
| 40 | + body := io.NopCloser(bytes.NewReader([]byte(`{ |
| 41 | + "items": [ |
| 42 | + { |
| 43 | + "id": "75c04e4e-f95c-4f24-a814-b8e753a5194d", |
| 44 | + "organizationId": "0d0111f9-e5a4-4acc-85b2-4c3a8318dfc2", |
| 45 | + "enabled": false, |
| 46 | + "name": "schedule", |
| 47 | + "pauseConfig": { |
| 48 | + "enabled": true, |
| 49 | + "schedule": { |
| 50 | + "cronExpression": "1 0 * * *" |
| 51 | + } |
| 52 | + }, |
| 53 | + "resumeConfig": { |
| 54 | + "enabled": true, |
| 55 | + "schedule": { |
| 56 | + "cronExpression": "1 0 * * *" |
| 57 | + }, |
| 58 | + "jobConfig": { |
| 59 | + "nodeConfig": { |
| 60 | + "instanceType": "e2-standard-4", |
| 61 | + "kubernetesLabels": {}, |
| 62 | + "kubernetesTaints": [] |
| 63 | + } |
| 64 | + } |
| 65 | + }, |
| 66 | + "clusterAssignments": { |
| 67 | + "items": [ |
| 68 | + { |
| 69 | + "clusterId": "38a49ce8-e900-4a10-be89-48fb2efb1025" |
| 70 | + } |
| 71 | + ] |
| 72 | + }, |
| 73 | + "createTime": "2025-04-10T12:52:07.732194Z", |
| 74 | + "updateTime": "2025-04-10T12:52:07.732194Z" |
| 75 | + } |
| 76 | + ], |
| 77 | + "nextPageCursor": "", |
| 78 | + "totalCount": 1 |
| 79 | +}`))) |
| 80 | + |
| 81 | + mockClient.EXPECT().UsersAPIListOrganizationsWithResponse(gomock.Any()).Return(&sdk.UsersAPIListOrganizationsResponse{ |
| 82 | + JSON200: &sdk.CastaiUsersV1beta1ListOrganizationsResponse{ |
| 83 | + Organizations: []sdk.CastaiUsersV1beta1UserOrganization{ |
| 84 | + {Id: lo.ToPtr(organizationID)}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + HTTPResponse: &http.Response{StatusCode: http.StatusOK}, |
| 88 | + }, nil).Times(1) |
| 89 | + |
| 90 | + clusterAutoscalerClient.EXPECT(). |
| 91 | + HibernationSchedulesAPIListHibernationSchedules(gomock.Any(), organizationID, gomock.Any()). |
| 92 | + Return(&http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(body), Header: map[string][]string{"Content-Type": {"json"}}}, nil) |
| 93 | + |
| 94 | + state := terraform.NewInstanceStateShimmedFromValue(cty.ObjectVal(map[string]cty.Value{}), 0) |
| 95 | + |
| 96 | + resource := dataSourceHibernationSchedule() |
| 97 | + data := resource.Data(state) |
| 98 | + |
| 99 | + r.NoError(data.Set("name", "schedule")) |
| 100 | + |
| 101 | + result := resource.ReadContext(ctx, data, provider) |
| 102 | + r.Nil(result) |
| 103 | + r.False(result.HasError()) |
| 104 | + |
| 105 | + expectedState := `ID = 75c04e4e-f95c-4f24-a814-b8e753a5194d |
| 106 | +cluster_assignments.# = 1 |
| 107 | +cluster_assignments.0.assignment.# = 1 |
| 108 | +cluster_assignments.0.assignment.0.cluster_id = 38a49ce8-e900-4a10-be89-48fb2efb1025 |
| 109 | +enabled = false |
| 110 | +name = schedule |
| 111 | +organization_id = 0d0111f9-e5a4-4acc-85b2-4c3a8318dfc2 |
| 112 | +pause_config.# = 1 |
| 113 | +pause_config.0.enabled = true |
| 114 | +pause_config.0.schedule.# = 1 |
| 115 | +pause_config.0.schedule.0.cron_expression = 1 0 * * * |
| 116 | +resume_config.# = 1 |
| 117 | +resume_config.0.enabled = true |
| 118 | +resume_config.0.job_config.# = 1 |
| 119 | +resume_config.0.job_config.0.node_config.# = 1 |
| 120 | +resume_config.0.job_config.0.node_config.0.config_id = |
| 121 | +resume_config.0.job_config.0.node_config.0.config_name = |
| 122 | +resume_config.0.job_config.0.node_config.0.gpu_config.# = 0 |
| 123 | +resume_config.0.job_config.0.node_config.0.instance_type = e2-standard-4 |
| 124 | +resume_config.0.job_config.0.node_config.0.kubernetes_labels.% = 0 |
| 125 | +resume_config.0.job_config.0.node_config.0.kubernetes_taints.# = 0 |
| 126 | +resume_config.0.job_config.0.node_config.0.node_affinity.# = 0 |
| 127 | +resume_config.0.job_config.0.node_config.0.spot_config.# = 0 |
| 128 | +resume_config.0.job_config.0.node_config.0.subnet_id = |
| 129 | +resume_config.0.job_config.0.node_config.0.volume.# = 0 |
| 130 | +resume_config.0.job_config.0.node_config.0.zone = |
| 131 | +resume_config.0.schedule.# = 1 |
| 132 | +resume_config.0.schedule.0.cron_expression = 1 0 * * * |
| 133 | +Tainted = false |
| 134 | +` |
| 135 | + r.Equal(expectedState, data.State().String()) |
| 136 | +} |
0 commit comments