Search in sources :

Example 26 with DatabaseStack

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.

the class GcpDatabaseServerCheckServiceTest method testCheckWhenDbInstanceDropTokenExceptionShouldReturnGcpResourceException.

@Test
public void testCheckWhenDbInstanceDropTokenExceptionShouldReturnGcpResourceException() throws IOException {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    DatabaseServer databaseServer = mock(DatabaseServer.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseServer.getServerId()).thenReturn("test");
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenThrow(new GcpResourceException("error"));
    GcpResourceException gcpResourceException = assertThrows(GcpResourceException.class, () -> underTest.check(authenticatedContext, databaseStack));
    Assert.assertEquals("error", gcpResourceException.getMessage());
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Test(org.junit.jupiter.api.Test)

Example 27 with DatabaseStack

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.

the class GcpDatabaseServerCheckServiceTest method testCheckWhenDbInstanceIsSuspendedShouldReturnStopped.

@Test
public void testCheckWhenDbInstanceIsSuspendedShouldReturnStopped() throws IOException {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    DatabaseServer databaseServer = mock(DatabaseServer.class);
    SQLAdmin sqlAdmin = mock(SQLAdmin.class);
    SQLAdmin.Instances sqlAdminInstances = mock(SQLAdmin.Instances.class);
    SQLAdmin.Instances.List sqlAdminInstancesList = mock(SQLAdmin.Instances.List.class);
    InstancesListResponse instancesListResponse = mock(InstancesListResponse.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseServer.getServerId()).thenReturn("test");
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    when(gcpStackUtil.getProjectId(any(CloudCredential.class))).thenReturn("project-id");
    when(sqlAdmin.instances()).thenReturn(sqlAdminInstances);
    when(sqlAdminInstances.list(anyString())).thenReturn(sqlAdminInstancesList);
    when(sqlAdminInstancesList.execute()).thenReturn(instancesListResponse);
    when(instancesListResponse.isEmpty()).thenReturn(false);
    DatabaseInstance databaseInstance = new DatabaseInstance();
    databaseInstance.setName("test");
    databaseInstance.setState("SUSPENDED");
    when(instancesListResponse.getItems()).thenReturn(List.of(databaseInstance));
    ExternalDatabaseStatus check = underTest.check(authenticatedContext, databaseStack);
    Assert.assertEquals(ExternalDatabaseStatus.STOPPED, check);
}
Also used : InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) ExternalDatabaseStatus(com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus) Test(org.junit.jupiter.api.Test)

Example 28 with DatabaseStack

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.

the class GcpDatabaseServerCheckServiceTest method testCheckWhenDbInstanceIsFailedShouldReturnDeleted.

@Test
public void testCheckWhenDbInstanceIsFailedShouldReturnDeleted() throws IOException {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    DatabaseServer databaseServer = mock(DatabaseServer.class);
    SQLAdmin sqlAdmin = mock(SQLAdmin.class);
    SQLAdmin.Instances sqlAdminInstances = mock(SQLAdmin.Instances.class);
    SQLAdmin.Instances.List sqlAdminInstancesList = mock(SQLAdmin.Instances.List.class);
    InstancesListResponse instancesListResponse = mock(InstancesListResponse.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseServer.getServerId()).thenReturn("test");
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    when(gcpStackUtil.getProjectId(any(CloudCredential.class))).thenReturn("project-id");
    when(sqlAdmin.instances()).thenReturn(sqlAdminInstances);
    when(sqlAdminInstances.list(anyString())).thenReturn(sqlAdminInstancesList);
    when(sqlAdminInstancesList.execute()).thenReturn(instancesListResponse);
    when(instancesListResponse.isEmpty()).thenReturn(false);
    DatabaseInstance databaseInstance = new DatabaseInstance();
    databaseInstance.setName("test");
    databaseInstance.setState("FAILED");
    when(instancesListResponse.getItems()).thenReturn(List.of(databaseInstance));
    ExternalDatabaseStatus check = underTest.check(authenticatedContext, databaseStack);
    Assert.assertEquals(ExternalDatabaseStatus.DELETED, check);
}
Also used : InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) ExternalDatabaseStatus(com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus) Test(org.junit.jupiter.api.Test)

Example 29 with DatabaseStack

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.

the class GcpDatabaseServerCheckServiceTest method testCheckWhenDbInstanceIsUnknownStateShouldReturnDeleted.

