You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by GitBox <gi...@apache.org> on 2022/03/08 08:18:29 UTC

[GitHub] [flink-kubernetes-operator] SteNicholas opened a new pull request #47: [FLINK-26137] Create webhook REST api test

SteNicholas opened a new pull request #47:
URL: https://github.com/apache/flink-kubernetes-operator/pull/47


   Adds unit tests to validate the webhook rest endpoint and make sure it returns the expected responses, status codes etc.
   
   **The brief change log**
   
   - Introduces `AdmissionHandlerTest` to adds the test cases `testHandleIllegalRequest`, `testHandleValidateRequestWithoutContent` and `testHandleValidateRequestWithAdmissionReview` for the validation of the webhook rest endpoint.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink-kubernetes-operator] SteNicholas commented on a change in pull request #47: [FLINK-26137] Create webhook REST api test

Posted by GitBox <gi...@apache.org>.
SteNicholas commented on a change in pull request #47:
URL: https://github.com/apache/flink-kubernetes-operator/pull/47#discussion_r821543133



##########
File path: flink-kubernetes-webhook/src/test/java/org/apache/flink/kubernetes/operator/admission/AdmissionHandlerTest.java
##########
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.kubernetes.operator.admission;
+
+import org.apache.flink.kubernetes.operator.validation.DefaultDeploymentValidator;
+import org.apache.flink.util.FileUtils;
+
+import org.apache.flink.shaded.netty4.io.netty.buffer.Unpooled;
+import org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse;
+
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Objects;
+
+import static org.apache.flink.kubernetes.operator.admission.AdmissionHandler.VALIDATE_REQUEST_PATH;
+import static org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod.GET;
+import static org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
+import static org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus.OK;
+import static org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
+import static org.apache.flink.shaded.netty4.io.netty.util.CharsetUtil.UTF_8;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/** @link AdmissionHandler unit tests */
+public class AdmissionHandlerTest {
+
+    private final AdmissionHandler admissionHandler =
+            new AdmissionHandler(new FlinkValidator(new DefaultDeploymentValidator()));
+
+    @Test
+    public void testHandleIllegalRequest() {
+        final EmbeddedChannel embeddedChannel = new EmbeddedChannel(admissionHandler);
+        final String illegalRequest = "/test";
+        embeddedChannel.writeInbound(new DefaultFullHttpRequest(HTTP_1_1, GET, illegalRequest));
+        embeddedChannel.writeOutbound(new DefaultFullHttpResponse(HTTP_1_1, OK));
+        final DefaultFullHttpResponse response = embeddedChannel.readOutbound();
+        assertEquals(INTERNAL_SERVER_ERROR, response.status());
+        assertEquals(
+                String.format(
+                        "Illegal path requested: %s. Only %s is accepted.",
+                        illegalRequest, VALIDATE_REQUEST_PATH),
+                new String(response.content().array()));
+        assertTrue(embeddedChannel.finish());
+    }
+
+    @Test
+    public void testHandleValidateRequestWithoutContent() {
+        final EmbeddedChannel embeddedChannel = new EmbeddedChannel(admissionHandler);
+        embeddedChannel.writeInbound(
+                new DefaultFullHttpRequest(HTTP_1_1, GET, VALIDATE_REQUEST_PATH));
+        embeddedChannel.writeOutbound(new DefaultFullHttpResponse(HTTP_1_1, OK));
+        final DefaultFullHttpResponse response = embeddedChannel.readOutbound();
+        assertEquals(INTERNAL_SERVER_ERROR, response.status());
+        assertTrue(
+                new String(response.content().array())
+                        .contains(MismatchedInputException.class.getName()));
+        assertTrue(embeddedChannel.finish());
+    }
+
+    @Test
+    public void testHandleValidateRequestWithAdmissionReview() throws IOException {
+        final EmbeddedChannel embeddedChannel = new EmbeddedChannel(admissionHandler);
+        embeddedChannel.writeInbound(
+                new DefaultFullHttpRequest(
+                        HTTP_1_1,
+                        GET,
+                        VALIDATE_REQUEST_PATH,
+                        Unpooled.wrappedBuffer(
+                                FileUtils.readFileUtf8(
+                                                new File(
+                                                        Objects.requireNonNull(
+                                                                        getClass()
+                                                                                .getClassLoader()
+                                                                                .getResource(
+                                                                                        "admission-review.json"))
+                                                                .getPath()))

Review comment:
       @gyfora, of course. IMO. the current implementation doesn't look so good to me.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink-kubernetes-operator] gyfora commented on pull request #47: [FLINK-26137] Create webhook REST api test

Posted by GitBox <gi...@apache.org>.
gyfora commented on pull request #47:
URL: https://github.com/apache/flink-kubernetes-operator/pull/47#issuecomment-1061529649


   Thanks a lot @SteNicholas , I will review this later today :) 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink-kubernetes-operator] SteNicholas commented on pull request #47: [FLINK-26137] Create webhook REST api test

Posted by GitBox <gi...@apache.org>.
SteNicholas commented on pull request #47:
URL: https://github.com/apache/flink-kubernetes-operator/pull/47#issuecomment-1061526567


   @gyfora @wangyang0918 @tweise , could you please help to review this pull request?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink-kubernetes-operator] SteNicholas commented on pull request #47: [FLINK-26137] Create webhook REST api test

Posted by GitBox <gi...@apache.org>.
SteNicholas commented on pull request #47:
URL: https://github.com/apache/flink-kubernetes-operator/pull/47#issuecomment-1061675060


   @gyfora, thanks for detailed review. I have addressed the above comments. Please help to take a look.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink-kubernetes-operator] SteNicholas removed a comment on pull request #47: [FLINK-26137] Create webhook REST api test

Posted by GitBox <gi...@apache.org>.
SteNicholas removed a comment on pull request #47:
URL: https://github.com/apache/flink-kubernetes-operator/pull/47#issuecomment-1061521374


   @gyfora @wangyang0918 @tweise, could you please help to review?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink-kubernetes-operator] gyfora merged pull request #47: [FLINK-26137] Create webhook REST api test

