Skip to content

Commit

Permalink
Merge pull request vmware-tanzu#853 from TaoZou1/cleanut
Browse files Browse the repository at this point in the history
Add UT for clean
  • Loading branch information
TaoZou1 authored Nov 1, 2024
2 parents 535a902 + 95fa681 commit 4246c65
Show file tree
Hide file tree
Showing 3 changed files with 398 additions and 0 deletions.
205 changes: 205 additions & 0 deletions pkg/clean/clean_dlb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package clean

import (
"context"
"errors"
"reflect"
"testing"

"github.com/agiledragon/gomonkey/v2"
"github.com/go-logr/logr"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/vmware-tanzu/nsx-operator/pkg/config"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
)

func TestHttpQueryDLBResources_Success(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
cluster := &nsx.Cluster{}
cf = &config.NSXOperatorConfig{NsxConfig: &config.NsxConfig{NsxApiManagers: []string{"10.0.0.1"}}, CoeConfig: &config.CoeConfig{Cluster: "test-cluster"}}
resource := "Group"
/*
expectedURL := "policy/api/v1/search/query?query=resource_type%3AGroup%20AND%20tags.scope%3Ancp%5C%2Fcluster%20AND%20tags.tag%3Atest-cluster%20AND%20tags.scope%3Ancp%5C%2Fcreated_for%20AND%20tags.tag%3ADLB"
*/
expectedResponse := map[string]interface{}{
"results": []interface{}{
map[string]interface{}{"path": "/test/path/1"},
map[string]interface{}{"path": "/test/path/2"},
},
}

patches := gomonkey.ApplyMethod(reflect.TypeOf(cluster), "HttpGet", func(cluster *nsx.Cluster, url string) (map[string]interface{}, error) {
return expectedResponse, nil
})
defer patches.Reset()

paths, err := httpQueryDLBResources(cluster, cf, resource)
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"/test/path/1", "/test/path/2"}, paths)
}

func TestHttpQueryDLBResources_Error(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
cluster := &nsx.Cluster{}
cf = &config.NSXOperatorConfig{NsxConfig: &config.NsxConfig{NsxApiManagers: []string{"10.0.0.1"}}, CoeConfig: &config.CoeConfig{Cluster: "test-cluster"}}
resource := "Group"
/*
expectedURL := "policy/api/v1/search/query?query=resource_type%3AGroup%20AND%20tags.scope%3Ancp%5C%2Fcluster%20AND%20tags.tag%3Atest-cluster%20AND%20tags.scope%3Ancp%5C%2Fcreated_for%20AND%20tags.tag%3ADLB"
*/
expectedError := errors.New("http error")

patches := gomonkey.ApplyMethod(reflect.TypeOf(cluster), "HttpGet", func(cluster *nsx.Cluster, url string) (map[string]interface{}, error) {
return nil, expectedError
})
defer patches.Reset()

paths, err := httpQueryDLBResources(cluster, cf, resource)
assert.Error(t, err)
assert.Nil(t, paths)
}

func TestHttpQueryDLBResources_EmptyResponse(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

cf = &config.NSXOperatorConfig{NsxConfig: &config.NsxConfig{NsxApiManagers: []string{"10.0.0.1"}}, CoeConfig: &config.CoeConfig{Cluster: "test-cluster"}}
resource := "Group"
cluster := &nsx.Cluster{}

expectedResponse := map[string]interface{}{
"results": []interface{}{},
}

patches := gomonkey.ApplyMethod(reflect.TypeOf(cluster), "HttpGet", func(cluster *nsx.Cluster, url string) (map[string]interface{}, error) {
return expectedResponse, nil
})
defer patches.Reset()

paths, err := httpQueryDLBResources(cluster, cf, resource)
assert.NoError(t, err)
assert.Empty(t, paths)
}

func TestCleanDLB_Success(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

cluster := &nsx.Cluster{}
cf = &config.NSXOperatorConfig{NsxConfig: &config.NsxConfig{NsxApiManagers: []string{"10.0.0.1"}}, CoeConfig: &config.CoeConfig{Cluster: "test-cluster"}}
log := logr.Discard()

expectedPaths := []string{"/test/path/1", "/test/path/2"}
patches := gomonkey.ApplyFunc(httpQueryDLBResources, func(cluster *nsx.Cluster, cf *config.NSXOperatorConfig, resource string) ([]string, error) {
return expectedPaths, nil
})
defer patches.Reset()

patches.ApplyMethod(reflect.TypeOf(cluster), "HttpDelete", func(cluster *nsx.Cluster, url string) error {
return nil
})

ctx := context.Background()
err := CleanDLB(ctx, cluster, cf, &log)
assert.NoError(t, err)
}

