|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.gradle.internal |
| 21 | + |
| 22 | +import org.apache.commons.compress.archivers.tar.TarArchiveEntry |
| 23 | +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream |
| 24 | +import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream |
| 25 | +import org.apache.tools.zip.ZipEntry |
| 26 | +import org.apache.tools.zip.ZipFile |
| 27 | +import org.elasticsearch.gradle.fixtures.AbstractGradleFuncTest |
| 28 | +import org.gradle.testkit.runner.BuildResult |
| 29 | +import org.gradle.testkit.runner.TaskOutcome |
| 30 | + |
| 31 | +class InternalDistributionArchiveSetupPluginFuncTest extends AbstractGradleFuncTest { |
| 32 | + |
| 33 | + def setup() { |
| 34 | + buildFile << """ |
| 35 | + import org.elasticsearch.gradle.tar.SymbolicLinkPreservingTar |
| 36 | + |
| 37 | + plugins { |
| 38 | + id 'elasticsearch.internal-distribution-archive-setup' |
| 39 | + } |
| 40 | + """ |
| 41 | + file('someFile.txt') << "some content" |
| 42 | + } |
| 43 | + |
| 44 | + def "applies defaults to tar tasks"() { |
| 45 | + given: |
| 46 | + file('someFile.txt') << "some content" |
| 47 | + buildFile << """ |
| 48 | + tasks.register('${buildTaskName}', SymbolicLinkPreservingTar) { |
| 49 | + from 'someFile.txt' |
| 50 | + } |
| 51 | + """ |
| 52 | + |
| 53 | + when: |
| 54 | + def result = gradleRunner(buildTaskName).build() |
| 55 | + |
| 56 | + then: |
| 57 | + file(expectedOutputArchivePath).exists() |
| 58 | + assertTarPermissionDefaults(file(expectedOutputArchivePath)) |
| 59 | + assertEmptyDirTasksTriggered(result) |
| 60 | + |
| 61 | + where: |
| 62 | + buildTaskName | expectedOutputArchivePath |
| 63 | + "buildDarwinTar" | "darwin-tar/build/distributions/elasticsearch.tar.gz" |
| 64 | + "buildOssDarwinTar" | "oss-darwin-tar/build/distributions/elasticsearch-oss.tar.gz" |
| 65 | + } |
| 66 | + |
| 67 | + def "applies defaults to zip tasks"() { |
| 68 | + given: |
| 69 | + buildFile << """ |
| 70 | + tasks.register('${buildTaskName}', Zip) { |
| 71 | + from 'someFile.txt' |
| 72 | + } |
| 73 | + """ |
| 74 | + |
| 75 | + when: |
| 76 | + def result = gradleRunner(buildTaskName).build() |
| 77 | + |
| 78 | + then: |
| 79 | + file(expectedOutputArchivePath).exists() |
| 80 | + assertZipPermissionDefaults(file(expectedOutputArchivePath)) |
| 81 | + assertEmptyDirTasksTriggered(result) |
| 82 | + |
| 83 | + where: |
| 84 | + buildTaskName | expectedOutputArchivePath |
| 85 | + "buildDarwinZip" | "darwin-zip/build/distributions/elasticsearch.zip" |
| 86 | + "buildOssDarwinZip" | "oss-darwin-zip/build/distributions/elasticsearch-oss.zip" |
| 87 | + } |
| 88 | + |
| 89 | + def "registered distribution provides archives and directory variant"() { |
| 90 | + given: |
| 91 | + file('someFile.txt') << "some content" |
| 92 | + |
| 93 | + settingsFile << """ |
| 94 | + include ':consumer' |
| 95 | + include ':producer-tar' |
| 96 | + """ |
| 97 | + |
| 98 | + buildFile << """ |
| 99 | + import org.gradle.api.artifacts.type.ArtifactTypeDefinition; |
| 100 | + import org.gradle.api.internal.artifacts.ArtifactAttributes; |
| 101 | +
|
| 102 | + distribution_archives { |
| 103 | + producerTar { |
| 104 | + content { |
| 105 | + project.copySpec { |
| 106 | + from 'someFile.txt' |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + project('consumer') { p -> |
| 113 | + configurations { |
| 114 | + consumeArchive {} |
| 115 | + consumeDir {} |
| 116 | + } |
| 117 | + |
| 118 | + dependencies { |
| 119 | + consumeDir project(path: ':producer-tar', configuration:'extracted') |
| 120 | + consumeArchive project(path: ':producer-tar', configuration:'default' ) |
| 121 | + } |
| 122 | + |
| 123 | + tasks.register("copyDir", Copy) { |
| 124 | + from(configurations.consumeDir) |
| 125 | + into('build/dir') |
| 126 | + } |
| 127 | + |
| 128 | + tasks.register("copyArchive", Copy) { |
| 129 | + from(configurations.consumeArchive) |
| 130 | + into('build/archives') |
| 131 | + } |
| 132 | + } |
| 133 | + """ |
| 134 | + when: |
| 135 | + def result = gradleRunner("copyArchive").build() |
| 136 | + |
| 137 | + then:"tar task executed and target folder contains plain tar" |
| 138 | + result.task(':buildProducerTar').outcome == TaskOutcome.SUCCESS |
| 139 | + result.task(':consumer:copyArchive').outcome == TaskOutcome.SUCCESS |
| 140 | + file("producer-tar/build/distributions/elasticsearch.tar.gz").exists() |
| 141 | + file("consumer/build/archives/elasticsearch.tar.gz").exists() |
| 142 | + |
| 143 | + when: |
| 144 | + result = gradleRunner("copyDir").build() |
| 145 | + then:"plain copy task executed and target folder contains plain content" |
| 146 | + result.task(':buildProducer').outcome == TaskOutcome.SUCCESS |
| 147 | + result.task(':consumer:copyDir').outcome == TaskOutcome.SUCCESS |
| 148 | + file("producer-tar/build/install/someFile.txt").exists() |
| 149 | + file("consumer/build/dir/someFile.txt").exists() |
| 150 | + } |
| 151 | + |
| 152 | + private static boolean assertTarPermissionDefaults(File tarArchive) { |
| 153 | + TarArchiveInputStream tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(tarArchive))) |
| 154 | + try { |
| 155 | + TarArchiveEntry currentEntry = tarInput.getNextTarEntry() |
| 156 | + while (currentEntry != null) { |
| 157 | + if (currentEntry.isDirectory()) { |
| 158 | + assertDefaultDirPermissions(currentEntry.getMode()) |
| 159 | + } else { |
| 160 | + assertDefaultFilePermissions(currentEntry.getMode()) |
| 161 | + } |
| 162 | + currentEntry = tarInput.getNextTarEntry() |
| 163 | + } |
| 164 | + return true |
| 165 | + } finally { |
| 166 | + tarInput.close() |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private static boolean assertZipPermissionDefaults(File archive) { |
| 171 | + ZipFile zip = new ZipFile(archive) |
| 172 | + try { |
| 173 | + Enumeration<ZipEntry> entries = zip.getEntries() |
| 174 | + while (entries.hasMoreElements()) { |
| 175 | + ZipEntry zipEntry = entries.nextElement() |
| 176 | + if (zipEntry.isDirectory()) { |
| 177 | + assertDefaultDirPermissions(zipEntry.getUnixMode()) |
| 178 | + } else { |
| 179 | + assertDefaultFilePermissions(zipEntry.getUnixMode()) |
| 180 | + } |
| 181 | + } |
| 182 | + } finally { |
| 183 | + zip.close() |
| 184 | + } |
| 185 | + true |
| 186 | + } |
| 187 | + |
| 188 | + private static boolean assertDefaultDirPermissions(int mode) { |
| 189 | + assert ((mode >> 6) & 07) == 7 |
| 190 | + assert ((mode >> 3) & 07) == 5 |
| 191 | + assert ((mode >> 0) & 07) == 5 |
| 192 | + true |
| 193 | + } |
| 194 | + |
| 195 | + private static boolean assertDefaultFilePermissions(int mode) { |
| 196 | + assert ((mode >> 6) & 07) == 6 |
| 197 | + assert ((mode >> 3) & 07) == 4 |
| 198 | + assert ((mode >> 0) & 07) == 4 |
| 199 | + true |
| 200 | + } |
| 201 | + |
| 202 | + private static boolean assertEmptyDirTasksTriggered(BuildResult result) { |
| 203 | + result.task(":createJvmOptionsDir").outcome == TaskOutcome.SUCCESS |
| 204 | + result.task(":createLogsDir").outcome == TaskOutcome.SUCCESS |
| 205 | + result.task(":createPluginsDir").outcome == TaskOutcome.SUCCESS |
| 206 | + true |
| 207 | + } |
| 208 | +} |
0 commit comments