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>
"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
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)
// 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
}
// 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
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)
}
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)
// 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.")
}
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 {
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
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
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)
// 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
}
// 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")
}
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 {
"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"
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{
}
}
-func TestDeleteLogicalCloud(t *testing.T) {
+func TestDeleteLogicalCloudWithSuccess(t *testing.T) {
key := LogicalCloudKey{
Project: "test_project",
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) {