Skip to content

Commit

Permalink
fix: unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dyx1234 committed Sep 25, 2024
1 parent 8705ba1 commit d8c51dd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,99 +53,94 @@ public void initClient() {
* Creates a Kubernetes ConfigMap.
*
* @param configMapNamespace the namespace of the ConfigMap
* @param name the name of the ConfigMap
* @param data the data to be stored in the ConfigMap
* @param name the name of the ConfigMap
* @param data the data to be stored in the ConfigMap
* @return the name of the created ConfigMap
* @throws RuntimeException if an error occurs while creating the ConfigMap
*/
public String createConfigMap(String configMapNamespace, String name, Map<String, String> data) {
if (configMapNamespace == null || configMapNamespace == "" || name == null || name == "") {
log.error("create config map failed due to null parameter");
throw new RuntimeException("ConfigMap namespace and name cannot be null or empty");
if (configMapNamespace == null || configMapNamespace.isEmpty() || name == null || name.isEmpty()) {
log.error("create config map failed due to null or empty parameter: configMapNamespace={}, name={}", configMapNamespace, name);
throw new IllegalArgumentException("ConfigMap namespace and name cannot be null or empty");
}
V1ConfigMap configMap = new V1ConfigMap().metadata(new V1ObjectMeta().name(name).namespace(configMapNamespace)).data(data);
try {
coreV1Api.createNamespacedConfigMap(configMapNamespace, configMap, null, null, null,null);
coreV1Api.createNamespacedConfigMap(configMapNamespace, configMap, null, null, null, null);
return name;
} catch (Exception e) {
log.error("create config map failed", e);
throw new RuntimeException("Failed to create ConfigMap: " + e.getMessage(), e);
}
}

/**
* get value from config map
*
* @param configMapNamespace
* @param name config map name (appId)
* @param name config map name (appId)
* @return configMap data(all key-value pairs in config map)
*/
public String loadFromConfigMap(String configMapNamespace, String name) {
if (configMapNamespace == null || configMapNamespace.isEmpty() || name == null || name.isEmpty() || name == null || name.isEmpty()) {
log.error("参数不能为空");
throw new RuntimeException(String
.format("参数不能为空, configMapNamespace: %s, name: %s", configMapNamespace, name));
if (configMapNamespace == null || configMapNamespace.isEmpty() || name == null || name.isEmpty() ) {
String errorMessage = String.format("Parameters can not be null or empty, configMapNamespace: '%s', name: '%s'", configMapNamespace, name);
log.error(errorMessage);
throw new IllegalArgumentException(errorMessage);
}
try {
log.info("Starting to read ConfigMap: " + name);
V1ConfigMap configMap = coreV1Api.readNamespacedConfigMap(name, configMapNamespace, null);
if (configMap == null) {
log.error("ConfigMap不存在");
throw new RuntimeException(String
.format("ConfigMap不存在, configMapNamespace: %s, name: %s", configMapNamespace, name));
throw new RuntimeException(String.format("ConfigMap does not exist, configMapNamespace: %s, name: %s", configMapNamespace, name));
}
Map<String, String> data = configMap.getData();
if (data != null && data.containsKey(name)) {
return data.get(name);
} else {
log.error("在ConfigMap中未找到指定的键: " + name);
throw new RuntimeException(String
.format("在ConfigMap中未找到指定的键: %s, configMapNamespace: %s, name: %s", name, configMapNamespace, name));
throw new RuntimeException(String.format("Specified key not found in ConfigMap: %s, configMapNamespace: %s, name: %s", name, configMapNamespace, name));
}
} catch (Exception e) {
log.error("get config map failed", e);
throw new RuntimeException(String
.format("get config map failed, configMapNamespace: %s, name: %s", configMapNamespace, name));
}
}

/**
* get value from config map
*
* @param configMapNamespace configMapNamespace
* @param name config map name (appId)
* @param key config map key (cluster+namespace)
* @param name config map name (appId)
* @param key config map key (cluster+namespace)
* @return value(json string)
*/
public String getValueFromConfigMap(String configMapNamespace, String name, String key) {
if (configMapNamespace == null || configMapNamespace.isEmpty() || name == null || name.isEmpty() || key == null || key.isEmpty()) {
log.error("参数不能为空");
log.error("Parameters can not be null or empty: configMapNamespace={}, name={}", configMapNamespace, name);
return null;
}
try {
V1ConfigMap configMap = coreV1Api.readNamespacedConfigMap(name, configMapNamespace, null);
if (configMap == null || configMap.getData() == null) {
log.error("ConfigMap不存在或没有数据");
return null;
if (configMap == null) {
throw new RuntimeException(String.format("ConfigMap does not exist, configMapNamespace: %s, name: %s", configMapNamespace, name));
}
if (!configMap.getData().containsKey(key)) {
log.error("在ConfigMap中未找到指定的键: " + key);
return null;
throw new RuntimeException(String.format("Specified key not found in ConfigMap: %s, configMapNamespace: %s, name: %s", name, configMapNamespace, name));
}
return configMap.getData().get(key);
} catch (Exception e) {
log.error("get config map failed", e);
return null;
}
}

/**
* update config map
*
* @param configMapNamespace
* @param name config map name (appId)
* @param data new data
* @param name config map name (appId)
* @param data new data
* @return config map name
*/
public String updateConfigMap(String configMapNamespace, String name, Map<String, String> data) {
if (configMapNamespace == null || configMapNamespace.isEmpty() || name == null || name.isEmpty() || data == null || data.isEmpty()) {
log.error("参数不能为空");
log.error("Parameters can not be null or empty: configMapNamespace={}, name={}", configMapNamespace, name);
return null;
}
try {
Expand All @@ -160,13 +155,14 @@ public String updateConfigMap(String configMapNamespace, String name, Map<String

/**
* check config map exist
*
* @param configMapNamespace config map namespace
* @param configMapName config map name
* @param configMapName config map name
* @return true if config map exist, false otherwise
*/
public boolean checkConfigMapExist(String configMapNamespace, String configMapName) {
if (configMapNamespace == null || configMapNamespace.isEmpty() || configMapName == null || configMapName.isEmpty()) {
log.error("参数不能为空");
log.error("Parameters can not be null or empty: configMapNamespace={}, configMapName={}", configMapNamespace, configMapName);
return false;
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testCreateConfigMapSuccess() throws Exception {
}

/**
* 测试 createConfigMap 传入空的命名空间
* 测试 createConfigMap 传入空的命名空间,抛出异常
*/
@Test
public void testCreateConfigMapEmptyNamespace() throws Exception {
Expand All @@ -80,15 +80,16 @@ public void testCreateConfigMapEmptyNamespace() throws Exception {
data.put("key", "value");

// act
String result = kubernetesManager.createConfigMap(namespace, name, data);
assertThrows("create config map failed due to null parameter", IllegalArgumentException.class, () -> {
kubernetesManager.createConfigMap(namespace, name, data);
});

// assert
verify(coreV1Api, never()).createNamespacedConfigMap(anyString(), any(V1ConfigMap.class), isNull(), isNull(), isNull(),isNull());
assert result == null;
}

/**
* 测试 createConfigMap 传入空的配置名
* 测试 createConfigMap 传入空的配置名,抛出异常
*/
@Test
public void testCreateConfigMapEmptyName() throws Exception {
Expand All @@ -99,15 +100,16 @@ public void testCreateConfigMapEmptyName() throws Exception {
data.put("key", "value");

// act
String result = kubernetesManager.createConfigMap(namespace, name, data);
assertThrows("create config map failed due to null parameter", IllegalArgumentException.class, () -> {
kubernetesManager.createConfigMap(namespace, name, data);
});

// assert
verify(coreV1Api, never()).createNamespacedConfigMap(anyString(), any(V1ConfigMap.class), isNull(), isNull(), isNull(),isNull());
assert result == null;
}

/**
* 测试 createConfigMap 传入 null 作为数据
* 测试 createConfigMap 传入 null 作为数据,正常执行
*/
@Test
public void testCreateConfigMapNullData() throws Exception {
Expand Down Expand Up @@ -153,11 +155,10 @@ public void testLoadFromConfigMapFailure() throws Exception {
String name = "TestName";
when(coreV1Api.readNamespacedConfigMap(name, namespace, null)).thenThrow(new ApiException("Kubernetes Manager Exception"));

// act
String result = kubernetesManager.loadFromConfigMap(namespace, name);
assertThrows(String.format("get config map failed, configMapNamespace: %s, name: %s", namespace, name), RuntimeException.class, () -> {
kubernetesManager.loadFromConfigMap(namespace, name);
});

// assert
assertNull(result);
}

/**
Expand All @@ -171,10 +172,10 @@ public void testLoadFromConfigMapConfigMapNotFound() throws Exception {
when(coreV1Api.readNamespacedConfigMap(name, namespace, null)).thenReturn(null);

// act
String result = kubernetesManager.loadFromConfigMap(namespace, name);
assertThrows(String.format("get config map failed, configMapNamespace: %s, name: %s", namespace, name), RuntimeException.class, () -> {
kubernetesManager.loadFromConfigMap(namespace, name);
});

// assert
assertNull(result);
}

/**
Expand Down

0 comments on commit d8c51dd

Please sign in to comment.