Skip to content

Commit ad35f5a

Browse files
authored
Add unit tests for transform job (#116)
Description of changes: - `Sdk.go` coverage 74.8 % - `Hooks.go` coverage 100 % - Covers majority of custom code will need late_initialize unit-test support to get `manager.go` > 70% coverage By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 780280d commit ad35f5a

22 files changed

+804
-19
lines changed

pkg/resource/transform_job/manager_test_suite_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import (
1717
"errors"
1818
"fmt"
1919

20+
"path/filepath"
21+
"testing"
22+
2023
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
2124
ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics"
2225
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
@@ -27,9 +30,7 @@ import (
2730
"github.com/google/go-cmp/cmp"
2831
"github.com/google/go-cmp/cmp/cmpopts"
2932
"go.uber.org/zap/zapcore"
30-
"path/filepath"
3133
ctrlrtzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
32-
"testing"
3334
)
3435

3536
// provideResourceManagerWithMockSDKAPI accepts MockSageMakerAPI and returns pointer to resourceManager
@@ -102,13 +103,25 @@ func (d *testRunnerDelegate) Equal(a acktypes.AWSResource, b acktypes.AWSResourc
102103
// Ignore LastTransitionTime since it gets updated each run.
103104
opts := []cmp.Option{cmpopts.EquateEmpty(), cmpopts.IgnoreFields(ackv1alpha1.Condition{}, "LastTransitionTime")}
104105

106+
var specMatch = false
107+
if cmp.Equal(ac.ko.Spec, bc.ko.Spec, opts...) {
108+
specMatch = true
109+
} else {
110+
fmt.Printf("Difference ko.Spec (-expected +actual):\n\n")
111+
fmt.Println(cmp.Diff(ac.ko.Spec, bc.ko.Spec, opts...))
112+
specMatch = false
113+
}
114+
115+
var statusMatch = false
105116
if cmp.Equal(ac.ko.Status, bc.ko.Status, opts...) {
106-
return true
117+
statusMatch = true
107118
} else {
108-
fmt.Printf("Difference (-expected +actual):\n\n")
119+
fmt.Printf("Difference ko.Status (-expected +actual):\n\n")
109120
fmt.Println(cmp.Diff(ac.ko.Status, bc.ko.Status, opts...))
110-
return false
121+
statusMatch = false
111122
}
123+
124+
return statusMatch && specMatch
112125
}
113126

114127
// Checks to see if the given yaml file, with name stored as expectation,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"TransformJobArn": "arn:aws:sagemaker:us-west-2:123456789012:transform-job/xgboost-transform-job"
3+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"AutoMLJobArn": null,
3+
"BatchStrategy": null,
4+
"CreationTime": "2021-10-05T05:47:02.644Z",
5+
"DataProcessing": {
6+
"InputFilter": "$",
7+
"JoinSource": "None",
8+
"OutputFilter": "$"
9+
},
10+
"Environment": null,
11+
"ExperimentConfig": null,
12+
"FailureReason": null,
13+
"LabelingJobArn": null,
14+
"MaxConcurrentTransforms": null,
15+
"MaxPayloadInMB": null,
16+
"ModelClientConfig": null,
17+
"ModelName": "xgboost-churn-config-model",
18+
"TransformEndTime": null,
19+
"TransformInput": {
20+
"CompressionType": "None",
21+
"ContentType": "text/csv",
22+
"DataSource": {
23+
"S3DataSource": {
24+
"S3DataType": "S3Prefix",
25+
"S3Uri": "s3://source-data-bucket-592697580195-us-west-2/sagemaker/batch/input-data"
26+
}
27+
},
28+
"SplitType": "None"
29+
},
30+
"TransformJobArn": "arn:aws:sagemaker:us-west-2:123456789012:transform-job/xgboost-transform-job",
31+
"TransformJobName": "xgboost-transform-job",
32+
"TransformJobStatus": "InProgress",
33+
"TransformOutput": {
34+
"Accept": null,
35+
"AssembleWith": "None",
36+
"KmsKeyId": "",
37+
"S3OutputPath": "s3://source-data-bucket-592697580195-us-west-2/sagemaker/batch/output"
38+
},
39+
"TransformResources": {
40+
"InstanceCount": 1,
41+
"InstanceType": "ml.m5.large",
42+
"VolumeKmsKeyId": null
43+
},
44+
"TransformStartTime": null
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"AutoMLJobArn": null,
3+
"BatchStrategy": null,
4+
"CreationTime": "2021-10-05T05:47:02.644Z",
5+
"DataProcessing": {
6+
"InputFilter": "$",
7+
"JoinSource": "None",
8+
"OutputFilter": "$"
9+
},
10+
"Environment": null,
11+
"ExperimentConfig": null,
12+
"FailureReason": null,
13+
"LabelingJobArn": null,
14+
"MaxConcurrentTransforms": null,
15+
"MaxPayloadInMB": null,
16+
"ModelClientConfig": null,
17+
"ModelName": "xgboost-churn-config-model",
18+
"TransformEndTime": null,
19+
"TransformInput": {
20+
"CompressionType": "None",
21+
"ContentType": "text/csv",
22+
"DataSource": {
23+
"S3DataSource": {
24+
"S3DataType": "S3Prefix",
25+
"S3Uri": "s3://source-data-bucket-592697580195-us-west-2/sagemaker/batch/input-data"
26+
}
27+
},
28+
"SplitType": "None"
29+
},
30+
"TransformJobArn": "arn:aws:sagemaker:us-west-2:123456789012:transform-job/xgboost-transform-job",
31+
"TransformJobName": "xgboost-transform-job",
32+
"TransformJobStatus": "Stopping",
33+
"TransformOutput": {
34+
"Accept": null,
35+
"AssembleWith": "None",
36+
"KmsKeyId": "",
37+
"S3OutputPath": "s3://source-data-bucket-592697580195-us-west-2/sagemaker/batch/output"
38+
},
39+
"TransformResources": {
40+
"InstanceCount": 1,
41+
"InstanceType": "ml.m5.large",
42+
"VolumeKmsKeyId": null
43+
},
44+
"TransformStartTime": null
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"AutoMLJobArn": null,
3+
"BatchStrategy": null,
4+
"CreationTime": "2021-10-05T05:47:02.644Z",
5+
"DataProcessing": {
6+
"InputFilter": "$",
7+
"JoinSource": "None",
8+
"OutputFilter": "$"
9+
},
10+
"Environment": null,
11+
"ExperimentConfig": null,
12+
"FailureReason": null,
13+
"LabelingJobArn": null,
14+
"MaxConcurrentTransforms": null,
15+
"MaxPayloadInMB": null,
16+
"ModelClientConfig": null,
17+
"ModelName": "xgboost-churn-config-model",
18+
"TransformEndTime": "2021-10-05T05:51:28.736Z",
19+
"TransformInput": {
20+
"CompressionType": "None",
21+
"ContentType": "text/csv",
22+
"DataSource": {
23+
"S3DataSource": {
24+
"S3DataType": "S3Prefix",
25+
"S3Uri": "s3://source-data-bucket-592697580195-us-west-2/sagemaker/batch/input-data"
26+
}
27+
},
28+
"SplitType": "None"
29+
},
30+
"TransformJobArn": "arn:aws:sagemaker:us-west-2:123456789012:transform-job/xgboost-transform-job",
31+
"TransformJobName": "xgboost-transform-job",
32+
"TransformJobStatus": "Completed",
33+
"TransformOutput": {
34+
"Accept": null,
35+
"AssembleWith": "None",
36+
"KmsKeyId": "",
37+
"S3OutputPath": "s3://source-data-bucket-592697580195-us-west-2/sagemaker/batch/output"
38+
},
39+
"TransformResources": {
40+
"InstanceCount": 1,
41+
"InstanceType": "ml.m5.large",
42+
"VolumeKmsKeyId": null
43+
},
44+
"TransformStartTime": "2021-10-05T05:50:15.223Z"
45+
}
Lines changed: 159 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,162 @@
11
tests:
22
- name: "Transform job create tests"
3-
description: "Part of transform job CRD tests."
3+
description: "Testing the Create operation"
44
scenarios:
5-
- name: "Create=InvalidInput"
6-
description: "Given one of the parameters is invalid, ko.Status shows a terminal condition"
7-
given:
8-
desired_state: "transform_job/v1alpha1/transformj_invalid_before_create.yaml"
9-
svc_api:
10-
- operation: CreateTransformJobWithContext
11-
error:
12-
code: InvalidParameterValue
13-
message: "The transform job name must not include a special character."
14-
invoke: Create
15-
expect:
16-
latest_state: "transform_job/v1alpha1/transformj_invalid_create_attempted.yaml"
17-
error: resource is in terminal condition
5+
- name: "Create=InvalidInput"
6+
description: "Given one of the parameters is invalid, ko.Status shows a terminal condition"
7+
given:
8+
desired_state: "v1alpha1/create/desired/invalid_before_create.yaml"
9+
svc_api:
10+
- operation: CreateTransformJobWithContext
11+
error:
12+
code: InvalidParameterValue
13+
message: "The transform job name must not include a special character."
14+
invoke: Create
15+
expect:
16+
latest_state: "v1alpha1/create/observed/invalid_create_attempted.yaml"
17+
error: resource is in terminal condition
18+
- name: "Create=Valid"
19+
description: "Create a new transform job successfully (ARN in status)."
20+
given:
21+
desired_state: "v1alpha1/create/desired/success_before_create.yaml"
22+
svc_api:
23+
- operation: CreateTransformJobWithContext
24+
output_fixture: "sdkapi/create/success_create.json"
25+
invoke: Create
26+
expect:
27+
latest_state: "v1alpha1/create/observed/success_after_create.yaml"
28+
error: nil
29+
- name: "Transform job readOne tests"
30+
description: "Testing the readOne operation"
31+
scenarios:
32+
- name: "ReadOne=MissingRequiredField"
33+
description: "Testing readOne when required field is missing. No API call is made and returns error."
34+
given:
35+
desired_state: "v1alpha1/readone/desired/missing_required_field.yaml"
36+
invoke: ReadOne
37+
expect:
38+
error: "resource not found"
39+
- name: "ReadOne=NotFound"
40+
description: "Testing readOne when Describe fails to find the resource on SageMaker"
41+
given:
42+
desired_state: "v1alpha1/readone/desired/after_create_inprogress.yaml"
43+
svc_api:
44+
- operation: DescribeTransformJobWithContext
45+
error:
46+
code: ValidationException
47+
message: "Could not find requested job with name"
48+
invoke: ReadOne
49+
expect:
50+
error: "resource not found"
51+
- name: "ReadOne=SuccessClearsConditions"
52+
description: Testing a successful reconciliation clears conditions if terminal/recoverable condition were already set to true
53+
given:
54+
desired_state: "v1alpha1/readone/desired/error_conditions_true.yaml"
55+
svc_api:
56+
- operation: DescribeTransformJobWithContext
57+
output_fixture: "sdkapi/describe/inprogress_describe.json"
58+
invoke: ReadOne
59+
expect:
60+
latest_state: "v1alpha1/readone/observed/conditions_clear_on_success.yaml"
61+
- name: "ReadOne=Fail"
62+
description: "This test checks if the condition is updated if describe fails and readOne returns error"
63+
given:
64+
desired_state: "v1alpha1/readone/desired/after_create_inprogress.yaml"
65+
svc_api:
66+
- operation: DescribeTransformJobWithContext
67+
error:
68+
code: ServiceUnavailable
69+
message: "Server is down"
70+
invoke: ReadOne
71+
expect:
72+
latest_state: "v1alpha1/readone/observed/error_on_describe.yaml"
73+
error: "ServiceUnavailable: Server is down\n\tstatus code: 0, request id: "
74+
- name: "ReadOne=AfterCreateInProgress"
75+
description: "Testing readOne right after create, the status should have ARN."
76+
given:
77+
desired_state: "v1alpha1/readone/desired/after_create_inprogress.yaml"
78+
svc_api:
79+
- operation: DescribeTransformJobWithContext
80+
output_fixture: "sdkapi/describe/inprogress_describe.json"
81+
invoke: ReadOne
82+
expect:
83+
latest_state: "v1alpha1/readone/observed/created.yaml"
84+
- name: "ReadOne=AfterCompletion"
85+
description: "Testing readOne after transform job completes, the status should have ARN."
86+
given:
87+
desired_state: "v1alpha1/readone/desired/after_create_inprogress.yaml"
88+
svc_api:
89+
- operation: DescribeTransformJobWithContext
90+
output_fixture: "sdkapi/describe/success_completed_describe.json"
91+
invoke: ReadOne
92+
expect:
93+
latest_state: "v1alpha1/readone/observed/completed.yaml"
94+
- name: "ReadOne=Stopping"
95+
description: "Testing readOne right after delete, the status should be in Stopping"
96+
given:
97+
desired_state: "v1alpha1/readone/desired/stopping.yaml"
98+
svc_api:
99+
- operation: DescribeTransformJobWithContext
100+
output_fixture: "sdkapi/describe/stopping_describe.json"
101+
invoke: ReadOne
102+
expect:
103+
latest_state: "v1alpha1/readone/observed/stopping.yaml"
104+
- name: "Transform job update tests"
105+
description: "Testing the Update operation"
106+
scenarios:
107+
- name: "Update=NotSupported"
108+
description: "This test checks if the controller throws error for update"
109+
given:
110+
desired_state: "v1alpha1/update/desired/updated_base.yaml"
111+
latest_state: "v1alpha1/create/observed/success_after_create.yaml"
112+
invoke: Update
113+
expect:
114+
latest_state: "v1alpha1/update/observed/error_on_update.yaml"
115+
error: "not implemented"
116+
- name: "Transform job delete tests"
117+
description: "Testing the delete operation"
118+
scenarios:
119+
- name: "Delete=Fail"
120+
description: "This test checks if the condition is updated if delete fails and returns error"
121+
given:
122+
desired_state: "v1alpha1/readone/desired/after_create_inprogress.yaml"
123+
svc_api:
124+
- operation: StopTransformJobWithContext
125+
error:
126+
code: ServiceUnavailable
127+
message: "Server is down"
128+
invoke: Delete
129+
expect:
130+
latest_state: "v1alpha1/delete/observed/error_on_delete.yaml"
131+
error: "ServiceUnavailable: Server is down\n\tstatus code: 0, request id: "
132+
- name: "Delete=Successful"
133+
description: "This test checks if the Transform job is deleted successfully"
134+
given:
135+
desired_state: "v1alpha1/readone/desired/after_create_inprogress.yaml"
136+
svc_api:
137+
- operation: StopTransformJobWithContext
138+
- operation: DescribeTransformJobWithContext
139+
error:
140+
code: ValidationException
141+
message: "Could not find requested job with name"
142+
invoke: Delete
143+
expect:
144+
error: nil
145+
- name: "Delete=MarkUnmanaged"
146+
description: "This test checks if the Transform job returns nil to mark the resource Unmanaged if job not in InProgress/Stopping state"
147+
given:
148+
desired_state: "v1alpha1/readone/observed/completed.yaml"
149+
svc_api:
150+
- operation: StopTransformJobWithContext
151+
invoke: Delete
152+
expect:
153+
error: nil
154+
- name: "Delete=RequeueDeleting"
155+
description: "This test checks if the Transform job requeues if job is in Stopping state"
156+
given:
157+
desired_state: "v1alpha1/readone/observed/stopping.yaml"
158+
svc_api:
159+
- operation: StopTransformJobWithContext
160+
invoke: Delete
161+
expect:
162+
error: TransformJob is Stopping.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apiVersion: sagemaker.services.k8s.aws/v1alpha1
2+
kind: TransformJob
3+
metadata:
4+
name: unit-testing-transform-job
5+
spec:
6+
transformJobName: xgboost-transform-job
7+
modelName: xgboost-churn-config-model
8+
transformInput:
9+
contentType: text/csv
10+
dataSource:
11+
s3DataSource:
12+
s3DataType: S3Prefix
13+
s3URI: s3://source-data-bucket-592697580195-us-west-2/sagemaker/batch/input-data
14+
transformOutput:
15+
s3OutputPath: s3://source-data-bucket-592697580195-us-west-2/sagemaker/batch/output
16+
transformResources:
17+
instanceCount: 1
18+
instanceType: ml.m5.large
19+
tags:
20+
- key: algorithm
21+
value: xgboost
22+
- key: environment
23+
value: testing
24+
- key: customer
25+
value: test-user

0 commit comments

Comments
 (0)