Skip to content

Commit fe267c2

Browse files
committed
Basic zone 221 to 222 DB upgrade. Unit test is included
1 parent 6c819c1 commit fe267c2

4 files changed

Lines changed: 2809 additions & 7 deletions

File tree

server/src/com/cloud/upgrade/dao/Upgrade217to22.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,18 @@ protected long insertNetwork(Connection conn, String name, String displayText, S
141141
}
142142
}
143143

144-
protected void upgradeUserIpAddress(Connection conn, long dcId, long networkId) throws SQLException {
145-
PreparedStatement pstmt = conn.prepareStatement("UPDATE user_ip_address INNER JOIN vlan ON user_ip_address.vlan_db_id=vlan.id SET source_network_id=? WHERE user_ip_address.data_center_id=? AND vlan.vlan_type='VirtualNetwork'");
144+
protected void upgradeUserIpAddress(Connection conn, long dcId, long networkId, String vlanType) throws SQLException {
145+
PreparedStatement pstmt = conn.prepareStatement("UPDATE user_ip_address INNER JOIN vlan ON user_ip_address.vlan_db_id=vlan.id SET source_network_id=? WHERE user_ip_address.data_center_id=? AND vlan.vlan_type=?");
146146
pstmt.setLong(1, networkId);
147147
pstmt.setLong(2, dcId);
148+
pstmt.setString(3, vlanType);
148149
pstmt.executeUpdate();
149150
pstmt.close();
150151

151-
pstmt = conn.prepareStatement("UPDATE vlan SET network_id = ? WHERE data_center_id=? AND vlan_type='VirtualNetwork'");
152+
pstmt = conn.prepareStatement("UPDATE vlan SET network_id = ? WHERE data_center_id=? AND vlan_type=?");
152153
pstmt.setLong(1, networkId);
153154
pstmt.setLong(2, dcId);
155+
pstmt.setString(3, vlanType);
154156
pstmt.executeUpdate();
155157
pstmt.close();
156158
}
@@ -186,6 +188,16 @@ protected void upgradeDataCenter(Connection conn) {
186188
pstmt.executeUpdate();
187189
pstmt.close();
188190

191+
//For basic zone vnet field should be NULL
192+
193+
if (_basicZone) {
194+
pstmt = conn.prepareStatement("UPDATE data_center SET vnet=?, guest_network_cidr=?");
195+
pstmt.setString(1, null);
196+
pstmt.setString(2, null);
197+
pstmt.executeUpdate();
198+
pstmt.close();
199+
}
200+
189201
pstmt = conn.prepareStatement("SELECT id, guest_network_cidr FROM data_center");
190202
rs = pstmt.executeQuery();
191203
ArrayList<Object[]> dcs = new ArrayList<Object[]>();
@@ -245,9 +257,32 @@ protected void upgradeDataCenter(Connection conn) {
245257
if (_basicZone) {
246258
for (Object[] dc : dcs) {
247259
Long dcId = (Long)dc[0];
248-
insertNetwork(conn, "BasicZoneDirectNetwork" + dcId, "Basic Zone Direct Network created for Zone " + dcId, "Guest", "Native", null, null, null, "Dhcp", 5, dcId, "DirectPodBasedNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null);
249-
}
260+
long basicDefaultDirectNetworkId = insertNetwork(conn, "BasicZoneDirectNetwork" + dcId, "Basic Zone Direct Network created for Zone " + dcId, "Guest", "Native", null, null, null, "Dhcp", 5, dcId, "DirectPodBasedNetworkGuru", "Setup", 1, 1, null, null, "Direct", true, null, true, null);
250261

262+
//update all public ips with the Default Direct network Id
263+
upgradeUserIpAddress(conn, dcId, basicDefaultDirectNetworkId, "DirectAttached");
264+
265+
//update Dhcp servers information in domain_router and vm_instance tables; all domRs belong to the same network
266+
pstmt = conn.prepareStatement("SELECT vm_instance.id, vm_instance.domain_id, vm_instance.account_id, domain_router.guest_ip_address, domain_router.domain, domain_router.dns1, domain_router.dns2, domain_router.vnet FROM vm_instance INNER JOIN domain_router ON vm_instance.id=domain_router.id WHERE vm_instance.removed IS NULL AND vm_instance.type='DomainRouter' AND vm_instance.data_center_id=?");
267+
pstmt.setLong(1, dcId);
268+
rs = pstmt.executeQuery();
269+
ArrayList<Object[]> routers = new ArrayList<Object[]>();
270+
while (rs.next()) {
271+
Object[] router = new Object[40];
272+
router[0] = rs.getLong(1); // router id
273+
routers.add(router);
274+
}
275+
rs.close();
276+
pstmt.close();
277+
278+
for (Object[] router : routers) {
279+
pstmt = conn.prepareStatement("UPDATE domain_router SET network_id = ? wHERE id = ? ");
280+
pstmt.setLong(1, basicDefaultDirectNetworkId);
281+
pstmt.setLong(2, (Long)router[0]);
282+
pstmt.executeUpdate();
283+
pstmt.close();
284+
}
285+
}
251286
} else {
252287
for (Object[] dc : dcs) {
253288
Long dcId = (Long)dc[0];
@@ -289,7 +324,7 @@ protected void upgradeDataCenter(Connection conn) {
289324
pstmt.close();
290325
}
291326

292-
upgradeUserIpAddress(conn, dcId, publicNetworkId);
327+
upgradeUserIpAddress(conn, dcId, publicNetworkId, "VirtualNetwork");
293328
}
294329

295330
}
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
/**
2+
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
3+
*
4+
* This software is licensed under the GNU General Public License v3 or later.
5+
*
6+
* It is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or any later version.
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
package com.cloud.upgrade.dao;
19+
20+
21+
import java.sql.Connection;
22+
import java.sql.PreparedStatement;
23+
import java.sql.ResultSet;
24+
import java.sql.SQLException;
25+
26+
import javax.naming.ConfigurationException;
27+
28+
import junit.framework.TestCase;
29+
30+
import org.apache.log4j.Logger;
31+
import org.junit.After;
32+
import org.junit.Before;
33+
34+
import com.cloud.upgrade.dao.VersionVO.Step;
35+
import com.cloud.utils.component.ComponentLocator;
36+
import com.cloud.utils.db.DbTestUtils;
37+
import com.cloud.utils.db.Transaction;
38+
import com.cloud.utils.exception.CloudRuntimeException;
39+
40+
public class BasicZone217To221UpgradeTest extends TestCase {
41+
private static final Logger s_logger = Logger.getLogger(BasicZone217To221UpgradeTest.class);
42+
43+
@Override
44+
@Before
45+
public void setUp() throws Exception {
46+
VersionVO version = new VersionVO("2.1.7");
47+
version.setStep(Step.Cleanup);
48+
DbTestUtils.executeScript("VersionDaoImplTest/clean-db.sql", false, true);
49+
}
50+
51+
@Override
52+
@After
53+
public void tearDown() throws Exception {
54+
}
55+
56+
public void test217to22Upgrade() {
57+
s_logger.debug("Finding sample data from 2.1.7");
58+
DbTestUtils.executeScript("VersionDaoImplTest/2.1.7/2.1.7_sample_basicZone_noSecurityGroups.sql", false, true);
59+
60+
Connection conn = Transaction.getStandaloneConnection();
61+
PreparedStatement pstmt;
62+
63+
VersionDaoImpl dao = ComponentLocator.inject(VersionDaoImpl.class);
64+
65+
String version = dao.getCurrentVersion();
66+
67+
if (!version.equals("2.1.7")) {
68+
s_logger.error("Version returned is not 2.1.7 but " + version);
69+
} else {
70+
s_logger.debug("Basic zone test version is " + version);
71+
}
72+
73+
try {
74+
dao.upgrade("2.1.7", "2.2.1");
75+
} catch (ConfigurationException e) {
76+
s_logger.warn("Exception: ", e);
77+
assert false : "The test failed. Check logs";
78+
}
79+
80+
conn = Transaction.getStandaloneConnection();
81+
try {
82+
83+
s_logger.debug("Starting tesing upgrade from 2.1.7 to 2.2.2 for Basic zone...");
84+
85+
//Version check
86+
pstmt = conn.prepareStatement("SELECT version FROM version");
87+
ResultSet rs = pstmt.executeQuery();
88+
89+
if (!rs.next()) {
90+
s_logger.error("ERROR: No version selected");
91+
} else if (!rs.getString(1).equals("2.2.1")) {
92+
s_logger.error("ERROR: VERSION stored is not 2.2.1: " + rs.getString(1));
93+
}
94+
rs.close();
95+
pstmt.close();
96+
97+
//Check that default network offerings are present
98+
pstmt = conn.prepareStatement("SELECT COUNT(*) FROM network_offerings");
99+
rs = pstmt.executeQuery();
100+
101+
if (!rs.next()) {
102+
s_logger.error("ERROR: Unable to get the count of network offerings.");
103+
} else if (rs.getInt(1) != 7) {
104+
s_logger.error("ERROR: Didn't find 7 network offerings but found " + rs.getInt(1));
105+
} else {
106+
s_logger.debug("Network offerings test passed");
107+
}
108+
109+
rs.close();
110+
pstmt.close();
111+
112+
113+
//Zone network type check
114+
pstmt = conn.prepareStatement("SELECT DISTINCT networktype FROM data_center");
115+
rs = pstmt.executeQuery();
116+
117+
if (!rs.next()) {
118+
s_logger.error("No zone exists after upgrade");
119+
} else if (!rs.getString(1).equals("Basic")) {
120+
s_logger.error("ERROR: Zone type is not Basic");
121+
} else if (rs.next()) {
122+
s_logger.error("ERROR: Why do we have more than 1 zone with different types??");
123+
System.exit(2);
124+
} else {
125+
s_logger.debug("Test passed. Zone was updated properly with type Basic");
126+
}
127+
rs.close();
128+
pstmt.close();
129+
130+
//Check that vnet/cidr were set to NULL for basic zone
131+
pstmt = conn.prepareStatement("SELECT vnet, guest_network_cidr FROM data_center");
132+
rs = pstmt.executeQuery();
133+
134+
if (!rs.next()) {
135+
s_logger.error("ERROR: vnet field is missing for the zone");
136+
} else if (rs.getString(1) != null || rs.getString(2) != null) {
137+
s_logger.error("ERROR: vnet/guestCidr should be NULL for basic zone; instead it's " + rs.getString(1));
138+
} else {
139+
s_logger.debug("Test passed. Vnet and cidr are set to NULL for the basic zone");
140+
}
141+
142+
rs.close();
143+
pstmt.close();
144+
145+
//Verify that default Direct guest network got created, and it's Shared and Default
146+
pstmt = conn.prepareStatement("SELECT traffic_type, guest_type, shared, is_default, id FROM networks WHERE name LIKE '%BasicZoneDirectNetwork%'");
147+
rs = pstmt.executeQuery();
148+
149+
if (!rs.next()) {
150+
s_logger.error("Direct network is missing for the Basic zone");
151+
} else if (!rs.getString(1).equalsIgnoreCase("Guest") || !rs.getString(2).equalsIgnoreCase("Direct") || !rs.getBoolean(3) || !rs.getBoolean(4)) {
152+
s_logger.error("Direct network for basic zone has incorrect setting");
153+
} else {
154+
s_logger.debug("Test passed. Default Direct Basic zone network parameters were set correctly");
155+
}
156+
157+
long defaultDirectNetworkId = rs.getInt(5);
158+
rs.close();
159+
pstmt.close();
160+
161+
//Verify that all vlans in the zone belong to default Direct network
162+
pstmt = conn.prepareStatement("SELECT network_id FROM vlan");
163+
rs = pstmt.executeQuery();
164+
165+
while (rs.next()) {
166+
if (rs.getInt(1) != defaultDirectNetworkId) {
167+
s_logger.error("ERROR: network_id is set incorrectly for public untagged vlans in Basic zone");
168+
System.exit(2);
169+
}
170+
}
171+
172+
s_logger.debug("Test passed for vlan table in Basic zone");
173+
174+
rs.close();
175+
pstmt.close();
176+
177+
//Verify user_ip_address table
178+
pstmt = conn.prepareStatement("SELECT source_network_id FROM user_ip_address");
179+
rs = pstmt.executeQuery();
180+
181+
while (rs.next()) {
182+
if (rs.getInt(1) != defaultDirectNetworkId) {
183+
s_logger.error("ERROR: network_id is set incorrectly for public Ip addresses (user_ip_address table) in Basic zone");
184+
System.exit(2);
185+
}
186+
}
187+
188+
s_logger.debug("Test passed for user_ip_address table in Basic zone");
189+
190+
rs.close();
191+
pstmt.close();
192+
193+
//Verify domain_router table
194+
pstmt = conn.prepareStatement("SELECT network_id FROM domain_router");
195+
rs = pstmt.executeQuery();
196+
197+
while (rs.next()) {
198+
if (rs.getInt(1) != defaultDirectNetworkId) {
199+
s_logger.error("ERROR: network_id is set incorrectly for domain routers (domain_router table) in Basic zone");
200+
System.exit(2);
201+
}
202+
}
203+
204+
s_logger.debug("Test passed for domain_router table in Basic zone");
205+
206+
rs.close();
207+
pstmt.close();
208+
209+
s_logger.debug("Basic zone test is finished");
210+
211+
} catch (SQLException e) {
212+
throw new CloudRuntimeException("Problem checking upgrade version", e);
213+
} finally {
214+
try {
215+
conn.close();
216+
} catch (SQLException e) {
217+
}
218+
}
219+
}
220+
221+
}

0 commit comments

Comments
 (0)