Skip to content

Commit b297f43

Browse files
committed
fix: added unit test
1 parent b349165 commit b297f43

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

core/src/main/java/io/javaoperatorsdk/admissioncontroller/AdmissionUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public static AdmissionResponse admissionResponseFromMutation(KubernetesResource
4141
var mutatedResNode = mapper.valueToTree(mutatedResource);
4242

4343
var diff = JsonDiff.asJson(originalResNode, mutatedResNode);
44-
String base64Diff =
45-
Base64.getEncoder().encodeToString(diff.textValue().getBytes(StandardCharsets.UTF_8));
44+
String base64Diff = Base64.getEncoder().encodeToString(diff.toString().getBytes(StandardCharsets.UTF_8));
4645
admissionResponse.setPatch(base64Diff);
4746
return admissionResponse;
4847
}

core/src/main/java/io/javaoperatorsdk/admissioncontroller/NotAllowedException.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
public class NotAllowedException extends AdmissionControllerException {
44

5-
private int code;
5+
private int code = 403;
66

77
public NotAllowedException() {}
88

9+
public NotAllowedException(String message) {
10+
super(message);
11+
}
12+
913
public NotAllowedException(int code) {
1014
this.code = code;
1115
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package io.javaoperatorsdk.admissioncontroller;
2+
3+
import io.fabric8.kubernetes.api.model.HasMetadata;
4+
import io.fabric8.kubernetes.api.model.admission.v1.AdmissionRequest;
5+
import io.fabric8.kubernetes.api.model.admission.v1.AdmissionReview;
6+
import io.fabric8.kubernetes.api.model.apps.Deployment;
7+
import io.fabric8.kubernetes.client.utils.Serialization;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.util.Base64;
13+
import java.util.UUID;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.assertj.core.api.Assertions.in;
17+
18+
class AdmissionControllerTest {
19+
20+
public static final String MISSING_REQUIRED_LABEL = "Missing required label.";
21+
public static final String LABEL_KEY = "app.kubernetes.io/name";
22+
public static final String LABEL_TEST_VALUE = "mutation-test";
23+
24+
@Test
25+
public void validatesResource() {
26+
AdmissionController<HasMetadata> admissionController = new AdmissionController<>((resource, operation) -> {
27+
if (resource.getMetadata().getLabels().get(LABEL_KEY) == null) {
28+
throw new NotAllowedException(MISSING_REQUIRED_LABEL);
29+
}
30+
});
31+
var inputAdmissionReview = createTestAdmissionReview();
32+
33+
var response = admissionController.handle(inputAdmissionReview);
34+
35+
assertThat(response.getResponse().getUid()).isEqualTo(inputAdmissionReview.getRequest().getUid());
36+
assertThat(response.getResponse().getStatus().getCode()).isEqualTo(403);
37+
assertThat(response.getResponse().getStatus().getMessage()).isEqualTo(MISSING_REQUIRED_LABEL);
38+
assertThat(response.getResponse().getAllowed()).isFalse();
39+
}
40+
41+
@Test
42+
public void mutatesResource() {
43+
AdmissionController<HasMetadata> admissionController = new AdmissionController<>((resource, operation) -> {
44+
resource.getMetadata().getLabels().putIfAbsent(LABEL_KEY, LABEL_TEST_VALUE);
45+
return resource;
46+
});
47+
var inputAdmissionReview = createTestAdmissionReview();
48+
49+
var response = admissionController.handle(inputAdmissionReview);
50+
51+
assertThat(response.getResponse().getAllowed()).isTrue();
52+
String patch = new String(Base64.getDecoder().decode(response.getResponse().getPatch()));
53+
assertThat(patch)
54+
.isEqualTo("[{\"op\":\"add\",\"path\":\"/metadata/labels/app.kubernetes.io~1name\",\"value\":\"mutation-test\"}]");
55+
}
56+
57+
private AdmissionReview createTestAdmissionReview() {
58+
AdmissionReview admissionReview = new AdmissionReview();
59+
AdmissionRequest request = new AdmissionRequest();
60+
admissionReview.setRequest(request);
61+
request.setOperation(Operation.CREATE.name());
62+
request.setUid(UUID.randomUUID().toString());
63+
Deployment deployment = null;
64+
try(InputStream is = getClass().getResourceAsStream("deployment.yaml")) {
65+
deployment = Serialization.unmarshal(is, Deployment.class);
66+
request.setObject(deployment);
67+
} catch (IOException e) {
68+
throw new IllegalStateException(e);
69+
}
70+
return admissionReview;
71+
}
72+
73+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: nginx-deployment
5+
labels:
6+
app: nginx
7+
spec:
8+
replicas: 3
9+
selector:
10+
matchLabels:
11+
app: nginx
12+
template:
13+
metadata:
14+
labels:
15+
app: nginx
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx:1.14.2
20+
ports:
21+
- containerPort: 80

0 commit comments

Comments
 (0)