Search in sources :

Example 1 with ApiConnectorMock

use of net.juniper.contrail.api.ApiConnectorMock in project cloudstack by apache.

the class NetworkProviderTest method dbSyncTest.

@Test
public void dbSyncTest() {
    Network network = lookupTestNetwork("test-db-only-net");
    if (network == null) {
        network = createTestNetwork("test-db-only-net");
    }
    UserVm vm = _server.createVM("test-db-only-vm", network);
    try {
        createFloatingIp(network, vm);
    } catch (Exception e) {
        fail("unable to create floating ip");
    }
    /* reset ApiServer objects to default config only, so above created objects
         * exists only in cludstack db but not in api server
         */
    ((ApiConnectorMock) _api).initConfig();
    /* reset model cached objects */
    _contrailMgr.getDatabase().initDb();
    /* Create one object of each type directly in api-server - these objects does not exist in cloudstack */
    net.juniper.contrail.api.types.Domain domain = new net.juniper.contrail.api.types.Domain();
    domain.setName("test-vnc-only-domain--1");
    domain.setUuid(UUID.randomUUID().toString());
    try {
        assertTrue(_api.create(domain));
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
    Project project = new Project();
    project.setName("test-vnc-only-project-1");
    project.setUuid(UUID.randomUUID().toString());
    project.setParent(domain);
    try {
        assertTrue(_api.create(project));
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
    VirtualNetwork net = new VirtualNetwork();
    net.setName("test-vnc-only-net-1");
    net.setUuid(UUID.randomUUID().toString());
    net.setParent(project);
    NetworkIpam ipam = null;
    try {
        // Find default-network-ipam
        String ipam_id = _api.findByName(NetworkIpam.class, null, "default-network-ipam");
        assertNotNull(ipam_id);
        ipam = (NetworkIpam) _api.findById(NetworkIpam.class, ipam_id);
        assertNotNull(ipam);
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
    VnSubnetsType subnet = new VnSubnetsType();
    subnet.addIpamSubnets(new SubnetType("10.0.2.0", 24), "10.0.2.254");
    net.addNetworkIpam(ipam, subnet);
    VirtualMachine vncVm = new VirtualMachine();
    vncVm.setName("test-vnc-only-vm-1");
    try {
        assertTrue(_api.create(vncVm));
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
    VirtualMachineInterface vmi = new VirtualMachineInterface();
    vmi.setParent(vncVm);
    vmi.setName("test-vnc-only-vmi-1");
    try {
        assertTrue(_api.create(vmi));
        assertTrue(_api.create(net));
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
    InstanceIp ip_obj = new InstanceIp();
    ip_obj.setName(net.getName() + ":0");
    ip_obj.setVirtualNetwork(net);
    ip_obj.setVirtualMachineInterface(vmi);
    try {
        assertTrue(_api.create(ip_obj));
        // Must perform a GET in order to update the object contents.
        assertTrue(_api.read(ip_obj));
        assertNotNull(ip_obj.getAddress());
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
    // now db sync
    if (_dbSync.syncAll(DBSyncGeneric.SYNC_MODE_UPDATE) == ServerDBSync.SYNC_STATE_OUT_OF_SYNC) {
        s_logger.info("# Cloudstack DB & VNC are out of sync - resync done");
    }
    if (_dbSync.syncAll(DBSyncGeneric.SYNC_MODE_CHECK) == ServerDBSync.SYNC_STATE_OUT_OF_SYNC) {
        s_logger.info("# Cloudstack DB & VNC are still out of sync");
        fail("DB Sync failed");
    }
}
Also used : InstanceIp(net.juniper.contrail.api.types.InstanceIp) IOException(java.io.IOException) SubnetType(net.juniper.contrail.api.types.SubnetType) CloudException(com.cloud.exception.CloudException) IOException(java.io.IOException) VirtualNetwork(net.juniper.contrail.api.types.VirtualNetwork) Project(net.juniper.contrail.api.types.Project) UserVm(com.cloud.uservm.UserVm) VirtualMachineInterface(net.juniper.contrail.api.types.VirtualMachineInterface) VirtualNetwork(net.juniper.contrail.api.types.VirtualNetwork) Network(com.cloud.network.Network) NetworkIpam(net.juniper.contrail.api.types.NetworkIpam) Domain(com.cloud.domain.Domain) ApiConnectorMock(net.juniper.contrail.api.ApiConnectorMock) VnSubnetsType(net.juniper.contrail.api.types.VnSubnetsType) VirtualMachine(net.juniper.contrail.api.types.VirtualMachine) Test(org.junit.Test)

Example 2 with ApiConnectorMock

use of net.juniper.contrail.api.ApiConnectorMock in project cloudstack by apache.

the class VirtualMachineModelTest method testCreateVirtualMachine.

@Test
public void testCreateVirtualMachine() throws IOException {
    String uuid = UUID.randomUUID().toString();
    ContrailManagerImpl contrailMgr = mock(ContrailManagerImpl.class);
    ModelController controller = mock(ModelController.class);
    ApiConnector api = new ApiConnectorMock(null, 0);
    when(controller.getManager()).thenReturn(contrailMgr);
    when(controller.getApiAccessor()).thenReturn(api);
    // Create Virtual-Network (VN)
    NetworkVO network = mock(NetworkVO.class);
    when(network.getName()).thenReturn("testnetwork");
    when(network.getState()).thenReturn(Network.State.Allocated);
    when(network.getGateway()).thenReturn("10.1.1.1");
    when(network.getCidr()).thenReturn("10.1.1.0/24");
    when(network.getPhysicalNetworkId()).thenReturn(42L);
    when(network.getDomainId()).thenReturn(10L);
    when(network.getAccountId()).thenReturn(42L);
    when(contrailMgr.getCanonicalName(network)).thenReturn("testnetwork");
    when(contrailMgr.getProjectId(network.getDomainId(), network.getAccountId())).thenReturn("testProjectId");
    // Create Virtual-Machine (VM)
    VMInstanceVO vm = mock(VMInstanceVO.class);
    when(vm.getInstanceName()).thenReturn("testVM1");
    when(vm.getState()).thenReturn(VirtualMachine.State.Starting);
    when(vm.getDomainId()).thenReturn(10L);
    when(vm.getAccountId()).thenReturn(42L);
    UserVmDao VmDao = mock(UserVmDao.class);
    when(VmDao.findById(anyLong())).thenReturn(null);
    when(controller.getVmDao()).thenReturn(VmDao);
    VirtualMachineModel vmModel = new VirtualMachineModel(vm, uuid);
    assertEquals(vmModel.getInstanceName(), "testVM1");
    assertEquals(vmModel.getUuid(), uuid);
    vmModel.build(controller, vm);
    try {
        vmModel.update(controller);
    } catch (Exception ex) {
        fail("virtual-network update failed ");
    }
    // verify
    assertTrue(vmModel.verify(controller));
}
Also used : ContrailManagerImpl(org.apache.cloudstack.network.contrail.management.ContrailManagerImpl) NetworkVO(com.cloud.network.dao.NetworkVO) ApiConnector(net.juniper.contrail.api.ApiConnector) VMInstanceVO(com.cloud.vm.VMInstanceVO) UserVmDao(com.cloud.vm.dao.UserVmDao) ApiConnectorMock(net.juniper.contrail.api.ApiConnectorMock) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with ApiConnectorMock

use of net.juniper.contrail.api.ApiConnectorMock in project cloudstack by apache.

the class VirtualNetworkModelTest method testCreateVirtualNetwork.

@Test
public void testCreateVirtualNetwork() throws IOException {
    String uuid = UUID.randomUUID().toString();
    ContrailManagerImpl contrailMgr = mock(ContrailManagerImpl.class);
    ModelController controller = mock(ModelController.class);
    ApiConnector api = new ApiConnectorMock(null, 0);
    when(controller.getManager()).thenReturn(contrailMgr);
    when(controller.getApiAccessor()).thenReturn(api);
    // Create Virtual-Network (VN)
    NetworkVO network = mock(NetworkVO.class);
    when(network.getName()).thenReturn("testnetwork");
    when(network.getState()).thenReturn(State.Allocated);
    when(network.getGateway()).thenReturn("10.1.1.1");
    when(network.getCidr()).thenReturn("10.1.1.0/24");
    when(network.getPhysicalNetworkId()).thenReturn(42L);
    when(network.getDomainId()).thenReturn(10L);
    when(network.getAccountId()).thenReturn(42L);
    when(contrailMgr.getCanonicalName(network)).thenReturn("testnetwork");
    when(contrailMgr.getProjectId(network.getDomainId(), network.getAccountId())).thenReturn("testProjectId");
    VirtualNetworkModel vnModel = new VirtualNetworkModel(network, uuid, "testnetwork", TrafficType.Guest);
    assertEquals(vnModel.getName(), "testnetwork");
    assertEquals(vnModel.getUuid(), uuid);
    vnModel.build(controller, network);
    try {
        vnModel.update(controller);
    } catch (Exception ex) {
        fail("virtual-network update failed ");
    }
    assertTrue(vnModel.verify(controller));
}
Also used : ContrailManagerImpl(org.apache.cloudstack.network.contrail.management.ContrailManagerImpl) NetworkVO(com.cloud.network.dao.NetworkVO) ApiConnector(net.juniper.contrail.api.ApiConnector) ApiConnectorMock(net.juniper.contrail.api.ApiConnectorMock) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with ApiConnectorMock

use of net.juniper.contrail.api.ApiConnectorMock in project cloudstack by apache.

the class VMInterfaceModelTest method testCreateVMInterface.

@Test
public void testCreateVMInterface() throws IOException {
    String uuid;
    ContrailManagerImpl contrailMgr = mock(ContrailManagerImpl.class);
    ModelController controller = mock(ModelController.class);
    ApiConnector api = new ApiConnectorMock(null, 0);
    when(controller.getManager()).thenReturn(contrailMgr);
    when(controller.getApiAccessor()).thenReturn(api);
    // Create Virtual-Network (VN)
    NetworkVO network = mock(NetworkVO.class);
    when(network.getName()).thenReturn("testnetwork");
    when(network.getState()).thenReturn(Network.State.Implemented);
    when(network.getGateway()).thenReturn("10.1.1.1");
    when(network.getCidr()).thenReturn("10.1.1.0/24");
    when(network.getPhysicalNetworkId()).thenReturn(42L);
    when(network.getDomainId()).thenReturn(10L);
    when(network.getAccountId()).thenReturn(42L);
    NetworkDao networkDao = mock(NetworkDao.class);
    when(networkDao.findById(anyLong())).thenReturn(network);
    when(controller.getNetworkDao()).thenReturn(networkDao);
    when(contrailMgr.getCanonicalName(network)).thenReturn("testnetwork");
    when(contrailMgr.getProjectId(network.getDomainId(), network.getAccountId())).thenReturn("testProjectId");
    VirtualNetworkModel vnModel = new VirtualNetworkModel(network, UUID.randomUUID().toString(), "test", TrafficType.Guest);
    vnModel.build(controller, network);
    try {
        vnModel.update(controller);
    } catch (Exception ex) {
        fail("virtual-network update failed ");
    }
    // Create Virtual-Machine (VM)
    VMInstanceVO vm = mock(VMInstanceVO.class);
    when(vm.getInstanceName()).thenReturn("testVM1");
    when(vm.getState()).thenReturn(VirtualMachine.State.Running);
    when(vm.getDomainId()).thenReturn(10L);
    when(vm.getAccountId()).thenReturn(42L);
    UserVmDao VmDao = mock(UserVmDao.class);
    when(VmDao.findById(anyLong())).thenReturn(null);
    when(controller.getVmDao()).thenReturn(VmDao);
    VirtualMachineModel vmModel = new VirtualMachineModel(vm, UUID.randomUUID().toString());
    vmModel.build(controller, vm);
    try {
        vmModel.update(controller);
    } catch (Exception ex) {
        fail("virtual-machine update failed ");
    }
    // Create Virtual=Machine-Interface (VMInterface)
    NicVO nic = mock(NicVO.class);
    when(nic.getIPv4Address()).thenReturn("10.1.1.2");
    when(nic.getMacAddress()).thenReturn("00:01:02:03:04:05");
    when(nic.getDeviceId()).thenReturn(100);
    when(nic.getState()).thenReturn(NicVO.State.Allocated);
    when(nic.getNetworkId()).thenReturn(10L);
    when(contrailMgr.getVifNameByVmName(anyString(), anyInt())).thenReturn("testVM1-100");
    uuid = UUID.randomUUID().toString();
    VMInterfaceModel vmiModel = new VMInterfaceModel(uuid);
    assertEquals(uuid, vmiModel.getUuid());
    vmiModel.addToVirtualMachine(vmModel);
    vmiModel.addToVirtualNetwork(vnModel);
    try {
        vmiModel.build(controller, vm, nic);
        vmiModel.setActive();
    } catch (Exception ex) {
        fail("vm-interface build failed ");
    }
    try {
        vmiModel.update(controller);
    } catch (Exception ex) {
        fail("vm-interface update failed ");
    }
    // Verify virtual-machine-interface creation.
    try {
        VirtualMachineInterface vmi = (VirtualMachineInterface) api.findById(VirtualMachineInterface.class, vmiModel.getUuid());
        assertNotNull(vmi);
    } catch (Exception ex) {
        fail("vm-interface verify failed ");
    }
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) ApiConnector(net.juniper.contrail.api.ApiConnector) VMInstanceVO(com.cloud.vm.VMInstanceVO) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) ContrailManagerImpl(org.apache.cloudstack.network.contrail.management.ContrailManagerImpl) NetworkDao(com.cloud.network.dao.NetworkDao) VirtualMachineInterface(net.juniper.contrail.api.types.VirtualMachineInterface) UserVmDao(com.cloud.vm.dao.UserVmDao) ApiConnectorMock(net.juniper.contrail.api.ApiConnectorMock) NicVO(com.cloud.vm.NicVO) Test(org.junit.Test)

Example 5 with ApiConnectorMock

use of net.juniper.contrail.api.ApiConnectorMock in project cloudstack by apache.

the class InstanceIpModelTest method testCreateInstanceIp.

@Test
public void testCreateInstanceIp() throws IOException {
    ContrailManagerImpl contrailMgr = mock(ContrailManagerImpl.class);
    ModelController controller = mock(ModelController.class);
    ApiConnector api = new ApiConnectorMock(null, 0);
    when(controller.getApiAccessor()).thenReturn(api);
    when(controller.getManager()).thenReturn(contrailMgr);
    // Create Virtual-Network (VN)
    NetworkVO network = mock(NetworkVO.class);
    when(network.getName()).thenReturn("testnetwork");
    when(network.getState()).thenReturn(Network.State.Implemented);
    when(network.getGateway()).thenReturn("10.1.1.1");
    when(network.getCidr()).thenReturn("10.1.1.0/24");
    when(network.getPhysicalNetworkId()).thenReturn(42L);
    when(network.getDomainId()).thenReturn(10L);
    when(network.getAccountId()).thenReturn(42L);
    NetworkDao networkDao = mock(NetworkDao.class);
    when(networkDao.findById(anyLong())).thenReturn(network);
    when(controller.getNetworkDao()).thenReturn(networkDao);
    when(contrailMgr.getCanonicalName(network)).thenReturn("testnetwork");
    when(contrailMgr.getProjectId(network.getDomainId(), network.getAccountId())).thenReturn("testProjectId");
    VirtualNetworkModel vnModel = new VirtualNetworkModel(network, UUID.randomUUID().toString(), "test", TrafficType.Guest);
    vnModel.build(controller, network);
    try {
        vnModel.update(controller);
    } catch (Exception ex) {
        fail("virtual-network update failed ");
    }
    // Create Virtual-Machine (VM)
    VMInstanceVO vm = mock(VMInstanceVO.class);
    when(vm.getInstanceName()).thenReturn("testVM1");
    when(vm.getState()).thenReturn(VirtualMachine.State.Running);
    when(vm.getDomainId()).thenReturn(10L);
    when(vm.getAccountId()).thenReturn(42L);
    UserVmDao VmDao = mock(UserVmDao.class);
    when(VmDao.findById(anyLong())).thenReturn(null);
    when(controller.getVmDao()).thenReturn(VmDao);
    VirtualMachineModel vmModel = new VirtualMachineModel(vm, UUID.randomUUID().toString());
    vmModel.build(controller, vm);
    try {
        vmModel.update(controller);
    } catch (Exception ex) {
        fail("virtual-machine update failed ");
    }
    // Create Virtual=Machine-Interface (VMInterface)
    NicVO nic = mock(NicVO.class);
    when(nic.getIPv4Address()).thenReturn("10.1.1.2");
    when(nic.getMacAddress()).thenReturn("00:01:02:03:04:05");
    when(nic.getDeviceId()).thenReturn(100);
    when(nic.getState()).thenReturn(NicVO.State.Allocated);
    when(nic.getNetworkId()).thenReturn(10L);
    when(contrailMgr.getVifNameByVmName(anyString(), anyInt())).thenReturn("testVM1-100");
    VMInterfaceModel vmiModel = new VMInterfaceModel(UUID.randomUUID().toString());
    vmiModel.addToVirtualMachine(vmModel);
    vmiModel.addToVirtualNetwork(vnModel);
    try {
        vmiModel.build(controller, vm, nic);
        vmiModel.setActive();
    } catch (Exception ex) {
        fail("vm-interface build failed ");
    }
    try {
        vmiModel.update(controller);
    } catch (Exception ex) {
        fail("vm-interface update failed ");
    }
    InstanceIpModel ipModel = new InstanceIpModel(vm.getInstanceName(), nic.getDeviceId());
    ipModel.addToVMInterface(vmiModel);
    ipModel.setAddress(nic.getIPv4Address());
    try {
        ipModel.update(controller);
    } catch (Exception ex) {
        fail("ipInstance update failed ");
    }
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) ApiConnector(net.juniper.contrail.api.ApiConnector) VMInstanceVO(com.cloud.vm.VMInstanceVO) IOException(java.io.IOException) ContrailManagerImpl(org.apache.cloudstack.network.contrail.management.ContrailManagerImpl) NetworkDao(com.cloud.network.dao.NetworkDao) UserVmDao(com.cloud.vm.dao.UserVmDao) ApiConnectorMock(net.juniper.contrail.api.ApiConnectorMock) NicVO(com.cloud.vm.NicVO) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)5 ApiConnectorMock (net.juniper.contrail.api.ApiConnectorMock)5 Test (org.junit.Test)5 NetworkVO (com.cloud.network.dao.NetworkVO)4 ApiConnector (net.juniper.contrail.api.ApiConnector)4 ContrailManagerImpl (org.apache.cloudstack.network.contrail.management.ContrailManagerImpl)4 VMInstanceVO (com.cloud.vm.VMInstanceVO)3 UserVmDao (com.cloud.vm.dao.UserVmDao)3 NetworkDao (com.cloud.network.dao.NetworkDao)2 NicVO (com.cloud.vm.NicVO)2 VirtualMachineInterface (net.juniper.contrail.api.types.VirtualMachineInterface)2 Domain (com.cloud.domain.Domain)1 CloudException (com.cloud.exception.CloudException)1 Network (com.cloud.network.Network)1 UserVm (com.cloud.uservm.UserVm)1 InstanceIp (net.juniper.contrail.api.types.InstanceIp)1 NetworkIpam (net.juniper.contrail.api.types.NetworkIpam)1 Project (net.juniper.contrail.api.types.Project)1 SubnetType (net.juniper.contrail.api.types.SubnetType)1 VirtualMachine (net.juniper.contrail.api.types.VirtualMachine)1