Skip to content

Commit

Permalink
fix js test
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Mészáros <[email protected]>
  • Loading branch information
csviri committed Mar 31, 2024
1 parent 360be15 commit 2d2c38b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/csviri/operator/resourceglue/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static Map<String, GenericKubernetesResource> getActualResourcesByNameInW
var dependentSpec = glue.getSpec().getResources().stream()
.filter(r -> Utils.getApiVersion(r).equals(sr.getApiVersion())
&& Utils.getKind(r).equals(sr.getKind())
// checking the name from annotation since it might be templated name
// comparing the name from annotation since the resource name might be templated in spec
// therefore "Utils.getName(relatedResourceSpec).equals(sr.getMetadata().getName())" would not
// work
&& r.getName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;

import static io.csviri.operator.resourceglue.reconciler.glue.GlueReconciler.DEPENDENT_NAME_ANNOTATION_KEY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class JavaScripConditionTest {

public static final String DR_NAME = "secondary";
Context<Glue> mockContext = mock(Context.class);
DependentResource<GenericKubernetesResource, Glue> dr = mock(DependentResource.class);
Glue dummyGlue = new Glue();
Expand Down Expand Up @@ -63,25 +65,27 @@ void injectsTargetResourceResource() {
}

@Test
void injectsSecondaryResourcesResource() {
when(mockContext.getSecondaryResources(any())).thenReturn(Set.of(configMap()));
when(dr.getSecondaryResource(any(), any())).thenReturn(Optional.of(configMap()));

Glue glue = new Glue();
glue.setSpec(new ResourceGlueSpec());
glue.getSpec().setResources(new ArrayList<>());
var drSpec = new DependentResourceSpec();
drSpec.setName("secondary");
drSpec.setResource(configMap());
glue.getSpec().getResources().add(drSpec);
void injectsSecondaryResourcesResource() {
var cm = configMap();
cm.getMetadata().getAnnotations().put(DEPENDENT_NAME_ANNOTATION_KEY, DR_NAME);
when(mockContext.getSecondaryResources(any())).thenReturn(Set.of(cm));
when(dr.getSecondaryResource(any(), any())).thenReturn(Optional.of(cm));

Glue glue = new Glue();
glue.setSpec(new ResourceGlueSpec());
glue.getSpec().setResources(new ArrayList<>());
var drSpec = new DependentResourceSpec();
drSpec.setName(DR_NAME);
drSpec.setResource(configMap());
glue.getSpec().getResources().add(drSpec);

var condition = new JavaScripCondition("""
secondary.data.key1 == "val1";
""");
var condition = new JavaScripCondition("""
secondary.data.key1 == "val1";
""");

var res = condition.isMet(dr, glue, mockContext);
assertThat(res).isTrue();
}
var res = condition.isMet(dr, glue, mockContext);
assertThat(res).isTrue();
}

private GenericKubernetesResource configMap() {
try (InputStream is = JavaScripConditionTest.class.getResourceAsStream("/ConfigMap.yaml")) {
Expand Down

0 comments on commit 2d2c38b

Please sign in to comment.