mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
1) Added comments to VPC/VPCService/VPCManager interfaces 2) Moved VPC offering related methods from VpcService to the new interface - VpcProvisioningService 3) Fixed static nat creation in the VPC - used to result in NPE due to invalid method referencing while obtaining VPC VR information
245 lines
8.9 KiB
Java
245 lines
8.9 KiB
Java
// 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.exception.InvalidParameterValueException;
|
|
import com.cloud.network.Network.Service;
|
|
import com.cloud.network.vpc.Vpc;
|
|
import com.cloud.network.vpc.VpcManagerImpl;
|
|
import com.cloud.user.AccountVO;
|
|
import com.cloud.utils.component.ComponentContext;
|
|
|
|
@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.validateNtwkOffForNtwkInVpc(2L, 1, "0.0.0.0", "111-", _vpcService.getVpc(1), "10.1.1.1", new AccountVO());
|
|
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.validateNtwkOffForNtwkInVpc(2L, 2, "0.0.0.0", "111-", _vpcService.getVpc(1), "10.1.1.1", new AccountVO());
|
|
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.validateNtwkOffForNtwkInVpc(2L, 3, "0.0.0.0", "111-", _vpcService.getVpc(1), "10.1.1.1", new AccountVO());
|
|
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.validateNtwkOffForNtwkInVpc(2L, 4, "0.0.0.0", "111-", _vpcService.getVpc(1), "10.1.1.1", new AccountVO());
|
|
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.validateNtwkOffForNtwkInVpc(2L, 5, "0.0.0.0", "111-", _vpcService.getVpc(1), "10.1.1.1", new AccountVO());
|
|
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.validateNtwkOffForNtwkInVpc(2L, 6, "0.0.0.0", "111-", _vpcService.getVpc(1), "10.1.1.1", new AccountVO());
|
|
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");
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|