Search in sources :

Example 6 with DatabaseServer

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

the class AzureTemplateBuilderDbTest method buildTestWhenDataEncryptionParametersPresent.

@Test
void buildTestWhenDataEncryptionParametersPresent() {
    Template template = Optional.ofNullable(factoryBean.getObject()).map(config -> {
        try {
            return config.getTemplate("templates/arm-dbstack.ftl", "UTF-8");
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }).orElseThrow();
    Subnet subnet = new Subnet(SUBNET_CIDR);
    Network network = new Network(subnet, List.of(NETWORK_CIDR), OutboundInternetTraffic.ENABLED);
    network.putParameter("subnets", FULL_SUBNET_ID);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("dbVersion", "10");
    params.put(KEY_URL, "https://dummyVault.vault.azure.net/keys/dummyKey/dummyVersion");
    params.put(KEY_VAULT_RESOURCE_GROUP_NAME, "dummyResourceGroup");
    DatabaseServer databaseServer = DatabaseServer.builder().serverId(SERVER_ID).rootUserName(ROOT_USER_NAME).rootPassword(ROOT_PASSWORD).location(REGION).params(params).build();
    DatabaseStack databaseStack = new DatabaseStack(network, databaseServer, Collections.emptyMap(), template.toString());
    Mockito.when(azureDatabaseTemplateProvider.getTemplate(databaseStack)).thenReturn(template);
    Mockito.when(azureUtils.encodeString(SUBNET_ID)).thenReturn("hash");
    String result = underTest.build(cloudContext, databaseStack);
    assertThat(JsonUtil.isValid(result)).overridingErrorMessage("Invalid JSON: " + result).isTrue();
    assertThat(result).contains("\"keyVaultName\": {\n" + "              \"type\": \"string\",\n" + "              \"defaultValue\" : \"dummyVault\",\n" + "              \"metadata\": {\n" + "                \"description\": \"Key vault name where the key to use is stored\"\n" + "              }\n" + "        },\n" + "        \"keyVaultResourceGroupName\": {\n" + "              \"type\": \"string\",\n" + "              \"defaultValue\" : \"dummyResourceGroup\",\n" + "              \"metadata\": {\n" + "                \"description\": \"Key vault resource group name where it is stored\"\n" + "              }\n" + "        },\n" + "        \"keyName\": {\n" + "              \"type\": \"string\",\n" + "              \"defaultValue\" : \"dummyKey\",\n" + "              \"metadata\": {\n" + "                \"description\": \"Key name in the key vault to use as encryption protector\"\n" + "              }\n" + "        },\n" + "        \"keyVersion\": {\n" + "              \"type\": \"string\",\n" + "              \"defaultValue\" : \"dummyVersion\",\n" + "              \"metadata\": {\n" + "                \"description\": \"Version of the key in the key vault to use as encryption protector\"\n" + "              }\n" + "        }");
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Region(com.sequenceiq.cloudbreak.cloud.model.Region) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) JsonUtil(com.sequenceiq.cloudbreak.common.json.JsonUtil) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) Spy(org.mockito.Spy) Location(com.sequenceiq.cloudbreak.cloud.model.Location) Network(com.sequenceiq.cloudbreak.cloud.model.Network) Template(freemarker.template.Template) MethodSource(org.junit.jupiter.params.provider.MethodSource) InjectMocks(org.mockito.InjectMocks) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) FreeMarkerConfigurationFactoryBean(org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) IOException(java.io.IOException) UUID(java.util.UUID) FreeMarkerTemplateUtils(com.sequenceiq.cloudbreak.util.FreeMarkerTemplateUtils) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Optional(java.util.Optional) OutboundInternetTraffic(com.sequenceiq.common.api.type.OutboundInternetTraffic) Collections(java.util.Collections) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) HashMap(java.util.HashMap) Network(com.sequenceiq.cloudbreak.cloud.model.Network) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) IOException(java.io.IOException) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with DatabaseServer

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

the class GcpDatabaseServerCheckServiceTest method testCheckWhenDbInstanceIsRunnableNOTAlwaysShouldReturnStarted.

@Test
public void testCheckWhenDbInstanceIsRunnableNOTAlwaysShouldReturnStarted() 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("RUNNABLE");
    Settings settings = new Settings();
    settings.setActivationPolicy("NOT_ALWAYS");
    databaseInstance.setSettings(settings);
    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) Settings(com.google.api.services.sqladmin.model.Settings) ExternalDatabaseStatus(com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus) Test(org.junit.jupiter.api.Test)

Example 8 with DatabaseServer

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

the class GcpDatabaseServerCheckServiceTest method testCheckWhenDbInstanceNotAvailableShouldReturnDeleted.

@Test
public void testCheckWhenDbInstanceNotAvailableShouldReturnDeleted() 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(true);
    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) ExternalDatabaseStatus(com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus) Test(org.junit.jupiter.api.Test)

Example 9 with DatabaseServer

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

the class GcpDatabaseServerCheckServiceTest method testCheckWhenDbInstanceDropGcpResourceExceptionShouldReturnGcpResourceException.

@Test
public void testCheckWhenDbInstanceDropGcpResourceExceptionShouldReturnGcpResourceException() 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);
    TokenResponseException tokenResponseException = mock(TokenResponseException.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()).thenThrow(tokenResponseException);
    when(gcpStackUtil.getMissingServiceAccountKeyError(any(TokenResponseException.class), anyString())).thenReturn(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) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException) Test(org.junit.jupiter.api.Test)

