|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.gradle.internal.transport |
| 11 | + |
| 12 | +import org.gradle.testkit.runner.BuildResult |
| 13 | +import org.gradle.testkit.runner.TaskOutcome |
| 14 | + |
| 15 | +class UpdateTransportVersionsFuncTest extends AbstractTransportVersionFuncTest { |
| 16 | + def runUpdateTask(String... additionalArgs) { |
| 17 | + List<String> args = new ArrayList<>() |
| 18 | + args.add(":myserver:updateTransportVersions") |
| 19 | + args.addAll(additionalArgs); |
| 20 | + return gradleRunner(args.toArray()) |
| 21 | + } |
| 22 | + |
| 23 | + void assertUpdateSuccess(BuildResult result) { |
| 24 | + assert result.task(":myserver:updateTransportVersions").outcome == TaskOutcome.SUCCESS |
| 25 | + } |
| 26 | + |
| 27 | + void assertUpdateFailure(BuildResult result, String expectedOutput) { |
| 28 | + assert result.task(":myserver:updateTransportVersions").outcome == TaskOutcome.FAILED |
| 29 | + assertOutputContains(result.output, expectedOutput) |
| 30 | + } |
| 31 | + |
| 32 | + void assertTransportVersionsCsv(String expectedContent) { |
| 33 | + File csvFile = file("myserver/src/main/resources/org/elasticsearch/TransportVersions.csv") |
| 34 | + assert csvFile.exists() |
| 35 | + // Normalize both sides: strip leading/trailing whitespace from each line and the whole string |
| 36 | + def actualLines = csvFile.text.readLines().collect { it.strip() }.findAll { it != "" } |
| 37 | + def expectedLines = expectedContent.readLines().collect { it.strip() }.findAll { it != "" } |
| 38 | + assert actualLines == expectedLines |
| 39 | + } |
| 40 | + |
| 41 | + def "updates csv with minor version"() { |
| 42 | + when: |
| 43 | + def result = runUpdateTask("--stack-version", "9.2.0").build() |
| 44 | + |
| 45 | + then: |
| 46 | + assertUpdateSuccess(result) |
| 47 | + assertTransportVersionsCsv(""" |
| 48 | + 9.0.0,8000000 |
| 49 | + 9.1.0,8012001 |
| 50 | + 9.2.0,8123000 |
| 51 | + """) |
| 52 | + } |
| 53 | + |
| 54 | + def "updates csv with patch version"() { |
| 55 | + given: |
| 56 | + execute("git checkout main") |
| 57 | + unreferableTransportVersion("initial_9.1.1", "8012002") |
| 58 | + transportVersionUpperBound("9.1", "initial_9.1.1", "8012002") |
| 59 | + execute('git commit -a -m "update"') |
| 60 | + |
| 61 | + when: |
| 62 | + def result = runUpdateTask("--stack-version", "9.1.1").build() |
| 63 | + |
| 64 | + then: |
| 65 | + assertUpdateSuccess(result) |
| 66 | + assertTransportVersionsCsv(""" |
| 67 | + 9.0.0,8000000 |
| 68 | + 9.1.0,8012001 |
| 69 | + 9.1.1,8012002 |
| 70 | + """) |
| 71 | + } |
| 72 | + |
| 73 | + def "fails when upper bound does not exist"() { |
| 74 | + when: |
| 75 | + def result = runUpdateTask("--stack-version", "9.3.0").buildAndFail() |
| 76 | + |
| 77 | + then: |
| 78 | + assertUpdateFailure(result, "Missing upper bound 9.3 for stack version 9.3.0") |
| 79 | + } |
| 80 | + |
| 81 | + def "is idempotent when version already exists"() { |
| 82 | + when: |
| 83 | + // Run the task twice with the same version |
| 84 | + def result1 = runUpdateTask("--stack-version", "9.2.0").build() |
| 85 | + def result2 = runUpdateTask("--stack-version", "9.2.0").build() |
| 86 | + |
| 87 | + then: |
| 88 | + assertUpdateSuccess(result1) |
| 89 | + assertUpdateSuccess(result2) |
| 90 | + assertOutputContains(result2.output, "Version 9.2.0 already exists in TransportVersions.csv, skipping") |
| 91 | + // Should only have one entry for 9.2.0 |
| 92 | + assertTransportVersionsCsv(""" |
| 93 | + 9.0.0,8000000 |
| 94 | + 9.1.0,8012001 |
| 95 | + 9.2.0,8123000 |
| 96 | + """) |
| 97 | + } |
| 98 | +} |
0 commit comments