|
| 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 feature_group |
| 15 | + |
| 16 | +import ( |
| 17 | + "errors" |
| 18 | + "fmt" |
| 19 | + mocksvcsdkapi "github.com/aws-controllers-k8s/sagemaker-controller/test/mocks/aws-sdk-go/sagemaker" |
| 20 | + "github.com/aws-controllers-k8s/sagemaker-controller/pkg/testutil" |
| 21 | + acktypes "github.com/aws-controllers-k8s/runtime/pkg/types" |
| 22 | + svcsdk "github.com/aws/aws-sdk-go/service/sagemaker" |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 25 | + "path/filepath" |
| 26 | + "testing" |
| 27 | + ctrlrtzap "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 28 | + ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics" |
| 29 | + "go.uber.org/zap/zapcore" |
| 30 | +) |
| 31 | + |
| 32 | +// provideResourceManagerWithMockSDKAPI accepts MockSageMakerAPI and returns pointer to resourceManager |
| 33 | +// the returned resourceManager is configured to use mockapi api. |
| 34 | +func provideResourceManagerWithMockSDKAPI(mockSageMakerAPI *mocksvcsdkapi.SageMakerAPI) *resourceManager { |
| 35 | + zapOptions := ctrlrtzap.Options{ |
| 36 | + Development: true, |
| 37 | + Level: zapcore.InfoLevel, |
| 38 | + } |
| 39 | + fakeLogger := ctrlrtzap.New(ctrlrtzap.UseFlagOptions(&zapOptions)) |
| 40 | + return &resourceManager{ |
| 41 | + rr: nil, |
| 42 | + awsAccountID: "", |
| 43 | + awsRegion: "", |
| 44 | + sess: nil, |
| 45 | + sdkapi: mockSageMakerAPI, |
| 46 | + log: fakeLogger, |
| 47 | + metrics: ackmetrics.NewMetrics("sagemaker"), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// TestDeclarativeTestSuite runs the test suite for feature group |
| 52 | +func TestDeclarativeTestSuite(t *testing.T) { |
| 53 | + defer func() { |
| 54 | + if r := recover(); r != nil { |
| 55 | + fmt.Println(testutil.RecoverPanicString, r) |
| 56 | + t.Fail() |
| 57 | + } |
| 58 | + }() |
| 59 | + var ts = testutil.TestSuite{} |
| 60 | + testutil.LoadFromFixture(filepath.Join("testdata", "test_suite.yaml"), &ts) |
| 61 | + var delegate = testRunnerDelegate{t: t} |
| 62 | + var runner = testutil.TestSuiteRunner{TestSuite: &ts, Delegate: &delegate} |
| 63 | + runner.RunTests() |
| 64 | +} |
| 65 | + |
| 66 | +// testRunnerDelegate implements testutil.TestRunnerDelegate |
| 67 | +type testRunnerDelegate struct { |
| 68 | + t *testing.T |
| 69 | +} |
| 70 | + |
| 71 | +func (d *testRunnerDelegate) ResourceDescriptor() acktypes.AWSResourceDescriptor { |
| 72 | + return &resourceDescriptor{} |
| 73 | +} |
| 74 | + |
| 75 | +func (d *testRunnerDelegate) ResourceManager(mocksdkapi *mocksvcsdkapi.SageMakerAPI) acktypes.AWSResourceManager { |
| 76 | + return provideResourceManagerWithMockSDKAPI(mocksdkapi) |
| 77 | +} |
| 78 | + |
| 79 | +func (d *testRunnerDelegate) GoTestRunner() *testing.T { |
| 80 | + return d.t |
| 81 | +} |
| 82 | + |
| 83 | +func (d *testRunnerDelegate) EmptyServiceAPIOutput(apiName string) (interface{}, error) { |
| 84 | + if apiName == "" { |
| 85 | + return nil, errors.New("no API name specified") |
| 86 | + } |
| 87 | + //TODO: use reflection, template to auto generate this block/method. |
| 88 | + switch apiName { |
| 89 | + case "CreateFeatureGroupWithContext": |
| 90 | + var output svcsdk.CreateFeatureGroupOutput |
| 91 | + return &output, nil |
| 92 | + case "DeleteFeatureGroupWithContext": |
| 93 | + var output svcsdk.DeleteFeatureGroupOutput |
| 94 | + return &output, nil |
| 95 | + case "DescribeFeatureGroupWithContext": |
| 96 | + var output svcsdk.DescribeFeatureGroupOutput |
| 97 | + return &output, nil |
| 98 | + } |
| 99 | + return nil, errors.New(fmt.Sprintf("no matching API name found for: %s", apiName)) |
| 100 | +} |
| 101 | + |
| 102 | +func (d *testRunnerDelegate) Equal(a acktypes.AWSResource, b acktypes.AWSResource) bool { |
| 103 | + ac := a.(*resource) |
| 104 | + bc := b.(*resource) |
| 105 | + opts := []cmp.Option{cmpopts.EquateEmpty()} |
| 106 | + |
| 107 | + if cmp.Equal(ac.ko.Status, bc.ko.Status, opts...) { |
| 108 | + return true |
| 109 | + } else { |
| 110 | + fmt.Printf("Difference (-expected +actual):\n\n") |
| 111 | + fmt.Println(cmp.Diff(ac.ko.Status, bc.ko.Status, opts...)) |
| 112 | + return false |
| 113 | + } |
| 114 | +} |
0 commit comments