Skip to content

Commit

Permalink
add unittest for service controller
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulait committed Dec 24, 2024
1 parent b1b9af7 commit dfef941
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions cloud/linode/service_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package linode

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client/mocks"
"github.com/stretchr/testify/assert"
"k8s.io/client-go/util/workqueue"
)

func Test_serviceController_processNextDeletion(t *testing.T) {
type fields struct {
loadbalancers *loadbalancers
queue workqueue.TypedDelayingInterface[any]
Client *mocks.MockClient
}
tests := []struct {
name string
fields fields
Setup func(*fields)
want bool
queueLen int
}{
{
name: "Invalid service type",
fields: fields{
loadbalancers: nil,
},
Setup: func(f *fields) {
f.loadbalancers = &loadbalancers{client: f.Client, zone: "test", loadBalancerType: Options.LoadBalancerType}
f.queue = workqueue.NewTypedDelayingQueueWithConfig(workqueue.TypedDelayingQueueConfig[any]{Name: "testQueue"})
f.queue.Add("test")
},
want: true,
queueLen: 0,
},
{
name: "Valid service type",
fields: fields{
loadbalancers: nil,
},
Setup: func(f *fields) {
f.loadbalancers = &loadbalancers{client: f.Client, zone: "test", loadBalancerType: Options.LoadBalancerType}
f.queue = workqueue.NewTypedDelayingQueueWithConfig(workqueue.TypedDelayingQueueConfig[any]{Name: "testQueue"})
svc := createTestService()
f.queue.Add(svc)
},
want: true,
queueLen: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &serviceController{
loadbalancers: tt.fields.loadbalancers,
queue: tt.fields.queue,
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
tt.fields.Client = client
tt.Setup(&tt.fields)
s.loadbalancers = tt.fields.loadbalancers
s.queue = tt.fields.queue
s.loadbalancers.client = tt.fields.Client
if got := s.processNextDeletion(); got != tt.want {
t.Errorf("serviceController.processNextDeletion() = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.queueLen, tt.fields.queue.Len())
})
}
}

0 comments on commit dfef941

Please sign in to comment.