|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | + |
| 19 | +# this script will cover VMdeployment with Userdata tests |
| 20 | + |
| 21 | +from marvin.cloudstackTestCase import cloudstackTestCase |
| 22 | +from marvin.integration.lib.base import * |
| 23 | +from marvin.integration.lib.utils import * |
| 24 | +from marvin.integration.lib.common import * |
| 25 | +from nose.plugins.attrib import attr |
| 26 | +from marvin import remoteSSHClient |
| 27 | +import unittest |
| 28 | +import random |
| 29 | +import string |
| 30 | + |
| 31 | +_multiprocess_shared_ = True |
| 32 | +import os |
| 33 | + |
| 34 | +class Services: |
| 35 | + def __init__(self): |
| 36 | + self.services = { |
| 37 | + "account": { |
| 38 | + "email": "test@test.com", |
| 39 | + "firstname": "Test", |
| 40 | + "lastname": "User", |
| 41 | + "username": "test", |
| 42 | + "password": "password", |
| 43 | + }, |
| 44 | + "virtual_machine": { |
| 45 | + "displayname": "TesVM1", |
| 46 | + "username": "root", |
| 47 | + "password": "password", |
| 48 | + "ssh_port": 22, |
| 49 | + "hypervisor": 'XenServer', |
| 50 | + "privateport": 22, |
| 51 | + "publicport": 22, |
| 52 | + "protocol": 'TCP', |
| 53 | + }, |
| 54 | + "ostype": 'CentOS 5.3 (64-bit)', |
| 55 | + "service_offering": { |
| 56 | + "name": "Tiny Instance", |
| 57 | + "displaytext": "Tiny Instance", |
| 58 | + "cpunumber": 1, |
| 59 | + "cpuspeed": 100, |
| 60 | + "memory": 256, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +class TestDeployVmWithUserData(cloudstackTestCase): |
| 69 | + """Tests for UserData |
| 70 | + """ |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def setUpClass(cls): |
| 74 | + cls.apiClient = super(TestDeployVmWithUserData, cls).getClsTestClient().getApiClient() |
| 75 | + cls.services = Services().services |
| 76 | + cls.zone = get_zone(cls.apiClient, cls.services) |
| 77 | + if cls.zone.localstorageenabled: |
| 78 | + #For devcloud since localstroage is enabled |
| 79 | + cls.services["service_offering"]["storagetype"] = "local" |
| 80 | + cls.service_offering = ServiceOffering.create( |
| 81 | + cls.apiClient, |
| 82 | + cls.services["service_offering"] |
| 83 | + ) |
| 84 | + cls.account = Account.create(cls.apiClient, services=cls.services["account"]) |
| 85 | + cls.cleanup = [cls.account] |
| 86 | + cls.template = get_template( |
| 87 | + cls.apiClient, |
| 88 | + cls.zone.id, |
| 89 | + cls.services["ostype"] |
| 90 | + ) |
| 91 | + # Generate userdata of 2500 bytes. This is larger than the 2048 bytes limit. |
| 92 | + # CS however allows for upto 4K bytes in the code. So this must succeed. |
| 93 | + # Overall, the query length must not exceed 4K, for then the json decoder |
| 94 | + # will fail this operation at the marvin client side itcls. |
| 95 | + |
| 96 | + |
| 97 | + cls.userdata = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(2500)) |
| 98 | + cls.user_data_2k= ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(2000)) |
| 99 | + cls.user_data_2kl = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(1900)) |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + @attr(tags=["simulator", "devcloud", "basic", "advanced"]) |
| 104 | + def test_deployvm_userdata_post(self): |
| 105 | + """Test userdata as POST, size > 2k |
| 106 | + """ |
| 107 | + |
| 108 | + |
| 109 | + self.userdata=base64.b64encode(self.userdata) |
| 110 | + self.services["virtual_machine"]["userdata"] = self.userdata |
| 111 | + |
| 112 | + deployVmResponse = VirtualMachine.create( |
| 113 | + self.apiClient, |
| 114 | + services=self.services["virtual_machine"], |
| 115 | + accountid=self.account.name, |
| 116 | + domainid=self.account.domainid, |
| 117 | + serviceofferingid=self.service_offering.id, |
| 118 | + templateid=self.template.id, |
| 119 | + zoneid=self.zone.id, |
| 120 | + method="POST" |
| 121 | + |
| 122 | + ) |
| 123 | + |
| 124 | + vms = list_virtual_machines( |
| 125 | + self.apiClient, |
| 126 | + account=self.account.name, |
| 127 | + domainid=self.account.domainid, |
| 128 | + id=deployVmResponse.id |
| 129 | + ) |
| 130 | + self.assert_(len(vms) > 0, "There are no Vms deployed in the account %s" % self.account.name) |
| 131 | + vm = vms[0] |
| 132 | + self.assert_(vm.id == str(deployVmResponse.id), "Vm deployed is different from the test") |
| 133 | + self.assert_(vm.state == "Running", "VM is not in Running state") |
| 134 | + ip_addr=deployVmResponse.ipaddress |
| 135 | + if self.zone.networktype == "Basic": |
| 136 | + list_router_response = list_routers( |
| 137 | + self.apiClient, |
| 138 | + listall="true" |
| 139 | + ) |
| 140 | + else: |
| 141 | + list_router_response = list_routers( |
| 142 | + self.apiClient, |
| 143 | + account=self.account.name, |
| 144 | + domainid=self.account.domainid |
| 145 | + ) |
| 146 | + self.assertEqual( |
| 147 | + isinstance(list_router_response, list), |
| 148 | + True, |
| 149 | + "Check list response returns a valid list" |
| 150 | + ) |
| 151 | + router = list_router_response[0] |
| 152 | + |
| 153 | + hosts = list_hosts( |
| 154 | + self.apiClient, |
| 155 | + zoneid=router.zoneid, |
| 156 | + type='Routing', |
| 157 | + state='Up', |
| 158 | + id=router.hostid |
| 159 | + ) |
| 160 | + self.assertEqual( |
| 161 | + isinstance(hosts, list), |
| 162 | + True, |
| 163 | + "Check list host returns a valid list" |
| 164 | + ) |
| 165 | + host = hosts[0] |
| 166 | + self.debug("Router ID: %s, state: %s" % (router.id, router.state)) |
| 167 | + |
| 168 | + self.assertEqual( |
| 169 | + router.state, |
| 170 | + 'Running', |
| 171 | + "Check list router response for router state" |
| 172 | + ) |
| 173 | + host.user="root" |
| 174 | + host.passwd="password" |
| 175 | + cmd="cat /var/www/html/userdata/"+deployVmResponse.ipaddress+"/user-data" |
| 176 | + |
| 177 | + if self.apiClient.hypervisor.lower() == 'vmware': |
| 178 | + |
| 179 | + try: |
| 180 | + result = get_process_status( |
| 181 | + self.apiClient.connection.mgtSvr, |
| 182 | + 22, |
| 183 | + self.apiClient.connection.user, |
| 184 | + self.apiClient.connection.passwd, |
| 185 | + router.linklocalip, |
| 186 | + cmd, |
| 187 | + hypervisor=self.apiClient.hypervisor |
| 188 | + ) |
| 189 | + res = str(result) |
| 190 | + self.assertEqual(res.__contains__(self.userdata),True,"Userdata Not applied Check the failures") |
| 191 | + |
| 192 | + |
| 193 | + except KeyError: |
| 194 | + self.skipTest("Marvin configuration has no host credentials to check USERDATA") |
| 195 | + |
| 196 | + else: |
| 197 | + try: |
| 198 | + result = get_process_status( |
| 199 | + host.ipaddress, |
| 200 | + 22, |
| 201 | + host.user, |
| 202 | + host.passwd, |
| 203 | + router.linklocalip, |
| 204 | + cmd |
| 205 | + ) |
| 206 | + res = str(result) |
| 207 | + self.assertEqual(res.__contains__(self.userdata),True,"Userdata Not applied Check the failures") |
| 208 | + except KeyError: |
| 209 | + self.skipTest("Marvin configuration has no host credentials to check router user data") |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | + @classmethod |
| 214 | + def tearDownClass(cls): |
| 215 | + try: |
| 216 | + #Cleanup resources used |
| 217 | + cleanup_resources(cls.apiClient, cls.cleanup) |
| 218 | + |
| 219 | + except Exception as e: |
| 220 | + raise Exception("Warning: Exception during cleanup : %s" % e) |
0 commit comments