forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVpcApiUnitTest.java
More file actions
269 lines (246 loc) · 9.99 KB
/
VpcApiUnitTest.java
File metadata and controls
269 lines (246 loc) · 9.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.vpc;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import junit.framework.TestCase;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.cloud.configuration.dao.ConfigurationDaoImpl;
import com.cloud.configuration.dao.ResourceCountDaoImpl;
import com.cloud.dc.dao.VlanDaoImpl;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.network.Network.Service;
import com.cloud.network.dao.FirewallRulesDaoImpl;
import com.cloud.network.dao.IPAddressDaoImpl;
import com.cloud.network.dao.PhysicalNetworkDaoImpl;
import com.cloud.network.dao.Site2SiteVpnGatewayDaoImpl;
import com.cloud.network.vpc.Vpc;
import com.cloud.network.vpc.VpcManager;
import com.cloud.network.vpc.VpcManagerImpl;
import com.cloud.network.vpc.dao.PrivateIpDaoImpl;
import com.cloud.network.vpc.dao.StaticRouteDaoImpl;
import com.cloud.network.vpc.dao.VpcGatewayDaoImpl;
import com.cloud.network.vpc.dao.VpcOfferingDaoImpl;
import com.cloud.server.ManagementService;
import com.cloud.tags.dao.ResourceTagsDaoImpl;
import com.cloud.user.AccountVO;
import com.cloud.user.MockAccountManagerImpl;
import com.cloud.user.dao.AccountDaoImpl;
import com.cloud.utils.component.ComponentContext;
import com.cloud.vm.dao.DomainRouterDaoImpl;
import com.cloud.vpc.dao.MockNetworkDaoImpl;
import com.cloud.vpc.dao.MockNetworkOfferingDaoImpl;
import com.cloud.vpc.dao.MockNetworkOfferingServiceMapDaoImpl;
import com.cloud.vpc.dao.MockNetworkServiceMapDaoImpl;
import com.cloud.vpc.dao.MockVpcDaoImpl;
import com.cloud.vpc.dao.MockVpcOfferingDaoImpl;
import com.cloud.vpc.dao.MockVpcOfferingServiceMapDaoImpl;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/VpcTestContext.xml")
public class VpcApiUnitTest extends TestCase{
private static final Logger s_logger = Logger.getLogger(VpcApiUnitTest.class);
@Inject VpcManagerImpl _vpcService = null;
@Override
@Before
public void setUp() throws Exception {
ComponentContext.initComponentsLifeCycle();
}
@Test
public void test() {
s_logger.debug("Starting test for VpcService interface");
//Vpc service methods
//getActiveVpc();
//deleteVpc();
//Vpc manager methods
validateNtwkOffForVpc();
//destroyVpc();
}
protected void deleteVpc() {
//delete existing offering
boolean result = false;
String msg = null;
try {
List<String> svcs = new ArrayList<String>();
svcs.add(Service.SourceNat.getName());
result = _vpcService.deleteVpc(1);
} catch (Exception ex) {
msg = ex.getMessage();
} finally {
if (result) {
s_logger.debug("Delete vpc: Test passed, vpc is deleted");
} else {
s_logger.error("Delete vpc: TEST FAILED, vpc failed to delete " + msg);
}
}
//delete non-existing offering
result = false;
msg = null;
try {
List<String> svcs = new ArrayList<String>();
svcs.add(Service.SourceNat.getName());
result = _vpcService.deleteVpc(100);
} catch (Exception ex) {
msg = ex.getMessage();
} finally {
if (!result) {
s_logger.debug("Delete vpc: Test passed, non existing vpc failed to delete ");
} else {
s_logger.error("Delete vpc: TEST FAILED, true is returned when try to delete non existing vpc");
}
}
}
protected void getActiveVpc() {
//test for active vpc
boolean result = false;
String msg = null;
Vpc vpc = null;
try {
List<String> svcs = new ArrayList<String>();
svcs.add(Service.SourceNat.getName());
vpc = _vpcService.getActiveVpc(1);
if (vpc != null) {
result = true;
}
} catch (Exception ex) {
msg = ex.getMessage();
} finally {
if (result) {
s_logger.debug("Get active Vpc: Test passed, active vpc is returned");
} else {
s_logger.error("Get active Vpc: TEST FAILED, active vpc is not returned " + msg);
}
}
//test for inactive vpc
result = false;
msg = null;
vpc = null;
try {
List<String> svcs = new ArrayList<String>();
svcs.add(Service.SourceNat.getName());
vpc = _vpcService.getActiveVpc(2);
if (vpc != null) {
result = true;
}
} catch (Exception ex) {
msg = ex.getMessage();
} finally {
if (!result) {
s_logger.debug("Get active Vpc: Test passed, no vpc is returned");
} else {
s_logger.error("Get active Vpc: TEST FAILED, non active vpc is returned");
}
}
}
protected void destroyVpc() {
try {
_vpcService.destroyVpc(_vpcService.getVpc(1), new AccountVO(), 1L);
} catch (Exception ex) {
s_logger.error("Destroy VPC TEST FAILED due to exc ", ex);
}
}
protected void validateNtwkOffForVpc() {
//validate network offering
//1) correct network offering
boolean result = false;
try {
_vpcService.validateNtkwOffForVpc(1, "0.0.0.0", "111-", new AccountVO(), _vpcService.getVpc(1), 2L, "10.1.1.1");
result = true;
s_logger.debug("Validate network offering: Test passed: the offering is valid for vpc creation");
} catch (Exception ex) {
s_logger.error("Validate network offering: TEST FAILED due to exc ", ex);
}
//2) invalid offering - source nat is not included
result = false;
String msg = null;
try {
_vpcService.validateNtkwOffForVpc(2, "0.0.0.0", "111-", new AccountVO(), _vpcService.getVpc(1), 2L, "10.1.1.1");
result = true;
} catch (InvalidParameterValueException ex) {
msg = ex.getMessage();
} finally {
if (!result) {
s_logger.debug("Validate network offering: Test passed: " + msg);
} else {
s_logger.error("Validate network offering: TEST FAILED, can't use network offering without SourceNat service");
}
}
//3) invalid offering - conserve mode is off
result = false;
msg = null;
try {
_vpcService.validateNtkwOffForVpc(3, "0.0.0.0", "111-", new AccountVO(), _vpcService.getVpc(1), 2L, "10.1.1.1");
result = true;
} catch (InvalidParameterValueException ex) {
msg = ex.getMessage();
} finally {
if (!result) {
s_logger.debug("Validate network offering: Test passed: " + msg);
} else {
s_logger.error("Validate network offering: TEST FAILED, can't use network offering without conserve mode = true");
}
}
//4) invalid offering - guest type shared
result = false;
try {
_vpcService.validateNtkwOffForVpc(4, "0.0.0.0", "111-", new AccountVO(), _vpcService.getVpc(1), 2L, "10.1.1.1");
result = true;
} catch (InvalidParameterValueException ex) {
msg = ex.getMessage();
} finally {
if (!result) {
s_logger.debug("Validate network offering: Test passed: " + msg);
} else {
s_logger.error("Validate network offering: TEST FAILED, can't use network offering with guest type = Shared");
}
}
//5) Invalid offering - no redundant router support
result = false;
try {
_vpcService.validateNtkwOffForVpc(5, "0.0.0.0", "111-", new AccountVO(), _vpcService.getVpc(1), 2L, "10.1.1.1");
result = true;
} catch (InvalidParameterValueException ex) {
msg = ex.getMessage();
} finally {
if (!result) {
s_logger.debug("Validate network offering: Test passed: " + msg);
} else {
s_logger.error("TEST FAILED, can't use network offering with guest type = Shared");
}
}
//6) Only one network in the VPC can support LB service - negative scenario
result = false;
try {
_vpcService.validateNtkwOffForVpc(6, "0.0.0.0", "111-", new AccountVO(), _vpcService.getVpc(1), 2L, "10.1.1.1");
result = true;
s_logger.debug("Validate network offering: Test passed: the offering is valid for vpc creation");
} catch (InvalidParameterValueException ex) {
msg = ex.getMessage();
} finally {
if (!result) {
s_logger.debug("Test passed : " + msg);
} else {
s_logger.error("Validate network offering: TEST FAILED, can't use network offering with guest type = Shared");
}
}
}
}