Skip to content

Commit 84d63e8

Browse files
committed
add update operation test
1 parent aea5757 commit 84d63e8

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

sample-operators/webpage/src/test/java/io/javaoperatorsdk/operator/sample/WebPageOperatorAbstractTest.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public abstract class WebPageOperatorAbstractTest {
3131

3232
static final KubernetesClient client = new DefaultKubernetesClient();
3333
public static final String TEST_PAGE = "test-page";
34+
public static final String TITLE1 = "Hello Operator World";
35+
public static final String TITLE2 = "Hello Operator World Title 2";
36+
public static final int WAIT_SECONDS = 20;
37+
public static final Duration POLL_INTERVAL = Duration.ofSeconds(1);
3438

3539
boolean isLocal() {
3640
String deployment = System.getProperty("test.deployment");
@@ -42,12 +46,12 @@ boolean isLocal() {
4246
@Test
4347
void testAddingWebPage() {
4448

45-
var webPage = createWebPage();
49+
var webPage = createWebPage(TITLE1);
4650
operator().create(WebPage.class, webPage);
4751

4852
await()
49-
.atMost(Duration.ofSeconds(20))
50-
.pollInterval(Duration.ofSeconds(1))
53+
.atMost(Duration.ofSeconds(WAIT_SECONDS))
54+
.pollInterval(POLL_INTERVAL)
5155
.untilAsserted(
5256
() -> {
5357
var actual = operator().get(WebPage.class, TEST_PAGE);
@@ -57,9 +61,17 @@ void testAddingWebPage() {
5761
assertThat(deployment.getSpec().getReplicas())
5862
.isEqualTo(deployment.getStatus().getReadyReplicas());
5963
});
64+
assertThat(httpGetForWebPage(webPage)).contains(TITLE1);
6065

61-
String response = httpGetForWebPage(webPage);
62-
assertThat(response).contains("<title>Hello Operator World</title>");
66+
// update test: changing title
67+
operator().replace(WebPage.class, createWebPage(TITLE2));
68+
69+
await().atMost(Duration.ofSeconds(WAIT_SECONDS))
70+
.pollInterval(POLL_INTERVAL)
71+
.untilAsserted(() -> {
72+
String page = httpGetForWebPage(webPage);
73+
assertThat(page).isNotNull().contains(TITLE2);
74+
});
6375
}
6476

6577
String httpGetForWebPage(WebPage webPage) {
@@ -75,7 +87,7 @@ String httpGetForWebPage(WebPage webPage) {
7587
.uri(new URI("http://localhost:" + portForward.getLocalPort())).build();
7688
return httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body();
7789
} catch (URISyntaxException | IOException | InterruptedException e) {
78-
throw new IllegalStateException(e);
90+
return null;
7991
} finally {
8092
if (portForward != null) {
8193
try {
@@ -87,7 +99,7 @@ String httpGetForWebPage(WebPage webPage) {
8799
}
88100
}
89101

90-
WebPage createWebPage() {
102+
WebPage createWebPage(String title) {
91103
WebPage webPage = new WebPage();
92104
webPage.setMetadata(new ObjectMeta());
93105
webPage.getMetadata().setName(TEST_PAGE);
@@ -98,7 +110,7 @@ WebPage createWebPage() {
98110
.setHtml(
99111
"<html>\n"
100112
+ " <head>\n"
101-
+ " <title>Hello Operator World</title>\n"
113+
+ " <title>" + title + "</title>\n"
102114
+ " </head>\n"
103115
+ " <body>\n"
104116
+ " Hello World! \n"

sample-operators/webpage/src/test/java/io/javaoperatorsdk/operator/sample/WebPageOperatorE2E.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ AbstractOperatorExtension operator() {
5252
}
5353

5454
@Override
55-
WebPage createWebPage() {
56-
WebPage page = super.createWebPage();
55+
WebPage createWebPage(String title) {
56+
WebPage page = super.createWebPage(title);
5757
page.getMetadata().setLabels(lowLevelLabel());
5858
return page;
5959
}

0 commit comments

Comments
 (0)