Skip to content

Commit 89cd614

Browse files
committed
Add PodLog support
1 parent 3eb0bb0 commit 89cd614

File tree

4 files changed

+72
-29
lines changed

4 files changed

+72
-29
lines changed

operator/src/test/java/oracle/kubernetes/operator/DomainUpPlanTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private DomainPresenceStep getDomainPresenceStep() {
6363
}
6464

6565
@Before
66-
public void setUp() {
66+
public void setUp() throws NoSuchFieldException {
6767
mementos.add(TestUtils.silenceOperatorLogger());
6868
mementos.add(testSupport.install());
6969

operator/src/test/java/oracle/kubernetes/operator/helpers/KubernetesTestSupport.java

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class KubernetesTestSupport extends FiberTestSupport {
8585
public static final String PV = "PersistentVolume";
8686
public static final String PVC = "PersistentVolumeClaim";
8787
public static final String POD = "Pod";
88+
public static final String PODLOG = "PodLog";
8889
public static final String SERVICE = "Service";
8990
public static final String SUBJECT_ACCESS_REVIEW = "SubjectAccessReview";
9091
public static final String TOKEN_REVIEW = "TokenReview";
@@ -94,7 +95,7 @@ public class KubernetesTestSupport extends FiberTestSupport {
9495
*
9596
* @return a memento which can be used to restore the production factory
9697
*/
97-
public Memento install() {
98+
public Memento install() throws NoSuchFieldException {
9899
support(CUSTOM_RESOURCE_DEFINITION, V1beta1CustomResourceDefinition.class);
99100
support(SUBJECT_ACCESS_REVIEW, V1SubjectAccessReview.class);
100101
support(TOKEN_REVIEW, V1TokenReview.class);
@@ -105,10 +106,11 @@ public Memento install() {
105106
supportNamespaced(EVENT, V1Event.class, this::createEventList);
106107
supportNamespaced(JOB, V1Job.class, this::createJobList);
107108
supportNamespaced(POD, V1Pod.class, this::createPodList);
109+
supportNamespaced(PODLOG, String.class);
108110
supportNamespaced(PVC, V1PersistentVolumeClaim.class, this::createPVCList);
109111
supportNamespaced(SERVICE, V1Service.class, this::createServiceList);
110112

111-
return new CallBuilderMemento();
113+
return new KubernetesTestSupportMemento();
112114
}
113115

114116
private V1ConfigMapList createConfigMapList(List<V1ConfigMap> items) {
@@ -159,6 +161,11 @@ private <T> void support(
159161
repositories.put(resourceName, new DataRepository<>(resourceClass, toList));
160162
}
161163

164+
private <T> void supportNamespaced(String resourceName, Class<T> resourceClass) {
165+
dataTypes.put(resourceClass, resourceName);
166+
repositories.put(resourceName, new NamespacedDataRepository<>(resourceClass, null));
167+
}
168+
162169
private <T> void supportNamespaced(
163170
String resourceName, Class<T> resourceClass, Function<List<T>, Object> toList) {
164171
dataTypes.put(resourceClass, resourceName);
@@ -184,6 +191,10 @@ public final <T> void defineResources(T... resources) {
184191
for (T resource : resources) getDataRepository(resource).createResourceInNamespace(resource);
185192
}
186193

194+
public void definePodLog(String name, String namespace, Object contents) {
195+
repositories.get(PODLOG).createResourceInNamespace(name, namespace, contents);
196+
}
197+
187198
@SuppressWarnings("unchecked")
188199
private <T> DataRepository<T> getDataRepository(T resource) {
189200
return (DataRepository<T>) repositories.get(dataTypes.get(resource.getClass()));
@@ -228,19 +239,21 @@ public void runOnOperation(String resourceType, String name, String namespace, C
228239
}
229240
*/
230241

231-
private class CallBuilderMemento implements Memento {
242+
private class KubernetesTestSupportMemento implements Memento {
232243

233-
{
234-
{
235-
CallBuilder.setStepFactory(new AsyncRequestStepFactoryImpl());
236-
CallBuilder.setCallDispatcher(new CallDispatcherImpl());
237-
}
244+
private List<Memento> mementos = new ArrayList<>();
245+
246+
public KubernetesTestSupportMemento() throws NoSuchFieldException {
247+
// mementos.add(installEngine());
248+
CallBuilder.setStepFactory(new AsyncRequestStepFactoryImpl());
249+
CallBuilder.setCallDispatcher(new CallDispatcherImpl());
238250
}
239251

240252
@Override
241253
public void revert() {
242254
CallBuilder.resetStepFactory();
243255
CallBuilder.resetCallDispatcher();
256+
for (Memento memento : mementos) memento.revert();
244257
}
245258

246259
@Override
@@ -307,6 +320,11 @@ void createResourceInNamespace(T resource) {
307320
createResource(getMetadata(resource).getNamespace(), resource);
308321
}
309322

323+
@SuppressWarnings("unchecked")
324+
void createResourceInNamespace(String name, String namespace, Object resource) {
325+
data.put(name, (T) resource);
326+
}
327+
310328
T createResource(String namespace, T resource) {
311329
String name = getName(resource);
312330
if (name != null) {
@@ -346,7 +364,7 @@ private boolean hasLabel(V1ObjectMeta metadata, String selector) {
346364
}
347365

348366
private boolean includesLabel(Map<String, String> labels, String key, String value) {
349-
if (!labels.containsKey(key)) return false;
367+
if (labels == null || !labels.containsKey(key)) return false;
350368
return value == null || value.equals(labels.get(key));
351369
}
352370

@@ -418,25 +436,30 @@ T replaceResource(String name, T resource) {
418436
return resource;
419437
}
420438

421-
V1Status deleteResource(String namespace, String name) {
422-
if (!hasElementWithName(name)) throw new NotFoundException();
439+
V1Status deleteResource(String name, String namespace) {
440+
if (!hasElementWithName(name))
441+
throw new NotFoundException(getResourceName(), name, namespace);
423442
data.remove(name);
424443

425444
return new V1Status().code(200);
426445
}
427446

447+
private String getResourceName() {
448+
return dataTypes.get(resourceType);
449+
}
450+
428451
public V1Status deleteResourceCollection(String namespace) {
429452
data.clear();
430453
return new V1Status().code(200);
431454
}
432455

433-
public T readResource(String namespace, String name) {
434-
if (!data.containsKey(name)) throw new NotFoundException();
456+
public T readResource(String name, String namespace) {
457+
if (!data.containsKey(name)) throw new NotFoundException(getResourceName(), name, namespace);
435458
return data.get(name);
436459
}
437460

438-
public T patchResource(String namespace, String name, List<JsonObject> body) {
439-
if (!data.containsKey(name)) throw new NotFoundException();
461+
public T patchResource(String name, String namespace, List<JsonObject> body) {
462+
if (!data.containsKey(name)) throw new NotFoundException(getResourceName(), name, namespace);
440463

441464
JsonPatch patch = Json.createPatch(toJsonArray(body));
442465
JsonStructure result = patch.apply(toJsonStructure(data.get(name)));
@@ -508,33 +531,38 @@ private class NamespacedDataRepository<T> extends DataRepository<T> {
508531
this.listFactory = listFactory;
509532
}
510533

534+
@Override
535+
void createResourceInNamespace(String name, String namespace, Object resource) {
536+
inNamespace(namespace).createResourceInNamespace(name, namespace, resource);
537+
}
538+
511539
@Override
512540
T createResource(String namespace, T resource) {
513-
return inNamespace(namespace).createResource(null, resource);
541+
return inNamespace(namespace).createResource(namespace, resource);
514542
}
515543

516544
private DataRepository<T> inNamespace(String namespace) {
517545
return repositories.computeIfAbsent(namespace, n -> new DataRepository<>(resourceType, this));
518546
}
519547

520548
@Override
521-
V1Status deleteResource(String namespace, String name) {
522-
return inNamespace(namespace).deleteResource(null, name);
549+
V1Status deleteResource(String name, String namespace) {
550+
return inNamespace(namespace).deleteResource(name, namespace);
523551
}
524552

525553
@Override
526554
public V1Status deleteResourceCollection(String namespace) {
527-
return inNamespace(namespace).deleteResourceCollection(null);
555+
return inNamespace(namespace).deleteResourceCollection(namespace);
528556
}
529557

530558
@Override
531-
public T readResource(String namespace, String name) {
532-
return inNamespace(namespace).readResource(null, name);
559+
public T readResource(String name, String namespace) {
560+
return inNamespace(namespace).readResource(name, namespace);
533561
}
534562

535563
@Override
536-
public T patchResource(String namespace, String name, List<JsonObject> body) {
537-
return inNamespace(namespace).patchResource(null, name, body);
564+
public T patchResource(String name, String namespace, List<JsonObject> body) {
565+
return inNamespace(namespace).patchResource(name, namespace, body);
538566
}
539567

540568
@Override
@@ -669,7 +697,7 @@ private <T> T replaceResource(DataRepository<T> dataRepository) {
669697
}
670698

671699
private Object deleteResource(DataRepository dataRepository) {
672-
return dataRepository.deleteResource(requestParams.namespace, requestParams.name);
700+
return dataRepository.deleteResource(requestParams.name, requestParams.namespace);
673701
}
674702

675703
@SuppressWarnings("unchecked")
@@ -679,15 +707,15 @@ private List<JsonObject> asJsonObject(Object body) {
679707

680708
private Object patchResource(DataRepository dataRepository) {
681709
return dataRepository.patchResource(
682-
requestParams.namespace, requestParams.name, asJsonObject(requestParams.body));
710+
requestParams.name, requestParams.namespace, asJsonObject(requestParams.body));
683711
}
684712

685713
private Object listResources(DataRepository dataRepository) {
686714
return dataRepository.listResources(requestParams.namespace, fieldSelector, labelSelector);
687715
}
688716

689717
private <T> T readResource(DataRepository<T> dataRepository) {
690-
return dataRepository.readResource(requestParams.namespace, requestParams.name);
718+
return dataRepository.readResource(requestParams.name, requestParams.namespace);
691719
}
692720

693721
public Object deleteCollection(DataRepository dataRepository) {
@@ -763,7 +791,11 @@ public HttpErrorException getException() {
763791
}
764792
}
765793

766-
static class NotFoundException extends RuntimeException {}
794+
class NotFoundException extends RuntimeException {
795+
public NotFoundException(String resourceType, String name, String namespace) {
796+
super(String.format("No %s named %s found in namespace %s", resourceType, name, namespace));
797+
}
798+
}
767799

768800
static class HttpErrorException extends RuntimeException {
769801
private int status;

operator/src/test/java/oracle/kubernetes/operator/helpers/KubernetesTestSupportTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
public class KubernetesTestSupportTest {
5353
private static final String NS = "namespace1";
54+
private static final String POD_LOG_CONTENTS = "asdfghjkl";
5455
private KubernetesTestSupport testSupport = new KubernetesTestSupport();
5556
List<Memento> mementos = new ArrayList<>();
5657

@@ -334,6 +335,16 @@ public void whenConfigMapNotFound_readStatusIsNotFound() {
334335
assertThat(packet.getSPI(CallResponse.class).getE(), notNullValue());
335336
}
336337

338+
@Test
339+
public void whenDefined_readPodLog() {
340+
TestResponseStep<String> endStep = new TestResponseStep<>();
341+
testSupport.definePodLog("name", "namespace", POD_LOG_CONTENTS);
342+
343+
testSupport.runSteps(new CallBuilder().readPodLogAsync("name", "namespace", endStep));
344+
345+
assertThat(endStep.callResponse.getResult(), equalTo(POD_LOG_CONTENTS));
346+
}
347+
337348
static class TestResponseStep<T> extends DefaultResponseStep<T> {
338349
private CallResponse<T> callResponse;
339350

operator/src/test/java/oracle/kubernetes/operator/helpers/ServiceHelperDeletionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private V1Service createMinimalService() {
3131
}
3232

3333
@Before
34-
public void setUpDeletionTest() {
34+
public void setUpDeletionTest() throws NoSuchFieldException {
3535
mementos.add(TestUtils.silenceOperatorLogger());
3636
mementos.add(testSupport.install());
3737

0 commit comments

Comments
 (0)