@Test
public void testCheckWhenDbInstanceIsUnknownStateShouldReturnDeleted() throws IOException {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    DatabaseServer databaseServer = mock(DatabaseServer.class);
    SQLAdmin sqlAdmin = mock(SQLAdmin.class);
    SQLAdmin.Instances sqlAdminInstances = mock(SQLAdmin.Instances.class);
    SQLAdmin.Instances.List sqlAdminInstancesList = mock(SQLAdmin.Instances.List.class);
    InstancesListResponse instancesListResponse = mock(InstancesListResponse.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseServer.getServerId()).thenReturn("test");
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    when(gcpStackUtil.getProjectId(any(CloudCredential.class))).thenReturn("project-id");
    when(sqlAdmin.instances()).thenReturn(sqlAdminInstances);
    when(sqlAdminInstances.list(anyString())).thenReturn(sqlAdminInstancesList);
    when(sqlAdminInstancesList.execute()).thenReturn(instancesListResponse);
    when(instancesListResponse.isEmpty()).thenReturn(false);
    DatabaseInstance databaseInstance = new DatabaseInstance();
    databaseInstance.setName("test");
    databaseInstance.setState("UNKNOWN_STATE");
    when(instancesListResponse.getItems()).thenReturn(List.of(databaseInstance));
    ExternalDatabaseStatus check = underTest.check(authenticatedContext, databaseStack);
    Assert.assertEquals(ExternalDatabaseStatus.DELETED, check);
}
Also used : InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) ExternalDatabaseStatus(com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus) Test(org.junit.jupiter.api.Test)

Example 30 with DatabaseStack

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.

the class GcpDatabaseServerLaunchServiceTest method testLaunchWhenDatabaseNOTAlreadyExistAndListThrowExceptionShouldThrowGcpResourceException.

@Test
public void testLaunchWhenDatabaseNOTAlreadyExistAndListThrowExceptionShouldThrowGcpResourceException() throws Exception {
    Map<String, Object> map = new HashMap<>();
    map.put("engineVersion", "1");
    DatabaseServer databaseServer = DatabaseServer.builder().connectionDriver("driver").serverId("driver").connectorJarUrl("driver").engine(DatabaseEngine.POSTGRESQL).location("location").port(99).storageSize(50L).rootUserName("rootUserName").rootPassword("rootPassword").flavor("flavor").useSslEnforcement(true).params(map).build();
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    PersistenceNotifier persistenceNotifier = mock(PersistenceNotifier.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    SQLAdmin sqlAdmin = mock(SQLAdmin.class);
    Compute compute = mock(Compute.class);
    SQLAdmin.Instances sqlAdminInstances = mock(SQLAdmin.Instances.class);
    SQLAdmin.Instances.List sqlAdminInstancesList = mock(SQLAdmin.Instances.List.class);
    IpMapping ipMapping = new IpMapping();
    ipMapping.setIpAddress("10.0.0.0");
    ipMapping.setType("PRIVATE");
    GoogleJsonResponseException googleJsonResponseException = mock(GoogleJsonResponseException.class);
    GoogleJsonError googleJsonError = mock(GoogleJsonError.class);
    when(googleJsonResponseException.getDetails()).thenReturn(googleJsonError);
    when(googleJsonError.getMessage()).thenReturn("error");
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    when(gcpComputeFactory.buildCompute(any(CloudCredential.class))).thenReturn(compute);
    when(gcpStackUtil.getProjectId(any(CloudCredential.class))).thenReturn("project-id");
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location(region("region"), availabilityZone("az1")));
    when(sqlAdmin.instances()).thenReturn(sqlAdminInstances);
    when(sqlAdminInstances.list(anyString())).thenReturn(sqlAdminInstancesList);
    when(sqlAdminInstancesList.execute()).thenThrow(googleJsonResponseException);
    GcpResourceException gcpResourceException = assertThrows(GcpResourceException.class, () -> underTest.launch(authenticatedContext, databaseStack, persistenceNotifier));
    Assert.assertEquals("error: [ resourceType: GCP_DATABASE,  resourceName: driver ]", gcpResourceException.getMessage());
}
Also used : HashMap(java.util.HashMap) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) IpMapping(com.google.api.services.sqladmin.model.IpMapping) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Compute(com.google.api.services.compute.Compute) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) Test(org.junit.jupiter.api.Test)

Aggregations

DatabaseStack (com.sequenceiq.cloudbreak.cloud.model.DatabaseStack)48 Test (org.junit.jupiter.api.Test)34 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)24 DatabaseServer (com.sequenceiq.cloudbreak.cloud.model.DatabaseServer)24 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)22 SQLAdmin (com.google.api.services.sqladmin.SQLAdmin)16 InstancesListResponse (com.google.api.services.sqladmin.model.InstancesListResponse)13 DatabaseInstance (com.google.api.services.sqladmin.model.DatabaseInstance)12 PersistenceNotifier (com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier)12 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)11 DatabaseServer (com.sequenceiq.redbeams.domain.stack.DatabaseServer)11 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)11 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)10 Network (com.sequenceiq.cloudbreak.cloud.model.Network)9 ExternalDatabaseStatus (com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus)8 Location (com.sequenceiq.cloudbreak.cloud.model.Location)8 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)8 Json (com.sequenceiq.cloudbreak.common.json.Json)8 DBStack (com.sequenceiq.redbeams.domain.stack.DBStack)7 Network (com.sequenceiq.redbeams.domain.stack.Network)7