我们正在使用kubernetes客户端测试我们的代码。
我们遇到了一些有趣的行为,如果删除,例如配置图:
GET
返回200
,DELETE
返回200
:delete()
返回true
GET
返回200
,DELETE
返回404
:delete()
返回true
GET
返回404
,DELETE
根本没有被调用:delete()
返回false
GET
返回200
,DELETE
返回400
:delete()
抛出KubernetesClientException
GET
返回400
:delete()
抛出KubernetesClientException
所以我们想知道,如果真的一致,200/404
会返回true
而404/nc
会返回false
。两者都应返回true
或两者都应返回false
。
所以我们想知道,这可能是一个错误吗?
根据io.fabric8.kubernetes.client.dsl.base.BaseOperation.delete()
,如果false
404
try {
deleteList();
return true;
} catch (KubernetesClientException e) {
if (e.getCode() != 404) {
throw e;
}
return false;
}
以下是测试示例:
// GET return 200
// DELETE return 200
// kubernetesClient.delete() return TRUE
server.expect().get().withPath(format("/api/v1/namespaces/{}/configmaps?labelSelector={}%3D{}", namespace, name, value)).andReturn(200, configMapList).once();
server.expect().delete().withPath(format("/api/v1/namespaces/{}/configmaps/{}", namespace, configMapName)).andReturn(200, StringUtils.EMPTY).once();
actual = kubernetesClient.configMaps().inNamespace(namespace).withLabelSelector(labelSelector).delete();
assertTrue(actual)
// GET return 200
// DELETE return 404
// kubernetesClient.delete() return TRUE
server.expect().get().withPath(format("/api/v1/namespaces/{}/configmaps?labelSelector={}%3D{}", namespace, name, value)).andReturn(200, configMapList).once();
server.expect().delete().withPath(format("/api/v1/namespaces/{}/configmaps/{}", namespace, configMapName)).andReturn(404, StringUtils.EMPTY).once();
actual = kubernetesClient.configMaps().inNamespace(namespace).withLabelSelector(labelSelector).delete();
assertTrue(actual)
// GET return 404
// kubernetesClient.delete() return FALSE
server.expect().get().withPath(format("/api/v1/namespaces/{}/configmaps?labelSelector={}%3D{}", namespace, name, value)).andReturn(404, StringUtils.EMPTY).once();
actual = kubernetesClient.configMaps().inNamespace(namespace).withLabelSelector(labelSelector).delete();
assertFalse(actual)
// GET return 200
// DELETE return 400
// kubernetesClient.delete() throws KubernetesClientException
server.expect().get().withPath(format("/api/v1/namespaces/{}/configmaps?labelSelector={}%3D{}", namespace, name, value)).andReturn(200, configMapList).once();
server.expect().delete().withPath(format("/api/v1/namespaces/{}/configmaps/{}", namespace, configMapName)).andReturn(400, StringUtils.EMPTY).once();
try {
kubernetesClient.configMaps().inNamespace(namespace).withLabelSelector(labelSelector).delete();
fail();
} catch (io.fabric8.kubernetes.client.KubernetesClientException e) {
assertTrue(true);
}
// GET return 400
// kubernetesClient.delete() throws KubernetesClientException
server.expect().get().withPath(format("/api/v1/namespaces/{}/configmaps?labelSelector={}%3D{}", namespace, name, value)).andReturn(400, StringUtils.EMPTY).once();
try {
kubernetesClient.configMaps().inNamespace(namespace).withLabelSelector(labelSelector).delete();
fail();
} catch (io.fabric8.kubernetes.client.KubernetesClientException e) {
assertTrue(true);
}