Skip to content

Commit a1e81a5

Browse files
Gaurav Aradhyeyadvr
authored andcommitted
CLOUDSTACK-8314: Add test case to validate VM.DESTROY event is logged when VM deployment fails
This closes #110 Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent c0c38f2 commit a1e81a5

1 file changed

Lines changed: 190 additions & 0 deletions

File tree

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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 cases for checking usage events
18+
"""
19+
from nose.plugins.attrib import attr
20+
from marvin.cloudstackTestCase import cloudstackTestCase
21+
from marvin.lib.utils import (cleanup_resources, validateList)
22+
from marvin.lib.base import (Account,
23+
ServiceOffering,
24+
VirtualMachine)
25+
from marvin.lib.common import (get_domain,
26+
get_zone,
27+
get_template)
28+
from marvin.codes import (PASS)
29+
30+
class TestUsageEvents(cloudstackTestCase):
31+
32+
@classmethod
33+
def setUpClass(cls):
34+
testClient = super(TestUsageEvents, cls).getClsTestClient()
35+
cls.apiclient = testClient.getApiClient()
36+
cls.testdata = testClient.getParsedTestDataConfig()
37+
38+
# Get Zone, Domain and templates
39+
cls.domain = get_domain(cls.apiclient)
40+
cls.zone = get_zone(cls.apiclient, testClient.getZoneForTests())
41+
42+
cls.template = get_template(
43+
cls.apiclient,
44+
cls.zone.id,
45+
cls.testdata["ostype"])
46+
cls._cleanup = []
47+
48+
try:
49+
# Create large service offering so that VM creation fails
50+
cls.testdata["service_offering"]["cpunumber"] = "8"
51+
cls.testdata["service_offering"]["cpuspeed"] = "8096"
52+
cls.testdata["service_offering"]["memory"] = "8096"
53+
54+
cls.service_offering = ServiceOffering.create(
55+
cls.apiclient,
56+
cls.testdata["service_offering"]
57+
)
58+
cls._cleanup.append(cls.service_offering)
59+
except Exception as e:
60+
cls.tearDownClass()
61+
raise e
62+
return
63+
64+
@classmethod
65+
def tearDownClass(cls):
66+
try:
67+
cleanup_resources(cls.apiclient, cls._cleanup)
68+
except Exception as e:
69+
raise Exception("Warning: Exception during cleanup : %s" % e)
70+
71+
def setUp(self):
72+
self.apiclient = self.testClient.getApiClient()
73+
self.dbclient = self.testClient.getDbConnection()
74+
self.cleanup = []
75+
# Create an account
76+
self.account = Account.create(
77+
self.apiclient,
78+
self.testdata["account"],
79+
domainid=self.domain.id
80+
)
81+
self.cleanup.append(self.account)
82+
83+
def tearDown(self):
84+
try:
85+
cleanup_resources(self.apiclient, self.cleanup)
86+
except Exception as e:
87+
raise Exception("Warning: Exception during cleanup : %s" % e)
88+
return
89+
90+
@attr(tags=["advanced, basic"], required_hardware="true")
91+
def test_01_positive_tests_usage(self):
92+
""" Check events in usage_events table when VM creation fails
93+
94+
Steps:
95+
1. Create service offering with large resource numbers
96+
2. Try to deploy a VM
97+
3. VM creation should fail and VM should be in error state
98+
4. Destroy the VM with expunge parameter True
99+
5. Check the events for the account in usage_events table
100+
6. There should be VM.CREATE, VM.DESTROY, VOLUME.CREATE and
101+
VOLUME.DELETE events present in the table
102+
"""
103+
# Create VM in account
104+
with self.assertRaises(Exception):
105+
VirtualMachine.create(
106+
self.apiclient,
107+
self.testdata["small"],
108+
templateid=self.template.id,
109+
accountid=self.account.name,
110+
domainid=self.account.domainid,
111+
serviceofferingid=self.service_offering.id,
112+
zoneid=self.zone.id
113+
)
114+
115+
vms = VirtualMachine.list(self.apiclient,
116+
account=self.account.name,
117+
domaind=self.account.domainid)
118+
119+
self.assertEqual(validateList(vms)[0], PASS,
120+
"Vm list validation failed")
121+
122+
self.assertEqual(vms[0].state.lower(), "error",
123+
"VM should be in error state")
124+
125+
qresultset = self.dbclient.execute(
126+
"select id from account where uuid = '%s';"
127+
% self.account.id
128+
)
129+
self.assertEqual(
130+
isinstance(qresultset, list),
131+
True,
132+
"Check DB query result set for valid data"
133+
)
134+
135+
self.assertNotEqual(
136+
len(qresultset),
137+
0,
138+
"Check DB Query result set"
139+
)
140+
qresult = qresultset[0]
141+
142+
account_id = qresult[0]
143+
self.debug("select type from usage_event where account_id = '%s';"
144+
% account_id)
145+
146+
qresultset = self.dbclient.execute(
147+
"select type from usage_event where account_id = '%s';"
148+
% account_id
149+
)
150+
self.assertEqual(
151+
isinstance(qresultset, list),
152+
True,
153+
"Check DB query result set for valid data"
154+
)
155+
156+
self.assertNotEqual(
157+
len(qresultset),
158+
0,
159+
"Check DB Query result set"
160+
)
161+
qresult = str(qresultset)
162+
self.debug("Query result: %s" % qresult)
163+
164+
# Check if VM.CREATE, VM.DESTROY events present in usage_event table
165+
self.assertEqual(
166+
qresult.count('VM.CREATE'),
167+
1,
168+
"Check VM.CREATE event in events table"
169+
)
170+
171+
self.assertEqual(
172+
qresult.count('VM.DESTROY'),
173+
1,
174+
"Check VM.DESTROY in list events"
175+
)
176+
177+
# Check if VOLUME.CREATE, VOLUME.DELETE events present in usage_event
178+
# table
179+
self.assertEqual(
180+
qresult.count('VOLUME.CREATE'),
181+
1,
182+
"Check VOLUME.CREATE in events table"
183+
)
184+
185+
self.assertEqual(
186+
qresult.count('VOLUME.DELETE'),
187+
1,
188+
"Check VM.DELETE in events table"
189+
)
190+
return

0 commit comments

Comments
 (0)