|
| 1 | +/* |
| 2 | + * Copyright 2023 NAVER Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.pinpoint.test.plugin.spring.data.r2dbc; |
| 17 | + |
| 18 | +import java.io.BufferedReader; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.InputStreamReader; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author youngjin.kim2 |
| 28 | + */ |
| 29 | +public class DockerTestUtils { |
| 30 | + public static boolean isArmDockerServer() { |
| 31 | + return getDockerArchitecture().contains("arm"); |
| 32 | + } |
| 33 | + |
| 34 | + private static String getDockerArchitecture() { |
| 35 | + final String dockerInfo = execute("docker version", 3000); |
| 36 | + final String dockerServerInfo = dockerInfo.substring(dockerInfo.indexOf("Server: ")); |
| 37 | + final String archLine = Arrays.stream(dockerServerInfo.split("\n")) |
| 38 | + .filter(line -> line.contains("OS/Arch:")) |
| 39 | + .findFirst() |
| 40 | + .orElseThrow(() -> new RuntimeException("Invalid docker version result")); |
| 41 | + return archLine.split(":")[1].trim(); |
| 42 | + } |
| 43 | + |
| 44 | + private static String execute(String command, long waitMillis) { |
| 45 | + try { |
| 46 | + return execute0(command, waitMillis); |
| 47 | + } catch (Throwable th) { |
| 48 | + throw new RuntimeException("Failed to run '" + command + "'"); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private static String execute0(String command, long waitMillis) throws InterruptedException, IOException { |
| 53 | + final Process proc = Runtime.getRuntime().exec(command); |
| 54 | + if (!proc.waitFor(waitMillis, TimeUnit.MILLISECONDS)) { |
| 55 | + throw new RuntimeException(); |
| 56 | + } |
| 57 | + return readAll(proc.getInputStream()); |
| 58 | + } |
| 59 | + |
| 60 | + private static String readAll(InputStream is) { |
| 61 | + return new BufferedReader(new InputStreamReader(is)) |
| 62 | + .lines() |
| 63 | + .collect(Collectors.joining("\n")); |
| 64 | + } |
| 65 | +} |
0 commit comments