Skip to content

Commit f8f71a5

Browse files
SudhansuRohit Yadav
authored andcommitted
CLOUDSTACK-9694: Unable to limit the Public IPs in VPC
Added missing clause to check for vpc_id (cherry picked from commit cdf2b07) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent 75c81d9 commit f8f71a5

2 files changed

Lines changed: 353 additions & 2 deletions

File tree

engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ public void init() {
135135
AllocatedIpCountForAccount.select(null, Func.COUNT, AllocatedIpCountForAccount.entity().getAddress());
136136
AllocatedIpCountForAccount.and("account", AllocatedIpCountForAccount.entity().getAllocatedToAccountId(), Op.EQ);
137137
AllocatedIpCountForAccount.and("allocated", AllocatedIpCountForAccount.entity().getAllocatedTime(), Op.NNULL);
138-
AllocatedIpCountForAccount.and("network", AllocatedIpCountForAccount.entity().getAssociatedWithNetworkId(), Op.NNULL);
139-
AllocatedIpCountForAccount.done();
138+
AllocatedIpCountForAccount.and().op("network", AllocatedIpCountForAccount.entity().getAssociatedWithNetworkId(), Op.NNULL);
139+
AllocatedIpCountForAccount.or("vpc", AllocatedIpCountForAccount.entity().getVpcId(), Op.NNULL);
140+
AllocatedIpCountForAccount.cp();AllocatedIpCountForAccount.done();
140141

141142
CountFreePublicIps = createSearchBuilder(Long.class);
142143
CountFreePublicIps.select(null, Func.COUNT, null);
Lines changed: 350 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,350 @@
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+
""" Component tests VM deployment in VPC network functionality
19+
"""
20+
#Import Local Modules
21+
from nose.plugins.attrib import attr
22+
from marvin.cloudstackTestCase import cloudstackTestCase, unittest
23+
from marvin.lib.base import (VirtualMachine,
24+
NetworkOffering,
25+
VpcOffering,
26+
VPC,
27+
NetworkACL,
28+
PrivateGateway,
29+
StaticRoute,
30+
Router,
31+
Network,
32+
Account,
33+
ServiceOffering,
34+
PublicIPAddress,
35+
NATRule,
36+
StaticNATRule,
37+
Configurations)
38+
39+
from marvin.lib.common import (get_domain,
40+
get_zone,
41+
get_template,
42+
wait_for_cleanup,
43+
get_free_vlan)
44+
45+
from marvin.lib.utils import (cleanup_resources, validateList)
46+
from marvin.codes import *
47+
from marvin.cloudstackAPI import rebootRouter
48+
from marvin.cloudstackAPI import updateResourceCount
49+
50+
class Services:
51+
"""Test IP count inn VPC network
52+
"""
53+
54+
def __init__(self):
55+
self.services = {
56+
"account": {
57+
"email": "test@test.com",
58+
"firstname": "Test",
59+
"lastname": "User",
60+
"username": "test",
61+
# Random characters are appended for unique
62+
# username
63+
"password": "password",
64+
},
65+
"service_offering": {
66+
"name": "Tiny Instance",
67+
"displaytext": "Tiny Instance",
68+
"cpunumber": 1,
69+
"cpuspeed": 100,
70+
"memory": 128,
71+
},
72+
"network_offering": {
73+
"name": 'VPC Network offering',
74+
"displaytext": 'VPC Network off',
75+
"guestiptype": 'Isolated',
76+
"supportedservices": 'Dhcp,Dns,SourceNat,PortForwarding,Lb,UserData,StaticNat,NetworkACL',
77+
"traffictype": 'GUEST',
78+
"availability": 'Optional',
79+
"useVpc": 'on',
80+
"serviceProviderList": {
81+
"Dhcp": 'VpcVirtualRouter',
82+
"Dns": 'VpcVirtualRouter',
83+
"SourceNat": 'VpcVirtualRouter',
84+
"PortForwarding": 'VpcVirtualRouter',
85+
"Lb": 'VpcVirtualRouter',
86+
"UserData": 'VpcVirtualRouter',
87+
"StaticNat": 'VpcVirtualRouter',
88+
"NetworkACL": 'VpcVirtualRouter'
89+
},
90+
},
91+
"network_offering_no_lb": {
92+
"name": 'VPC Network offering',
93+
"displaytext": 'VPC Network off',
94+
"guestiptype": 'Isolated',
95+
"supportedservices": 'Dhcp,Dns,SourceNat,PortForwarding,UserData,StaticNat,NetworkACL',
96+
"traffictype": 'GUEST',
97+
"availability": 'Optional',
98+
"useVpc": 'on',
99+
"serviceProviderList": {
100+
"Dhcp": 'VpcVirtualRouter',
101+
"Dns": 'VpcVirtualRouter',
102+
"SourceNat": 'VpcVirtualRouter',
103+
"PortForwarding": 'VpcVirtualRouter',
104+
"UserData": 'VpcVirtualRouter',
105+
"StaticNat": 'VpcVirtualRouter',
106+
"NetworkACL": 'VpcVirtualRouter'
107+
},
108+
},
109+
"vpc_offering": {
110+
"name": 'VPC off',
111+
"displaytext": 'VPC off',
112+
"supportedservices": 'Dhcp,Dns,SourceNat,PortForwarding,Lb,UserData,StaticNat',
113+
},
114+
"vpc": {
115+
"name": "TestVPC",
116+
"displaytext": "TestVPC",
117+
"cidr": '10.0.0.1/24'
118+
},
119+
"network": {
120+
"name": "Test Network",
121+
"displaytext": "Test Network",
122+
"netmask": '255.255.255.0',
123+
"limit": 5,
124+
# Max networks allowed as per hypervisor
125+
# Xenserver -> 5, VMWare -> 9
126+
},
127+
128+
"virtual_machine": {
129+
"displayname": "Test VM",
130+
"username": "root",
131+
"password": "password",
132+
"ssh_port": 22,
133+
"hypervisor": 'XenServer',
134+
# Hypervisor type should be same as
135+
# hypervisor type of cluster
136+
"privateport": 22,
137+
"publicport": 22,
138+
"protocol": 'TCP',
139+
},
140+
"ostype": 'CentOS 5.3 (64-bit)',
141+
# Cent OS 5.3 (64 bit)
142+
"timeout": 10,
143+
"mode": 'advanced'
144+
}
145+
146+
147+
class TestIPResourceCountVPC(cloudstackTestCase):
148+
149+
@classmethod
150+
def setUpClass(cls):
151+
cls.testClient = super(TestIPResourceCountVPC, cls).getClsTestClient()
152+
cls.api_client = cls.testClient.getApiClient()
153+
154+
cls.services = Services().services
155+
# Get Zone, Domain and templates
156+
cls.domain = get_domain(cls.api_client)
157+
cls.zone = get_zone(cls.api_client, cls.testClient.getZoneForTests())
158+
cls.template = get_template(
159+
cls.api_client,
160+
cls.zone.id,
161+
cls.services["ostype"]
162+
)
163+
cls.services["virtual_machine"]["zoneid"] = cls.zone.id
164+
cls.services["virtual_machine"]["template"] = cls.template.id
165+
166+
cls.service_offering = ServiceOffering.create(
167+
cls.api_client,
168+
cls.services["service_offering"]
169+
)
170+
cls.vpc_off = VpcOffering.create(
171+
cls.api_client,
172+
cls.services["vpc_offering"]
173+
)
174+
cls.vpc_off.update(cls.api_client, state='Enabled')
175+
cls._cleanup = [
176+
cls.service_offering,
177+
cls.vpc_off
178+
]
179+
return
180+
181+
@classmethod
182+
def tearDownClass(cls):
183+
try:
184+
#Cleanup resources used
185+
cleanup_resources(cls.api_client, cls._cleanup)
186+
except Exception as e:
187+
raise Exception("Warning: Exception during cleanup : %s" % e)
188+
return
189+
190+
def setUp(self):
191+
self.apiclient = self.testClient.getApiClient()
192+
self.dbclient = self.testClient.getDbConnection()
193+
self.account = Account.create(
194+
self.apiclient,
195+
self.services["account"],
196+
admin=True,
197+
domainid=self.domain.id
198+
)
199+
self.cleanup = [self.account]
200+
return
201+
202+
def tearDown(self):
203+
try:
204+
#Clean up, terminate the created network offerings
205+
cleanup_resources(self.apiclient, self.cleanup)
206+
except Exception as e:
207+
raise Exception("Warning: Exception during cleanup : %s" % e)
208+
return
209+
210+
def validate_vpc_offering(self, vpc_offering):
211+
"""Validates the VPC offering"""
212+
213+
self.debug("Check if the VPC offering is created successfully?")
214+
vpc_offs = VpcOffering.list(
215+
self.apiclient,
216+
id=vpc_offering.id
217+
)
218+
self.assertEqual(
219+
isinstance(vpc_offs, list),
220+
True,
221+
"List VPC offerings should return a valid list"
222+
)
223+
self.assertEqual(
224+
vpc_offering.name,
225+
vpc_offs[0].name,
226+
"Name of the VPC offering should match with listVPCOff data"
227+
)
228+
self.debug(
229+
"VPC offering is created successfully - %s" %
230+
vpc_offering.name)
231+
return
232+
233+
def validate_vpc_network(self, network, state=None):
234+
"""Validates the VPC network"""
235+
236+
self.debug("Check if the VPC network is created successfully?")
237+
vpc_networks = VPC.list(
238+
self.apiclient,
239+
id=network.id
240+
)
241+
self.assertEqual(
242+
isinstance(vpc_networks, list),
243+
True,
244+
"List VPC network should return a valid list"
245+
)
246+
self.assertEqual(
247+
network.name,
248+
vpc_networks[0].name,
249+
"Name of the VPC network should match with listVPC data"
250+
)
251+
if state:
252+
self.assertEqual(
253+
vpc_networks[0].state,
254+
state,
255+
"VPC state should be '%s'" % state
256+
)
257+
self.debug("VPC network validated - %s" % network.name)
258+
return
259+
260+
261+
def updateIPCount(self):
262+
cmd=updateResourceCount.updateResourceCountCmd()
263+
cmd.account=self.account.name
264+
cmd.domainid=self.domain.id
265+
266+
responce=self.apiclient.updateResourceCount(cmd)
267+
268+
def acquire_publicip(self, network, vpc):
269+
self.debug("Associating public IP for network: %s" % network.name)
270+
public_ip = PublicIPAddress.create(self.apiclient,
271+
accountid=self.account.name,
272+
zoneid=self.zone.id,
273+
domainid=self.account.domainid,
274+
networkid=network.id,
275+
vpcid=vpc.id
276+
)
277+
self.debug("Associated {} with network {}".format(public_ip.ipaddress.ipaddress, network.id))
278+
return public_ip
279+
280+
@attr(tags=["advanced", "intervlan"], required_hardware="false")
281+
def test_01_ip_resouce_count_vpc_network(self):
282+
""" Test IP count in VPC networks
283+
"""
284+
self.debug("Creating a VPC offering..")
285+
vpc_off = VpcOffering.create(
286+
self.apiclient,
287+
self.services["vpc_offering"]
288+
)
289+
290+
self.validate_vpc_offering(vpc_off)
291+
292+
self.debug("Enabling the VPC offering created")
293+
vpc_off.update(self.apiclient, state='Enabled')
294+
295+
self.debug("creating a VPC network in the account: %s" %
296+
self.account.name)
297+
self.services["vpc"]["cidr"] = '10.1.1.1/16'
298+
vpc = VPC.create(
299+
self.apiclient,
300+
self.services["vpc"],
301+
vpcofferingid=vpc_off.id,
302+
zoneid=self.zone.id,
303+
account=self.account.name,
304+
domainid=self.account.domainid
305+
)
306+
self.validate_vpc_network(vpc)
307+
308+
309+
nw_off = NetworkOffering.create(
310+
self.apiclient,
311+
self.services["network_offering"],
312+
conservemode=False
313+
)
314+
# Enable Network offering
315+
nw_off.update(self.apiclient, state='Enabled')
316+
self._cleanup.append(nw_off)
317+
318+
# Creating network using the network offering created
319+
self.debug("Creating network with network offering: %s" % nw_off.id)
320+
network_1 = Network.create(
321+
self.apiclient,
322+
self.services["network"],
323+
accountid=self.account.name,
324+
domainid=self.account.domainid,
325+
networkofferingid=nw_off.id,
326+
zoneid=self.zone.id,
327+
gateway='10.1.1.1',
328+
vpcid=vpc.id
329+
)
330+
self.debug("Created network with ID: %s" % network_1.id)
331+
332+
account_list = Account.list(self.apiclient, id=self.account.id)
333+
totalip_1 = account_list[0].iptotal
334+
self.debug("Total IP: %s" % totalip_1)
335+
336+
public_ip_1 = self.acquire_publicip(network_1, vpc)
337+
public_ip_2 = self.acquire_publicip(network_1, vpc)
338+
public_ip_3 = self.acquire_publicip(network_1, vpc)
339+
340+
account_list = Account.list(self.apiclient, id=self.account.id)
341+
totalip = account_list[0].iptotal
342+
343+
self.debug("Total IP: %s" % totalip)
344+
345+
self.assertTrue(totalip - totalip_1 == 3,"publicip count is 3")
346+
self.updateIPCount()
347+
348+
account_list = Account.list(self.apiclient, id=self.account.id)
349+
totalip = account_list[0].iptotal
350+
self.assertTrue(totalip - totalip_1 == 3, "publicip count is 3")

0 commit comments

Comments
 (0)