|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.tools.skaffold.downloader; |
| 18 | + |
| 19 | +import com.google.cloud.tools.skaffold.filesystem.OperatingSystem; |
| 20 | +import com.google.common.io.CharStreams; |
| 21 | +import com.google.common.io.Resources; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStreamReader; |
| 24 | +import java.net.URL; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.List; |
| 30 | +import org.hamcrest.CoreMatchers; |
| 31 | +import org.junit.Assert; |
| 32 | +import org.junit.Assume; |
| 33 | +import org.junit.Rule; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.rules.TemporaryFolder; |
| 36 | + |
| 37 | +/** Integration tests for {@link Downloader}. */ |
| 38 | +public class DownloaderIntegationTest { |
| 39 | + |
| 40 | + private static String downloadAndRun(URL url, Path destination, String... command) |
| 41 | + throws IOException, InterruptedException { |
| 42 | + // Downloads a script that says "hello". |
| 43 | + Downloader.download(url, destination, 1); |
| 44 | + Assert.assertTrue(destination.toFile().setExecutable(true)); |
| 45 | + |
| 46 | + // Runs the downloaded script. |
| 47 | + List<String> commandList = new ArrayList<>(Arrays.asList(command)); |
| 48 | + commandList.add(destination.toString()); |
| 49 | + Process process = new ProcessBuilder(commandList).start(); |
| 50 | + String stdout = |
| 51 | + CharStreams.toString( |
| 52 | + new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)); |
| 53 | + String stderr = |
| 54 | + CharStreams.toString( |
| 55 | + new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8)); |
| 56 | + Assert.assertEquals("", stderr); |
| 57 | + Assert.assertEquals(0, process.waitFor()); |
| 58 | + return stdout; |
| 59 | + } |
| 60 | + |
| 61 | + @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testDownload() throws IOException, InterruptedException { |
| 65 | + Assume.assumeTrue("non-Windows test", OperatingSystem.resolve() != OperatingSystem.WINDOWS); |
| 66 | + |
| 67 | + Assert.assertEquals( |
| 68 | + "hello\n", |
| 69 | + downloadAndRun( |
| 70 | + Resources.getResource("helloScript.sh"), |
| 71 | + temporaryFolder.newFolder().toPath().resolve("hello.sh"), |
| 72 | + System.getenv("SHELL"))); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testDownload_windows() throws IOException, InterruptedException { |
| 77 | + Assume.assumeTrue("Windows test", OperatingSystem.resolve() == OperatingSystem.WINDOWS); |
| 78 | + |
| 79 | + Assert.assertThat( |
| 80 | + downloadAndRun( |
| 81 | + Resources.getResource("helloScript.bat"), |
| 82 | + temporaryFolder.newFolder().toPath().resolve("hello.bat"), |
| 83 | + "cmd", |
| 84 | + "/c"), |
| 85 | + CoreMatchers.containsString("hello world")); |
| 86 | + } |
| 87 | +} |
0 commit comments