|
| 1 | +/* |
| 2 | +Copyright 2025 Akamai Technologies, Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package scope |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + "go.uber.org/mock/gomock" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | + |
| 28 | + infrav1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2" |
| 29 | + "github.com/linode/cluster-api-provider-linode/mock" |
| 30 | +) |
| 31 | + |
| 32 | +func TestValidateMachineTemplateScope(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + LinodeMachineTemplate *infrav1alpha2.LinodeMachineTemplate |
| 37 | + expErr string |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "Success - valid LinodeMachineTemplate", |
| 41 | + LinodeMachineTemplate: &infrav1alpha2.LinodeMachineTemplate{ |
| 42 | + ObjectMeta: metav1.ObjectMeta{ |
| 43 | + Name: "test-lmt", |
| 44 | + }, |
| 45 | + }, |
| 46 | + expErr: "", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "Failure - nil LinodeMachineTemplate", |
| 50 | + LinodeMachineTemplate: nil, |
| 51 | + expErr: "LinodeMachineTemplate is required when creating a MachineTemplateScope", |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tt := range tests { |
| 56 | + t.Run(tt.name, func(t *testing.T) { |
| 57 | + t.Parallel() |
| 58 | + |
| 59 | + err := validateMachineTemplateScope(MachineTemplateScopeParams{ |
| 60 | + LinodeMachineTemplate: tt.LinodeMachineTemplate, |
| 61 | + }) |
| 62 | + |
| 63 | + if tt.expErr != "" { |
| 64 | + assert.ErrorContains(t, err, tt.expErr) |
| 65 | + } else { |
| 66 | + assert.NoError(t, err) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestNewMachineTemplateScope(t *testing.T) { |
| 73 | + t.Parallel() |
| 74 | + tests := []struct { |
| 75 | + name string |
| 76 | + LinodeMachineTemplate *infrav1alpha2.LinodeMachineTemplate |
| 77 | + expErr string |
| 78 | + expects func(mock *mock.MockK8sClient) |
| 79 | + }{ |
| 80 | + { |
| 81 | + name: "Success - able to create a new MachineTemplateScope", |
| 82 | + LinodeMachineTemplate: &infrav1alpha2.LinodeMachineTemplate{ |
| 83 | + ObjectMeta: metav1.ObjectMeta{ |
| 84 | + Name: "test-lmt", |
| 85 | + }, |
| 86 | + }, |
| 87 | + expects: func(mock *mock.MockK8sClient) { |
| 88 | + scheme := runtime.NewScheme() |
| 89 | + infrav1alpha2.AddToScheme(scheme) |
| 90 | + mock.EXPECT().Scheme().Return(scheme) |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "Failure - nil LinodeMachineTemplate", |
| 95 | + LinodeMachineTemplate: nil, |
| 96 | + expErr: "LinodeMachineTemplate is required", |
| 97 | + }, |
| 98 | + } |
| 99 | + for _, tt := range tests { |
| 100 | + testcase := tt |
| 101 | + t.Run(testcase.name, func(t *testing.T) { |
| 102 | + t.Parallel() |
| 103 | + |
| 104 | + ctrl := gomock.NewController(t) |
| 105 | + defer ctrl.Finish() |
| 106 | + |
| 107 | + mockK8sClient := mock.NewMockK8sClient(ctrl) |
| 108 | + |
| 109 | + if tt.expects != nil { |
| 110 | + tt.expects(mockK8sClient) |
| 111 | + } |
| 112 | + |
| 113 | + lmtScope, err := NewMachineTemplateScope( |
| 114 | + t.Context(), |
| 115 | + MachineTemplateScopeParams{ |
| 116 | + Client: mockK8sClient, |
| 117 | + LinodeMachineTemplate: testcase.LinodeMachineTemplate, |
| 118 | + }, |
| 119 | + ) |
| 120 | + |
| 121 | + if tt.expErr != "" { |
| 122 | + require.ErrorContains(t, err, tt.expErr) |
| 123 | + } else { |
| 124 | + require.NoError(t, err) |
| 125 | + require.NotNil(t, lmtScope) |
| 126 | + require.Equal(t, lmtScope.LinodeMachineTemplate, testcase.LinodeMachineTemplate) |
| 127 | + } |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
0 commit comments