Posted by GitBox <gi...@apache.org>.
gyfora merged pull request #47:
URL: https://github.com/apache/flink-kubernetes-operator/pull/47


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink-kubernetes-operator] SteNicholas commented on pull request #47: [FLINK-26137] Create webhook REST api test

Posted by GitBox <gi...@apache.org>.
SteNicholas commented on pull request #47:
URL: https://github.com/apache/flink-kubernetes-operator/pull/47#issuecomment-1061521374


   @gyfora @wangyang0918 @tweise, could you please help to review?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink-kubernetes-operator] gyfora commented on a change in pull request #47: [FLINK-26137] Create webhook REST api test

Posted by GitBox <gi...@apache.org>.
gyfora commented on a change in pull request #47:
URL: https://github.com/apache/flink-kubernetes-operator/pull/47#discussion_r821524484



##########
File path: flink-kubernetes-webhook/src/test/java/org/apache/flink/kubernetes/operator/admission/AdmissionHandlerTest.java
##########
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.kubernetes.operator.admission;
+
+import org.apache.flink.kubernetes.operator.validation.DefaultDeploymentValidator;
+import org.apache.flink.util.FileUtils;
+
+import org.apache.flink.shaded.netty4.io.netty.buffer.Unpooled;
+import org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse;
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse;
+
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Objects;
+
+import static org.apache.flink.kubernetes.operator.admission.AdmissionHandler.VALIDATE_REQUEST_PATH;
+import static org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod.GET;
+import static org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
+import static org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus.OK;
+import static org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
+import static org.apache.flink.shaded.netty4.io.netty.util.CharsetUtil.UTF_8;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/** @link AdmissionHandler unit tests */
+public class AdmissionHandlerTest {
+
+    private final AdmissionHandler admissionHandler =
+            new AdmissionHandler(new FlinkValidator(new DefaultDeploymentValidator()));
+
+    @Test
+    public void testHandleIllegalRequest() {
+        final EmbeddedChannel embeddedChannel = new EmbeddedChannel(admissionHandler);
+        final String illegalRequest = "/test";
+        embeddedChannel.writeInbound(new DefaultFullHttpRequest(HTTP_1_1, GET, illegalRequest));
+        embeddedChannel.writeOutbound(new DefaultFullHttpResponse(HTTP_1_1, OK));
+        final DefaultFullHttpResponse response = embeddedChannel.readOutbound();
+        assertEquals(INTERNAL_SERVER_ERROR, response.status());
+        assertEquals(
+                String.format(
+                        "Illegal path requested: %s. Only %s is accepted.",
+                        illegalRequest, VALIDATE_REQUEST_PATH),
+                new String(response.content().array()));
+        assertTrue(embeddedChannel.finish());
+    }
+
+    @Test
+    public void testHandleValidateRequestWithoutContent() {
+        final EmbeddedChannel embeddedChannel = new EmbeddedChannel(admissionHandler);
+        embeddedChannel.writeInbound(
+                new DefaultFullHttpRequest(HTTP_1_1, GET, VALIDATE_REQUEST_PATH));
+        embeddedChannel.writeOutbound(new DefaultFullHttpResponse(HTTP_1_1, OK));
+        final DefaultFullHttpResponse response = embeddedChannel.readOutbound();
+        assertEquals(INTERNAL_SERVER_ERROR, response.status());
+        assertTrue(
+                new String(response.content().array())
+                        .contains(MismatchedInputException.class.getName()));
+        assertTrue(embeddedChannel.finish());
+    }
+
+    @Test
+    public void testHandleValidateRequestWithAdmissionReview() throws IOException {
+        final EmbeddedChannel embeddedChannel = new EmbeddedChannel(admissionHandler);
+        embeddedChannel.writeInbound(
+                new DefaultFullHttpRequest(
+                        HTTP_1_1,
+                        GET,
+                        VALIDATE_REQUEST_PATH,
+                        Unpooled.wrappedBuffer(
+                                FileUtils.readFileUtf8(
+                                                new File(
+                                                        Objects.requireNonNull(
+                                                                        getClass()
+                                                                                .getClassLoader()
+                                                                                .getResource(
+                                                                                        "admission-review.json"))
+                                                                .getPath()))

Review comment:
       Could we simply use the objectmapper to generate the request instead of hardcoding the json? 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org