diff --git a/cloud-control-manager/cloud-driver/drivers/mock/MockDriver.go b/cloud-control-manager/cloud-driver/drivers/mock/MockDriver.go index 75540984f..26a8fc747 100644 --- a/cloud-control-manager/cloud-driver/drivers/mock/MockDriver.go +++ b/cloud-control-manager/cloud-driver/drivers/mock/MockDriver.go @@ -11,7 +11,6 @@ package mock import ( - cblog "github.com/cloud-barista/cb-log" "github.com/sirupsen/logrus" @@ -62,11 +61,11 @@ func (driver *MockDriver) ConnectCloud(connectionInfo idrv.ConnectionInfo) (icon Region: connectionInfo.RegionInfo, MockName: connectionInfo.CredentialInfo.MockName, } - + // Please, do not delete this line. - mkrs.PrepareVMImage(iConn.MockName) - mkrs.PrepareVMSpec(iConn.MockName) + mkrs.PrepareVMImage(iConn.MockName) + mkrs.PrepareVMSpec(iConn.MockName) + mkrs.PrepareRegionZone(iConn.MockName) return &iConn, nil } - diff --git a/cloud-control-manager/cloud-driver/drivers/mock/connect/MockCloudConnection.go b/cloud-control-manager/cloud-driver/drivers/mock/connect/MockCloudConnection.go index 61b2208d2..c119a2d06 100644 --- a/cloud-control-manager/cloud-driver/drivers/mock/connect/MockCloudConnection.go +++ b/cloud-control-manager/cloud-driver/drivers/mock/connect/MockCloudConnection.go @@ -32,11 +32,6 @@ type MockConnection struct { MockName string } -// CreateRegionZoneHandler implements connect.CloudConnection. -func (*MockConnection) CreateRegionZoneHandler() (irs.RegionZoneHandler, error) { - return nil, errors.New("Driver: not implemented") -} - func (cloudConn *MockConnection) CreateImageHandler() (irs.ImageHandler, error) { cblogger.Info("Mock Driver: called CreateImageHandler()!") handler := mkrs.MockImageHandler{cloudConn.MockName} @@ -114,3 +109,10 @@ func (cloudConn *MockConnection) CreateAnyCallHandler() (irs.AnyCallHandler, err handler := mkrs.MockAnyCallHandler{cloudConn.MockName} return &handler, nil } + +// CreateRegionZoneHandler implements connect.CloudConnection. +func (cloudConn *MockConnection) CreateRegionZoneHandler() (irs.RegionZoneHandler, error) { + cblogger.Info("Mock Driver: called CreateRegionZoneHandler()!") + handler := mkrs.MockRegionZoneHandler{Region: cloudConn.Region, MockName: cloudConn.MockName} + return &handler, nil +} diff --git a/cloud-control-manager/cloud-driver/drivers/mock/resources/RegionZoneHandler.go b/cloud-control-manager/cloud-driver/drivers/mock/resources/RegionZoneHandler.go new file mode 100644 index 000000000..660ecb406 --- /dev/null +++ b/cloud-control-manager/cloud-driver/drivers/mock/resources/RegionZoneHandler.go @@ -0,0 +1,293 @@ +// Cloud Driver Interface of CB-Spider. +// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project. +// The CB-Spider Mission is to connect all the clouds with a single interface. +// +// * Cloud-Barista: https://github.com/cloud-barista +// +// This is Mock Driver. +// +// by CB-Spider Team, 2023.09. + +package resources + +import ( + "encoding/json" + "fmt" + + cblog "github.com/cloud-barista/cb-log" + idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces" + irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources" +) + +var regionZoneInfoMap map[string][]*irs.RegionZoneInfo + +type MockRegionZoneHandler struct { + Region idrv.RegionInfo + MockName string +} + +var prepareRegionZoneInfoList []*irs.RegionZoneInfo + +func init() { + regionZoneInfoMap = make(map[string][]*irs.RegionZoneInfo) +} + +// Be called before using the User function. +// Called in MockDriver +func PrepareRegionZone(mockName string) { + cblogger := cblog.GetLogger("CB-SPIDER") + cblogger.Info("Mock Driver: called prepare()!") + + if regionZoneInfoMap[mockName] != nil { + return + } + + prepareRegionZoneInfoList = []*irs.RegionZoneInfo{ + { + Name: "mercury", + DisplayName: "Mercury Region", + ZoneList: []irs.ZoneInfo{ + { + Name: "mercury-z1", + DisplayName: "Mercury Zone 1", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "mercury-z2", + DisplayName: "Mercury Zone 2", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "mercury-z3", + DisplayName: "Mercury Zone 3", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + }, + KeyValueList: nil, + }, + { + Name: "venus", + DisplayName: "Venus Region", + ZoneList: []irs.ZoneInfo{ + { + Name: "venus-z1", + DisplayName: "Venus Zone 1", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "venus-z2", + DisplayName: "Venus Zone 2", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "venus-z3", + DisplayName: "Venus Zone 3", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + }, + KeyValueList: nil, + }, + { + Name: "mars", + DisplayName: "Mars Region", + ZoneList: []irs.ZoneInfo{ + { + Name: "mars-z1", + DisplayName: "Mars Zone 1", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "mars-z2", + DisplayName: "Mars Zone 2", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "mars-z3", + DisplayName: "Mars Zone 3", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + }, + KeyValueList: nil, + }, + { + Name: "jupiter", + DisplayName: "Jupiter Region", + ZoneList: []irs.ZoneInfo{ + { + Name: "jupiter-z1", + DisplayName: "Jupiter Zone 1", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "jupiter-z2", + DisplayName: "Jupiter Zone 2", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "jupiter-z3", + DisplayName: "Jupiter Zone 3", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + }, + KeyValueList: nil, + }, + { + Name: "saturn", + DisplayName: "Saturn Region", + ZoneList: []irs.ZoneInfo{ + { + Name: "saturn-z1", + DisplayName: "Saturn Zone 1", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "saturn-z2", + DisplayName: "Saturn Zone 2", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "saturn-z3", + DisplayName: "Saturn Zone 3", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + }, + KeyValueList: nil, + }, + { + Name: "uranus", + DisplayName: "Uranus Region", + ZoneList: []irs.ZoneInfo{ + { + Name: "uranus-z1", + DisplayName: "Uranus Zone 1", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "uranus-z2", + DisplayName: "Uranus Zone 2", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "uranus-z3", + DisplayName: "Uranus Zone 3", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + }, + KeyValueList: nil, + }, + { + Name: "neptune", + DisplayName: "Neptune Region", + ZoneList: []irs.ZoneInfo{ + { + Name: "neptune-z1", + DisplayName: "Neptune Zone 1", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "neptune-z2", + DisplayName: "Neptune Zone 2", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + { + Name: "neptune-z3", + DisplayName: "Neptune Zone 3", + Status: irs.ZoneAvailable, + KeyValueList: nil, + }, + }, + KeyValueList: nil, + }, + } + regionZoneInfoMap[mockName] = prepareRegionZoneInfoList +} + +func (handler *MockRegionZoneHandler) ListRegionZone() ([]*irs.RegionZoneInfo, error) { + cblogger := cblog.GetLogger("CB-SPIDER") + cblogger.Info("Mock Driver: called ListRegionZone()!") + + mockName := handler.MockName + + infoList, ok := regionZoneInfoMap[mockName] + if !ok { + return []*irs.RegionZoneInfo{}, nil + } + + // cloning list of RegionZoneInfo + resultList := make([]*irs.RegionZoneInfo, len(infoList)) + copy(resultList, infoList) + return resultList, nil +} + +func (handler *MockRegionZoneHandler) GetRegionZone(Name string) (irs.RegionZoneInfo, error) { + cblogger := cblog.GetLogger("CB-SPIDER") + cblogger.Info("Mock Driver: called GetRegionZone()!") + + infoList, err := handler.ListRegionZone() + if err != nil { + cblogger.Error(err) + return irs.RegionZoneInfo{}, err + } + + for _, info := range infoList { + if (*info).Name == Name { + return *info, nil + } + } + + return irs.RegionZoneInfo{}, fmt.Errorf("%s Name does not exist!!", Name) +} + +// ListOrgRegion implements resources.RegionZoneHandler. +func (handler *MockRegionZoneHandler) ListOrgRegion() (string, error) { + cblogger := cblog.GetLogger("CB-SPIDER") + cblogger.Info("Mock Driver: called ListOrgRegion()!") + + // Convert prepareRegionZoneInfoList to JSON + jsonData, err := json.MarshalIndent(prepareRegionZoneInfoList, "", " ") + if err != nil { + cblogger.Error("Error while converting to JSON: ", err) + return "", err + } + + return string(jsonData), nil +} + +// ListOrgZone implements resources.RegionZoneHandler. +func (handler *MockRegionZoneHandler) ListOrgZone() (string, error) { + cblogger := cblog.GetLogger("CB-SPIDER") + cblogger.Info("Mock Driver: called ListOrgZone()!") + + for _, info := range prepareRegionZoneInfoList { + if (*info).Name == handler.Region.Region { + jsonData, err := json.MarshalIndent(info, "", " ") + if err != nil { + cblogger.Error("Error while converting to JSON: ", err) + return "", err + } + return string(jsonData), nil + } + } + + return "", fmt.Errorf("%s VMSpec does not exist!!", handler.Region.Region) +} diff --git a/cloud-control-manager/cloud-driver/drivers/mock/test/regionzone_test.go b/cloud-control-manager/cloud-driver/drivers/mock/test/regionzone_test.go new file mode 100644 index 000000000..2ef8385aa --- /dev/null +++ b/cloud-control-manager/cloud-driver/drivers/mock/test/regionzone_test.go @@ -0,0 +1,100 @@ +// Mock Driver Test of CB-Spider. +// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project. +// The CB-Spider Mission is to connect all the clouds with a single interface. +// +// * Cloud-Barista: https://github.com/cloud-barista +// +// by CB-Spider Team, 2020.09. + +package mocktest + +import ( + "encoding/json" + _ "fmt" + "testing" + + cblog "github.com/cloud-barista/cb-log" + mockdrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/mock" + idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces" + irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources" +) + +var regionZoneHandler irs.RegionZoneHandler + +func init() { + // make the log level lower to print clearly + cblog.SetLevel("error") + + cred := idrv.CredentialInfo{ + MockName: "MockDriver-01", + } + connInfo := idrv.ConnectionInfo{ + CredentialInfo: cred, + RegionInfo: idrv.RegionInfo{Region: "neptune", Zone: "neptune-zone01"}, + } + cloudConn, _ := (&mockdrv.MockDriver{}).ConnectCloud(connInfo) + regionZoneHandler, _ = cloudConn.CreateRegionZoneHandler() +} + +func TestRegionZoneList(t *testing.T) { + // check the list size and values + infoList, err := regionZoneHandler.ListRegionZone() + if err != nil { + t.Error(err.Error()) + } + + if len(infoList) == 0 { + t.Errorf("The number of RegionZone is 0.") + } else { + jsonData, err := json.MarshalIndent(infoList, "", " ") + if err != nil { + t.Error("Error while converting to JSON: ", err) + } + t.Logf("%s", jsonData) + } +} + +func TestGetRegionZone(t *testing.T) { + // check the list size and values + info, err := regionZoneHandler.GetRegionZone("uranus") + if err != nil { + t.Error(err.Error()) + } + + if info.Name != "uranus" { + t.Errorf("Region Name is not uranus.") + } else { + jsonData, err := json.MarshalIndent(info, "", " ") + if err != nil { + t.Error("Error while converting to JSON: ", err) + } + t.Logf("%s", jsonData) + } +} + +func TestListOrgRegion(t *testing.T) { + orgRegionList, err := regionZoneHandler.ListOrgRegion() + if err != nil { + t.Error(err.Error()) + } + + if orgRegionList == "" { + t.Errorf("The orginal Region List is empty.") + } else { + t.Logf("%s", orgRegionList) + } +} + +func TestListOrgZone(t *testing.T) { + // Region should be "neptune", because this session is set to "neptune" + orgZoneList, err := regionZoneHandler.ListOrgZone() + if err != nil { + t.Error(err.Error()) + } + + if orgZoneList == "" { + t.Errorf("The original Zone List is empty.") + } else { + t.Logf("%s", orgZoneList) + } +}