Restore commented unit test TestDeleteLogicalCloud 49/113749/1
authorIgor D.C <igor.duarte.cardoso@intel.com>
Thu, 8 Oct 2020 18:44:14 +0000 (18:44 +0000)
committerIgor D.C <igor.duarte.cardoso@intel.com>
Sat, 10 Oct 2020 05:12:08 +0000 (05:12 +0000)
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>
src/dcm/api/logicalCloudHandler.go
src/dcm/pkg/module/apply.go
src/dcm/pkg/module/cluster.go
src/dcm/pkg/module/logicalcloud.go
src/dcm/pkg/module/logicalcloud_test.go

index b305b20..ad9a380 100644 (file)
@@ -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
index c3378ab..0eaa75a 100644 (file)
@@ -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)
index 9aecc6c..6ad4640 100644 (file)
@@ -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 {
index 580e902..3fe981b 100644 (file)
@@ -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 {
index efce568..cbdc130 100644 (file)
@@ -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) {