From 816a5bf415b4a10a743cc3cebe651606a95574a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 3 Feb 2016 15:28:09 +0100 Subject: [PATCH] Feature #4215: Add vrouter to java oca --- .../client/vrouter/VirtualRouter.java | 441 ++++++++++++++++++ .../client/vrouter/VirtualRouterPool.java | 263 +++++++++++ src/oca/java/test/VirtualRouterTest.java | 291 ++++++++++++ src/oca/java/test/all_tests.sh | 3 + src/oca/ruby/opennebula/virtual_router.rb | 2 +- 5 files changed, 999 insertions(+), 1 deletion(-) create mode 100644 src/oca/java/src/org/opennebula/client/vrouter/VirtualRouter.java create mode 100644 src/oca/java/src/org/opennebula/client/vrouter/VirtualRouterPool.java create mode 100644 src/oca/java/test/VirtualRouterTest.java diff --git a/src/oca/java/src/org/opennebula/client/vrouter/VirtualRouter.java b/src/oca/java/src/org/opennebula/client/vrouter/VirtualRouter.java new file mode 100644 index 0000000000..5c61a65966 --- /dev/null +++ b/src/oca/java/src/org/opennebula/client/vrouter/VirtualRouter.java @@ -0,0 +1,441 @@ +/******************************************************************************* + * Copyright 2002-2015, OpenNebula Project, OpenNebula Systems + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ +package org.opennebula.client.vrouter; + +import org.opennebula.client.Client; +import org.opennebula.client.OneResponse; +import org.opennebula.client.PoolElement; +import org.w3c.dom.Node; + +/** + * This class represents an OpenNebula Virtual Router + * It also offers static XML-RPC call wrappers. + */ +public class VirtualRouter extends PoolElement +{ + + private static final String METHOD_PREFIX = "vrouter."; + private static final String ALLOCATE = METHOD_PREFIX + "allocate"; + private static final String INSTANTIATE = METHOD_PREFIX + "instantiate"; + private static final String INFO = METHOD_PREFIX + "info"; + private static final String UPDATE = METHOD_PREFIX + "update"; + private static final String DELETE = METHOD_PREFIX + "delete"; + private static final String CHOWN = METHOD_PREFIX + "chown"; + private static final String CHMOD = METHOD_PREFIX + "chmod"; + private static final String RENAME = METHOD_PREFIX + "rename"; + private static final String ATTACHNIC = METHOD_PREFIX + "attachnic"; + private static final String DETACHNIC = METHOD_PREFIX + "detachnic"; + + /** + * Creates a new VirtualRouter representation. + * @param id The VirtualRouter id. + * @param client XML-RPC Client. + */ + public VirtualRouter(int id, Client client) + { + super(id, client); + } + + /** + * @see PoolElement + */ + protected VirtualRouter(Node xmlElement, Client client) + { + super(xmlElement, client); + } + + // ================================= + // Static XML-RPC methods + // ================================= + + + /** + * Allocates a new VirtualRouter in OpenNebula. + * + * @param client XML-RPC Client. + * @param description A string containing the template of the VirtualRouter. + * @return If successful the message contains the associated + * id generated for this VirtualRouter. + */ + public static OneResponse allocate(Client client, String description) + { + return client.call(ALLOCATE, description); + } + + /** + * Retrieves the information of the given VirtualRouter. + * + * @param client XML-RPC Client. + * @param id The VirtualRouter id for the VirtualRouter to retrieve the information from + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public static OneResponse info(Client client, int id) + { + return client.call(INFO, id); + } + + /** + * Creates VM instances from a VM Template. New VMs will be associated + * to this Virtual Router, and its Virtual Networks + * + * @param client XML-RPC Client. + * @param id The id of the target VirtualRouter. + * @param nVMs Number of VMs to instantiate + * @param templateId VM Template id to instantiate + * @param name Name for the VM instances. If it is an empty string + * OpenNebula will set a default name. Wildcard %i can be used. + * @param onHold False to create this VM in pending state, true on hold + * @param template User provided Template to merge with the one + * being instantiated + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse instantiate(Client client, int id, int nVMs, + int templateId, String name, boolean onHold, String template) + { + return client.call(INSTANTIATE, id, nVMs, templateId, name, onHold, template); + } + + /** + * Deletes a VirtualRouter from OpenNebula. + * + * @param client XML-RPC Client. + * @param id The id of the target VirtualRouter we want to delete. + * @return A encapsulated response. + */ + public static OneResponse delete(Client client, int id) + { + return client.call(DELETE, id); + } + + /** + * Replaces the template contents. + * + * @param client XML-RPC Client. + * @param id The id of the target VirtualRouter we want to modify. + * @param new_template New template contents. + * @param append True to append new attributes instead of replace the whole template + * @return If successful the message contains the VirtualRouter id. + */ + public static OneResponse update(Client client, int id, String new_template, + boolean append) + { + return client.call(UPDATE, id, new_template, append ? 1 : 0); + } + + /** + * Publishes or unpublishes a VirtualRouter. + * + * @param client XML-RPC Client. + * @param id The id of the target VirtualRouter we want to modify. + * @param publish True for publishing, false for unpublishing. + * @return If successful the message contains the VirtualRouter id. + */ + public static OneResponse publish(Client client, int id, boolean publish) + { + int group_u = publish ? 1 : 0; + + return chmod(client, id, -1, -1, -1, group_u, -1, -1, -1, -1, -1); + } + + /** + * Changes the owner/group + * + * @param client XML-RPC Client. + * @param id The id of the target VirtualRouter we want to modify. + * @param uid The new owner user ID. Set it to -1 to leave the current one. + * @param gid The new group ID. Set it to -1 to leave the current one. + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse chown(Client client, int id, int uid, int gid) + { + return client.call(CHOWN, id, uid, gid); + } + + /** + * Changes the VirtualRouter permissions + * + * @param client XML-RPC Client. + * @param id The id of the target VirtualRouter. + * @param owner_u 1 to allow, 0 deny, -1 do not change + * @param owner_m 1 to allow, 0 deny, -1 do not change + * @param owner_a 1 to allow, 0 deny, -1 do not change + * @param group_u 1 to allow, 0 deny, -1 do not change + * @param group_m 1 to allow, 0 deny, -1 do not change + * @param group_a 1 to allow, 0 deny, -1 do not change + * @param other_u 1 to allow, 0 deny, -1 do not change + * @param other_m 1 to allow, 0 deny, -1 do not change + * @param other_a 1 to allow, 0 deny, -1 do not change + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse chmod(Client client, int id, + int owner_u, int owner_m, int owner_a, + int group_u, int group_m, int group_a, + int other_u, int other_m, int other_a) + { + return chmod(client, CHMOD, id, + owner_u, owner_m, owner_a, + group_u, group_m, group_a, + other_u, other_m, other_a); + } + + /** + * Changes the permissions + * + * @param client XML-RPC Client. + * @param id The id of the target object. + * @param octet Permissions octed , e.g. 640 + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse chmod(Client client, int id, String octet) + { + return chmod(client, CHMOD, id, octet); + } + + /** + * Changes the permissions + * + * @param client XML-RPC Client. + * @param id The id of the target object. + * @param octet Permissions octed , e.g. 640 + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse chmod(Client client, int id, int octet) + { + return chmod(client, CHMOD, id, octet); + } + + /** + * Renames this VirtualRouter + * + * @param client XML-RPC Client. + * @param id The id of the target VirtualRouter. + * @param name New name for the VirtualRouter. + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse rename(Client client, int id, String name) + { + return client.call(RENAME, id, name); + } + + // ================================= + // Instanced object XML-RPC methods + // ================================= + + /** + * Retrieves the information of the VirtualRouter. + * + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public OneResponse info() + { + OneResponse response = info(client, id); + super.processInfo(response); + return response; + } + + /** + * Deletes the VirtualRouter from OpenNebula. + * + * @return A encapsulated response. + */ + public OneResponse delete() + { + return delete(client, id); + } + + /** + * Replaces the VirtualRouter contents. + * + * @param new_template New template contents. + * @return If successful the message contains the VirtualRouter id. + */ + public OneResponse update(String new_template) + { + return update(new_template, false); + } + + /** + * Replaces the template contents. + * + * @param new_template New template contents. + * @param append True to append new attributes instead of replace the whole template + * @return If successful the message contains the VirtualRouter id. + */ + public OneResponse update(String new_template, boolean append) + { + return update(client, id, new_template, append); + } + + /** + * Publishes or unpublishes the VirtualRouter. + * + * @param publish True for publishing, false for unpublishing. + * @return If successful the message contains the VirtualRouter id. + */ + public OneResponse publish(boolean publish) + { + return publish(client, id, publish); + } + + /** + * Publishes the VirtualRouter. + * + * @return If successful the message contains the VirtualRouter id. + */ + public OneResponse publish() + { + return publish(true); + } + + /** + * Unpublishes the VirtualRouter. + * + * @return If successful the message contains the VirtualRouter id. + */ + public OneResponse unpublish() + { + return publish(false); + } + + /** + * Changes the owner/group + * + * @param uid The new owner user ID. Set it to -1 to leave the current one. + * @param gid The new group ID. Set it to -1 to leave the current one. + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chown(int uid, int gid) + { + return chown(client, id, uid, gid); + } + + /** + * Changes the owner + * + * @param uid The new owner user ID. + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chown(int uid) + { + return chown(uid, -1); + } + + /** + * Changes the group + * + * @param gid The new group ID. + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chgrp(int gid) + { + return chown(-1, gid); + } + + /** + * Changes the VirtualRouter permissions + * + * @param owner_u 1 to allow, 0 deny, -1 do not change + * @param owner_m 1 to allow, 0 deny, -1 do not change + * @param owner_a 1 to allow, 0 deny, -1 do not change + * @param group_u 1 to allow, 0 deny, -1 do not change + * @param group_m 1 to allow, 0 deny, -1 do not change + * @param group_a 1 to allow, 0 deny, -1 do not change + * @param other_u 1 to allow, 0 deny, -1 do not change + * @param other_m 1 to allow, 0 deny, -1 do not change + * @param other_a 1 to allow, 0 deny, -1 do not change + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chmod(int owner_u, int owner_m, int owner_a, + int group_u, int group_m, int group_a, + int other_u, int other_m, int other_a) + { + return chmod(client, id, + owner_u, owner_m, owner_a, + group_u, group_m, group_a, + other_u, other_m, other_a); + } + + /** + * Changes the permissions + * + * @param octet Permissions octed , e.g. 640 + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chmod(String octet) + { + return chmod(client, id, octet); + } + + /** + * Changes the permissions + * + * @param octet Permissions octed , e.g. 640 + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chmod(int octet) + { + return chmod(client, id, octet); + } + + /** + * Creates VM instances from a VM Template. New VMs will be associated + * to this Virtual Router, and its Virtual Networks + * + * @param nVMs Number of VMs to instantiate + * @param templateId VM Template id to instantiate + * @param name Name for the VM instances. If it is an empty string + * OpenNebula will set a default name. Wildcard %i can be used. + * @param onHold False to create this VM in pending state, true on hold + * @param template User provided Template to merge with the one + * being instantiated + * @return If an error occurs the error message contains the reason. + */ + public OneResponse instantiate(int nVMs, int templateId, String name, + boolean onHold, String template) + { + return instantiate(client, id, nVMs, templateId, name, onHold, template); + } + + /** + * Creates VM instances from a VM Template. New VMs will be associated + * to this Virtual Router, and its Virtual Networks + * + * @param nVMs Number of VMs to instantiate + * @param templateId VM Template id to instantiate + * @return If an error occurs the error message contains the reason. + */ + public OneResponse instantiate(int nVMs, int templateId) + { + return instantiate(client, id, nVMs, templateId, "", false, ""); + } + + /** + * Renames this VirtualRouter + * + * @param name New name for the VirtualRouter. + * @return If an error occurs the error message contains the reason. + */ + public OneResponse rename(String name) + { + return rename(client, id, name); + } + + // ================================= + // Helpers + // ================================= +} diff --git a/src/oca/java/src/org/opennebula/client/vrouter/VirtualRouterPool.java b/src/oca/java/src/org/opennebula/client/vrouter/VirtualRouterPool.java new file mode 100644 index 0000000000..69d9215cad --- /dev/null +++ b/src/oca/java/src/org/opennebula/client/vrouter/VirtualRouterPool.java @@ -0,0 +1,263 @@ +/******************************************************************************* + * Copyright 2002-2015, OpenNebula Project, OpenNebula Systems + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ +package org.opennebula.client.vrouter; + +import java.util.AbstractList; +import java.util.Iterator; + +import org.opennebula.client.Client; +import org.opennebula.client.OneResponse; +import org.opennebula.client.Pool; +import org.opennebula.client.PoolElement; +import org.w3c.dom.Node; + +/** + * This class represents an OpenNebula VirtualRouter pool. + * It also offers static XML-RPC call wrappers. + */ +public class VirtualRouterPool extends Pool implements Iterable +{ + private static final String ELEMENT_NAME = "VROUTER"; + private static final String INFO_METHOD = "vrouterpool.info"; + + private int filter; + + /** + * Creates a new VirtualRouter pool with the default filter flag value + * set to {@link Pool#MINE_GROUP} (VirtualRouter belonging to the connected user, + * and the ones in his group) + * + * @param client XML-RPC Client. + * + * @see VirtualRouterPool#VirtualRouterPool(Client, int) + */ + public VirtualRouterPool(Client client) + { + super(ELEMENT_NAME, client, INFO_METHOD); + this.filter = MINE_GROUP; + } + + /** + * Creates a new VirtualRouter pool. + * + * @param client XML-RPC Client. + * @param filter Filter flag to use by default in the method + * {@link VirtualRouterPool#info()}. Possible values: + * + */ + public VirtualRouterPool(Client client, int filter) + { + super(ELEMENT_NAME, client, INFO_METHOD); + this.filter = filter; + } + + /* (non-Javadoc) + * @see org.opennebula.client.Pool#factory(org.w3c.dom.Node) + */ + @Override + public PoolElement factory(Node node) + { + return new VirtualRouter(node, client); + } + + /** + * Retrieves all or part of the VirtualRouters in the pool. + * + * @param client XML-RPC Client. + * @param filter Filter flag to use. Possible values: + * + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public static OneResponse info(Client client, int filter) + { + return Pool.info(client, INFO_METHOD, filter, -1, -1); + } + + /** + * Retrieves all the VirtualRouters in the pool. + * + * @param client XML-RPC Client. + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public static OneResponse infoAll(Client client) + { + return Pool.infoAll(client, INFO_METHOD); + } + + /** + * Retrieves all the connected user's VirtualRouters. + * + * @param client XML-RPC Client. + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public static OneResponse infoMine(Client client) + { + return Pool.infoMine(client, INFO_METHOD); + } + + /** + * Retrieves all the connected user's VirtualRouters and the ones in + * his group. + * + * @param client XML-RPC Client. + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public static OneResponse infoGroup(Client client) + { + return Pool.infoGroup(client, INFO_METHOD); + } + + /** + * Retrieves all or part of the VirtualRouters in the pool. The VirtualRouters to retrieve + * can be also filtered by Id, specifying the first and last Id to include. + * + * @param client XML-RPC Client. + * @param filter Filter flag to use. Possible values: + * + * @param startId Lowest Id to retrieve + * @param endId Biggest Id to retrieve + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public static OneResponse info(Client client, int filter, + int startId, int endId) + { + return Pool.info(client, INFO_METHOD, filter, startId, endId); + } + + /** + * Loads the xml representation of all or part of the + * VirtualRouters in the pool. The filter used is the one set in + * the constructor. + * + * @see VirtualRouterPool#info(Client, int) + * + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public OneResponse info() + { + return super.info(filter, -1, -1); + } + + /** + * Loads the xml representation of all the VirtualRouters in the pool. + * + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public OneResponse infoAll() + { + return super.infoAll(); + } + + /** + * Loads the xml representation of all the connected user's VirtualRouters. + * + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public OneResponse infoMine() + { + return super.infoMine(); + } + + /** + * Loads the xml representation of all the connected user's VirtualRouters and + * the ones in his group. + * + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public OneResponse infoGroup() + { + return super.infoGroup(); + } + + /** + * Retrieves all or part of the VirtualRouters in the pool. The VirtualRouters to retrieve + * can be also filtered by Id, specifying the first and last Id to include. + * + * @param filter Filter flag to use. Possible values: + * + * @param startId Lowest Id to retrieve + * @param endId Biggest Id to retrieve + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public OneResponse info(int filter, int startId, int endId) + { + return super.info(filter, startId, endId); + } + + public Iterator iterator() + { + AbstractList ab = new AbstractList() + { + public int size() + { + return getLength(); + } + + public VirtualRouter get(int index) + { + return (VirtualRouter) item(index); + } + }; + + return ab.iterator(); + } + + /** + * Returns the VirtualRouter with the given Id from the pool. If it is not found, + * then returns null. The method {@link #info()} must be called before. + * + * @param id of the ACl rule to retrieve + * @return The VirtualRouter with the given Id, or null if it was not found. + */ + public VirtualRouter getById(int id) + { + return (VirtualRouter) super.getById(id); + } +} diff --git a/src/oca/java/test/VirtualRouterTest.java b/src/oca/java/test/VirtualRouterTest.java new file mode 100644 index 0000000000..d2b16223d8 --- /dev/null +++ b/src/oca/java/test/VirtualRouterTest.java @@ -0,0 +1,291 @@ +/******************************************************************************* + * Copyright 2002-2015, OpenNebula Project, OpenNebula Systems + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.opennebula.client.Client; +import org.opennebula.client.OneResponse; +import org.opennebula.client.group.Group; +import org.opennebula.client.template.*; +import org.opennebula.client.vrouter.*; +import org.opennebula.client.vm.*; +import org.opennebula.client.user.User; +import org.opennebula.client.vm.VirtualMachine; + + + +public class VirtualRouterTest +{ + private static VirtualRouter vrouter; + private static VirtualRouterPool vrouterPool; + + private static Client client; + + private static OneResponse res; + private static String name = "new_test_vrouter"; + + + private static String template_str = + "NAME = \"" + name + "\"\n" + + "ATT1 = \"VAL1\"\n" + + "ATT2 = \"VAL2\""; + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception + { + client = new Client(); + vrouterPool = new VirtualRouterPool(client); + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception + { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception + { + res = VirtualRouter.allocate(client, template_str); + + int oid = res.isError() ? -1 : Integer.parseInt(res.getMessage()); + vrouter = new VirtualRouter(oid, client); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception + { + vrouter.delete(); + } + + @Test + public void allocate() + { + vrouter.delete(); + + res = VirtualRouter.allocate(client, template_str); + assertTrue( res.getErrorMessage(), !res.isError() ); + + int oid = res.isError() ? -1 : Integer.parseInt(res.getMessage()); + vrouter = new VirtualRouter(oid, client); + + + vrouterPool.info(); + + boolean found = false; + for(VirtualRouter temp : vrouterPool) + { + found = found || temp.getName().equals(name); + } + + assertTrue( found ); + } + + @Test + public void info() + { + res = vrouter.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + assertTrue( vrouter.getName().equals(name) ); + } + + @Test + public void publish() + { + res = vrouter.publish(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + vrouter.info(); + assertTrue( vrouter.xpath("PERMISSIONS/GROUP_U").equals("1") ); + } + + @Test + public void unpublish() + { + res = vrouter.unpublish(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + vrouter.info(); + assertTrue( vrouter.xpath("PERMISSIONS/GROUP_U").equals("0") ); + } + + @Test + public void chmod() + { + res = vrouter.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + String owner_a = vrouter.xpath("PERMISSIONS/OWNER_A"); + String group_a = vrouter.xpath("PERMISSIONS/GROUP_A"); + + res = vrouter.chmod(0, 1, -1, 1, 0, -1, 1, 1, 0); + assertTrue( res.getErrorMessage(), !res.isError() ); + + res = vrouter.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + assertTrue( vrouter.xpath("PERMISSIONS/OWNER_U").equals("0") ); + assertTrue( vrouter.xpath("PERMISSIONS/OWNER_M").equals("1") ); + assertTrue( vrouter.xpath("PERMISSIONS/OWNER_A").equals(owner_a) ); + assertTrue( vrouter.xpath("PERMISSIONS/GROUP_U").equals("1") ); + assertTrue( vrouter.xpath("PERMISSIONS/GROUP_M").equals("0") ); + assertTrue( vrouter.xpath("PERMISSIONS/GROUP_A").equals(group_a) ); + assertTrue( vrouter.xpath("PERMISSIONS/OTHER_U").equals("1") ); + assertTrue( vrouter.xpath("PERMISSIONS/OTHER_M").equals("1") ); + assertTrue( vrouter.xpath("PERMISSIONS/OTHER_A").equals("0") ); + } + + @Test + public void chmod_octet() + { + res = vrouter.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + res = vrouter.chmod(640); + assertTrue( res.getErrorMessage(), !res.isError() ); + + res = vrouter.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + assertTrue( vrouter.xpath("PERMISSIONS/OWNER_U").equals("1") ); + assertTrue( vrouter.xpath("PERMISSIONS/OWNER_M").equals("1") ); + assertTrue( vrouter.xpath("PERMISSIONS/OWNER_A").equals("0") ); + assertTrue( vrouter.xpath("PERMISSIONS/GROUP_U").equals("1") ); + assertTrue( vrouter.xpath("PERMISSIONS/GROUP_M").equals("0") ); + assertTrue( vrouter.xpath("PERMISSIONS/GROUP_A").equals("0") ); + assertTrue( vrouter.xpath("PERMISSIONS/OTHER_U").equals("0") ); + assertTrue( vrouter.xpath("PERMISSIONS/OTHER_M").equals("0") ); + assertTrue( vrouter.xpath("PERMISSIONS/OTHER_A").equals("0") ); + + res = vrouter.chmod("147"); + assertTrue( res.getErrorMessage(), !res.isError() ); + + res = vrouter.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + assertTrue( vrouter.xpath("PERMISSIONS/OWNER_U").equals("0") ); + assertTrue( vrouter.xpath("PERMISSIONS/OWNER_M").equals("0") ); + assertTrue( vrouter.xpath("PERMISSIONS/OWNER_A").equals("1") ); + assertTrue( vrouter.xpath("PERMISSIONS/GROUP_U").equals("1") ); + assertTrue( vrouter.xpath("PERMISSIONS/GROUP_M").equals("0") ); + assertTrue( vrouter.xpath("PERMISSIONS/GROUP_A").equals("0") ); + assertTrue( vrouter.xpath("PERMISSIONS/OTHER_U").equals("1") ); + assertTrue( vrouter.xpath("PERMISSIONS/OTHER_M").equals("1") ); + assertTrue( vrouter.xpath("PERMISSIONS/OTHER_A").equals("1") ); + } + + @Test + public void attributes() + { + res = vrouter.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + assertTrue( vrouter.xpath("NAME").equals(name) ); + } + + @Test + public void delete() + { + res = vrouter.delete(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + res = vrouter.info(); + assertTrue( res.isError() ); + } + + @Test + public void chown() + { + // Create a new User and Group + res = User.allocate(client, "template_test_user", "password"); + assertTrue( res.getErrorMessage(), !res.isError() ); + + int uid = Integer.parseInt(res.getMessage()); + + res = Group.allocate(client, "template_test_group"); + assertTrue( res.getErrorMessage(), !res.isError() ); + + int gid = Integer.parseInt(res.getMessage()); + + res = vrouter.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + assertTrue( vrouter.uid() == 0 ); + assertTrue( vrouter.gid() == 0 ); + + res = vrouter.chown(uid, gid); + assertTrue( res.getErrorMessage(), !res.isError() ); + + res = vrouter.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + assertTrue( vrouter.uid() == uid ); + assertTrue( vrouter.gid() == gid ); + + res = vrouter.chgrp(0); + + res = vrouter.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + assertTrue( vrouter.uid() == uid ); + assertTrue( vrouter.gid() == 0 ); + } + + @Test + public void instantiate() + { + VirtualMachinePool vmPool = new VirtualMachinePool(client); + + res = vmPool.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + assertTrue( vmPool.getLength() == 0 ); + + String tmpl_str = + "NAME = vrtemplate\n"+ + "CPU = 0.1\n"+ + "MEMORY = 64\n"; + + res = Template.allocate(client, tmpl_str); + assertTrue( res.getErrorMessage(), !res.isError() ); + + int tmplid = Integer.parseInt(res.getMessage()); + + res = vrouter.instantiate(3, tmplid); + assertTrue( res.getErrorMessage(), !res.isError() ); + + res = vmPool.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + assertTrue( vmPool.getLength() == 3 ); + } +} diff --git a/src/oca/java/test/all_tests.sh b/src/oca/java/test/all_tests.sh index 5488dd8518..dff793416c 100755 --- a/src/oca/java/test/all_tests.sh +++ b/src/oca/java/test/all_tests.sh @@ -73,4 +73,7 @@ let RC=RC+$? ./test.sh SecurityGroupTest let RC=RC+$? +./test.sh VirtualRouterTest +let RC=RC+$? + exit $RC \ No newline at end of file diff --git a/src/oca/ruby/opennebula/virtual_router.rb b/src/oca/ruby/opennebula/virtual_router.rb index fa46c961e9..6f47e02e35 100644 --- a/src/oca/ruby/opennebula/virtual_router.rb +++ b/src/oca/ruby/opennebula/virtual_router.rb @@ -87,7 +87,7 @@ module OpenNebula # @para n_vms [Integer] Number of VMs to instantiate # @para template_id [Integer] VM Template id to instantiate # @param name [String] Name for the VM instances. If it is an empty - # string OpenNebula will set a default name + # string OpenNebula will set a default name. Wildcard %i can be used. # @param hold [true,false] false to create the VM in pending state, # true to create it on hold # @param template [String] User provided Template to merge with the