func TestCleanDLB_HttpQueryError(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

cluster := &nsx.Cluster{}
cf = &config.NSXOperatorConfig{NsxConfig: &config.NsxConfig{NsxApiManagers: []string{"10.0.0.1"}}, CoeConfig: &config.CoeConfig{Cluster: "test-cluster"}}
log := logr.Discard()

expectedError := errors.New("http query error")
patches := gomonkey.ApplyFunc(httpQueryDLBResources, func(cluster *nsx.Cluster, cf *config.NSXOperatorConfig, resource string) ([]string, error) {
return nil, expectedError
})
defer patches.Reset()

ctx := context.Background()
err := CleanDLB(ctx, cluster, cf, &log)
assert.Error(t, err)
assert.Equal(t, expectedError, err)
}

func TestCleanDLB_HttpDeleteError(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

cluster := &nsx.Cluster{}
cf = &config.NSXOperatorConfig{NsxConfig: &config.NsxConfig{NsxApiManagers: []string{"10.0.0.1"}}, CoeConfig: &config.CoeConfig{Cluster: "test-cluster"}}
log := logr.Discard()

expectedPaths := []string{"/test/path/1", "/test/path/2"}
patches := gomonkey.ApplyFunc(httpQueryDLBResources, func(cluster *nsx.Cluster, cf *config.NSXOperatorConfig, resource string) ([]string, error) {
return expectedPaths, nil
})
defer patches.Reset()

expectedError := errors.New("http delete error")
patches.ApplyMethod(reflect.TypeOf(cluster), "HttpDelete", func(cluster *nsx.Cluster, url string) error {
return expectedError
})

ctx := context.Background()
err := CleanDLB(ctx, cluster, cf, &log)
assert.Error(t, err)
assert.Equal(t, expectedError, err)
}

func TestCleanDLB_ContextDone(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

cluster := &nsx.Cluster{}
cf = &config.NSXOperatorConfig{NsxConfig: &config.NsxConfig{NsxApiManagers: []string{"10.0.0.1"}}, CoeConfig: &config.CoeConfig{Cluster: "test-cluster"}}
log := logr.Discard()

expectedPaths := []string{"/test/path/1", "/test/path/2"}
patches := gomonkey.ApplyFunc(httpQueryDLBResources, func(cluster *nsx.Cluster, cf *config.NSXOperatorConfig, resource string) ([]string, error) {
return expectedPaths, nil
})
defer patches.Reset()

ctx, cancel := context.WithCancel(context.Background())
cancel()

err := CleanDLB(ctx, cluster, cf, &log)
assert.Error(t, err)
assert.True(t, errors.Is(err, context.Canceled))
}

func TestAppendIfNotExist_ItemExists(t *testing.T) {
slice := []string{"test"}
result := appendIfNotExist(slice, "test")
assert.Equal(t, []string{"test"}, result)
}

func TestAppendIfNotExist_ItemDoesNotExist(t *testing.T) {
slice := []string{"test1"}
result := appendIfNotExist(slice, "test2")
assert.Equal(t, []string{"test1", "test2"}, result)
}

func TestAppendIfNotExist_MultipleItems(t *testing.T) {
slice := []string{"test1", "test2"}
result := appendIfNotExist(slice, "test3")
assert.Equal(t, []string{"test1", "test2", "test3"}, result)
}

func TestAppendIfNotExist_DuplicateItems(t *testing.T) {
slice := []string{"test1", "test2", "test1"}
result := appendIfNotExist(slice, "test1")
assert.Equal(t, []string{"test1", "test2", "test1"}, result)
}

func TestAppendIfNotExist_EmptySlice(t *testing.T) {
slice := []string{}
result := appendIfNotExist(slice, "test")
assert.Equal(t, []string{"test"}, result)
}
147 changes: 147 additions & 0 deletions pkg/clean/clean_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package clean

