Skip to content

Commit 7ca8d00

Browse files
authored
Merge pull request #2106 from Accelerite/verify-netmask
CLOUDSTACK-9168: TestPath to check if wrong value is inserted into nics table netmask field when creating a VM.
2 parents 3870107 + 6e406a6 commit 7ca8d00

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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 to Check Snapshots size in database
18+
"""
19+
20+
21+
from nose.plugins.attrib import attr
22+
from marvin.cloudstackTestCase import cloudstackTestCase
23+
from marvin.lib.utils import (cleanup_resources,
24+
)
25+
from marvin.lib.base import (Account,
26+
Network,
27+
NetworkOffering,
28+
ServiceOffering,
29+
VirtualMachine,
30+
)
31+
from marvin.lib.common import (get_domain,
32+
get_zone,
33+
get_template,
34+
list_networks)
35+
36+
37+
def ipv4_cidr_to_netmask(bits):
38+
""" Convert CIDR bits to netmask """
39+
netmask = ''
40+
for i in range(4):
41+
if i:
42+
netmask += '.'
43+
if bits >= 8:
44+
netmask += '%d' % (2 ** 8 - 1)
45+
bits -= 8
46+
else:
47+
netmask += '%d' % (256 - 2 ** (8 - bits))
48+
bits = 0
49+
return netmask
50+
51+
52+
class TestCheckNetmask(cloudstackTestCase):
53+
54+
@classmethod
55+
def setUpClass(cls):
56+
testClient = super(TestCheckNetmask, cls).getClsTestClient()
57+
cls.apiclient = testClient.getApiClient()
58+
cls.testdata = testClient.getParsedTestDataConfig()
59+
cls.hypervisor = cls.testClient.getHypervisorInfo()
60+
61+
# Get Zone, Domain and templates
62+
cls.domain = get_domain(cls.apiclient)
63+
cls.zone = get_zone(cls.apiclient, testClient.getZoneForTests())
64+
65+
cls.template = get_template(
66+
cls.apiclient,
67+
cls.zone.id,
68+
cls.testdata["ostype"])
69+
70+
cls._cleanup = []
71+
72+
cls.skiptest = False
73+
74+
if cls.hypervisor.lower() not in ["xenserver"]:
75+
cls.skiptest = True
76+
return
77+
78+
try:
79+
80+
# Create an account
81+
cls.account = Account.create(
82+
cls.apiclient,
83+
cls.testdata["account"],
84+
domainid=cls.domain.id
85+
)
86+
87+
# Create Service offering
88+
cls.service_offering = ServiceOffering.create(
89+
cls.apiclient,
90+
cls.testdata["service_offering"],
91+
)
92+
93+
cls.testdata["shared_network_offering"]["specifyVlan"] = 'True'
94+
cls.testdata["shared_network_offering"]["specifyIpRanges"] = 'True'
95+
96+
cls.shared_network_offering = NetworkOffering.create(
97+
cls.apiclient,
98+
cls.testdata["shared_network_offering"]
99+
)
100+
101+
NetworkOffering.update(
102+
cls.shared_network_offering,
103+
cls.apiclient,
104+
id=cls.shared_network_offering.id,
105+
state="enabled"
106+
)
107+
108+
cls.network = Network.create(
109+
cls.apiclient,
110+
cls.testdata["network2"],
111+
networkofferingid=cls.shared_network_offering.id,
112+
zoneid=cls.zone.id,
113+
accountid=cls.account.name,
114+
domainid=cls.account.domainid
115+
)
116+
117+
cls.vm = VirtualMachine.create(
118+
cls.apiclient,
119+
cls.testdata["small"],
120+
templateid=cls.template.id,
121+
accountid=cls.account.name,
122+
domainid=cls.account.domainid,
123+
serviceofferingid=cls.service_offering.id,
124+
zoneid=cls.zone.id,
125+
networkids=cls.network.id,
126+
)
127+
128+
cls._cleanup.extend([cls.account,
129+
cls.service_offering,
130+
cls.shared_network_offering])
131+
132+
except Exception as e:
133+
cls.tearDownClass()
134+
raise e
135+
return
136+
137+
@classmethod
138+
def tearDownClass(cls):
139+
try:
140+
cleanup_resources(cls.apiclient, cls._cleanup)
141+
except Exception as e:
142+
raise Exception("Warning: Exception during cleanup : %s" % e)
143+
144+
def setUp(self):
145+
if self.skiptest:
146+
self.skipTest(
147+
"Test not to be run on %s" %
148+
self.hypervisor)
149+
self.dbclient = self.testClient.getDbConnection()
150+
self.cleanup = []
151+
152+
def tearDown(self):
153+
try:
154+
cleanup_resources(self.apiclient, self.cleanup)
155+
except Exception as e:
156+
raise Exception("Warning: Exception during cleanup : %s" % e)
157+
return
158+
159+
@attr(tags=["advanced"], required_hardware="false")
160+
def test_01_netmask_value_check(self):
161+
""" Check Netmask value in database
162+
1. Check if netmask attribute in nics table
163+
stores correct value.
164+
"""
165+
166+
# Step 1
167+
# Get the netmask from ipv4 address of the VM
168+
qryresult_netmask = self.dbclient.execute(
169+
" select id, uuid, netmask\
170+
from nics where ip4_address='%s';" %
171+
self.vm.nic[0].ipaddress)
172+
173+
self.assertNotEqual(
174+
len(qryresult_netmask),
175+
0,
176+
"Check if netmask attribute in nics table \
177+
stores correct value")
178+
179+
# Step 2
180+
netmask_id = qryresult_netmask[0][2]
181+
netlist = list_networks(self.apiclient,
182+
account=self.account.name,
183+
domainid=self.account.domainid)
184+
185+
self.assertNotEqual(len(netlist), 0,
186+
"Check if list networks returned an empty list.")
187+
188+
cidr = netlist[0].cidr.split("/")[1]
189+
# Get netmask from CIDR
190+
netmask = ipv4_cidr_to_netmask(int(cidr))
191+
192+
# Validate the netmask
193+
self.assertEqual(netmask_id,
194+
netmask,
195+
"Check if the netmask is from guest CIDR")
196+
return

0 commit comments

Comments
 (0)