|
| 1 | +package com.alibaba.dcm.tool |
| 2 | + |
| 3 | +import io.kotest.assertions.withClue |
| 4 | +import io.kotest.core.annotation.EnabledCondition |
| 5 | +import io.kotest.core.annotation.EnabledIf |
| 6 | +import io.kotest.core.spec.Spec |
| 7 | +import io.kotest.core.spec.style.AnnotationSpec |
| 8 | +import io.kotest.matchers.nulls.shouldNotBeNull |
| 9 | +import io.kotest.matchers.shouldBe |
| 10 | +import org.apache.commons.io.FileUtils |
| 11 | +import org.apache.commons.lang3.SystemUtils |
| 12 | +import java.io.File |
| 13 | +import java.net.InetAddress |
| 14 | +import kotlin.reflect.KClass |
| 15 | +import kotlin.streams.toList |
| 16 | + |
| 17 | +/** |
| 18 | + * https://kotest.io/docs/framework/testing-styles.html#annotation-spec |
| 19 | + */ |
| 20 | +// Ignore "attach to current VM" test for jdk 9+, since java 9+ does not support |
| 21 | +// "java.io.IOException: Can not attach to current VM" |
| 22 | +@EnabledIf(Java8Only::class) |
| 23 | +class DcmToolTests : AnnotationSpec() { |
| 24 | + |
| 25 | + private lateinit var agentFilePath: String |
| 26 | + |
| 27 | + @BeforeAll |
| 28 | + fun prepareAgentFilePath() { |
| 29 | + val agentFile = findAgentFileFromLibProject() ?: findAgentFileFromMavenLocal() |
| 30 | + withClue("Not found agent file") { |
| 31 | + agentFile.shouldNotBeNull() |
| 32 | + } |
| 33 | + |
| 34 | + agentFilePath = agentFile!! |
| 35 | + println("Found agent file: $agentFilePath") |
| 36 | + } |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + fun setUp() { |
| 40 | + val outputFile = File.createTempFile("dcm-output-", ".log") |
| 41 | + FileUtils.deleteQuietly(outputFile) |
| 42 | + FileUtils.touch(outputFile) |
| 43 | + outputFile.length() shouldBe 0 |
| 44 | + |
| 45 | + val outputFilePath = outputFile.canonicalPath |
| 46 | + println("Prepared output file: $outputFilePath") |
| 47 | + |
| 48 | + System.setProperty(DcmTool.DCM_TOOLS_TMP_FILE_KEY, outputFilePath) |
| 49 | + System.setProperty(DcmTool.DCM_TOOLS_AGENT_JAR_KEY, agentFilePath) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun test_main_getPolicy() { |
| 54 | + DcmTool.main(arrayOf("-p", DcmTool.pid(), "getPolicy")) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun test_main_set() { |
| 59 | + val ip = "1.1.2.2" |
| 60 | + val host = "bing.com" |
| 61 | + |
| 62 | + DcmTool.main(arrayOf("-p", DcmTool.pid(), "set", host, ip)) |
| 63 | + InetAddress.getByName(host).hostAddress shouldBe ip |
| 64 | + } |
| 65 | + |
| 66 | + private fun findAgentFileFromLibProject(): String? { |
| 67 | + val dcmLibProjectDir = listOf("library", "../library", "../../library") |
| 68 | + .asSequence() |
| 69 | + .map { File(it) } |
| 70 | + .filter { it.exists() } |
| 71 | + .firstOrNull() |
| 72 | + ?: return null |
| 73 | + |
| 74 | + val targetDir = File(dcmLibProjectDir, "target") |
| 75 | + if (!targetDir.exists()) return null |
| 76 | + println("Found target dir: ${targetDir.canonicalPath}") |
| 77 | + |
| 78 | + return FileUtils.streamFiles(targetDir, false, "jar") |
| 79 | + .filter { isAgentJar(it) } |
| 80 | + .findFirst() |
| 81 | + .map { it.canonicalPath } |
| 82 | + .orElse(null) |
| 83 | + } |
| 84 | + |
| 85 | + private fun findAgentFileFromMavenLocal(): String? { |
| 86 | + val home = System.getProperty("user.home") |
| 87 | + val m2DcmLibDependencyDir = File("$home/.m2/repository/com/alibaba/dns-cache-manipulator") |
| 88 | + |
| 89 | + return FileUtils.streamFiles(m2DcmLibDependencyDir, true, "jar") |
| 90 | + .filter { isAgentJar(it) } |
| 91 | + .map { it.canonicalPath } |
| 92 | + .toList() |
| 93 | + .maxOrNull() |
| 94 | + } |
| 95 | + |
| 96 | + private fun isAgentJar(file: File): Boolean { |
| 97 | + val fileName = file.name |
| 98 | + if (!fileName.startsWith("dns-cache-manipulator")) return false |
| 99 | + |
| 100 | + val replaced = fileName.replace("dns-cache-manipulator-", "").replace("-SNAPSHOT", "") |
| 101 | + return !replaced.contains("-") |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * https://kotest.io/docs/framework/conditional/spec-annotations-conditional-evaluation.html |
| 107 | + */ |
| 108 | +class Java8Only : EnabledCondition { |
| 109 | + override fun enabled(kclass: KClass<out Spec>): Boolean = SystemUtils.IS_JAVA_1_8 |
| 110 | +} |
0 commit comments