diff options
author | Igor D.C <igor.duarte.cardoso@intel.com> | 2020-10-08 18:44:14 +0000 |
---|---|---|
committer | Igor D.C <igor.duarte.cardoso@intel.com> | 2020-10-10 05:12:08 +0000 |
commit | 5e013d0d327dee9f5cd53fee6ec8d2d2593e0258 (patch) | |
tree | 2c50e2e4341dd302ddb1091dbf23171c2d7a43fc | |
parent | edaf4416c5002254b60199a0b9710ebb23ccdf7b (diff) |
Restore commented unit test TestDeleteLogicalCloud
Restore the previously commented-out unit test TestDeleteLogicalCloud.
That test was disabled due to a failure introduced by interacting with
AppContext for the first time in module/logicalcloud.go and it not
being ready to do so.
This commit restores it and modifies code so dependent mocks can plug
in correctly. This was done in order to keep testing the code that was
previously being tested, not so much to add additional coverage.
Although it would be a significant undertaking, the different types and
interfaces in pkg/module should be redesigned to achieve better
decoupling and thus make unit testing more straightforward.
Issue-ID: MULTICLOUD-1143
Change-Id: I1e6b7bb9111fc6883f0c9cee887329a9e0b27fbd
Signed-off-by: Igor D.C <igor.duarte.cardoso@intel.com>
-rw-r--r-- | src/dcm/api/logicalCloudHandler.go | 13 | ||||
-rw-r--r-- | src/dcm/pkg/module/apply.go | 12 | ||||
-rw-r--r-- | src/dcm/pkg/module/cluster.go | 11 | ||||
-rw-r--r-- | src/dcm/pkg/module/logicalcloud.go | 18 | ||||
-rw-r--r-- | src/dcm/pkg/module/logicalcloud_test.go | 33 |
5 files changed, 48 insertions, 39 deletions
diff --git a/src/dcm/api/logicalCloudHandler.go b/src/dcm/api/logicalCloudHandler.go index b305b202..ad9a3807 100644 --- a/src/dcm/api/logicalCloudHandler.go +++ b/src/dcm/api/logicalCloudHandler.go @@ -26,7 +26,6 @@ import ( "github.com/gorilla/mux" "github.com/onap/multicloud-k8s/src/dcm/pkg/module" orch "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" - pkgerrors "github.com/pkg/errors" ) // logicalCloudHandler is used to store backend implementations objects @@ -266,13 +265,6 @@ func (h logicalCloudHandler) terminateHandler(w http.ResponseWriter, r *http.Req return } - _, ctxVal, err := h.client.GetLogicalCloudContext(project, name) - if ctxVal == "" { - err = pkgerrors.New("Logical Cloud hasn't been applied yet") - http.Error(w, err.Error(), http.StatusConflict) - return - } - // Get Clusters clusters, err := h.clusterClient.GetAllClusters(project, name) @@ -291,8 +283,11 @@ func (h logicalCloudHandler) terminateHandler(w http.ResponseWriter, r *http.Req // Terminate the Logical Cloud err = module.Terminate(project, lc, clusters, quotas) if err != nil { + if err.Error() == "Logical Cloud doesn't seem applied: "+name { + http.Error(w, err.Error(), http.StatusConflict) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) - return } return diff --git a/src/dcm/pkg/module/apply.go b/src/dcm/pkg/module/apply.go index c3378ab8..0eaa75ab 100644 --- a/src/dcm/pkg/module/apply.go +++ b/src/dcm/pkg/module/apply.go @@ -342,11 +342,11 @@ func Apply(project string, logicalcloud LogicalCloud, clusterList []Cluster, } // Check if there was a previous context for this logical cloud - ac, cid, err := lcclient.GetLogicalCloudContext(project, logicalCloudName) + ac, cid, err := lcclient.util.GetLogicalCloudContext(lcclient.storeName, lckey, lcclient.tagMeta, project, logicalCloudName) if cid != "" { // Make sure rsync status for this logical cloud is Terminated, // otherwise we can't re-apply logical cloud yet - acStatus, _ := getAppContextStatus(ac) + acStatus, _ := lcclient.util.GetAppContextStatus(ac) switch acStatus.Status { case appcontext.AppContextStatusEnum.Terminated: // We now know Logical Cloud has terminated, so let's update the entry before we process the apply @@ -544,8 +544,12 @@ func Terminate(project string, logicalcloud LogicalCloud, clusterList []Cluster, logicalCloudName := logicalcloud.MetaData.LogicalCloudName lcclient := NewLogicalCloudClient() + lckey := LogicalCloudKey{ + LogicalCloudName: logicalcloud.MetaData.LogicalCloudName, + Project: project, + } - ac, cid, err := lcclient.GetLogicalCloudContext(project, logicalCloudName) + ac, cid, err := lcclient.util.GetLogicalCloudContext(lcclient.storeName, lckey, lcclient.tagMeta, project, logicalCloudName) if err != nil { return pkgerrors.Wrapf(err, "Logical Cloud doesn't seem applied: %v", logicalCloudName) } @@ -554,7 +558,7 @@ func Terminate(project string, logicalcloud LogicalCloud, clusterList []Cluster, if cid != "" { // Make sure rsync status for this logical cloud is Terminated, // otherwise we can't re-apply logical cloud yet - acStatus, _ := getAppContextStatus(ac) + acStatus, _ := lcclient.util.GetAppContextStatus(ac) switch acStatus.Status { case appcontext.AppContextStatusEnum.Terminated: return pkgerrors.New("The Logical Cloud has already been terminated: " + logicalCloudName) diff --git a/src/dcm/pkg/module/cluster.go b/src/dcm/pkg/module/cluster.go index 9aecc6ca..6ad46404 100644 --- a/src/dcm/pkg/module/cluster.go +++ b/src/dcm/pkg/module/cluster.go @@ -260,7 +260,11 @@ func (v *ClusterClient) UpdateCluster(project, logicalCloud, clusterReference st // Get returns Cluster's kubeconfig for corresponding cluster reference func (v *ClusterClient) GetClusterConfig(project, logicalCloud, clusterReference string) (string, error) { lcClient := NewLogicalCloudClient() - context, ctxVal, err := lcClient.GetLogicalCloudContext(project, logicalCloud) + lckey := LogicalCloudKey{ + Project: project, + LogicalCloudName: logicalCloud, + } + context, ctxVal, err := lcClient.util.GetLogicalCloudContext(lcClient.storeName, lckey, lcClient.tagMeta, project, logicalCloud) if err != nil { return "", pkgerrors.Wrap(err, "Error getting logical cloud context.") } @@ -268,11 +272,6 @@ func (v *ClusterClient) GetClusterConfig(project, logicalCloud, clusterReference return "", pkgerrors.New("Logical Cloud hasn't been applied yet") } - // private key comes from logical cloud - lckey := LogicalCloudKey{ - Project: project, - LogicalCloudName: logicalCloud, - } // get logical cloud resource lc, err := lcClient.Get(project, logicalCloud) if err != nil { diff --git a/src/dcm/pkg/module/logicalcloud.go b/src/dcm/pkg/module/logicalcloud.go index 580e9022..3fe981b8 100644 --- a/src/dcm/pkg/module/logicalcloud.go +++ b/src/dcm/pkg/module/logicalcloud.go @@ -75,7 +75,6 @@ type LogicalCloudManager interface { GetAll(project string) ([]LogicalCloud, error) Delete(project, name string) error Update(project, name string, c LogicalCloud) (LogicalCloud, error) - GetLogicalCloudContext(project string, name string) (appcontext.AppContext, string, error) } // Interface facilitates unit testing by mocking functions @@ -86,6 +85,8 @@ type Utility interface { DBRemove(storeName string, key db.Key) error CheckProject(project string) error CheckLogicalCloud(project, logicalCloud string) error + GetLogicalCloudContext(storeName string, key db.Key, meta string, project string, name string) (appcontext.AppContext, string, error) + GetAppContextStatus(ac appcontext.AppContext) (*appcontext.AppContextStatus, error) } // LogicalCloudClient implements the LogicalCloudManager @@ -208,7 +209,7 @@ func (v *LogicalCloudClient) Delete(project, logicalCloudName string) error { return pkgerrors.New("Logical Cloud does not exist") } - context, _, err := v.GetLogicalCloudContext(project, logicalCloudName) + context, _, err := v.util.GetLogicalCloudContext(v.storeName, key, v.tagMeta, project, logicalCloudName) // If there's no context for Logical Cloud, just go ahead and delete it now if err != nil { err = v.util.DBRemove(v.storeName, key) @@ -220,7 +221,7 @@ func (v *LogicalCloudClient) Delete(project, logicalCloudName string) error { // Make sure rsync status for this logical cloud is Terminated, // otherwise we can't remove appcontext yet - acStatus, _ := getAppContextStatus(context) + acStatus, _ := v.util.GetAppContextStatus(context) switch acStatus.Status { case appcontext.AppContextStatusEnum.Terminated: // remove the appcontext @@ -267,14 +268,9 @@ func (v *LogicalCloudClient) Update(project, logicalCloudName string, c LogicalC } // GetLogicalCloudContext returns the AppContext for corresponding provider and name -func (v *LogicalCloudClient) GetLogicalCloudContext(project string, name string) (appcontext.AppContext, string, error) { - //Construct key and tag to select the entry - key := LogicalCloudKey{ - LogicalCloudName: name, - Project: project, - } +func (d DBService) GetLogicalCloudContext(storeName string, key db.Key, meta string, project string, name string) (appcontext.AppContext, string, error) { - value, err := v.util.DBFind(v.storeName, key, v.tagContext) + value, err := d.DBFind(storeName, key, meta) if err != nil { return appcontext.AppContext{}, "", pkgerrors.Wrap(err, "Get Logical Cloud Context") } @@ -353,7 +349,7 @@ func (d DBService) CheckLogicalCloud(project, logicalCloud string) error { return nil } -func getAppContextStatus(ac appcontext.AppContext) (*appcontext.AppContextStatus, error) { +func (d DBService) GetAppContextStatus(ac appcontext.AppContext) (*appcontext.AppContextStatus, error) { h, err := ac.GetCompositeAppHandle() if err != nil { diff --git a/src/dcm/pkg/module/logicalcloud_test.go b/src/dcm/pkg/module/logicalcloud_test.go index efce568f..cbdc1304 100644 --- a/src/dcm/pkg/module/logicalcloud_test.go +++ b/src/dcm/pkg/module/logicalcloud_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" "github.com/pkg/errors" "github.com/stretchr/testify/mock" @@ -55,6 +56,20 @@ func (m *mockValues) CheckLogicalCloud(project, logicalCloud string) error { return args.Error(0) } +func (m *mockValues) GetLogicalCloudContext(name string, key db.Key, meta string, project, logicalCloud string) (appcontext.AppContext, string, error) { + fmt.Println("Mocked Get Logical Cloud Context") + args := m.Called(name, key, meta, project, logicalCloud) + + return appcontext.AppContext{}, "", args.Error(2) +} + +func (m *mockValues) GetAppContextStatus(ac appcontext.AppContext) (*appcontext.AppContextStatus, error) { + fmt.Println("Mocked GetAppContextStatus") + args := m.Called(ac) + + return &appcontext.AppContextStatus{}, args.Error(1) +} + func TestCreateLogicalCloud(t *testing.T) { mData := MetaDataList{ @@ -110,7 +125,7 @@ func TestGetLogicalCloud(t *testing.T) { } } -func TestDeleteLogicalCloud(t *testing.T) { +func TestDeleteLogicalCloudWithSuccess(t *testing.T) { key := LogicalCloudKey{ Project: "test_project", @@ -128,14 +143,14 @@ func TestDeleteLogicalCloud(t *testing.T) { myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) myMocks.On("DBUnmarshal", data2).Return(nil) myMocks.On("DBFind", "test_dcm", key, "test_context").Return(data1, nil) - // TODO also test for when the logical cloud doesn't exist - - // TODO: fix Etcd-related test crash - // lcClient := LogicalCloudClient{"test_dcm", "test_meta", "test_context", myMocks} - // err := lcClient.Delete("test_project", "test_asdf") - // if err != nil { - // t.Errorf("Some error occured!") - // } + myMocks.On("GetLogicalCloudContext", "test_dcm", key, "test_meta", "test_project", "test_asdf").Return(appcontext.AppContext{}, "", nil) + myMocks.On("GetAppContextStatus", appcontext.AppContext{}).Return(&appcontext.AppContextStatus{}, nil) + + lcClient := LogicalCloudClient{"test_dcm", "test_meta", "test_context", myMocks} + err := lcClient.Delete("test_project", "test_asdf") + if err.Error() != "The Logical Cloud can't be deleted yet at this point." { + t.Errorf("Some unexpected error occurred!") + } } func TestUpdateLogicalCloud(t *testing.T) { |