Skip to content

Commit ed8bce0

Browse files
authored
model resource unit tests (#101)
Description of changes: - Unit tests for model - sdk.go is at 72.5%. overall resource at 58.6% - use gocmp to compare spec in addition to status. Will create a separate PR to add this to other resources Testing: `make test` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 56e859b commit ed8bce0

20 files changed

+517
-16
lines changed

pkg/resource/model/manager_test_suite_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,25 @@ func (d *testRunnerDelegate) Equal(a acktypes.AWSResource, b acktypes.AWSResourc
102102
// Ignore LastTransitionTime since it gets updated each run.
103103
opts := []cmp.Option{cmpopts.EquateEmpty(), cmpopts.IgnoreFields(ackv1alpha1.Condition{}, "LastTransitionTime")}
104104

105+
var specMatch = false
106+
if cmp.Equal(ac.ko.Spec, bc.ko.Spec, opts...) {
107+
specMatch = true
108+
} else {
109+
fmt.Printf("Difference ko.Spec (-expected +actual):\n\n")
110+
fmt.Println(cmp.Diff(ac.ko.Spec, bc.ko.Spec, opts...))
111+
specMatch = false
112+
}
113+
114+
var statusMatch = false
105115
if cmp.Equal(ac.ko.Status, bc.ko.Status, opts...) {
106-
return true
116+
statusMatch = true
107117
} else {
108-
fmt.Printf("Difference (-expected +actual):\n\n")
118+
fmt.Printf("Difference ko.Status (-expected +actual):\n\n")
109119
fmt.Println(cmp.Diff(ac.ko.Status, bc.ko.Status, opts...))
110-
return false
120+
statusMatch = false
111121
}
122+
123+
return statusMatch && specMatch
112124
}
113125

114126
// 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+
"ModelArn": "arn:aws:sagemaker:us-west-2:123456789012:model/xgboost-model"
3+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"Containers": [
3+
{
4+
"ContainerHostname": "xgboost",
5+
"Environment": {
6+
"my_var": "my_value",
7+
"my_var2": "my_value2"
8+
},
9+
"Image": "246618743249.dkr.ecr.us-west-2.amazonaws.com/sagemaker-xgboost:1.0-1-cpu-py3",
10+
"ImageConfig": {
11+
"RepositoryAccessMode": "Platform",
12+
"RepositoryAuthConfig": {
13+
"RepositoryCredentialsProviderArn": null
14+
}
15+
},
16+
"Mode": "SingleModel",
17+
"ModelDataUrl": "s3://source-data-bucket-592697580195-us-west-2/sagemaker/model/xgboost-mnist-model.tar.gz",
18+
"ModelPackageName": null,
19+
"MultiModelConfig": null
20+
}
21+
],
22+
"CreationTime": "2021-09-06T17:58:24.933Z",
23+
"EnableNetworkIsolation": false,
24+
"ExecutionRoleArn": "arn:aws:iam::123456789012:role/ack-sagemaker-execution-role",
25+
"InferenceExecutionConfig": null,
26+
"ModelArn": "arn:aws:sagemaker:us-west-2:123456789012:model/xgboost-model",
27+
"ModelName": "xgboost-model",
28+
"PrimaryContainer": null,
29+
"VpcConfig": null
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"Containers": null,
3+
"CreationTime": "2021-09-06T17:50:53.793Z",
4+
"EnableNetworkIsolation": false,
5+
"ExecutionRoleArn": "arn:aws:iam::123456789012:role/ack-sagemaker-execution-role",
6+
"InferenceExecutionConfig": null,
7+
"ModelArn": "arn:aws:sagemaker:us-west-2:123456789012:model/xgboost-model",
8+
"ModelName": "xgboost-model",
9+
"PrimaryContainer": {
10+
"ContainerHostname": "xgboost",
11+
"Environment": {
12+
"my_var": "my_value",
13+
"my_var2": "my_value2"
14+
},
15+
"Image": "433757028032.dkr.ecr.us-west-2.amazonaws.com/xgboost:latest",
16+
"ImageConfig": null,
17+
"Mode": "MultiModel",
18+
"ModelDataUrl": "s3://source-data-bucket-592697580195-us-west-2/sagemaker/model/xgboost-mnist-model.tar.gz",
19+
"ModelPackageName": null,
20+
"MultiModelConfig": {
21+
"ModelCacheSetting" : "Disabled"
22+
}
23+
},
24+
"VpcConfig": null
25+
}

