|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package pipeline |
| 15 | + |
| 16 | +import ( |
| 17 | + "errors" |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "path/filepath" |
| 21 | + "testing" |
| 22 | + |
| 23 | + ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" |
| 24 | + ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics" |
| 25 | + acktypes "github.com/aws-controllers-k8s/runtime/pkg/types" |
| 26 | + svcapitypes "github.com/aws-controllers-k8s/sagemaker-controller/apis/v1alpha1" |
| 27 | + "github.com/aws-controllers-k8s/sagemaker-controller/pkg/testutil" |
| 28 | + mocksvcsdkapi "github.com/aws-controllers-k8s/sagemaker-controller/test/mocks/aws-sdk-go/sagemaker" |
| 29 | + svcsdk "github.com/aws/aws-sdk-go/service/sagemaker" |
| 30 | + "github.com/google/go-cmp/cmp" |
| 31 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 32 | + "go.uber.org/zap/zapcore" |
| 33 | + ctrlrtzap "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 34 | +) |
| 35 | + |
| 36 | +// provideResourceManagerWithMockSDKAPI accepts MockSageMakerAPI and returns pointer to resourceManager |
| 37 | +// the returned resourceManager is configured to use mockapi api. |
| 38 | +func provideResourceManagerWithMockSDKAPI(mockSageMakerAPI *mocksvcsdkapi.SageMakerAPI) *resourceManager { |
| 39 | + zapOptions := ctrlrtzap.Options{ |
| 40 | + Development: true, |
| 41 | + Level: zapcore.InfoLevel, |
| 42 | + } |
| 43 | + fakeLogger := ctrlrtzap.New(ctrlrtzap.UseFlagOptions(&zapOptions)) |
| 44 | + return &resourceManager{ |
| 45 | + rr: nil, |
| 46 | + awsAccountID: "", |
| 47 | + awsRegion: "", |
| 48 | + sess: nil, |
| 49 | + sdkapi: mockSageMakerAPI, |
| 50 | + log: fakeLogger, |
| 51 | + metrics: ackmetrics.NewMetrics("sagemaker"), |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// TestPipelineTestSuite runs the test suite for pipeline |
| 56 | +func TestPipelineTestSuite(t *testing.T) { |
| 57 | + var ts = testutil.TestSuite{} |
| 58 | + testutil.LoadFromFixture(filepath.Join("testdata", "test_suite.yaml"), &ts) |
| 59 | + var delegate = testRunnerDelegate{t: t} |
| 60 | + var runner = testutil.TestSuiteRunner{TestSuite: &ts, Delegate: &delegate} |
| 61 | + runner.RunTests() |
| 62 | +} |
| 63 | + |
| 64 | +// testRunnerDelegate implements testutil.TestRunnerDelegate |
| 65 | +type testRunnerDelegate struct { |
| 66 | + t *testing.T |
| 67 | +} |
| 68 | + |
| 69 | +func (d *testRunnerDelegate) ResourceDescriptor() acktypes.AWSResourceDescriptor { |
| 70 | + return &resourceDescriptor{} |
| 71 | +} |
| 72 | + |
| 73 | +func (d *testRunnerDelegate) ResourceManager(mocksdkapi *mocksvcsdkapi.SageMakerAPI) acktypes.AWSResourceManager { |
| 74 | + return provideResourceManagerWithMockSDKAPI(mocksdkapi) |
| 75 | +} |
| 76 | + |
| 77 | +func (d *testRunnerDelegate) GoTestRunner() *testing.T { |
| 78 | + return d.t |
| 79 | +} |
| 80 | + |
| 81 | +func (d *testRunnerDelegate) EmptyServiceAPIOutput(apiName string) (interface{}, error) { |
| 82 | + if apiName == "" { |
| 83 | + return nil, errors.New("no API name specified") |
| 84 | + } |
| 85 | + //TODO: use reflection, template to auto generate this block/method. |
| 86 | + switch apiName { |
| 87 | + case "CreatePipelineWithContext": |
| 88 | + var output svcsdk.CreatePipelineOutput |
| 89 | + return &output, nil |
| 90 | + case "DescribePipelineWithContext": |
| 91 | + var output svcsdk.DescribePipelineOutput |
| 92 | + return &output, nil |
| 93 | + case "DeletePipelineWithContext": |
| 94 | + var output svcsdk.DeletePipelineOutput |
| 95 | + return &output, nil |
| 96 | + case "UpdatePipelineWithContext": |
| 97 | + var output svcsdk.UpdatePipelineOutput |
| 98 | + return &output, nil |
| 99 | + } |
| 100 | + return nil, errors.New(fmt.Sprintf("no matching API name found for: %s", apiName)) |
| 101 | +} |
| 102 | + |
| 103 | +func (d *testRunnerDelegate) Equal(a acktypes.AWSResource, b acktypes.AWSResource) bool { |
| 104 | + ac := a.(*resource) |
| 105 | + bc := b.(*resource) |
| 106 | + // Ignore LastTransitionTime since it gets updated each run. |
| 107 | + opts := []cmp.Option{cmpopts.EquateEmpty(), cmpopts.IgnoreFields(ackv1alpha1.Condition{}, "LastTransitionTime"), |
| 108 | + cmpopts.IgnoreFields(svcapitypes.PipelineStatus{}, "CreationTime"), |
| 109 | + cmpopts.IgnoreFields(svcapitypes.PipelineStatus{}, "LastModifiedTime")} |
| 110 | + |
| 111 | + var specMatch = false |
| 112 | + if cmp.Equal(ac.ko.Spec, bc.ko.Spec, opts...) { |
| 113 | + specMatch = true |
| 114 | + } else { |
| 115 | + fmt.Printf("Difference ko.Spec (-expected +actual):\n\n") |
| 116 | + fmt.Println(cmp.Diff(ac.ko.Spec, bc.ko.Spec, opts...)) |
| 117 | + specMatch = false |
| 118 | + } |
| 119 | + |
| 120 | + var statusMatch = false |
| 121 | + if cmp.Equal(ac.ko.Status, bc.ko.Status, opts...) { |
| 122 | + statusMatch = true |
| 123 | + } else { |
| 124 | + fmt.Printf("Difference ko.Status (-expected +actual):\n\n") |
| 125 | + fmt.Println(cmp.Diff(ac.ko.Status, bc.ko.Status, opts...)) |
| 126 | + statusMatch = false |
| 127 | + } |
| 128 | + return statusMatch && specMatch |
| 129 | +} |
0 commit comments