|
| 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 | +""" P1 tests for user provide hostname cases |
| 18 | +""" |
| 19 | +#Import Local Modules |
| 20 | +import marvin |
| 21 | +from nose.plugins.attrib import attr |
| 22 | +from marvin.cloudstackTestCase import * |
| 23 | +from marvin.cloudstackAPI import * |
| 24 | +from marvin.integration.lib.utils import * |
| 25 | +from marvin.integration.lib.base import * |
| 26 | +from marvin.integration.lib.common import * |
| 27 | +from marvin.remoteSSHClient import remoteSSHClient |
| 28 | +import datetime |
| 29 | + |
| 30 | + |
| 31 | +class Services: |
| 32 | + """Test user provided hostname Services |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__(self): |
| 36 | + self.services = { |
| 37 | + "domain": { |
| 38 | + "name": "Domain", |
| 39 | + }, |
| 40 | + "project": { |
| 41 | + "name": "Project", |
| 42 | + "displaytext": "Test project", |
| 43 | + }, |
| 44 | + "account": { |
| 45 | + "email": "administrator@clogeny.com", |
| 46 | + "firstname": "Test", |
| 47 | + "lastname": "User", |
| 48 | + "username": "test", |
| 49 | + # Random characters are appended for unique |
| 50 | + # username |
| 51 | + "password": "password", |
| 52 | + }, |
| 53 | + "user": { |
| 54 | + "email": "administrator@clogeny.com", |
| 55 | + "firstname": "User", |
| 56 | + "lastname": "User", |
| 57 | + "username": "User", |
| 58 | + # Random characters are appended for unique |
| 59 | + # username |
| 60 | + "password": "password", |
| 61 | + }, |
| 62 | + "disk_offering": { |
| 63 | + "displaytext": "Tiny Disk Offering", |
| 64 | + "name": "Tiny Disk Offering", |
| 65 | + "disksize": 1 |
| 66 | + }, |
| 67 | + "volume": { |
| 68 | + "diskname": "Test Volume", |
| 69 | + }, |
| 70 | + "service_offering": { |
| 71 | + "name": "Tiny Instance", |
| 72 | + "displaytext": "Tiny Instance", |
| 73 | + "cpunumber": 1, |
| 74 | + "cpuspeed": 100, # in MHz |
| 75 | + "memory": 128, # In MBs |
| 76 | + }, |
| 77 | + "virtual_machine": { |
| 78 | + "displayname": "TestVM", |
| 79 | + "username": "root", |
| 80 | + "password": "password", |
| 81 | + "ssh_port": 22, |
| 82 | + "hypervisor": 'XenServer', |
| 83 | + # Hypervisor type should be same as |
| 84 | + # hypervisor type of cluster |
| 85 | + "privateport": 22, |
| 86 | + "publicport": 22, |
| 87 | + "protocol": 'TCP', |
| 88 | + }, |
| 89 | + "ostype": 'CentOS 5.3 (64-bit)', |
| 90 | + # Cent OS 5.3 (64 bit) |
| 91 | + "sleep": 60, |
| 92 | + "timeout": 10, |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | +class TestInstanceNameFlagTrue(cloudstackTestCase): |
| 97 | + |
| 98 | + @classmethod |
| 99 | + def setUpClass(cls): |
| 100 | + cls.api_client = super( |
| 101 | + TestInstanceNameFlagTrue, |
| 102 | + cls |
| 103 | + ).getClsTestClient().getApiClient() |
| 104 | + cls.services = Services().services |
| 105 | + # Get Zone, default template |
| 106 | + cls.zone = get_zone(cls.api_client, cls.services) |
| 107 | + cls.services["mode"] = cls.zone.networktype |
| 108 | + cls.template = get_template( |
| 109 | + cls.api_client, |
| 110 | + cls.zone.id, |
| 111 | + cls.services["ostype"] |
| 112 | + ) |
| 113 | + |
| 114 | + # Create domains, account etc. |
| 115 | + cls.domain = get_domain( |
| 116 | + cls.api_client, |
| 117 | + cls.services |
| 118 | + ) |
| 119 | + |
| 120 | + cls.account = Account.create( |
| 121 | + cls.api_client, |
| 122 | + cls.services["account"], |
| 123 | + admin=True, |
| 124 | + domainid=cls.domain.id |
| 125 | + ) |
| 126 | + |
| 127 | + cls.services["virtual_machine"]["zoneid"] = cls.zone.id |
| 128 | + cls.services["virtual_machine"]["template"] = cls.template.id |
| 129 | + |
| 130 | + cls.service_offering = ServiceOffering.create( |
| 131 | + cls.api_client, |
| 132 | + cls.services["service_offering"] |
| 133 | + ) |
| 134 | + cls._cleanup = [cls.account] |
| 135 | + return |
| 136 | + |
| 137 | + @classmethod |
| 138 | + def tearDownClass(cls): |
| 139 | + try: |
| 140 | + #Cleanup resources used |
| 141 | + cleanup_resources(cls.api_client, cls._cleanup) |
| 142 | + except Exception as e: |
| 143 | + raise Exception("Warning: Exception during cleanup : %s" % e) |
| 144 | + return |
| 145 | + |
| 146 | + def setUp(self): |
| 147 | + self.apiclient = self.testClient.getApiClient() |
| 148 | + self.dbclient = self.testClient.getDbConnection() |
| 149 | + self.cleanup = [] |
| 150 | + return |
| 151 | + |
| 152 | + def tearDown(self): |
| 153 | + try: |
| 154 | + #Clean up, terminate the created accounts, domains etc |
| 155 | + cleanup_resources(self.apiclient, self.cleanup) |
| 156 | + except Exception as e: |
| 157 | + raise Exception("Warning: Exception during cleanup : %s" % e) |
| 158 | + return |
| 159 | + |
| 160 | + @attr(configuration='vm.instancename.flag') |
| 161 | + @attr(tags=["advanced", "basic", "sg", "eip", "advancedns", "simulator"]) |
| 162 | + def test_01_user_provided_hostname(self): |
| 163 | + """ Verify user provided hostname to an instance |
| 164 | + """ |
| 165 | + |
| 166 | + # Validate the following |
| 167 | + # 1. Set the vm.instancename.flag to true. Hostname and displayname |
| 168 | + # should be user provided display name |
| 169 | + # 2. Give the user provided user name. Internal name should be |
| 170 | + # i-<userid>-<vmid>-display name |
| 171 | + |
| 172 | + self.debug("Deploying VM in account: %s" % self.account.account.name) |
| 173 | + # Spawn an instance in that network |
| 174 | + virtual_machine = VirtualMachine.create( |
| 175 | + self.apiclient, |
| 176 | + self.services["virtual_machine"], |
| 177 | + accountid=self.account.account.name, |
| 178 | + domainid=self.account.account.domainid, |
| 179 | + serviceofferingid=self.service_offering.id, |
| 180 | + ) |
| 181 | + self.debug( |
| 182 | + "Checking if the virtual machine is created properly or not?") |
| 183 | + vms = VirtualMachine.list( |
| 184 | + self.apiclient, |
| 185 | + id=virtual_machine.id, |
| 186 | + listall=True |
| 187 | + ) |
| 188 | + |
| 189 | + self.assertEqual( |
| 190 | + isinstance(vms, list), |
| 191 | + True, |
| 192 | + "List vms should return a valid name" |
| 193 | + ) |
| 194 | + vm = vms[0] |
| 195 | + self.assertEqual( |
| 196 | + vm.state, |
| 197 | + "Running", |
| 198 | + "Vm state should be running after deployment" |
| 199 | + ) |
| 200 | + self.debug("vm.displayname: %s, original: %s" % |
| 201 | + (vm.displayname, |
| 202 | + self.services["virtual_machine"]["displayname"])) |
| 203 | + self.assertEqual( |
| 204 | + vm.displayname, |
| 205 | + self.services["virtual_machine"]["displayname"], |
| 206 | + "Vm display name should match the given name" |
| 207 | + ) |
| 208 | + |
| 209 | + # Fetch account ID and VMID from database to check internal name |
| 210 | + self.debug("select id from account where uuid = '%s';" \ |
| 211 | + % self.account.account.id) |
| 212 | + |
| 213 | + qresultset = self.dbclient.execute( |
| 214 | + "select id from account where uuid = '%s';" \ |
| 215 | + % self.account.account.id |
| 216 | + ) |
| 217 | + self.assertEqual( |
| 218 | + isinstance(qresultset, list), |
| 219 | + True, |
| 220 | + "Check DB query result set for valid data" |
| 221 | + ) |
| 222 | + |
| 223 | + self.assertNotEqual( |
| 224 | + len(qresultset), |
| 225 | + 0, |
| 226 | + "Check DB Query result set" |
| 227 | + ) |
| 228 | + qresult = qresultset[0] |
| 229 | + account_id = qresult[0] |
| 230 | + |
| 231 | + self.debug("select id from vm_instance where uuid = '%s';" % vm.id) |
| 232 | + |
| 233 | + qresultset = self.dbclient.execute( |
| 234 | + "select id from vm_instance where uuid = '%s';" % |
| 235 | + vm.id) |
| 236 | + |
| 237 | + self.assertEqual( |
| 238 | + isinstance(qresultset, list), |
| 239 | + True, |
| 240 | + "Check DB query result set for valid data" |
| 241 | + ) |
| 242 | + |
| 243 | + self.assertNotEqual( |
| 244 | + len(qresultset), |
| 245 | + 0, |
| 246 | + "Check DB Query result set" |
| 247 | + ) |
| 248 | + qresult = qresultset[0] |
| 249 | + self.debug("Query result: %s" % qresult) |
| 250 | + vmid = qresult[0] |
| 251 | + |
| 252 | + #internal Name = i-<user ID>-<VM ID>-Display name |
| 253 | + internal_name = "i-" + str(account_id) + "-" + str(vmid) + "-" + vm.displayname |
| 254 | + self.debug("Internal name: %s" % internal_name) |
| 255 | + self.assertEqual( |
| 256 | + vm.instancename, |
| 257 | + internal_name, |
| 258 | + "VM internal name should match with that of the format" |
| 259 | + ) |
| 260 | + return |
| 261 | + |
| 262 | + @attr(configuration='vm.instancename.flag') |
| 263 | + @attr(tags=["advanced", "basic", "sg", "eip", "advancedns", "simulator"]) |
| 264 | + def test_02_instancename_from_default_configuration(self): |
| 265 | + """ Verify for globally set instancename |
| 266 | + """ |
| 267 | + |
| 268 | + # Validate the following |
| 269 | + # 1. Set the vm.instancename.flag to true. Hostname and displayname |
| 270 | + # should be user provided display name |
| 271 | + # 2. Dont give the user provided user name. Internal name should be |
| 272 | + # i-<userid>-<vmid>-<instance.name> in global config |
| 273 | + |
| 274 | + # Removing display name from config |
| 275 | + del self.services["virtual_machine"]["displayname"] |
| 276 | + self.debug("Deploying VM in account: %s" % self.account.account.name) |
| 277 | + virtual_machine = VirtualMachine.create( |
| 278 | + self.apiclient, |
| 279 | + self.services["virtual_machine"], |
| 280 | + accountid=self.account.account.name, |
| 281 | + domainid=self.account.account.domainid, |
| 282 | + serviceofferingid=self.service_offering.id, |
| 283 | + ) |
| 284 | + self.debug( |
| 285 | + "Checking if the virtual machine is created properly or not?") |
| 286 | + vms = VirtualMachine.list( |
| 287 | + self.apiclient, |
| 288 | + id=virtual_machine.id, |
| 289 | + listall=True |
| 290 | + ) |
| 291 | + |
| 292 | + self.assertEqual( |
| 293 | + isinstance(vms, list), |
| 294 | + True, |
| 295 | + "List vms should retuen a valid name" |
| 296 | + ) |
| 297 | + vm = vms[0] |
| 298 | + self.assertEqual( |
| 299 | + vm.state, |
| 300 | + "Running", |
| 301 | + "Vm state should be running after deployment" |
| 302 | + ) |
| 303 | + self.assertEqual( |
| 304 | + vm.displayname, |
| 305 | + vm.id, |
| 306 | + "Vm display name should not match the given name" |
| 307 | + ) |
| 308 | + # Fetch account ID and VMID from database to check internal name |
| 309 | + self.debug("select id from account where uuid = '%s';" \ |
| 310 | + % self.account.account.id) |
| 311 | + |
| 312 | + qresultset = self.dbclient.execute( |
| 313 | + "select id from account where uuid = '%s';" \ |
| 314 | + % self.account.account.id |
| 315 | + ) |
| 316 | + self.assertEqual( |
| 317 | + isinstance(qresultset, list), |
| 318 | + True, |
| 319 | + "Check DB query result set for valid data" |
| 320 | + ) |
| 321 | + |
| 322 | + self.assertNotEqual( |
| 323 | + len(qresultset), |
| 324 | + 0, |
| 325 | + "Check DB Query result set" |
| 326 | + ) |
| 327 | + qresult = qresultset[0] |
| 328 | + account_id = qresult[0] |
| 329 | + |
| 330 | + self.debug("select id from vm_instance where uuid = '%s';" % vm.id) |
| 331 | + |
| 332 | + qresultset = self.dbclient.execute( |
| 333 | + "select id from vm_instance where uuid = '%s';" % |
| 334 | + vm.id) |
| 335 | + |
| 336 | + self.assertEqual( |
| 337 | + isinstance(qresultset, list), |
| 338 | + True, |
| 339 | + "Check DB query result set for valid data" |
| 340 | + ) |
| 341 | + |
| 342 | + self.assertNotEqual( |
| 343 | + len(qresultset), |
| 344 | + 0, |
| 345 | + "Check DB Query result set" |
| 346 | + ) |
| 347 | + qresult = qresultset[0] |
| 348 | + self.debug("Query result: %s" % qresult) |
| 349 | + vmid = qresult[0] |
| 350 | + |
| 351 | + self.debug("Fetching the global config value for instance.name") |
| 352 | + configs = Configurations.list( |
| 353 | + self.apiclient, |
| 354 | + name="instance.name", |
| 355 | + listall=True |
| 356 | + ) |
| 357 | + |
| 358 | + config = configs[0] |
| 359 | + instance_name = config.value |
| 360 | + self.debug("Instance.name: %s" % instance_name) |
| 361 | + |
| 362 | + #internal Name = i-<user ID>-<VM ID>- Instance_name |
| 363 | + internal_name = "i-" + str(account_id) + "-" + str(vmid) + "-" + instance_name |
| 364 | + self.assertEqual( |
| 365 | + vm.instancename, |
| 366 | + internal_name, |
| 367 | + "VM internal name should match with that of the format" |
| 368 | + ) |
| 369 | + return |
0 commit comments