diff --git a/src/oca/go/src/goca/helper_test.go b/src/oca/go/src/goca/helper_test.go index 901f46e18a..8a5adadeb9 100644 --- a/src/oca/go/src/goca/helper_test.go +++ b/src/oca/go/src/goca/helper_test.go @@ -43,3 +43,25 @@ func WaitResource(f func() bool) bool { } return false } + +// Get User Main Group name +func GetUserGroup(t *testing.T, user string) (string, error){ + u, err := NewUserFromName(user) + if err != nil { + t.Error("Cannot retreive caller user ID") + } + + // Get User Info + err = u.Info() + if err != nil { + t.Error("Cannot retreive caller user Info") + } + + // Get Caller Group + ugroup, ok := u.XPath("/USER/GNAME") + if !ok { + t.Errorf("Could not get caller group name") + } + + return ugroup, nil +} diff --git a/src/oca/go/src/goca/image_test.go b/src/oca/go/src/goca/image_test.go new file mode 100644 index 0000000000..f2ecf9a976 --- /dev/null +++ b/src/oca/go/src/goca/image_test.go @@ -0,0 +1,171 @@ +package goca + +import ( + "testing" + "strings" +) + +var imageTpl = ` +NAME = "test-image" +SIZE = 1 +TYPE = "DATABLOCK" +DEV_PREFIX = "sd" +DRIVER = "qcow2" +PERSISTENT = "YES" +TM_MAD = "ssh" +` + +func ImageExpectState(image *Image, state string) func() bool { + return func() bool { + image.Info() + + s, err := image.StateString() + if err != nil { + return false + } + + if state != "" && s == state { + return true + } + + return false + } +} + +// Helper to create a Image +func createImage(t *testing.T) *Image { + // Datastore ID 1 means default for image + id, err := CreateImage(imageTpl, 1) + if err != nil { + t.Error(err) + } + + // Get Image by ID + image := NewImage(id) + + err = image.Info() + if err != nil { + t.Error(err) + } + + return image +} + +func TestImage(t *testing.T) { + image := createImage(t) + + idParse, err := GetID(t, image, "IMAGE") + if err != nil { + t.Error(err) + } + + if idParse != image.ID { + t.Errorf("Image ID does not match") + } + + // Get image by Name + name, ok := image.XPath("/IMAGE/NAME") + if !ok { + t.Errorf("Could not get name") + } + + image, err = NewImageFromName(name) + if err != nil { + t.Error(err) + } + + err = image.Info() + if err != nil { + t.Error(err) + } + + idParse, err = GetID(t, image, "IMAGE") + + if idParse != image.ID { + t.Errorf("Image ID does not match") + } + + + // Wait image is ready + wait := WaitResource(ImageExpectState(image, "READY")) + if wait == false { + t.Error("Image not READY") + } + + // Change Owner to user call + err = image.Chown(-1, -1) + if err != nil { + t.Error(err) + } + + err = image.Info() + if err != nil { + t.Error(err) + } + + // Get Image Owner Name + uname, ok := image.XPath("/IMAGE/UNAME") + if !ok { + t.Errorf("Could not get user name") + } + + // Get Image owner group Name + gname, ok := image.XPath("/IMAGE/GNAME") + if !ok { + t.Errorf("Could not get group name") + } + + // Compare with caller username + caller := strings.Split(client.token, ":")[0] + if caller != uname { + t.Error("Caller user and image owner user mismatch") + } + + group, err := GetUserGroup(t, caller) + if err != nil { + t.Error("Cannot retreive caller group") + } + + // Compare with caller group + if group != gname { + t.Error("Caller group and image owner group mismatch") + } + + // Change Owner to oneadmin call + err = image.Chown(0, 0) + if err != nil { + t.Error(err) + } + + err = image.Info() + if err != nil { + t.Error(err) + } + + // Get Image Owner Name + uname, ok = image.XPath("/IMAGE/UNAME") + if !ok { + t.Errorf("Could not get user name") + } + + // Get Image Owner Name + gname, ok = image.XPath("/IMAGE/GNAME") + if !ok { + t.Errorf("Could not get user name") + } + + if "oneadmin" != uname { + t.Error("Image owner is not oneadmin") + } + + // Compare with caller group + if "oneadmin" != gname { + t.Error("Image owner group is not oneadmin") + } + + // Delete template + err = image.Delete() + if err != nil { + t.Error(err) + } +} diff --git a/src/oca/go/src/goca/marketplace.go b/src/oca/go/src/goca/marketplace.go index 48d7a2b63d..2a667449ed 100644 --- a/src/oca/go/src/goca/marketplace.go +++ b/src/oca/go/src/goca/marketplace.go @@ -111,8 +111,8 @@ func (market *MarketPlace) Chmod(uu, um, ua, gu, gm, ga, ou, om, oa int) error { // Chown changes the ownership of a marketplace. // * userID: The User ID of the new owner. If set to -1, it will not change. // * groupID: The Group ID of the new group. If set to -1, it will not change. -func (market *MarketPlace) Chown(userID, groupID uint) error { - _, err := client.Call("one.market.chown", market.ID, int(userID), int(groupID)) +func (market *MarketPlace) Chown(userID, groupID int) error { + _, err := client.Call("one.market.chown", market.ID, userID, groupID) return err } diff --git a/src/oca/go/src/goca/marketplace_test.go b/src/oca/go/src/goca/marketplace_test.go new file mode 100644 index 0000000000..754df58b66 --- /dev/null +++ b/src/oca/go/src/goca/marketplace_test.go @@ -0,0 +1,145 @@ +package goca + +import ( + "testing" + "strings" +) + +var mpTpl = ` +NAME = "MPTEST" +MARKET_MAD = "http" +ZONE_ID = 0 +BASE_URL = "http://frontend.opennebula.org/" +PUBLIC_DIR = "/var/loca/market-http" +BRIDGE_LIST = "web-server.opennebula.org" +` + +// Helper to create a Marketplace +func createMarketPlace(t *testing.T) *MarketPlace { + id, err := CreateMarketPlace(mpTpl) + if err != nil { + t.Error(err) + } + + // Get Marketplace by ID + mp := NewMarketPlace(id) + + err = mp.Info() + if err != nil { + t.Error(err) + } + + return mp +} + +func TestMarketPlace(t *testing.T) { + mp := createMarketPlace(t) + + idParse, err := GetID(t, mp, "MARKETPLACE") + if err != nil { + t.Error(err) + } + + if idParse != mp.ID { + t.Errorf("Marketplace ID does not match") + } + + // Get security group by Name + name, ok := mp.XPath("/MARKETPLACE/NAME") + if !ok { + t.Errorf("Could not get name") + } + + mp, err = NewMarketPlaceFromName(name) + if err != nil { + t.Error(err) + } + + err = mp.Info() + if err != nil { + t.Error(err) + } + + idParse, err = GetID(t, mp, "MARKETPLACE") + + if idParse != mp.ID { + t.Errorf("Marketplace ID does not match") + } + + // Change Owner to user call + err = mp.Chown(-1, -1) + if err != nil { + t.Error(err) + } + + err = mp.Info() + if err != nil { + t.Error(err) + } + + // Get Marketplace Owner Name + uname, ok := mp.XPath("/MARKETPLACE/UNAME") + if !ok { + t.Errorf("Could not get user name") + } + + // Get Marketplace owner group Name + gname, ok := mp.XPath("/MARKETPLACE/GNAME") + if !ok { + t.Errorf("Could not get group name") + } + + // Compare with caller username + caller := strings.Split(client.token, ":")[0] + if caller != uname { + t.Error("Caller user and marketplace owner user mismatch") + } + + group, err := GetUserGroup(t, caller) + if err != nil { + t.Error("Cannot retreive caller group") + } + + // Compare with caller group + if group != gname { + t.Error("Caller group and security group owner group mismatch") + } + + // Change Owner to oneadmin call + err = mp.Chown(0, 0) + if err != nil { + t.Error(err) + } + + err = mp.Info() + if err != nil { + t.Error(err) + } + + // Get Marketplace Owner Name + uname, ok = mp.XPath("/MARKETPLACE/UNAME") + if !ok { + t.Errorf("Could not get user name") + } + + // Get Marketplace Owner Name + gname, ok = mp.XPath("/MARKETPLACE/GNAME") + if !ok { + t.Errorf("Could not get user name") + } + + if "oneadmin" != uname { + t.Error("MarketPlace owner is not oneadmin") + } + + // Compare with caller group + if "oneadmin" != gname { + t.Error("MarketPlace owner group is not oneadmin") + } + + // Delete template + err = mp.Delete() + if err != nil { + t.Error(err) + } +} diff --git a/src/oca/go/src/goca/marketplaceapp.go b/src/oca/go/src/goca/marketplaceapp.go index 7423ec8914..3baac110f7 100644 --- a/src/oca/go/src/goca/marketplaceapp.go +++ b/src/oca/go/src/goca/marketplaceapp.go @@ -119,8 +119,8 @@ func (marketApp *MarketPlaceApp) Chmod(uu, um, ua, gu, gm, ga, ou, om, oa int) e // Chown changes the ownership of a marketplace app. // * userID: The User ID of the new owner. If set to -1, it will not change. // * groupID: The Group ID of the new group. If set to -1, it will not change. -func (marketApp *MarketPlaceApp) Chown(userID, groupID uint) error { - _, err := client.Call("one.marketapp.chown", marketApp.ID, int(userID), int(groupID)) +func (marketApp *MarketPlaceApp) Chown(userID, groupID int) error { + _, err := client.Call("one.marketapp.chown", marketApp.ID, userID, groupID) return err } diff --git a/src/oca/go/src/goca/securitygroup.go b/src/oca/go/src/goca/securitygroup.go index 6dddd53a0d..f1ac0556bb 100644 --- a/src/oca/go/src/goca/securitygroup.go +++ b/src/oca/go/src/goca/securitygroup.go @@ -128,8 +128,8 @@ func (sg *SecurityGroup) Chmod(uu, um, ua, gu, gm, ga, ou, om, oa int) error { // Chown changes the ownership of a security group. // * userID: The User ID of the new owner. If set to -1, it will not change. // * groupID: The Group ID of the new group. If set to -1, it will not change. -func (sg *SecurityGroup) Chown(userID, groupID uint) error { - _, err := client.Call("one.secgroup.chown", sg.ID, int(userID), int(groupID)) +func (sg *SecurityGroup) Chown(userID, groupID int) error { + _, err := client.Call("one.secgroup.chown", sg.ID, userID, groupID) return err } diff --git a/src/oca/go/src/goca/securitygroup_test.go b/src/oca/go/src/goca/securitygroup_test.go new file mode 100644 index 0000000000..230e432356 --- /dev/null +++ b/src/oca/go/src/goca/securitygroup_test.go @@ -0,0 +1,149 @@ +package goca + +import ( + "testing" + "strings" +) + +var sgTpl = ` +NAME = "ssh-test" +RULE = [ + PROTOCOL = "TCP", + RANGE = 22, + RULE_TYPE = "inbound" +] +RULE = [ + PROTOCOL = "TCP", + RULE_TYPE = "outbound" +] +` + +// Helper to create a Security Group +func createSecurityGroup(t *testing.T) *SecurityGroup { + id, err := CreateSecurityGroup(sgTpl) + if err != nil { + t.Error(err) + } + + // Get Security Group by ID + secgroup := NewSecurityGroup(id) + + err = secgroup.Info() + if err != nil { + t.Error(err) + } + + return secgroup +} + +func TestSecurityGroup(t *testing.T) { + secgroup := createSecurityGroup(t) + + idParse, err := GetID(t, secgroup, "SECURITY_GROUP") + if err != nil { + t.Error(err) + } + + if idParse != secgroup.ID { + t.Errorf("Security Group ID does not match") + } + + // Get security group by Name + name, ok := secgroup.XPath("/SECURITY_GROUP/NAME") + if !ok { + t.Errorf("Could not get name") + } + + secgroup, err = NewSecurityGroupFromName(name) + if err != nil { + t.Error(err) + } + + err = secgroup.Info() + if err != nil { + t.Error(err) + } + + idParse, err = GetID(t, secgroup, "SECURITY_GROUP") + + if idParse != secgroup.ID { + t.Errorf("Security Group ID does not match") + } + + // Change Owner to user call + err = secgroup.Chown(-1, -1) + if err != nil { + t.Error(err) + } + + err = secgroup.Info() + if err != nil { + t.Error(err) + } + + // Get Security Group Owner Name + uname, ok := secgroup.XPath("/SECURITY_GROUP/UNAME") + if !ok { + t.Errorf("Could not get user name") + } + + // Get Security Group owner group Name + gname, ok := secgroup.XPath("/SECURITY_GROUP/GNAME") + if !ok { + t.Errorf("Could not get group name") + } + + // Compare with caller username + caller := strings.Split(client.token, ":")[0] + if caller != uname { + t.Error("Caller user and security group owner user mismatch") + } + + group, err := GetUserGroup(t, caller) + if err != nil { + t.Error("Cannot retreive caller group") + } + + // Compare with caller group + if group != gname { + t.Error("Caller group and security group owner group mismatch") + } + + // Change Owner to oneadmin call + err = secgroup.Chown(0, 0) + if err != nil { + t.Error(err) + } + + err = secgroup.Info() + if err != nil { + t.Error(err) + } + + // Get Security Group Owner Name + uname, ok = secgroup.XPath("/SECURITY_GROUP/UNAME") + if !ok { + t.Errorf("Could not get user name") + } + + // Get Security Group Owner Name + gname, ok = secgroup.XPath("/SECURITY_GROUP/GNAME") + if !ok { + t.Errorf("Could not get user name") + } + + if "oneadmin" != uname { + t.Error("Security group owner is not oneadmin") + } + + // Compare with caller group + if "oneadmin" != gname { + t.Error("Security group owner group is not oneadmin") + } + + // Delete template + err = secgroup.Delete() + if err != nil { + t.Error(err) + } +} diff --git a/src/oca/go/src/goca/virtualnetwork.go b/src/oca/go/src/goca/virtualnetwork.go index 25894a8424..85d167f74c 100644 --- a/src/oca/go/src/goca/virtualnetwork.go +++ b/src/oca/go/src/goca/virtualnetwork.go @@ -68,10 +68,10 @@ func NewVirtualNetworkFromName(name string) (*VirtualNetwork, error) { return NewVirtualNetwork(id), nil } -// CreateVirtualnetwork allocates a new virtualnetwork. It returns the new virtualnetwork ID. +// CreateVirtualNetwork allocates a new virtualnetwork. It returns the new virtualnetwork ID. // * tpl: template of the virtualnetwork // * clusterID: The cluster ID. If it is -1, the default one will be used. -func CreateVirtualnetwork(tpl string, clusterID int) (uint, error) { +func CreateVirtualNetwork(tpl string, clusterID int) (uint, error) { response, err := client.Call("one.vn.allocate", tpl, clusterID) if err != nil { return 0, err diff --git a/src/oca/go/src/goca/virtualnetwork_test.go b/src/oca/go/src/goca/virtualnetwork_test.go new file mode 100644 index 0000000000..1ccd32fe00 --- /dev/null +++ b/src/oca/go/src/goca/virtualnetwork_test.go @@ -0,0 +1,145 @@ +package goca + +import ( + "testing" + "strings" +) + +var vnTpl = ` +NAME = "vntest" +BRIDGE = "vnetbr" +PHYDEV = "eth0" +SECURITY_GROUPS = 0 +VLAN_ID = 8000042 +VN_MAD = "vxlan" +` + +// Helper to create a Virtual Network +func createVirtualNetwork(t *testing.T) *VirtualNetwork { + id, err := CreateVirtualNetwork(vnTpl, -1) + if err != nil { + t.Error(err) + } + + // Get Virtual Network by ID + vnet := NewVirtualNetwork(id) + + err = vnet.Info() + if err != nil { + t.Error(err) + } + + return vnet +} + +func TestVirtualNetwork(t *testing.T) { + vnet := createVirtualNetwork(t) + + idParse, err := GetID(t, vnet, "VNET") + if err != nil { + t.Error(err) + } + + if idParse != vnet.ID { + t.Errorf("Virtual Network ID does not match") + } + + // Get virtual network by Name + name, ok := vnet.XPath("/VNET/NAME") + if !ok { + t.Errorf("Could not get name") + } + + vnet, err = NewVirtualNetworkFromName(name) + if err != nil { + t.Error(err) + } + + err = vnet.Info() + if err != nil { + t.Error(err) + } + + idParse, err = GetID(t, vnet, "VNET") + + if idParse != vnet.ID { + t.Errorf("Virtual Network ID does not match") + } + + // Change Owner to user call + err = vnet.Chown(-1, -1) + if err != nil { + t.Error(err) + } + + err = vnet.Info() + if err != nil { + t.Error(err) + } + + // Get Virtual Network Owner Name + uname, ok := vnet.XPath("/VNET/UNAME") + if !ok { + t.Errorf("Could not get user name") + } + + // Get Virtual Network owner group Name + gname, ok := vnet.XPath("/VNET/GNAME") + if !ok { + t.Errorf("Could not get group name") + } + + // Compare with caller username + caller := strings.Split(client.token, ":")[0] + if caller != uname { + t.Error("Caller user and virtual network owner user mismatch") + } + + group, err := GetUserGroup(t, caller) + if err != nil { + t.Error("Cannot retreive caller group") + } + + // Compare with caller group + if group != gname { + t.Error("Caller group and security group owner group mismatch") + } + + // Change Owner to oneadmin call + err = vnet.Chown(0, 0) + if err != nil { + t.Error(err) + } + + err = vnet.Info() + if err != nil { + t.Error(err) + } + + // Get Virtual Network Owner Name + uname, ok = vnet.XPath("/VNET/UNAME") + if !ok { + t.Errorf("Could not get user name") + } + + // Get Virtual Network Owner Name + gname, ok = vnet.XPath("/VNET/GNAME") + if !ok { + t.Errorf("Could not get user name") + } + + if "oneadmin" != uname { + t.Error("Virtual network owner is not oenadmin") + } + + // Compare with caller group + if "oneadmin" != gname { + t.Error("Virtual network owner group is not oneadmin") + } + + // Delete template + err = vnet.Delete() + if err != nil { + t.Error(err) + } +}