Skip to content

Commit

Permalink
fixup! fix: linodemachine: add finalizer to credentials reference
Browse files Browse the repository at this point in the history
  • Loading branch information
cbang-akamai committed May 1, 2024
1 parent 89ec65c commit 36dd046
Showing 1 changed file with 187 additions and 0 deletions.
187 changes: 187 additions & 0 deletions cloud/scope/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -393,3 +394,189 @@ func TestMachineScopeGetBootstrapData(t *testing.T) {
}),
)
}

func TestMachineAddCredentialsRefFinalizer(t *testing.T) {
t.Parallel()
type fields struct {
LinodeMachine *infrav1alpha1.LinodeMachine
}
tests := []struct {
name string
fields fields
expects func(mock *mock.MockK8sClient)
}{
{
"Success - finalizer should be added to the Linode Machine credential Secret",
fields{
LinodeMachine: &infrav1alpha1.LinodeMachine{
Spec: infrav1alpha1.LinodeMachineSpec{
CredentialsRef: &corev1.SecretReference{
Name: "example",
Namespace: "test",
},
},
},
},
func(mock *mock.MockK8sClient) {
mock.EXPECT().Scheme().DoAndReturn(func() *runtime.Scheme {
s := runtime.NewScheme()
infrav1alpha1.AddToScheme(s)
return s
})
mock.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, key types.NamespacedName, obj *corev1.Secret, opts ...client.GetOption) error {
cred := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "example",
Namespace: "test",
},
Data: map[string][]byte{
"apiToken": []byte("example"),
},
}
*obj = cred

return nil
}).Times(2)
mock.EXPECT().Update(gomock.Any(), gomock.Any()).Return(nil)
},
},
{
name: "No-op - no Linode Machine credential Secret",
fields: fields{
LinodeMachine: &infrav1alpha1.LinodeMachine{},
},
expects: func(mock *mock.MockK8sClient) {
mock.EXPECT().Scheme().DoAndReturn(func() *runtime.Scheme {
s := runtime.NewScheme()
infrav1alpha1.AddToScheme(s)
return s
})
},
},
}
for _, tt := range tests {
testcase := tt
t.Run(testcase.name, func(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockK8sClient := mock.NewMockK8sClient(ctrl)

testcase.expects(mockK8sClient)

mScope, err := NewMachineScope(
context.Background(),
"test-key",
MachineScopeParams{
Client: mockK8sClient,
Cluster: &clusterv1.Cluster{},
Machine: &clusterv1.Machine{},
LinodeCluster: &infrav1alpha1.LinodeCluster{},
LinodeMachine: testcase.fields.LinodeMachine,
},
)
if err != nil {
t.Errorf("NewMachineScope() error = %v", err)
}

if err := mScope.AddCredentialsRefFinalizer(context.Background()); err != nil {
t.Errorf("MachineScope.AddCredentialsRefFinalizer() error = %v", err)
}
})
}
}

func TestMachineRemoveCredentialsRefFinalizer(t *testing.T) {
t.Parallel()
type fields struct {
LinodeMachine *infrav1alpha1.LinodeMachine
}
tests := []struct {
name string
fields fields
expects func(mock *mock.MockK8sClient)
}{
{
"Success - finalizer should be added to the Linode Machine credential Secret",
fields{
LinodeMachine: &infrav1alpha1.LinodeMachine{
Spec: infrav1alpha1.LinodeMachineSpec{
CredentialsRef: &corev1.SecretReference{
Name: "example",
Namespace: "test",
},
},
},
},
func(mock *mock.MockK8sClient) {
mock.EXPECT().Scheme().DoAndReturn(func() *runtime.Scheme {
s := runtime.NewScheme()
infrav1alpha1.AddToScheme(s)
return s
})
mock.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, key types.NamespacedName, obj *corev1.Secret, opts ...client.GetOption) error {
cred := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "example",
Namespace: "test",
},
Data: map[string][]byte{
"apiToken": []byte("example"),
},
}
*obj = cred

return nil
}).Times(2)
mock.EXPECT().Update(gomock.Any(), gomock.Any()).Return(nil)
},
},
{
name: "No-op - no Linode Machine credential Secret",
fields: fields{
LinodeMachine: &infrav1alpha1.LinodeMachine{},
},
expects: func(mock *mock.MockK8sClient) {
mock.EXPECT().Scheme().DoAndReturn(func() *runtime.Scheme {
s := runtime.NewScheme()
infrav1alpha1.AddToScheme(s)
return s
})
},
},
}
for _, tt := range tests {
testcase := tt
t.Run(testcase.name, func(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockK8sClient := mock.NewMockK8sClient(ctrl)

testcase.expects(mockK8sClient)

mScope, err := NewMachineScope(
context.Background(),
"test-key",
MachineScopeParams{
Client: mockK8sClient,
Cluster: &clusterv1.Cluster{},
Machine: &clusterv1.Machine{},
LinodeCluster: &infrav1alpha1.LinodeCluster{},
LinodeMachine: testcase.fields.LinodeMachine,
},
)
if err != nil {
t.Errorf("NewMachineScope() error = %v", err)
}

if err := mScope.RemoveCredentialsRefFinalizer(context.Background()); err != nil {
t.Errorf("MachineScope.RemoveCredentialsRefFinalizer() error = %v", err)
}
})
}
}

0 comments on commit 36dd046

Please sign in to comment.