Skip to content

Commit

Permalink
test: added uts for createResource, getResource and generateMachineMa…
Browse files Browse the repository at this point in the history
…nifest (#163)
  • Loading branch information
smritidahal653 committed Nov 13, 2023
1 parent 6c4fa45 commit e3cc935
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/machine/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ func TestCreateMachine(t *testing.T) {
expectedError error
}{
"Machine creation fails": {
callMocks: func(c *utils.MockClient) {
c.On("Create", mock.IsType(context.Background()), mock.IsType(&v1alpha5.Machine{}), mock.Anything).Return(errors.New("Failed to create machine"))
},
expectedError: errors.New("Failed to create machine"),
},
"Machine creation fails because SKU is not available": {
callMocks: func(c *utils.MockClient) {
c.On("Create", mock.IsType(context.Background()), mock.IsType(&v1alpha5.Machine{}), mock.Anything).Return(nil)
c.On("Get", mock.IsType(context.Background()), mock.Anything, mock.IsType(&v1alpha5.Machine{}), mock.Anything).Return(nil)
Expand Down Expand Up @@ -156,3 +162,14 @@ func TestWaitForPendingMachines(t *testing.T) {
})
}
}

func TestGenerateMachineManifiest(t *testing.T) {
t.Run("Should generate a machine object from the given workspace", func(t *testing.T) {
mockWorkspace := utils.MockWorkspace

machine := GenerateMachineManifest(context.Background(), "0", mockWorkspace)

assert.Check(t, machine != nil, "Machine must not be nil")
assert.Equal(t, machine.Namespace, mockWorkspace.Namespace, "Machine must have same namespace as workspace")
})
}
85 changes: 85 additions & 0 deletions pkg/resources/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
package resources

import (
"context"
"errors"
"testing"
"time"

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"

"github.com/azure/kaito/pkg/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
goassert "gotest.tools/assert"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

Expand Down Expand Up @@ -108,3 +115,81 @@ func TestCheckResourceStatus(t *testing.T) {
assert.Equal(t, "unsupported resource type", err.Error())
})
}

func TestCreateResource(t *testing.T) {
testcases := map[string]struct {
callMocks func(c *utils.MockClient)
expectedResource client.Object
expectedError error
}{
"Resource creation fails with Deployment object": {
callMocks: func(c *utils.MockClient) {
c.On("Create", mock.IsType(context.Background()), mock.IsType(&v1.Deployment{}), mock.Anything).Return(errors.New("Failed to create resource"))
},
expectedResource: &v1.Deployment{},
expectedError: errors.New("Failed to create resource"),
},
"Resource creation succeeds with Statefulset object": {
callMocks: func(c *utils.MockClient) {
c.On("Create", mock.IsType(context.Background()), mock.IsType(&v1.StatefulSet{}), mock.Anything).Return(nil)
},
expectedResource: &v1.StatefulSet{},
expectedError: nil,
},
"Resource creation succeeds with Service object": {
callMocks: func(c *utils.MockClient) {
c.On("Create", mock.IsType(context.Background()), mock.IsType(&corev1.Service{}), mock.Anything).Return(nil)
},
expectedResource: &corev1.Service{},
expectedError: nil,
},
}

for k, tc := range testcases {
t.Run(k, func(t *testing.T) {
mockClient := utils.NewClient()
tc.callMocks(mockClient)

err := CreateResource(context.Background(), tc.expectedResource, mockClient)
if tc.expectedError == nil {
goassert.Check(t, err == nil, "Not expected to return error")
} else {
assert.Equal(t, tc.expectedError.Error(), err.Error())
}
})
}
}

func TestGetResource(t *testing.T) {
testcases := map[string]struct {
callMocks func(c *utils.MockClient)
expectedError error
}{
"GetResource fails": {
callMocks: func(c *utils.MockClient) {
c.On("Get", mock.IsType(context.Background()), mock.Anything, mock.IsType(&corev1.Node{}), mock.Anything).Return(errors.New("Failed to get resource"))
},
expectedError: errors.New("Failed to get resource"),
},
"Resource creation succeeds with Statefulset object": {
callMocks: func(c *utils.MockClient) {
c.On("Get", mock.IsType(context.Background()), mock.Anything, mock.IsType(&corev1.Node{}), mock.Anything).Return(nil)
},
expectedError: nil,
},
}

for k, tc := range testcases {
t.Run(k, func(t *testing.T) {
mockClient := utils.NewClient()
tc.callMocks(mockClient)

err := GetResource(context.Background(), "fakeName", "fakeNamespace", mockClient, &corev1.Node{})
if tc.expectedError == nil {
goassert.Check(t, err == nil, "Not expected to return error")
} else {
assert.Equal(t, tc.expectedError.Error(), err.Error())
}
})
}
}

0 comments on commit e3cc935

Please sign in to comment.