import (
"context"
"errors"
"reflect"
"testing"

"github.com/agiledragon/gomonkey/v2"
"github.com/go-logr/logr"
"github.com/stretchr/testify/assert"

"github.com/vmware-tanzu/nsx-operator/pkg/config"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
)

var (
cf = &config.NSXOperatorConfig{NsxConfig: &config.NsxConfig{NsxApiManagers: []string{"10.0.0.1"}}}
)

func TestClean_ValidationFailed(t *testing.T) {
ctx := context.Background()
log := logr.Discard()
debug := false
logLevel := 0
patches := gomonkey.ApplyMethod(reflect.TypeOf(cf.NsxConfig), "ValidateConfigFromCmd", func(_ *config.NsxConfig) error {
return errors.New("validation failed")
})

defer patches.Reset()

err := Clean(ctx, cf, &log, debug, logLevel)
assert.Error(t, err)
assert.Contains(t, err.Error(), "validation failed")
}

func TestClean_GetClientFailed(t *testing.T) {
ctx := context.Background()

log := logr.Discard()
debug := false
logLevel := 0

patches := gomonkey.ApplyMethod(reflect.TypeOf(cf.NsxConfig), "ValidateConfigFromCmd", func(_ *config.NsxConfig) error {
return nil
})
defer patches.Reset()
patches.ApplyFunc(nsx.GetClient, func(_ *config.NSXOperatorConfig) *nsx.Client {
return nil
})

err := Clean(ctx, cf, &log, debug, logLevel)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to get nsx client")
}

func TestClean_InitError(t *testing.T) {
ctx := context.Background()

log := logr.Discard()
debug := false
logLevel := 0

patches := gomonkey.ApplyMethod(reflect.TypeOf(cf.NsxConfig), "ValidateConfigFromCmd", func(_ *config.NsxConfig) error {
return nil
})
defer patches.Reset()
patches.ApplyFunc(nsx.GetClient, func(_ *config.NSXOperatorConfig) *nsx.Client {
return &nsx.Client{}
})

patches.ApplyFunc(InitializeCleanupService, func(_ *config.NSXOperatorConfig, _ *nsx.Client) (*CleanupService, error) {
return nil, errors.New("init cleanup service failed")
})

err := Clean(ctx, cf, &log, debug, logLevel)
assert.Error(t, err)
assert.Contains(t, err.Error(), "init cleanup service failed")
}

func TestClean_CleanupSucc(t *testing.T) {
ctx := context.Background()

log := logr.Discard()
debug := false
logLevel := 0

patches := gomonkey.ApplyMethod(reflect.TypeOf(cf.NsxConfig), "ValidateConfigFromCmd", func(_ *config.NsxConfig) error {
return nil
})
defer patches.Reset()
patches.ApplyFunc(nsx.GetClient, func(_ *config.NSXOperatorConfig) *nsx.Client {
return &nsx.Client{}
})

cleanupService := &CleanupService{}
clean := &MockCleanup{
CleanupFunc: func(ctx context.Context) error {
return nil
},
}
cleanupService.cleans = append(cleanupService.cleans, clean)
patches.ApplyFunc(InitializeCleanupService, func(_ *config.NSXOperatorConfig, _ *nsx.Client) (*CleanupService, error) {
return cleanupService, nil
})

patches.ApplyFunc(CleanDLB, func(ctx context.Context, cluster *nsx.Cluster, cf *config.NSXOperatorConfig, log *logr.Logger) error {
return nil
})
err := Clean(ctx, cf, &log, debug, logLevel)
assert.Nil(t, err)
}

type MockCleanup struct {
CleanupFunc func(ctx context.Context) error
}

func (m *MockCleanup) Cleanup(ctx context.Context) error {
return m.CleanupFunc(ctx)
}

func TestWrapCleanFunc(t *testing.T) {
// succ case
ctx := context.Background()
clean := &MockCleanup{
CleanupFunc: func(ctx context.Context) error {
return nil
},
}

wrappedFunc := wrapCleanFunc(ctx, clean)
err := wrappedFunc()
assert.NoError(t, err)

// error case
clean = &MockCleanup{
CleanupFunc: func(ctx context.Context) error {
return errors.New("cleanup failed")
},
}

wrappedFunc = wrapCleanFunc(ctx, clean)
err = wrappedFunc()
assert.Error(t, err)
assert.Equal(t, "cleanup failed", err.Error())

}
Loading

0 comments on commit 4246c65

Please sign in to comment.