Skip to content

Commit 452b0e6

Browse files
committed
Add upgrade test
1 parent 1e91364 commit 452b0e6

File tree

2 files changed

+245
-2
lines changed

2 files changed

+245
-2
lines changed

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42100to42200.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void performDataMigration(Connection conn) {
5353
updateBackupScheduleOwnership(conn);
5454
}
5555

56-
private void updateSnapshotPolicyOwnership(Connection conn) {
56+
protected void updateSnapshotPolicyOwnership(Connection conn) {
5757
// set account_id and domain_id in snapshot_policy table from volume table
5858
String selectSql = "SELECT sp.id, v.account_id, v.domain_id FROM snapshot_policy sp, volumes v WHERE sp.volume_id = v.id AND (sp.account_id IS NULL AND sp.domain_id IS NULL)";
5959
String updateSql = "UPDATE snapshot_policy SET account_id = ?, domain_id = ? WHERE id = ?";
@@ -77,7 +77,7 @@ private void updateSnapshotPolicyOwnership(Connection conn) {
7777
}
7878
}
7979

80-
private void updateBackupScheduleOwnership(Connection conn) {
80+
protected void updateBackupScheduleOwnership(Connection conn) {
8181
// Set account_id and domain_id in backup_schedule table from vm_instance table
8282
String selectSql = "SELECT bs.id, vm.account_id, vm.domain_id FROM backup_schedule bs, vm_instance vm WHERE bs.vm_id = vm.id AND (bs.account_id IS NULL AND bs.domain_id IS NULL)";
8383
String updateSql = "UPDATE backup_schedule SET account_id = ?, domain_id = ? WHERE id = ?";
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
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+
package com.cloud.upgrade.dao;
18+
19+
import static org.mockito.ArgumentMatchers.anyString;
20+
import static org.mockito.ArgumentMatchers.eq;
21+
import static org.mockito.Mockito.verify;
22+
import static org.mockito.Mockito.when;
23+
import static org.mockito.Mockito.times;
24+
import static org.mockito.Mockito.inOrder;
25+
26+
import java.sql.Connection;
27+
import java.sql.PreparedStatement;
28+
import java.sql.ResultSet;
29+
import java.sql.SQLException;
30+
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.mockito.InOrder;
34+
import org.mockito.Mock;
35+
import org.mockito.Spy;
36+
import org.mockito.junit.MockitoJUnitRunner;
37+
38+
@RunWith(MockitoJUnitRunner.class)
39+
public class Upgrade42100to42200Test {
40+
41+
@Spy
42+
Upgrade42100to42200 upgrade;
43+
44+
@Mock
45+
private Connection conn;
46+
47+
@Mock
48+
private PreparedStatement selectStmt;
49+
50+
@Mock
51+
private PreparedStatement updateStmt;
52+
53+
@Mock
54+
private ResultSet resultSet;
55+
56+
@Test
57+
public void testUpdateSnapshotPolicyOwnership() throws SQLException {
58+
// Setup mock data for snapshot policies without ownership
59+
when(conn.prepareStatement("SELECT sp.id, v.account_id, v.domain_id FROM snapshot_policy sp, volumes v WHERE sp.volume_id = v.id AND (sp.account_id IS NULL AND sp.domain_id IS NULL)"))
60+
.thenReturn(selectStmt);
61+
when(conn.prepareStatement("UPDATE snapshot_policy SET account_id = ?, domain_id = ? WHERE id = ?"))
62+
.thenReturn(updateStmt);
63+
when(selectStmt.executeQuery()).thenReturn(resultSet);
64+
65+
when(resultSet.next())
66+
.thenReturn(true)
67+
.thenReturn(true)
68+
.thenReturn(false);
69+
70+
when(resultSet.getLong(1))
71+
.thenReturn(1L)
72+
.thenReturn(2L);
73+
when(resultSet.getLong(2))
74+
.thenReturn(100L)
75+
.thenReturn(200L);
76+
when(resultSet.getLong(3))
77+
.thenReturn(1L)
78+
.thenReturn(2L);
79+
80+
upgrade.updateSnapshotPolicyOwnership(conn);
81+
82+
verify(conn).prepareStatement("SELECT sp.id, v.account_id, v.domain_id FROM snapshot_policy sp, volumes v WHERE sp.volume_id = v.id AND (sp.account_id IS NULL AND sp.domain_id IS NULL)");
83+
verify(conn).prepareStatement("UPDATE snapshot_policy SET account_id = ?, domain_id = ? WHERE id = ?");
84+
85+
InOrder inOrder = inOrder(updateStmt);
86+
87+
inOrder.verify(updateStmt).setLong(1, 100L); // account_id
88+
inOrder.verify(updateStmt).setLong(2, 1L); // domain_id
89+
inOrder.verify(updateStmt).setLong(3, 1L); // policy_id
90+
inOrder.verify(updateStmt).executeUpdate();
91+
92+
inOrder.verify(updateStmt).setLong(1, 200L); // account_id
93+
inOrder.verify(updateStmt).setLong(2, 2L); // domain_id
94+
inOrder.verify(updateStmt).setLong(3, 2L); // policy_id
95+
inOrder.verify(updateStmt).executeUpdate();
96+
97+
verify(updateStmt, times(2)).executeUpdate();
98+
}
99+
100+
@Test
101+
public void testUpdateBackupScheduleOwnership() throws SQLException {
102+
when(conn.prepareStatement("SELECT bs.id, vm.account_id, vm.domain_id FROM backup_schedule bs, vm_instance vm WHERE bs.vm_id = vm.id AND (bs.account_id IS NULL AND bs.domain_id IS NULL)"))
103+
.thenReturn(selectStmt);
104+
when(conn.prepareStatement("UPDATE backup_schedule SET account_id = ?, domain_id = ? WHERE id = ?"))
105+
.thenReturn(updateStmt);
106+
when(selectStmt.executeQuery()).thenReturn(resultSet);
107+
108+
when(resultSet.next())
109+
.thenReturn(true)
110+
.thenReturn(true)
111+
.thenReturn(true)
112+
.thenReturn(false);
113+
114+
when(resultSet.getLong(1))
115+
.thenReturn(10L)
116+
.thenReturn(20L)
117+
.thenReturn(30L);
118+
when(resultSet.getLong(2))
119+
.thenReturn(500L)
120+
.thenReturn(600L)
121+
.thenReturn(700L);
122+
when(resultSet.getLong(3))
123+
.thenReturn(5L)
124+
.thenReturn(6L)
125+
.thenReturn(7L);
126+
127+
upgrade.updateBackupScheduleOwnership(conn);
128+
129+
verify(conn).prepareStatement("SELECT bs.id, vm.account_id, vm.domain_id FROM backup_schedule bs, vm_instance vm WHERE bs.vm_id = vm.id AND (bs.account_id IS NULL AND bs.domain_id IS NULL)");
130+
verify(conn).prepareStatement("UPDATE backup_schedule SET account_id = ?, domain_id = ? WHERE id = ?");
131+
132+
InOrder inOrder = inOrder(updateStmt);
133+
134+
inOrder.verify(updateStmt).setLong(1, 500L);
135+
inOrder.verify(updateStmt).setLong(2, 5L);
136+
inOrder.verify(updateStmt).setLong(3, 10L);
137+
inOrder.verify(updateStmt).executeUpdate();
138+
139+
inOrder.verify(updateStmt).setLong(1, 600L);
140+
inOrder.verify(updateStmt).setLong(2, 6L);
141+
inOrder.verify(updateStmt).setLong(3, 20L);
142+
inOrder.verify(updateStmt).executeUpdate();
143+
144+
inOrder.verify(updateStmt).setLong(1, 700L);
145+
inOrder.verify(updateStmt).setLong(2, 7L);
146+
inOrder.verify(updateStmt).setLong(3, 30L);
147+
inOrder.verify(updateStmt).executeUpdate();
148+
149+
verify(updateStmt, times(3)).executeUpdate();
150+
}
151+
152+
@Test
153+
public void testUpdateSnapshotPolicyOwnershipNoResults() throws SQLException {
154+
when(conn.prepareStatement("SELECT sp.id, v.account_id, v.domain_id FROM snapshot_policy sp, volumes v WHERE sp.volume_id = v.id AND (sp.account_id IS NULL AND sp.domain_id IS NULL)"))
155+
.thenReturn(selectStmt);
156+
when(conn.prepareStatement("UPDATE snapshot_policy SET account_id = ?, domain_id = ? WHERE id = ?"))
157+
.thenReturn(updateStmt);
158+
when(selectStmt.executeQuery()).thenReturn(resultSet);
159+
160+
when(resultSet.next()).thenReturn(false);
161+
162+
upgrade.updateSnapshotPolicyOwnership(conn);
163+
164+
verify(selectStmt).executeQuery();
165+
verify(updateStmt, times(0)).executeUpdate();
166+
}
167+
168+
@Test
169+
public void testUpdateBackupScheduleOwnershipNoResults() throws SQLException {
170+
when(conn.prepareStatement("SELECT bs.id, vm.account_id, vm.domain_id FROM backup_schedule bs, vm_instance vm WHERE bs.vm_id = vm.id AND (bs.account_id IS NULL AND bs.domain_id IS NULL)"))
171+
.thenReturn(selectStmt);
172+
when(conn.prepareStatement("UPDATE backup_schedule SET account_id = ?, domain_id = ? WHERE id = ?"))
173+
.thenReturn(updateStmt);
174+
when(selectStmt.executeQuery()).thenReturn(resultSet);
175+
176+
when(resultSet.next()).thenReturn(false);
177+
178+
upgrade.updateBackupScheduleOwnership(conn);
179+
180+
verify(selectStmt).executeQuery();
181+
verify(updateStmt, times(0)).executeUpdate();
182+
}
183+
184+
@Test
185+
public void testPerformDataMigration() throws SQLException {
186+
when(conn.prepareStatement(anyString())).thenReturn(selectStmt);
187+
when(selectStmt.executeQuery()).thenReturn(resultSet);
188+
when(resultSet.next()).thenReturn(false);
189+
190+
upgrade.performDataMigration(conn);
191+
192+
verify(conn).prepareStatement("SELECT sp.id, v.account_id, v.domain_id FROM snapshot_policy sp, volumes v WHERE sp.volume_id = v.id AND (sp.account_id IS NULL AND sp.domain_id IS NULL)");
193+
verify(conn).prepareStatement("SELECT bs.id, vm.account_id, vm.domain_id FROM backup_schedule bs, vm_instance vm WHERE bs.vm_id = vm.id AND (bs.account_id IS NULL AND bs.domain_id IS NULL)");
194+
}
195+
196+
@Test
197+
public void testUpdateSnapshotPolicyOwnershipSingleRecord() throws SQLException {
198+
when(conn.prepareStatement("SELECT sp.id, v.account_id, v.domain_id FROM snapshot_policy sp, volumes v WHERE sp.volume_id = v.id AND (sp.account_id IS NULL AND sp.domain_id IS NULL)"))
199+
.thenReturn(selectStmt);
200+
when(conn.prepareStatement("UPDATE snapshot_policy SET account_id = ?, domain_id = ? WHERE id = ?"))
201+
.thenReturn(updateStmt);
202+
when(selectStmt.executeQuery()).thenReturn(resultSet);
203+
204+
when(resultSet.next())
205+
.thenReturn(true)
206+
.thenReturn(false);
207+
208+
when(resultSet.getLong(1)).thenReturn(42L);
209+
when(resultSet.getLong(2)).thenReturn(999L);
210+
when(resultSet.getLong(3)).thenReturn(10L);
211+
212+
upgrade.updateSnapshotPolicyOwnership(conn);
213+
214+
verify(updateStmt).setLong(1, 999L);
215+
verify(updateStmt).setLong(2, 10L);
216+
verify(updateStmt).setLong(3, 42L);
217+
verify(updateStmt, times(1)).executeUpdate();
218+
}
219+
220+
@Test
221+
public void testUpdateBackupScheduleOwnershipSingleRecord() throws SQLException {
222+
when(conn.prepareStatement("SELECT bs.id, vm.account_id, vm.domain_id FROM backup_schedule bs, vm_instance vm WHERE bs.vm_id = vm.id AND (bs.account_id IS NULL AND bs.domain_id IS NULL)"))
223+
.thenReturn(selectStmt);
224+
when(conn.prepareStatement("UPDATE backup_schedule SET account_id = ?, domain_id = ? WHERE id = ?"))
225+
.thenReturn(updateStmt);
226+
when(selectStmt.executeQuery()).thenReturn(resultSet);
227+
228+
when(resultSet.next())
229+
.thenReturn(true)
230+
.thenReturn(false);
231+
232+
when(resultSet.getLong(1)).thenReturn(55L);
233+
when(resultSet.getLong(2)).thenReturn(888L);
234+
when(resultSet.getLong(3)).thenReturn(15L);
235+
236+
upgrade.updateBackupScheduleOwnership(conn);
237+
238+
verify(updateStmt).setLong(1, 888L);
239+
verify(updateStmt).setLong(2, 15L);
240+
verify(updateStmt).setLong(3, 55L);
241+
verify(updateStmt, times(1)).executeUpdate();
242+
}
243+
}

0 commit comments

Comments
 (0)