Example 10 with DatabaseServer

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

the class GcpDatabaseServerLaunchServiceTest method testLaunchWhenDatabaseNOTAlreadyExistAndNOTSharedProjectIdShouldCreate.

@Test
public void testLaunchWhenDatabaseNOTAlreadyExistAndNOTSharedProjectIdShouldCreate() throws Exception {
    Network network = new Network(new Subnet("10.0.0.0/16"), Map.of("subnetId", "s-1", "availabilityZone", "a"));
    Map<String, Object> map = new HashMap<>();
    map.put("engineVersion", "1");
    map.put("key", "value");
    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);
    DatabaseInstance databaseInstance = mock(DatabaseInstance.class);
    Compute.Subnetworks subnetworks = mock(Compute.Subnetworks.class);
    Compute.Subnetworks.Get subnetworksGet = mock(Compute.Subnetworks.Get.class);
    SQLAdmin.Users users = mock(SQLAdmin.Users.class);
    SQLAdmin.Users.Insert usersInsert = mock(SQLAdmin.Users.Insert.class);
    SQLAdmin.Instances.Get instancesGet = mock(SQLAdmin.Instances.Get.class);
    SQLAdmin.Instances sqlAdminInstances = mock(SQLAdmin.Instances.class);
    SQLAdmin.Instances.List sqlAdminInstancesList = mock(SQLAdmin.Instances.List.class);
    InstancesListResponse instancesListResponse = mock(InstancesListResponse.class);
    Operation operation = mock(Operation.class);
    SQLAdmin.Instances.Insert sqlAdminInstancesInsert = mock(SQLAdmin.Instances.Insert.class);
    IpMapping ipMapping = new IpMapping();
    ipMapping.setIpAddress("10.0.0.0");
    ipMapping.setType("PRIVATE");
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseStack.getNetwork()).thenReturn(network);
    when(gcpLabelUtil.createLabelsFromTagsMap(anyMap())).thenReturn(new HashMap<>());
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    when(gcpComputeFactory.buildCompute(any(CloudCredential.class))).thenReturn(compute);
    when(compute.subnetworks()).thenReturn(subnetworks);
    when(subnetworks.get(anyString(), anyString(), anyString())).thenReturn(subnetworksGet);
    when(subnetworksGet.execute()).thenReturn(new Subnetwork());
    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.getItems()).thenReturn(List.of());
    when(databaseInstance.getName()).thenReturn("name");
    when(sqlAdminInstances.get(anyString(), anyString())).thenReturn(instancesGet);
    when(instancesGet.execute()).thenReturn(databaseInstance);
    when(sqlAdminInstances.insert(anyString(), databaseInstanceArgumentCaptor.capture())).thenReturn(sqlAdminInstancesInsert);
    when(sqlAdminInstancesInsert.setPrettyPrint(anyBoolean())).thenReturn(sqlAdminInstancesInsert);
    when(sqlAdminInstancesInsert.execute()).thenReturn(operation);
    when(databaseInstance.getIpAddresses()).thenReturn(List.of(ipMapping));
    doNothing().when(databasePollerService).launchDatabasePoller(any(AuthenticatedContext.class), anyList());
    when(sqlAdmin.users()).thenReturn(users);
    when(users.insert(anyString(), anyString(), any(User.class))).thenReturn(usersInsert);
    when(usersInsert.execute()).thenReturn(operation);
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location(region("region"), availabilityZone("az1")));
    List<CloudResource> launch = underTest.launch(authenticatedContext, databaseStack, persistenceNotifier);
    Assert.assertEquals("value", databaseInstanceArgumentCaptor.getValue().getDiskEncryptionConfiguration().getKmsKeyName());
    Assert.assertEquals(1, launch.size());
}
Also used : User(com.google.api.services.sqladmin.model.User) HashMap(java.util.HashMap) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Operation(com.google.api.services.sqladmin.model.Operation) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) Network(com.sequenceiq.cloudbreak.cloud.model.Network) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) Subnetwork(com.google.api.services.compute.model.Subnetwork) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) IpMapping(com.google.api.services.sqladmin.model.IpMapping) Compute(com.google.api.services.compute.Compute) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Test(org.junit.jupiter.api.Test)

Aggregations

DatabaseServer (com.sequenceiq.cloudbreak.cloud.model.DatabaseServer)36 Test (org.junit.jupiter.api.Test)27 DatabaseStack (com.sequenceiq.cloudbreak.cloud.model.DatabaseStack)23 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)18 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)17 SQLAdmin (com.google.api.services.sqladmin.SQLAdmin)16 DatabaseInstance (com.google.api.services.sqladmin.model.DatabaseInstance)13 InstancesListResponse (com.google.api.services.sqladmin.model.InstancesListResponse)13 PersistenceNotifier (com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 Network (com.sequenceiq.cloudbreak.cloud.model.Network)8 ExternalDatabaseStatus (com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus)7 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)7 HashMap (java.util.HashMap)7 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)6 Settings (com.google.api.services.sqladmin.model.Settings)5 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)5 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)4 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)4 Compute (com.google.api.services.compute.Compute)4