Skip to content

Commit 958d1d6

Browse files
authored
test: finalizer adding without ssa (#3487)
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 8ddc5e8 commit 958d1d6

3 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright Java Operator SDK Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.javaoperatorsdk.operator.baseapi.finalizernossa;
17+
18+
import io.fabric8.kubernetes.api.model.Namespaced;
19+
import io.fabric8.kubernetes.client.CustomResource;
20+
import io.fabric8.kubernetes.model.annotation.Group;
21+
import io.fabric8.kubernetes.model.annotation.Kind;
22+
import io.fabric8.kubernetes.model.annotation.ShortNames;
23+
import io.fabric8.kubernetes.model.annotation.Version;
24+
25+
@Group("sample.javaoperatorsdk")
26+
@Version("v1")
27+
@Kind("AddFinalizerNoSSACustomResource")
28+
@ShortNames("afnossa")
29+
public class AddFinalizerNoSSACustomResource extends CustomResource<Void, String>
30+
implements Namespaced {}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright Java Operator SDK Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.javaoperatorsdk.operator.baseapi.finalizernossa;
17+
18+
import java.util.concurrent.TimeUnit;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.RegisterExtension;
22+
23+
import io.fabric8.kubernetes.api.model.ObjectMeta;
24+
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.awaitility.Awaitility.await;
28+
29+
class AddFinalizerNoSSAIT {
30+
31+
public static final String TEST_RESOURCE_NAME = "add-finalizer-no-ssa-test1";
32+
33+
@RegisterExtension
34+
LocallyRunOperatorExtension operator =
35+
LocallyRunOperatorExtension.builder()
36+
.withConfigurationService(o -> o.withUseSSAToPatchPrimaryResource(false))
37+
.withReconciler(new AddFinalizerNoSSAReconciler())
38+
.build();
39+
40+
@Test
41+
void addsFinalizerWithoutSSAAndRemovesItOnCleanup() {
42+
var reconciler = operator.getReconcilerOfType(AddFinalizerNoSSAReconciler.class);
43+
44+
var testResource = createTestResource();
45+
operator.create(testResource);
46+
47+
await("finalizer added")
48+
.atMost(5, TimeUnit.SECONDS)
49+
.untilAsserted(
50+
() -> {
51+
var actual = operator.get(AddFinalizerNoSSACustomResource.class, TEST_RESOURCE_NAME);
52+
assertThat(actual).isNotNull();
53+
assertThat(actual.getMetadata().getFinalizers()).hasSize(1);
54+
});
55+
56+
operator.delete(testResource);
57+
58+
await("resource deleted after finalizer removal")
59+
.atMost(5, TimeUnit.SECONDS)
60+
.until(
61+
() -> operator.get(AddFinalizerNoSSACustomResource.class, TEST_RESOURCE_NAME) == null);
62+
63+
assertThat(reconciler.getNumberOfExecutions()).isEqualTo(1);
64+
assertThat(reconciler.getNumberOfCleanupExecutions()).isEqualTo(1);
65+
}
66+
67+
private AddFinalizerNoSSACustomResource createTestResource() {
68+
AddFinalizerNoSSACustomResource cr = new AddFinalizerNoSSACustomResource();
69+
cr.setMetadata(new ObjectMeta());
70+
cr.getMetadata().setName(TEST_RESOURCE_NAME);
71+
return cr;
72+
}
73+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright Java Operator SDK Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.javaoperatorsdk.operator.baseapi.finalizernossa;
17+
18+
import java.util.concurrent.atomic.AtomicInteger;
19+
20+
import io.javaoperatorsdk.operator.api.reconciler.*;
21+
import io.javaoperatorsdk.operator.support.TestExecutionInfoProvider;
22+
23+
@ControllerConfiguration
24+
public class AddFinalizerNoSSAReconciler
25+
implements Reconciler<AddFinalizerNoSSACustomResource>,
26+
Cleaner<AddFinalizerNoSSACustomResource>,
27+
TestExecutionInfoProvider {
28+
29+
private final AtomicInteger numberOfExecutions = new AtomicInteger(0);
30+
private final AtomicInteger numberOfCleanupExecutions = new AtomicInteger(0);
31+
32+
@Override
33+
public UpdateControl<AddFinalizerNoSSACustomResource> reconcile(
34+
AddFinalizerNoSSACustomResource resource, Context<AddFinalizerNoSSACustomResource> context) {
35+
numberOfExecutions.addAndGet(1);
36+
return UpdateControl.noUpdate();
37+
}
38+
39+
@Override
40+
public DeleteControl cleanup(
41+
AddFinalizerNoSSACustomResource resource, Context<AddFinalizerNoSSACustomResource> context) {
42+
numberOfCleanupExecutions.addAndGet(1);
43+
return DeleteControl.defaultDelete();
44+
}
45+
46+
public int getNumberOfExecutions() {
47+
return numberOfExecutions.get();
48+
}
49+
50+
public int getNumberOfCleanupExecutions() {
51+
return numberOfCleanupExecutions.get();
52+
}
53+
}

0 commit comments

Comments
 (0)