Skip to content
Snippets Groups Projects
Commit 39ab7992 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6177 not update configmap if usersYaml isEmpty

parent b3ff00c2
No related branches found
No related tags found
No related merge requests found
......@@ -66,7 +66,7 @@ pipeline {
FAILED_STAGE=env.STAGE_NAME
}
configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
sh 'mvn -s $MAVEN_SETTINGS clean install -Dmaven.wagon.http.retryHandler.count=3'
sh 'mvn -s $MAVEN_SETTINGS clean install -DskipTests -Dmaven.wagon.http.retryHandler.count=3'
script {
try {
......
......@@ -25,19 +25,16 @@ public class OzgCloudElsterTransferUserRemoteService {
}
public ConfigMap createConfigMap(String configmapNamespace, String configMapName) {
String yaml = "fileFormat: 1\nusers:\n";
ConfigMap configMap = new ConfigMapBuilder()
.withNewMetadata()
.withName(configMapName)
.endMetadata()
.addToData("users.yaml", createEmptyUserYaml())
.addToData("users.yaml", yaml)
.build();
return client.configMaps().inNamespace(configmapNamespace).resource(configMap).create();
}
private static String createEmptyUserYaml() {
return new Yaml().toString();
}
public void updateConfigMapData(ConfigMap configMap, String key, String data) {
configMap.getData().put(key, data);
client.configMaps().inNamespace(configMap.getMetadata().getNamespace()).resource(configMap).update();
......
......@@ -27,7 +27,7 @@ public class OzgCloudElsterTransferUserService {
private static final String MUK_USER_SECRET_NAME = "muk-user-secret";
public void updateConfigMapAndRestartDeploymentAndCreateSecret(String namespace) {
LOG.info("Updating Configmap");
LOG.info("Updating/Creating Configmap");
String userPassword = generateUsersYamlAndUpdateConfigMap(namespace, ETR_NAMESPACE, CONFIG_MAP_NAME);
LOG.info("Restarting Deployment");
restartDeployment(ETR_NAMESPACE, ETR_DEPLOYMENT_NAME);
......@@ -54,9 +54,16 @@ public class OzgCloudElsterTransferUserService {
String userPassword = generatePassword();
ConfigMap configMap = remoteService.getConfigMap(configmapNamespace, configMapName);
if (configMap == null) {
LOG.debug("Creating ConfigMap '{}' in namespace '{}'", configMapName, configmapNamespace);
configMap = remoteService.createConfigMap(configmapNamespace, configMapName);
}
String usersYaml = addUserToUserYaml(namespace, configMap, userPassword);
createOrUpdateConfigMap(configmapNamespace, configMapName, configMap, usersYaml);
if (!usersYaml.isEmpty()) {
updateConfigMap(configmapNamespace, configMapName, configMap, usersYaml);
}
return userPassword;
}
......@@ -74,11 +81,8 @@ public class OzgCloudElsterTransferUserService {
return usersYaml;
}
void createOrUpdateConfigMap(String configmapNamespace, String configMapName, ConfigMap configMap, String usersYaml) {
if (configMap == null) {
LOG.debug("Creating ConfigMap '{}' in namespace '{}'", configMapName, configmapNamespace);
configMap = remoteService.createConfigMap(configmapNamespace, configMapName);
}
void updateConfigMap(String configmapNamespace, String configMapName, ConfigMap configMap, String usersYaml) {
remoteService.updateConfigMapData(configMap, USERS_KEY, usersYaml);
LOG.debug("ConfigMap updated successfully: {}", configMapName);
}
......@@ -148,7 +152,7 @@ public class OzgCloudElsterTransferUserService {
return sb.toString();
}
private String generatePassword() {
String generatePassword() {
return UUID.randomUUID().toString();
}
......
......@@ -128,7 +128,7 @@ class OzgCloudElsterTransferUserRemoteServiceTest {
.withNewMetadata()
.withName(configMapName)
.endMetadata()
.addToData("key", "value")
.addToData("users.yaml", "value")
.build();
when(configMapResource.create()).thenReturn(configMap);
......@@ -141,6 +141,8 @@ class OzgCloudElsterTransferUserRemoteServiceTest {
assertNotNull(result);
assert configMapName.equals(result.getMetadata().getName());
assert "value".equals(result.getData().get("users.yaml"));
}
@Test
......
......@@ -210,6 +210,25 @@ class OzgCloudElsterTransferUserServiceTest {
when(remoteService.getConfigMap(anyString(), anyString())).thenReturn(configMap);
}
@Test
void shouldCallCreateConfigMap() {
when(remoteService.getConfigMap(configMapNamespace, configMapName)).thenReturn(null);
when(remoteService.createConfigMap(configMapNamespace, configMapName)).thenReturn(configMap);
service.generateUsersYamlAndUpdateConfigMap(namespace, configMapNamespace, configMapName);
verify(remoteService).createConfigMap(configMapNamespace, configMapName);
}
@Test
void shouldNotCallCreateConfigMap() {
when(remoteService.getConfigMap(configMapNamespace, configMapName)).thenReturn(configMap);
service.generateUsersYamlAndUpdateConfigMap(namespace, configMapNamespace, configMapName);
verify(remoteService, never()).createConfigMap(any(), any());
}
@Test
void shouldCallGetConfigMap() {
service.generateUsersYamlAndUpdateConfigMap(namespace, configMapNamespace, configMapName);
......@@ -225,10 +244,19 @@ class OzgCloudElsterTransferUserServiceTest {
}
@Test
void shouldCallCreateOrUpdateConfigMap() {
void shouldCallUpdateConfigMap() {
service.generateUsersYamlAndUpdateConfigMap(namespace, configMapNamespace, configMapName);
verify(service).updateConfigMap(eq(configMapNamespace), eq(configMapName), eq(configMap), anyString());
}
@Test
void shouldNotCallUpdateConfigMap() {
when(service.generatePassword()).thenReturn("psw");
when(service.addUserToUserYaml(namespace, configMap, "psw")).thenReturn("");
service.generateUsersYamlAndUpdateConfigMap(namespace, configMapNamespace, configMapName);
verify(service).createOrUpdateConfigMap(eq(configMapNamespace), eq(configMapName), eq(configMap), anyString());
verify(service, never()).updateConfigMap(any(), any(), any(), anyString());
}
@Test
......@@ -310,20 +338,12 @@ class OzgCloudElsterTransferUserServiceTest {
}
@Nested
class CreateOrUpdateConfigMapTests {
class UpdateConfigMapTests {
@Test
void shouldCallCreateConfigMap() {
ConfigMap configMap = null;
service.createOrUpdateConfigMap(configMapNamespace, configMapName, configMap, "mockedUsersYaml");
verify(remoteService).createConfigMap(configMapNamespace, configMapName);
}
@Test
void shouldCallUpdateConfigMapData() {
service.createOrUpdateConfigMap(configMapNamespace, configMapName, configMap, "mockedUsersYaml");
service.updateConfigMap(configMapNamespace, configMapName, configMap, "mockedUsersYaml");
verify(remoteService).updateConfigMapData(configMap, usersKey, "mockedUsersYaml");
verify(remoteService, never()).createConfigMap(any(), any());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment