From 31e167e43cff6d846982a2a7233691e59584fadf Mon Sep 17 00:00:00 2001 From: Marcos Trejo Date: Thu, 9 Jul 2020 21:55:16 -0400 Subject: [PATCH] Fix #267: openshift-maven-plugin does not update Routes Signed-off-by: Marcos Trejo --- .../kit/config/service/ApplyService.java | 2 +- .../kit/config/service/ApplyServiceTest.java | 53 +++++++++++++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/ApplyService.java b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/ApplyService.java index 391bee61e9..34ebde36ed 100644 --- a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/ApplyService.java +++ b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/ApplyService.java @@ -928,7 +928,7 @@ protected void doCreateResource(T resource, String n private void doPatchEntity(T oldEntity, T newEntity, String namespace, String sourceName) { String kind = newEntity.getKind(); - log.info("Updating {} from {}", kind, sourceName); + log.info("Updating %s from %s", kind, sourceName); try { Object answer = patchService.compareAndPatchEntity(namespace, newEntity, oldEntity); logGeneratedEntity("Updated " + kind + ": ", namespace, newEntity, answer); diff --git a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/ApplyServiceTest.java b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/ApplyServiceTest.java index 9c420d3e3c..262f362b86 100644 --- a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/ApplyServiceTest.java +++ b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/ApplyServiceTest.java @@ -1,3 +1,17 @@ +/** + * Copyright (c) 2019 Red Hat, Inc. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at: + * + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + */ + package org.eclipse.jkube.kit.config.service; import io.fabric8.openshift.api.model.Route; @@ -9,6 +23,7 @@ import org.junit.Before; import org.junit.Test; +import static java.net.HttpURLConnection.*; import static org.junit.Assert.assertEquals; public class ApplyServiceTest { @@ -32,11 +47,11 @@ public void testCreateRoute() throws Exception { WebServerEventCollector collector = new WebServerEventCollector<>(mockServer); mockServer.expect().get() .withPath("/apis/route.openshift.io/v1/namespaces/default/routes/route") - .andReply(collector.record("get-route").andReturn(404, "")) + .andReply(collector.record("get-route").andReturn(HTTP_NOT_FOUND, "")) .always(); mockServer.expect().post() .withPath("/apis/route.openshift.io/v1/namespaces/default/routes") - .andReply(collector.record("new-route").andReturn(201, route)) + .andReply(collector.record("new-route").andReturn(HTTP_CREATED, route)) .once(); applyService.apply(route, "route.yml"); @@ -44,6 +59,35 @@ public void testCreateRoute() throws Exception { collector.assertEventsRecordedInOrder("get-route", "new-route"); } + @Test + public void testUpdateRoute() throws Exception { + Route oldRoute = buildRoute(); + Route newRoute = new RouteBuilder() + .withNewMetadataLike(oldRoute.getMetadata()) + .addToAnnotations("haproxy.router.openshift.io/balance", "roundrobin") + .endMetadata() + .withSpec(oldRoute.getSpec()) + .build(); + + WebServerEventCollector collector = new WebServerEventCollector<>(mockServer); + mockServer.expect().get() + .withPath("/apis/route.openshift.io/v1/namespaces/default/routes/route") + .andReply(collector.record("get-route").andReturn(HTTP_OK, oldRoute)) + .always(); + mockServer.expect().patch() + .withPath("/apis/route.openshift.io/v1/namespaces/default/routes/route") + .andReply(collector.record("patch-route") + .andReturn(HTTP_OK, new RouteBuilder() + .withMetadata(newRoute.getMetadata()) + .withSpec(oldRoute.getSpec()) + .build())) + .once(); + + applyService.apply(newRoute, "route.yml"); + + collector.assertEventsRecordedInOrder("get-route", "patch-route"); + } + @Test public void testCreateRouteInServiceOnlyMode() throws Exception { Route route = buildRoute(); @@ -51,7 +95,7 @@ public void testCreateRouteInServiceOnlyMode() throws Exception { WebServerEventCollector collector = new WebServerEventCollector<>(mockServer); mockServer.expect().get() .withPath("/apis/route.openshift.io/v1/namespaces/default/routes/route") - .andReply(collector.record("get-route").andReturn(404, "")) + .andReply(collector.record("get-route").andReturn(HTTP_NOT_FOUND, "")) .always(); applyService.setServicesOnlyMode(true); @@ -68,7 +112,7 @@ public void testCreateRouteNotAllowed() throws Exception { WebServerEventCollector collector = new WebServerEventCollector<>(mockServer); mockServer.expect().get() .withPath("/apis/route.openshift.io/v1/namespaces/default/routes/route") - .andReply(collector.record("get-route").andReturn(404, "")) + .andReply(collector.record("get-route").andReturn(HTTP_NOT_FOUND, "")) .always(); applyService.setAllowCreate(false); @@ -81,6 +125,7 @@ public void testCreateRouteNotAllowed() throws Exception { private Route buildRoute() { return new RouteBuilder() .withNewMetadata() + .withNamespace("default") .withName("route") .endMetadata() .withNewSpec()