|
1 | 1 | package io.javaoperatorsdk.admissioncontroller;
|
2 | 2 |
|
3 |
| -import java.io.IOException; |
4 |
| -import java.io.InputStream; |
5 |
| -import java.util.Base64; |
6 |
| -import java.util.UUID; |
7 |
| - |
8 | 3 | import org.junit.jupiter.api.Test;
|
9 | 4 |
|
10 | 5 | import io.fabric8.kubernetes.api.model.HasMetadata;
|
11 |
| -import io.fabric8.kubernetes.api.model.admission.v1.AdmissionRequest; |
12 |
| -import io.fabric8.kubernetes.api.model.admission.v1.AdmissionReview; |
13 |
| -import io.fabric8.kubernetes.api.model.apps.Deployment; |
14 |
| -import io.fabric8.kubernetes.client.utils.Serialization; |
15 | 6 |
|
16 |
| -import static org.assertj.core.api.Assertions.assertThat; |
| 7 | +import static io.javaoperatorsdk.admissioncontroller.Commons.*; |
17 | 8 |
|
18 | 9 | class AdmissionControllerTest {
|
19 | 10 |
|
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 | 11 | @Test
|
25 |
| - public void validatesResource() { |
| 12 | + void validatesResource() { |
26 | 13 | AdmissionController<HasMetadata> admissionController =
|
27 | 14 | new AdmissionController<>((resource, operation) -> {
|
28 |
| - if (resource.getMetadata().getLabels().get(LABEL_KEY) == null) { |
| 15 | + if (resource.getMetadata().getLabels().get(Commons.LABEL_KEY) == null) { |
29 | 16 | throw new NotAllowedException(MISSING_REQUIRED_LABEL);
|
30 | 17 | }
|
31 | 18 | });
|
32 | 19 | var inputAdmissionReview = createTestAdmissionReview();
|
33 | 20 |
|
34 | 21 | var response = admissionController.handle(inputAdmissionReview);
|
35 | 22 |
|
36 |
| - assertThat(response.getResponse().getUid()) |
37 |
| - .isEqualTo(inputAdmissionReview.getRequest().getUid()); |
38 |
| - assertThat(response.getResponse().getStatus().getCode()).isEqualTo(403); |
39 |
| - assertThat(response.getResponse().getStatus().getMessage()).isEqualTo(MISSING_REQUIRED_LABEL); |
40 |
| - assertThat(response.getResponse().getAllowed()).isFalse(); |
| 23 | + assertValidation(response, inputAdmissionReview.getRequest().getUid()); |
41 | 24 | }
|
42 | 25 |
|
43 | 26 | @Test
|
44 |
| - public void mutatesResource() { |
| 27 | + void mutatesResource() { |
45 | 28 | AdmissionController<HasMetadata> admissionController =
|
46 | 29 | new AdmissionController<>((resource, operation) -> {
|
47 |
| - resource.getMetadata().getLabels().putIfAbsent(LABEL_KEY, LABEL_TEST_VALUE); |
| 30 | + resource.getMetadata().getLabels().putIfAbsent(Commons.LABEL_KEY, LABEL_TEST_VALUE); |
48 | 31 | return resource;
|
49 | 32 | });
|
50 | 33 | var inputAdmissionReview = createTestAdmissionReview();
|
51 | 34 |
|
52 | 35 | var response = admissionController.handle(inputAdmissionReview);
|
53 | 36 |
|
54 |
| - assertThat(response.getResponse().getAllowed()).isTrue(); |
55 |
| - String patch = new String(Base64.getDecoder().decode(response.getResponse().getPatch())); |
56 |
| - assertThat(patch) |
57 |
| - .isEqualTo( |
58 |
| - "[{\"op\":\"add\",\"path\":\"/metadata/labels/app.kubernetes.io~1name\",\"value\":\"mutation-test\"}]"); |
| 37 | + assertMutation(response); |
59 | 38 | }
|
60 |
| - |
61 |
| - private AdmissionReview createTestAdmissionReview() { |
62 |
| - AdmissionReview admissionReview = new AdmissionReview(); |
63 |
| - AdmissionRequest request = new AdmissionRequest(); |
64 |
| - admissionReview.setRequest(request); |
65 |
| - request.setOperation(Operation.CREATE.name()); |
66 |
| - request.setUid(UUID.randomUUID().toString()); |
67 |
| - Deployment deployment = null; |
68 |
| - try (InputStream is = getClass().getResourceAsStream("deployment.yaml")) { |
69 |
| - deployment = Serialization.unmarshal(is, Deployment.class); |
70 |
| - request.setObject(deployment); |
71 |
| - } catch (IOException e) { |
72 |
| - throw new IllegalStateException(e); |
73 |
| - } |
74 |
| - return admissionReview; |
75 |
| - } |
76 |
| - |
77 | 39 | }
|
0 commit comments