pkg/resource/model/testdata/test_suite.yaml

Lines changed: 127 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,130 @@ tests:
22
- name: "Model create tests"
33
description: "Part of model CRD tests."
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: "model/v1alpha1/m_invalid_before_create.yaml"
9-
svc_api:
10-
- operation: CreateModelWithContext
11-
error:
12-
code: InvalidParameterValue
13-
message: "The model name must not include a special character."
14-
invoke: Create
15-
expect:
16-
latest_state: "model/v1alpha1/m_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: CreateModelWithContext
11+
error:
12+
code: InvalidParameterValue
13+
message: "The model 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 model successfully (ARN in status)."
20+
given:
21+
desired_state: "v1alpha1/create/desired/success_before_create.yaml"
22+
svc_api:
23+
- operation: CreateModelWithContext
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: "Create=ValidVariation2"
30+
description: "Create a new model successfully. This test is similar to Create=Valid with different set of inputs"
31+
given:
32+
desired_state: "v1alpha1/create/desired/before_create_primary_container.yaml"
33+
svc_api:
34+
- operation: CreateModelWithContext
35+
output_fixture: "sdkapi/create/success_create.json"
36+
invoke: Create
37+
expect:
38+
latest_state: "v1alpha1/create/observed/success_created_primary_container.yaml"
39+
error: nil
40+
- name: "Model readOne tests"
41+
description: "Testing the readOne operation"
42+
scenarios:
43+
- name: "ReadOne=MissingRequiredField"
44+
description: "Testing readOne when required field is missing. No API call is made and returns error."
45+
given:
46+
desired_state: "v1alpha1/readone/desired/missing_required_field.yaml"
47+
invoke: ReadOne
48+
expect:
49+
error: "resource not found"
50+
- name: "ReadOne=NotFound"
51+
description: "Testing readOne when Describe fails to find the resource on SageMaker"
52+
given:
53+
desired_state: "v1alpha1/readone/desired/after_create.yaml"
54+
svc_api:
55+
- operation: DescribeModelWithContext
56+
error:
57+
code: ValidationException
58+
message: "Could not find model xgboost-model"
59+
invoke: ReadOne
60+
expect:
61+
error: "resource not found"
62+
- name: "ReadOne=Fail"
63+
description: "This test checks if the condition is updated if describe fails and readOne returns error"
64+
given:
65+
desired_state: "v1alpha1/readone/desired/after_create.yaml"
66+
svc_api:
67+
- operation: DescribeModelWithContext
68+
error:
69+
code: ServiceUnavailable
70+
message: "Server is down"
71+
invoke: ReadOne
72+
expect:
73+
latest_state: "v1alpha1/readone/observed/error_on_describe.yaml"
74+
error: "ServiceUnavailable: Server is down\n\tstatus code: 0, request id: "
75+
- name: "ReadOne=AfterCreate"
76+
description: "Testing readOne right after create, the status should have ARN."
77+
given:
78+
desired_state: "v1alpha1/readone/desired/after_create.yaml"
79+
svc_api:
80+
- operation: DescribeModelWithContext
81+
output_fixture: "sdkapi/describe/success_describe.json"
82+
invoke: ReadOne
83+
expect:
84+
latest_state: "v1alpha1/readone/observed/created.yaml"
85+
- name: "ReadOne=AfterCreateVariation2"
86+
description: "Testing readOne right after create, the status should have ARN."
87+
given:
88+
desired_state: "v1alpha1/create/observed/success_created_primary_container.yaml"
89+
svc_api:
90+
- operation: DescribeModelWithContext
91+
output_fixture: "sdkapi/describe/success_primary_container.json"
92+
invoke: ReadOne
93+
expect:
94+
latest_state: "v1alpha1/readone/observed/created_primary_container.yaml"
95+
- name: "Model update tests"
96+
description: "Testing the Update operation"
97+
scenarios:
98+
- name: "Update=NotSupported"
99+
description: "This test checks if the contrller throws error for update"
100+
given:
101+
desired_state: "v1alpha1/update/desired/updated_base.yaml"
102+
latest_state: "v1alpha1/update/desired/latest_post_create.yaml"
103+
invoke: Update
104+
expect:
105+
latest_state: "v1alpha1/update/observed/error_on_update.yaml"
106+
error: "not implemented"
107+
- name: "Model delete tests"
108+
description: "Testing the delete operation"
109+
scenarios:
110+
- name: "Delete=Fail"
111+
description: "This test checks if the condition is updated if delete fails and returns error"
112+
given:
113+
desired_state: "v1alpha1/readone/desired/after_create.yaml"
114+
svc_api:
115+
- operation: DeleteModelWithContext
116+
error:
117+
code: ServiceUnavailable
118+
message: "Server is down"
119+
invoke: Delete
120+
expect:
121+
latest_state: "v1alpha1/delete/observed/error_on_delete.yaml"
122+
error: "ServiceUnavailable: Server is down\n\tstatus code: 0, request id: "
123+
- name: "Delete=Successful"
124+
description: "This test checks if the Model is deleted successfully"
125+
given:
126+
desired_state: "v1alpha1/readone/desired/after_create.yaml"
127+
svc_api:
128+
- operation: DeleteModelWithContext
129+
invoke: Delete
130+
expect:
131+
error: nil
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: Model
3+
metadata:
4+
name: xgboost-model
5+
spec:
6+
executionRoleARN: arn:aws:iam::123456789012:role/ack-sagemaker-execution-role
7+
modelName: xgboost-model
8+
primaryContainer:
9+
containerHostname: xgboost
10+
environment:
11+
my_var: my_value
12+
my_var2: my_value2
13+
image: 433757028032.dkr.ecr.us-west-2.amazonaws.com/xgboost:latest
14+
modelDataURL: s3://source-data-bucket-592697580195-us-west-2/sagemaker/model/xgboost-mnist-model.tar.gz
15+
mode: MultiModel
16+
multiModelConfig:
17+
modelCacheSetting: Disabled
18+
enableNetworkIsolation: false
19+
tags:
20+
- key: algorithm
21+
value: xgboost
22+
- key: environment
23+
value: testing
24+
- key: customer
25+
value: test-user
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: sagemaker.services.k8s.aws/v1alpha1
2+
kind: Model
3+
metadata:
4+
name: xgboost-model
5+
spec:
6+
containers:
7+
- containerHostname: xgboost
8+
environment:
9+
my_var: my_value
10+
my_var2: my_value2
11+
image: 246618743249.dkr.ecr.us-west-2.amazonaws.com/sagemaker-xgboost:1.0-1-cpu-py3
12+
imageConfig:
13+
repositoryAccessMode: Platform
14+
modelDataURL: s3://source-data-bucket-592697580195-us-west-2/sagemaker/model/xgboost-mnist-model.tar.gz
15+
executionRoleARN: arn:aws:iam::123456789012:role/ack-sagemaker-execution-role
16+
modelName: xgboost-model
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: sagemaker.services.k8s.aws/v1alpha1
2+
kind: Model
3+
metadata:
4+
creationTimestamp: null
5+
name: xgboost-model
6+
spec:
7+
containers:
8+
- containerHostname: xgboost
9+
environment:
10+
my_var: my_value
11+
my_var2: my_value2
12+
image: 246618743249.dkr.ecr.us-west-2.amazonaws.com/sagemaker-xgboost:1.0-1-cpu-py3
13+
imageConfig:
14+
repositoryAccessMode: Platform
15+
modelDataURL: s3://source-data-bucket-592697580195-us-west-2/sagemaker/model/xgboost-mnist-model.tar.gz
16+
executionRoleARN: arn:aws:iam::123456789012:role/ack-sagemaker-execution-role
17+
modelName: xgboost-model
18+
status:
19+
ackResourceMetadata:
20+
arn: arn:aws:sagemaker:us-west-2:123456789012:model/xgboost-model
21+
ownerAccountID: ""
22+
conditions: []

0 commit comments

Comments
 (0)