Skip to content

Commit f09004e

Browse files
Python automated test case for updateResourceCount API method
1 parent 7f934c0 commit f09004e

1 file changed

Lines changed: 235 additions & 0 deletions

File tree

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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+
""" Test update resource count API method
18+
"""
19+
# Import Local Modules
20+
from marvin.cloudstackTestCase import cloudstackTestCase
21+
from marvin.lib.utils import (random_gen,
22+
cleanup_resources)
23+
from marvin.lib.base import (Domain,
24+
Account,
25+
ServiceOffering,
26+
VirtualMachine,
27+
Network,
28+
User,
29+
NATRule,
30+
Template,
31+
PublicIPAddress,
32+
Resources)
33+
from marvin.lib.common import (get_domain,
34+
get_zone,
35+
get_template,
36+
list_accounts,
37+
list_virtual_machines,
38+
list_service_offering,
39+
list_templates,
40+
list_users,
41+
get_builtin_template_info,
42+
wait_for_cleanup)
43+
from nose.plugins.attrib import attr
44+
from marvin.cloudstackException import CloudstackAPIException
45+
import time
46+
47+
48+
class Services:
49+
50+
"""These are the configurations that will be implemented in the target ACS env in order to execute the tests
51+
"""
52+
53+
def __init__(self):
54+
self.services = {
55+
"account": {
56+
"email": "test@test.com",
57+
"firstname": "Test",
58+
"lastname": "Tester",
59+
"username": "test",
60+
"password": "acountFirstUserPass",
61+
},
62+
"service_offering_custom": {
63+
"name": "Custom service offering test",
64+
"displaytext": "Custom service offering test",
65+
"cpunumber": None,
66+
"cpuspeed": None,
67+
# in MHz
68+
"memory": None,
69+
# In MBs
70+
},
71+
"service_offering_normal": {
72+
"name": "Normal service offering",
73+
"displaytext": "Normal service offering",
74+
"cpunumber": 2,
75+
"cpuspeed": 1000,
76+
# in MHz
77+
"memory": 512,
78+
# In MBs
79+
},
80+
"virtual_machine": {
81+
"displayname": "Test VM",
82+
"username": "root",
83+
"password": "password",
84+
"ssh_port": 22,
85+
"hypervisor": 'XenServer',
86+
# Hypervisor type should be same as
87+
# hypervisor type of cluster
88+
"privateport": 22,
89+
"publicport": 22,
90+
"protocol": 'TCP',
91+
},
92+
"ostype": 'CentOS 5.3 (64-bit)',
93+
"sleep": 60,
94+
"timeout": 10
95+
}
96+
97+
98+
class TestUpdateResourceCount(cloudstackTestCase):
99+
100+
@classmethod
101+
def setUpClass(cls):
102+
cls.testClient = super(TestUpdateResourceCount, cls).getClsTestClient()
103+
cls.api_client = cls.testClient.getApiClient()
104+
105+
cls.services = Services().services
106+
cls.zone = get_zone(cls.api_client, cls.testClient.getZoneForTests())
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+
cls.services["virtual_machine"]["zoneid"] = cls.zone.id
114+
cls.services["virtual_machine"]["template"] = cls.template.id
115+
116+
cls.service_offering_custom = ServiceOffering.create(
117+
cls.api_client,
118+
cls.services["service_offering_custom"]
119+
)
120+
cls.service_offering_normal = ServiceOffering.create(
121+
cls.api_client,
122+
cls.services["service_offering_normal"]
123+
)
124+
cls._cleanup = [cls.service_offering_custom, cls.service_offering_normal]
125+
return
126+
127+
@classmethod
128+
def tearDownClass(cls):
129+
try:
130+
# Cleanup resources used
131+
cleanup_resources(cls.api_client, cls._cleanup)
132+
except Exception as e:
133+
raise Exception("Warning: Exception during cleanup : %s" % e)
134+
return
135+
136+
def setUp(self):
137+
self.apiclient = self.testClient.getApiClient()
138+
self.dbclient = self.testClient.getDbConnection()
139+
self.cleanup = []
140+
self.account = Account.create(
141+
self.apiclient,
142+
self.services["account"]
143+
)
144+
self.debug("Created account: %s" % self.account.name)
145+
self.cleanup.append(self.account)
146+
147+
return
148+
149+
def tearDown(self):
150+
try:
151+
# Clean up, terminate the created accounts, domains etc
152+
cleanup_resources(self.apiclient, self.cleanup)
153+
except Exception as e:
154+
raise Exception("Warning: Exception during cleanup : %s" % e)
155+
return
156+
157+
@attr(
158+
tags=[
159+
"advanced",
160+
"basic",
161+
"eip",
162+
"advancedns",
163+
"sg"],
164+
required_hardware="false")
165+
def test_01_updateResourceCount(self):
166+
"""Test update resource count for an account using a custom service offering
167+
"""
168+
169+
# This test will execute the following steps to assure resource count update is working properly
170+
# 1. Create an account.
171+
# 2. Start 2 VMs; one with normal service offering and other with a custom service offering
172+
# 3. Call the update resource count method and check the CPU and memory values.
173+
# The two VMs will add up to 3 CPUs and 1024Mb of RAM.
174+
# 4. If the return of updateResourceCount method matches with the expected one, the test passes; otherwise, it fails.
175+
# 5. Remove everything created by deleting the account
176+
177+
vm_1 = VirtualMachine.create(
178+
self.apiclient,
179+
self.services["virtual_machine"],
180+
accountid=self.account.name,
181+
domainid=self.account.domainid,
182+
serviceofferingid=self.service_offering_custom.id,
183+
customcpunumber = 1,
184+
customcpuspeed = 1000,
185+
custommemory = 512
186+
)
187+
188+
self.debug("Deployed VM 1 in account: %s, ID: %s" % (
189+
self.account.name,
190+
vm_1.id
191+
))
192+
193+
vm_2 = VirtualMachine.create(
194+
self.apiclient,
195+
self.services["virtual_machine"],
196+
accountid=self.account.name,
197+
domainid=self.account.domainid,
198+
serviceofferingid=self.service_offering_normal.id
199+
)
200+
self.debug("Deployed VM 2 in account: %s, ID: %s" % (
201+
self.account.name,
202+
vm_2.id
203+
))
204+
205+
resourceCountCpu = Resources.updateCount(
206+
self.apiclient,
207+
resourcetype=8,
208+
account=self.account.name,
209+
domainid=self.account.domainid
210+
)
211+
212+
self.debug("ResourceCount for CPU: %s" % (
213+
resourceCountCpu[0].resourcecount
214+
))
215+
self.assertEqual(
216+
resourceCountCpu[0].resourcecount,
217+
3,
218+
"The number of CPU cores does not seem to be right."
219+
)
220+
resourceCountMemory = Resources.updateCount(
221+
self.apiclient,
222+
resourcetype=9,
223+
account=self.account.name,
224+
domainid=self.account.domainid
225+
)
226+
227+
self.debug("ResourceCount for memory: %s" % (
228+
resourceCountMemory[0].resourcecount
229+
))
230+
self.assertEqual(
231+
resourceCountMemory[0].resourcecount,
232+
1024,
233+
"The memory amount does not seem to be right."
234+
)
235+
return

0 commit comments

Comments
 (0)