|
| 1 | +/** |
| 2 | + * ContainerProxy |
| 3 | + * |
| 4 | + * Copyright (C) 2016-2024 Open Analytics |
| 5 | + * |
| 6 | + * =========================================================================== |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the Apache License as published by |
| 10 | + * The Apache Software Foundation, either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * Apache License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the Apache License |
| 19 | + * along with this program. If not, see <http://www.apache.org/licenses/> |
| 20 | + */ |
| 21 | +package eu.openanalytics.containerproxy.test.proxy; |
| 22 | + |
| 23 | +import eu.openanalytics.containerproxy.backend.dispatcher.proxysharing.ProxySharingScaler; |
| 24 | +import eu.openanalytics.containerproxy.backend.dispatcher.proxysharing.SeatIdKey; |
| 25 | +import eu.openanalytics.containerproxy.model.runtime.Proxy; |
| 26 | +import eu.openanalytics.containerproxy.model.runtime.runtimevalues.PublicPathKey; |
| 27 | +import eu.openanalytics.containerproxy.model.runtime.runtimevalues.TargetIdKey; |
| 28 | +import eu.openanalytics.containerproxy.test.helpers.ContainerSetup; |
| 29 | +import eu.openanalytics.containerproxy.test.helpers.ShinyProxyInstance; |
| 30 | +import eu.openanalytics.containerproxy.util.Retrying; |
| 31 | +import org.junit.jupiter.api.Assertions; |
| 32 | +import org.junit.jupiter.params.ParameterizedTest; |
| 33 | +import org.junit.jupiter.params.provider.Arguments; |
| 34 | +import org.junit.jupiter.params.provider.MethodSource; |
| 35 | + |
| 36 | +import java.time.Duration; |
| 37 | +import java.time.Instant; |
| 38 | +import java.util.Map; |
| 39 | +import java.util.stream.Stream; |
| 40 | + |
| 41 | +public class TestPreInitialization { |
| 42 | + |
| 43 | + private static Stream<Arguments> backends() { |
| 44 | + return Stream.of( |
| 45 | + Arguments.of("docker", Map.of("proxy.container-backend", "docker")), |
| 46 | + Arguments.of("docker-swarm", Map.of("proxy.container-backend", "docker-swarm")), |
| 47 | + Arguments.of("kubernetes", Map.of("proxy.container-backend", "kubernetes")) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + @ParameterizedTest |
| 52 | + @MethodSource("backends") |
| 53 | + public void simpleTest(String backend, Map<String, String> properties) { |
| 54 | + try (ContainerSetup containerSetup = new ContainerSetup(backend)) { |
| 55 | + try (ShinyProxyInstance inst = new ShinyProxyInstance("application-test-pre-initialization.yml", properties, true)) { |
| 56 | + ProxySharingScaler proxySharingScaler = inst.getBean("proxySharingScaler_simpleTest", ProxySharingScaler.class); |
| 57 | + try { |
| 58 | + String id = inst.client.startProxy("simpleTest"); |
| 59 | + Proxy proxy = inst.proxyService.getProxy(id); |
| 60 | + inst.client.testProxyReachable(proxy.getTargetId()); |
| 61 | + |
| 62 | + // target id should be different from proxy id |
| 63 | + Assertions.assertNotEquals(proxy.getTargetId(), proxy.getId()); |
| 64 | + Assertions.assertEquals("/api/route/" + proxy.getTargetId() + "/", proxy.getRuntimeValue(PublicPathKey.inst)); |
| 65 | + Assertions.assertEquals(proxy.getTargetId(), proxy.getRuntimeValue(TargetIdKey.inst)); |
| 66 | + Assertions.assertNotNull(proxy.getRuntimeValue(SeatIdKey.inst)); |
| 67 | + |
| 68 | + waitUntilNoPendingSeats(proxySharingScaler); |
| 69 | + Assertions.assertEquals(1, proxySharingScaler.getNumClaimedSeats()); |
| 70 | + Assertions.assertEquals(1, proxySharingScaler.getNumUnclaimedSeats()); |
| 71 | + |
| 72 | + // start an additional app |
| 73 | + Instant start = Instant.now(); |
| 74 | + String id2 = inst.client.startProxy("simpleTest"); |
| 75 | + Proxy proxy2 = inst.proxyService.getProxy(id2); |
| 76 | + inst.client.testProxyReachable(proxy2.getTargetId()); |
| 77 | + |
| 78 | + // target id should be different from first app |
| 79 | + Assertions.assertNotEquals(proxy2.getTargetId(), proxy.getTargetId()); |
| 80 | + // seat id should be different from first app |
| 81 | + Assertions.assertNotEquals(proxy2.getRuntimeValue(SeatIdKey.inst), proxy.getRuntimeValue(SeatIdKey.inst)); |
| 82 | + |
| 83 | + // should have scaled-up |
| 84 | + waitUntilNoPendingSeats(proxySharingScaler); |
| 85 | + Assertions.assertEquals(2, proxySharingScaler.getNumClaimedSeats()); |
| 86 | + Assertions.assertEquals(1, proxySharingScaler.getNumUnclaimedSeats()); |
| 87 | + |
| 88 | + // stop first app |
| 89 | + inst.client.stopProxy(id); |
| 90 | + Assertions.assertEquals(0, proxySharingScaler.getNumPendingSeats()); |
| 91 | + Assertions.assertEquals(1, proxySharingScaler.getNumClaimedSeats()); |
| 92 | + Assertions.assertEquals(2, proxySharingScaler.getNumUnclaimedSeats()); |
| 93 | + |
| 94 | + // wait until scale-down happened |
| 95 | + waitUntilUnNumberOfClaimedSeats(proxySharingScaler, 1); |
| 96 | + Instant stop = Instant.now(); |
| 97 | + Assertions.assertEquals(0, proxySharingScaler.getNumPendingSeats()); |
| 98 | + Assertions.assertEquals(1, proxySharingScaler.getNumClaimedSeats()); |
| 99 | + Assertions.assertEquals(1, proxySharingScaler.getNumUnclaimedSeats()); |
| 100 | + // scale-down should take at least two minutes |
| 101 | + Assertions.assertTrue(Duration.between(start, stop).toSeconds() > 120); |
| 102 | + } finally { |
| 103 | + proxySharingScaler.stopAll(); |
| 104 | + inst.stopAllApps(); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private void waitUntilNoPendingSeats(ProxySharingScaler proxySharingScaler) { |
| 111 | + boolean noPendingSeats = Retrying.retry((c, m) -> { |
| 112 | + return proxySharingScaler.getNumPendingSeats() == 0; |
| 113 | + }, 60_000, "assert no pending seats", 1, true); |
| 114 | + Assertions.assertTrue(noPendingSeats); |
| 115 | + } |
| 116 | + |
| 117 | + private void waitUntilUnNumberOfClaimedSeats(ProxySharingScaler proxySharingScaler, int numSeats) { |
| 118 | + boolean noPendingSeats = Retrying.retry((c, m) -> { |
| 119 | + return proxySharingScaler.getNumUnclaimedSeats() == numSeats; |
| 120 | + }, 180_000, "assert number of unclaimed seats", 1, true); |
| 121 | + Assertions.assertTrue(noPendingSeats); |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments