Skip to content

Commit

Permalink
F #6311: GOCA - marketplace state
Browse files Browse the repository at this point in the history
- Add state to Markeplace
- Add state Markeplace Appliance
- Add enable method for Marketplace
- Add tests and use gocheck

co-authored-by: Pavel Czerny <[email protected]>

Signed-off-by: Pierre Lafievre <[email protected]>
  • Loading branch information
treywelsh authored and rsmontero committed Sep 12, 2023
1 parent 4c1ab6e commit 4673721
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 166 deletions.
13 changes: 10 additions & 3 deletions src/oca/go/src/goca/marketplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,17 @@ func (mc *MarketPlaceController) Delete() error {
return err
}

// Enable enables or disables a marketplace.
// * enable: True for enabling, False for disabling
func (mc *MarketPlaceController) Enable(enable bool) error {
_, err := mc.c.Client.Call("one.market.enable", mc.ID, enable)
return err
}

// Update adds marketplace content.
// * tpl: The new marketplace contents. Syntax can be the usual attribute=value or XML.
// * uType: Update type: Replace: Replace the whole template.
// Merge: Merge new template with the existing one.
// - tpl: The new marketplace contents. Syntax can be the usual attribute=value or XML.
// - uType: Update type: Replace: Replace the whole template.
// Merge: Merge new template with the existing one.
func (mc *MarketPlaceController) Update(tpl string, uType parameters.UpdateType) error {
_, err := mc.c.Client.Call("one.market.update", mc.ID, tpl, uType)
return err
Expand Down
198 changes: 99 additions & 99 deletions src/oca/go/src/goca/marketplace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,142 +17,142 @@
package goca

import (
"testing"

"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/marketplace"
"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/marketplace/keys"
"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/shared"
"github.com/OpenNebula/one/src/oca/go/src/goca/parameters"
. "gopkg.in/check.v1"
)

func TestMarketplace(t *testing.T) {
var mkt_name string = "marketplace_test_go"
type MarketPlaceSuite struct {
marketName string
marketID int
}

var _ = Suite(&MarketPlaceSuite{})

var market *marketplace.MarketPlace
func (s *MarketPlaceSuite) SetUpTest(c *C) {
// Create Marketpkace
s.marketName = "marketplace_test_go"

tpl := marketplace.NewTemplate()
tpl.Add(keys.Name, mkt_name)
tpl.Add(keys.Name, s.marketName)
tpl.Add(keys.MarketMAD, "http")
tpl.Add(keys.BaseUrl, "http://url/")
tpl.Add(keys.PublicDir, "/var/loca/market-http")
tpl.Add(keys.PublicDir, "/var/local/market-http")

id, err := testCtrl.MarketPlaces().Create(tpl.String())
c.Assert(err, IsNil)
s.marketID = id
}

func (s *MarketPlaceSuite) TearDownTest(c *C) {
// Delete Marketpkace
marketC := testCtrl.MarketPlace(s.marketID)
err := marketC.Delete()

//Create Marketpkace
market_id, err := testCtrl.MarketPlaces().Create(tpl.String())
if err != nil {
t.Fatalf("Test failed:\n" + err.Error())
}

if err != nil {
t.Errorf("Test failed:\n" + err.Error())
}
c.Assert(err, IsNil)
}

marketCtrl := testCtrl.MarketPlace(market_id)
market, err = marketCtrl.Info(false)
if err != nil {
t.Errorf("Test failed:\n" + err.Error())
}
func (s *MarketPlaceSuite) TestGetByNameAndID(c *C) {
// Get MarketPlace by ID
market, err := testCtrl.MarketPlace(s.marketID).Info(false)

actual := market.Name
c.Assert(err, IsNil)
c.Assert(market.ID, Equals, s.marketID)
c.Assert(market.Name, Equals, s.marketName)
c.Assert(market.MarketMad, Equals, "http")

if actual != mkt_name {
t.Errorf("Test failed, expected: '%s', got: '%s'", mkt_name, actual)
}
state, err := market.State()
c.Assert(err, IsNil)
c.Assert(state, Equals, marketplace.Enabled);

tmpl := "ATT1 = \"VAL1\""
// Test value from MarketPlace template
baseUrl, err := market.Template.Get(keys.BaseUrl)

//Update Marketpkace
err = marketCtrl.Update(tmpl, 1)
c.Assert(err, IsNil)
c.Assert(baseUrl, Equals, "http://url/")

if err != nil {
t.Errorf("Test failed:\n" + err.Error())
}
// Get Backup Job by Name
id, err := testCtrl.MarketPlaces().ByName(s.marketName)
c.Assert(err, IsNil)
c.Assert(id, Equals, s.marketID)
}

market, err = marketCtrl.Info(false)
if err != nil {
t.Errorf("Test failed:\n" + err.Error())
}
func (s *MarketPlaceSuite) TestUpdate(c *C) {
marketC := testCtrl.MarketPlace(s.marketID)
err := marketC.Update(`ATT1 = "VAL1"`, parameters.Merge)

actual_mm := market.MarketMad
actual_1, err := market.Template.GetStr("ATT1")
if err != nil {
t.Errorf("Test failed, can't retrieve '%s', error: %s", "ATT1", err.Error())
} else {
if actual_1 != "VAL1" {
t.Errorf("Test failed, expected: '%s', got: '%s'", "VAL1", actual_1)
}
}
c.Assert(err, IsNil)

if actual_mm != "http" {
t.Errorf("Test failed, expected: '%s', got: '%s'", "http", actual_mm)
}
market, err := testCtrl.MarketPlace(s.marketID).Info(false)
c.Assert(err, IsNil)

//Change permissions for Marketpkace
err = marketCtrl.Chmod(shared.Permissions{1, 1, 1, 1, 1, 1, 1, 1, 1})
att, err := market.Template.Get("ATT1")

if err != nil {
t.Errorf("Test failed:\n" + err.Error())
}
c.Assert(err, IsNil)
c.Assert(att, Equals, "VAL1")
c.Assert(market.MarketMad, Equals, "http")
}

market, err = marketCtrl.Info(false)
if err != nil {
t.Errorf("Test failed:\n" + err.Error())
}
func (s *MarketPlaceSuite) TestRename(c *C) {
marketC := testCtrl.MarketPlace(s.marketID)
marketC.Rename("new_name")

expected_perm := shared.Permissions{1, 1, 1, 1, 1, 1, 1, 1, 1}
actual_perm := *market.Permissions
market, err := testCtrl.MarketPlace(s.marketID).Info(false)
c.Assert(err, IsNil)
c.Assert(market.Name, Equals, "new_name");
}

if actual_perm != expected_perm {
t.Errorf("Test failed, expected: '%s', got: '%s'", expected_perm.String(), actual_perm.String())
}
func (s *MarketPlaceSuite) TestChown(c *C) {
// Test only if the call exists, no real change
marketC := testCtrl.MarketPlace(s.marketID)
err := marketC.Chown(1, 1)

//Change owner of Marketpkace
err = marketCtrl.Chown(1, 1)
c.Assert(err, IsNil)

if err != nil {
t.Errorf("Test failed:\n" + err.Error())
}
market, err := testCtrl.MarketPlace(s.marketID).Info(false)
c.Assert(err, IsNil)
c.Assert(market.UID, Equals, 1);
c.Assert(market.GID, Equals, 1);
}

market, err = marketCtrl.Info(false)
if err != nil {
t.Errorf("Test failed:\n" + err.Error())
}
func (s *MarketPlaceSuite) TestChmod(c *C) {
new_permissions := shared.Permissions{1, 1, 1, 1, 1, 1, 1, 1, 1}

expected_usr := 1
expected_grp := 1
actual_usr := market.UID
actual_grp := market.GID
marketC := testCtrl.MarketPlace(s.marketID)

if actual_usr != expected_usr {
t.Errorf("Test failed, expected: '%d', got: '%d'", expected_usr, actual_usr)
}
err := marketC.Chmod(new_permissions)

if actual_grp != expected_grp {
t.Errorf("Test failed, expected: '%d', got: '%d'", expected_grp, actual_grp)
}
c.Assert(err, IsNil)

rename := mkt_name + "-renamed"
market, err := marketC.Info(false)

//Rename Marketpkace
err = marketCtrl.Rename(rename)
c.Assert(err, IsNil)
c.Assert(*market.Permissions, Equals, new_permissions);
}

if err != nil {
t.Errorf("Test failed:\n" + err.Error())
}
func (s *MarketPlaceSuite) TestEnable(c *C) {
// Disable
marketC := testCtrl.MarketPlace(s.marketID)
err := marketC.Enable(false)
c.Assert(err, IsNil)

market, err = marketCtrl.Info(false)
if err != nil {
t.Errorf("Test failed:\n" + err.Error())
}
market, err := marketC.Info(false)
c.Assert(err, IsNil)

actual = market.Name
state, err := market.State()
c.Assert(err, IsNil)
c.Assert(state, Equals, marketplace.Disabled);

if actual != rename {
t.Errorf("Test failed, expected: '%s', got: '%s'", rename, actual)
}
// Enable
err = marketC.Enable(true)
c.Assert(err, IsNil)

//Delete Marketpkace
err = marketCtrl.Delete()
market, err = marketC.Info(false)
c.Assert(err, IsNil)

if err != nil {
t.Errorf("Test failed:\n" + err.Error())
}
state, err = market.State()
c.Assert(err, IsNil)
c.Assert(state, Equals, marketplace.Enabled);
}
Loading

0 comments on commit 4673721

Please sign in to comment.