|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.engine.server.rest; |
| 19 | + |
| 20 | +import java.io.BufferedReader; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStreamReader; |
| 23 | +import java.net.HttpURLConnection; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +import com.hazelcast.config.Config; |
| 28 | +import com.hazelcast.internal.serialization.Data; |
| 29 | +import org.apache.seatunnel.engine.common.config.SeaTunnelConfig; |
| 30 | +import org.apache.seatunnel.engine.common.config.server.CheckpointConfig; |
| 31 | +import org.apache.seatunnel.engine.common.config.server.HttpConfig; |
| 32 | +import org.apache.seatunnel.engine.common.runtime.ExecutionMode; |
| 33 | +import org.apache.seatunnel.engine.common.utils.PassiveCompletableFuture; |
| 34 | +import org.apache.seatunnel.engine.core.dag.logical.LogicalDag; |
| 35 | +import org.apache.seatunnel.engine.core.job.JobImmutableInformation; |
| 36 | +import org.apache.seatunnel.engine.server.AbstractSeaTunnelServerTest; |
| 37 | +import org.apache.seatunnel.engine.server.SeaTunnelServer; |
| 38 | +import org.apache.seatunnel.engine.server.SeaTunnelServerStarter; |
| 39 | +import org.apache.seatunnel.engine.server.TestUtils; |
| 40 | +import org.junit.jupiter.api.Assertions; |
| 41 | +import org.junit.jupiter.api.BeforeAll; |
| 42 | +import org.junit.jupiter.api.Test; |
| 43 | +import org.junit.jupiter.api.condition.DisabledOnOs; |
| 44 | +import org.junit.jupiter.api.condition.OS; |
| 45 | + |
| 46 | +/** Test for Rest API with Basic. */ |
| 47 | +@DisabledOnOs(OS.MAC) |
| 48 | +public class RestApiHttpBasicTest extends AbstractSeaTunnelServerTest { |
| 49 | + |
| 50 | + private static final int HTTP_PORT = 18080; |
| 51 | + private static final Long JOB_1 = System.currentTimeMillis() + 1L; |
| 52 | + public static final String USER = "admin"; |
| 53 | + public static final String PASS = "admin"; |
| 54 | + |
| 55 | + @BeforeAll |
| 56 | + void setUp() { |
| 57 | + String name = this.getClass().getName(); |
| 58 | + Config hazelcastConfig = Config.loadFromString(getHazelcastConfig()); |
| 59 | + hazelcastConfig.setClusterName(TestUtils.getClusterName("RestApiServletHttpBasicTest_" + name)); |
| 60 | + SeaTunnelConfig seaTunnelConfig = loadSeaTunnelConfig(); |
| 61 | + seaTunnelConfig.setHazelcastConfig(hazelcastConfig); |
| 62 | + seaTunnelConfig.getEngineConfig().setMode(ExecutionMode.LOCAL); |
| 63 | + |
| 64 | + HttpConfig httpConfig = seaTunnelConfig.getEngineConfig().getHttpConfig(); |
| 65 | + httpConfig.setEnabled(true); |
| 66 | + httpConfig.setPort(HTTP_PORT); |
| 67 | + |
| 68 | + instance = SeaTunnelServerStarter.createHazelcastInstance(seaTunnelConfig); |
| 69 | + nodeEngine = instance.node.nodeEngine; |
| 70 | + server = nodeEngine.getService(SeaTunnelServer.SERVICE_NAME); |
| 71 | + LOGGER = nodeEngine.getLogger(AbstractSeaTunnelServerTest.class); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testRestApiHttp() throws Exception { |
| 76 | + HttpURLConnection conn = |
| 77 | + (HttpURLConnection) |
| 78 | + new java.net.URL("http://localhost:" + HTTP_PORT + "/overview") |
| 79 | + .openConnection(); |
| 80 | + setBasicAuth(conn); |
| 81 | + try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { |
| 82 | + |
| 83 | + Assertions.assertEquals(200, conn.getResponseCode()); |
| 84 | + String response = in.lines().collect(Collectors.joining()); |
| 85 | + Assertions.assertTrue(response.contains("projectVersion")); |
| 86 | + } finally { |
| 87 | + conn.disconnect(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public SeaTunnelConfig loadSeaTunnelConfig() { |
| 93 | + SeaTunnelConfig seaTunnelConfig = super.loadSeaTunnelConfig(); |
| 94 | + |
| 95 | + HttpConfig httpConfig = seaTunnelConfig.getEngineConfig().getHttpConfig(); |
| 96 | + httpConfig.setEnabled(Boolean.TRUE); |
| 97 | + httpConfig.setEnableBasicAuth(Boolean.TRUE); |
| 98 | + httpConfig.setBasicAuthUsername("admin"); |
| 99 | + httpConfig.setBasicAuthPassword("admin"); |
| 100 | + httpConfig.setPort(HTTP_PORT); |
| 101 | + |
| 102 | + CheckpointConfig checkpointConfig = seaTunnelConfig.getEngineConfig().getCheckpointConfig(); |
| 103 | + |
| 104 | + checkpointConfig.setCheckpointInterval(Integer.MAX_VALUE); |
| 105 | + seaTunnelConfig.getEngineConfig().setCheckpointConfig(checkpointConfig); |
| 106 | + return seaTunnelConfig; |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void testWriteJsonWithObject() throws IOException { |
| 111 | + startJob(); |
| 112 | + testLogRestApiResponse("JSON"); |
| 113 | + } |
| 114 | + |
| 115 | + public void setBasicAuth(HttpURLConnection connection){ |
| 116 | + // Basic Auth |
| 117 | + String token = java.util.Base64.getEncoder() |
| 118 | + .encodeToString((USER + ":" + PASS).getBytes(java.nio.charset.StandardCharsets.UTF_8)); |
| 119 | + connection.setRequestProperty("Authorization", "Basic " + token); |
| 120 | + } |
| 121 | + |
| 122 | + public void testLogRestApiResponse(String format) throws IOException { |
| 123 | + HttpURLConnection conn = null; |
| 124 | + try { |
| 125 | + java.net.URL url = |
| 126 | + new java.net.URL("http://localhost:" + HTTP_PORT + "/logs?format=" + format); |
| 127 | + conn = (HttpURLConnection) url.openConnection(); |
| 128 | + setBasicAuth(conn); |
| 129 | + |
| 130 | + try (BufferedReader in = |
| 131 | + new BufferedReader(new InputStreamReader(conn.getInputStream()))) { |
| 132 | + // [ { |
| 133 | + // "node" : "localhost:18080", |
| 134 | + // "logLink" : "http://localhost:18080/logs/job-1760939539658.log", |
| 135 | + // "logName" : "job-1760939539658.log" |
| 136 | + //}, { |
| 137 | + // "node" : "localhost:18080", |
| 138 | + // "logLink" : "http://localhost:18080/logs/job-${ctx:ST-JID}.log", |
| 139 | + // "logName" : "job-${ctx:ST-JID}.log" |
| 140 | + //} ] |
| 141 | + String response = in.lines().collect(Collectors.joining()); |
| 142 | + Assertions.assertNotNull(response); |
| 143 | + } |
| 144 | + |
| 145 | + Assertions.assertEquals(200, conn.getResponseCode()); |
| 146 | + Assertions.assertTrue( |
| 147 | + conn.getHeaderFields() |
| 148 | + .get("Content-Type") |
| 149 | + .toString() |
| 150 | + .contains("charset=utf-8")); |
| 151 | + } finally { |
| 152 | + if (conn != null) { |
| 153 | + conn.disconnect(); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private void startJob() { |
| 159 | + LogicalDag testLogicalDag = TestUtils.createTestLogicalPlan("fake_to_console.conf", RestApiHttpBasicTest.JOB_1.toString(), |
| 160 | + RestApiHttpBasicTest.JOB_1); |
| 161 | + |
| 162 | + JobImmutableInformation jobImmutableInformation = |
| 163 | + new JobImmutableInformation( |
| 164 | + RestApiHttpBasicTest.JOB_1, |
| 165 | + "Test", |
| 166 | + nodeEngine.getSerializationService(), |
| 167 | + testLogicalDag, |
| 168 | + Collections.emptyList(), |
| 169 | + Collections.emptyList()); |
| 170 | + |
| 171 | + Data data = nodeEngine.getSerializationService().toData(jobImmutableInformation); |
| 172 | + |
| 173 | + PassiveCompletableFuture<Void> voidPassiveCompletableFuture = |
| 174 | + server.getCoordinatorService() |
| 175 | + .submitJob(RestApiHttpBasicTest.JOB_1, data, jobImmutableInformation.isStartWithSavePoint()); |
| 176 | + voidPassiveCompletableFuture.join(); |
| 177 | + } |
| 178 | +} |
0 commit comments