From 4be6b085de6f67f3ebfd1b744e0dc256c7dc04bc Mon Sep 17 00:00:00 2001 From: Marco Vermeulen Date: Fri, 28 Oct 2016 13:38:16 +0100 Subject: [PATCH] Introduce first pass of multi-platform functionality. --- src/main/bash/sdkman-env-helpers.sh | 8 ++++-- src/main/bash/sdkman-install.sh | 2 +- src/test/cucumber/default_version.feature | 2 +- src/test/cucumber/install_candidate.feature | 2 +- .../cucumber/multi_platform_binaries.feature | 21 ++++++++++++++ src/test/cucumber/use_version.feature | 2 +- .../sdkman/env/SdkmanBashEnvBuilder.groovy | 2 +- src/test/groovy/sdkman/steps/env.groovy | 4 +-- .../sdkman/steps/initialisation_steps.groovy | 12 ++++++++ .../groovy/sdkman/steps/stub_steps.groovy | 20 ++++++++++--- src/test/groovy/sdkman/stubs/UnameStub.groovy | 27 ++++++++++++++++++ .../groovy/sdkman/stubs/WebServiceStub.groovy | 9 +----- .../groovy/sdkman/support/UnixUtils.groovy | 22 ++++++++++---- src/test/resources/__files/java-8u101.zip | Bin 0 -> 588 bytes 14 files changed, 106 insertions(+), 27 deletions(-) create mode 100644 src/test/cucumber/multi_platform_binaries.feature create mode 100644 src/test/groovy/sdkman/stubs/UnameStub.groovy create mode 100644 src/test/resources/__files/java-8u101.zip diff --git a/src/main/bash/sdkman-env-helpers.sh b/src/main/bash/sdkman-env-helpers.sh index fe77f2de..0d547bbc 100644 --- a/src/main/bash/sdkman-env-helpers.sh +++ b/src/main/bash/sdkman-env-helpers.sh @@ -62,7 +62,11 @@ function __sdkman_determine_version { VERSION=$(__sdkman_secure_curl "${SDKMAN_LEGACY_API}/candidates/${candidate}/default") else - VERSION_VALID=$(__sdkman_secure_curl "${SDKMAN_LEGACY_API}/candidates/${candidate}/${version}") + local validation_url="${SDKMAN_LEGACY_API}/candidates/${candidate}/${version}/$(echo $SDKMAN_PLATFORM | tr '[:upper:]' '[:lower:]')" + VERSION_VALID=$(__sdkman_secure_curl "$validation_url") + __sdkman_echo_debug "Validate $candidate $version for $SDKMAN_PLATFORM: $VERSION_VALID" + __sdkman_echo_debug "Validation URL: $validation_url" + if [[ "$VERSION_VALID" == 'valid' || "$VERSION_VALID" == 'invalid' && -n "$folder" ]]; then VERSION="$version" @@ -74,7 +78,7 @@ function __sdkman_determine_version { else echo "" - echo "Stop! $version is not a valid ${candidate} version." + echo "Stop! $candidate $version is not available on this platform." return 1 fi fi diff --git a/src/main/bash/sdkman-install.sh b/src/main/bash/sdkman-install.sh index d1670cbc..93b875c4 100644 --- a/src/main/bash/sdkman-install.sh +++ b/src/main/bash/sdkman-install.sh @@ -106,7 +106,7 @@ function __sdkman_download { echo "" echo "In progress..." echo "" - local download_url="${SDKMAN_CURRENT_API}/broker/download/${candidate}/${version}?platform=${SDKMAN_PLATFORM}" + local download_url="${SDKMAN_CURRENT_API}/broker/download/${candidate}/${version}/$(echo $SDKMAN_PLATFORM | tr '[:upper:]' '[:lower:]')" local download_binary="${SDKMAN_DIR}/tmp/$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1).bin" __sdkman_secure_curl_download "$download_url" > "$download_binary" diff --git a/src/test/cucumber/default_version.feature b/src/test/cucumber/default_version.feature index ff514e70..abded94b 100644 --- a/src/test/cucumber/default_version.feature +++ b/src/test/cucumber/default_version.feature @@ -30,4 +30,4 @@ Feature: Default Version Given the candidate "groovy" version "2.9.9" is not available for download And the system is bootstrapped When I enter "sdk default groovy 2.9.9" - Then I see "Stop! 2.9.9 is not a valid groovy version." \ No newline at end of file + Then I see "Stop! groovy 2.9.9 is not available on this platform." \ No newline at end of file diff --git a/src/test/cucumber/install_candidate.feature b/src/test/cucumber/install_candidate.feature index 6e13aee7..aa9fe88e 100644 --- a/src/test/cucumber/install_candidate.feature +++ b/src/test/cucumber/install_candidate.feature @@ -20,7 +20,7 @@ Feature: Install Candidate Scenario: Install a Candidate version that does not exist Given the candidate "grails" version "1.4.4" is not available for download When I enter "sdk install grails 1.4.4" - Then I see "Stop! 1.4.4 is not a valid grails version." + Then I see "Stop! grails 1.4.4 is not available on this platform." Scenario: Install a Candidate version that is already installed Given the candidate "grails" version "1.3.9" is available for download diff --git a/src/test/cucumber/multi_platform_binaries.feature b/src/test/cucumber/multi_platform_binaries.feature new file mode 100644 index 00000000..b021bcd2 --- /dev/null +++ b/src/test/cucumber/multi_platform_binaries.feature @@ -0,0 +1,21 @@ +Feature: Multi Platform Binary Distribution + + Background: + Given the internet is reachable + And an initialised environment + + Scenario: A compatible binary is installed + And a machine with "Mac OSX" installed + And the system is bootstrapped + And the candidate "java" version "8u101" is available for download on "Mac OSX" + When I enter "sdk install java 8u101" and answer "Y" + Then I see "Done installing!" + And the candidate "java" version "8u101" is installed + + Scenario: Platform is not supported + And a machine with "FreeBSD" installed + And the system is bootstrapped + And the candidate "java" version "8u101" is not available for download on "FreeBSD" + When I enter "sdk install java 8u101" + Then I see "Stop! java 8u101 is not available on this platform." + And the candidate "java" version "8u101" is not installed \ No newline at end of file diff --git a/src/test/cucumber/use_version.feature b/src/test/cucumber/use_version.feature index 9346f5f9..37a22bfd 100644 --- a/src/test/cucumber/use_version.feature +++ b/src/test/cucumber/use_version.feature @@ -39,7 +39,7 @@ Feature: Use Version Given the candidate "groovy" version "1.9.9" is not available for download And the system is bootstrapped When I enter "sdk use groovy 1.9.9" - Then I see "Stop! 1.9.9 is not a valid groovy version." + Then I see "Stop! groovy 1.9.9 is not available on this platform." Scenario: Use a candidate version that only exists locally Given the candidate "grails" version "2.0.0.M1" is not available for download diff --git a/src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy b/src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy index 07c0a9ea..3a87bab9 100644 --- a/src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy +++ b/src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy @@ -11,7 +11,7 @@ class SdkmanBashEnvBuilder { //optional fields with sensible defaults private CurlStub curlStub - private List candidates = ['groovy', 'grails'] + private List candidates = ['groovy', 'grails', 'java'] private List availableCandidates = candidates private boolean offlineMode = false private String broadcast = "This is a LIVE broadcast!" diff --git a/src/test/groovy/sdkman/steps/env.groovy b/src/test/groovy/sdkman/steps/env.groovy index 8224ceed..79da16fe 100644 --- a/src/test/groovy/sdkman/steps/env.groovy +++ b/src/test/groovy/sdkman/steps/env.groovy @@ -9,7 +9,7 @@ import static cucumber.api.groovy.Hooks.After import static cucumber.api.groovy.Hooks.Before HTTP_PROXY = System.getProperty("httpProxy") ?: "" -PLATFORM = UnixUtils.platform +PLATFORM = UnixUtils.platform.toLowerCase() FAKE_JDK_PATH = "/path/to/my/openjdk" SERVICE_UP_HOST="localhost" @@ -70,4 +70,4 @@ After(){ scenario -> scenario.write("\nOutput: \n${output}") } bash?.stop() -} +} \ No newline at end of file diff --git a/src/test/groovy/sdkman/steps/initialisation_steps.groovy b/src/test/groovy/sdkman/steps/initialisation_steps.groovy index 36027136..e6cd67c2 100644 --- a/src/test/groovy/sdkman/steps/initialisation_steps.groovy +++ b/src/test/groovy/sdkman/steps/initialisation_steps.groovy @@ -1,5 +1,8 @@ package sdkman.steps +import sdkman.stubs.UnameStub +import sdkman.support.UnixUtils + import java.util.zip.ZipException import java.util.zip.ZipFile @@ -9,6 +12,8 @@ import static sdkman.stubs.WebServiceStub.primeSelfupdate import sdkman.env.SdkmanBashEnvBuilder +import static sdkman.support.UnixUtils.asUname + And(~'^the sdkman work folder is created$') { -> assert sdkmanDir.isDirectory(), "The SDKMAN directory does not exist." } @@ -75,6 +80,13 @@ And(~'^offline mode is enabled with unreachable internet$') {-> javaHome = FAKE_JDK_PATH } +And(~'^a machine with "(.*)" installed$') { String platform -> + def binFolder = "$sdkmanBaseDir/bin" as File + UnameStub.prepareIn(binFolder) + .forPlatform(asUname(platform)) + .build() +} + And(~'^an initialised environment$') {-> bash = SdkmanBashEnvBuilder.create(sdkmanBaseDir) .withOfflineMode(offlineMode) diff --git a/src/test/groovy/sdkman/steps/stub_steps.groovy b/src/test/groovy/sdkman/steps/stub_steps.groovy index d63e5417..7aec6b8b 100644 --- a/src/test/groovy/sdkman/steps/stub_steps.groovy +++ b/src/test/groovy/sdkman/steps/stub_steps.groovy @@ -1,5 +1,7 @@ package sdkman.steps +import sdkman.support.UnixUtils + import static cucumber.api.groovy.EN.And import static sdkman.stubs.WebServiceStub.* import static sdkman.support.FilesystemUtils.readCurrentFromCandidateFolder @@ -15,12 +17,22 @@ And(~'^an available selfupdate$') { -> } And(~'^the candidate "([^"]*)" version "([^"]*)" is available for download$') { String candidate, String version -> - primeEndpointWithString("/candidates/${candidate}/${version}", "valid") + primeEndpointWithString("/candidates/${candidate}/${version}/${PLATFORM}", "valid") primeDownloadFor(SERVICE_UP_URL, candidate, version, PLATFORM) } And(~'^the candidate "([^"]*)" version "([^"]*)" is not available for download$') { String candidate, String version -> - primeEndpointWithString("/candidates/${candidate}/${version}", "invalid") + primeEndpointWithString("/candidates/${candidate}/${version}/${PLATFORM}", "invalid") +} + +And(~/^the candidate "(.*?)" version "(.*?)" is available for download on "(.*?)"$/) { String candidate, String version, String platform -> + String lowerCaseUname = UnixUtils.asUname(platform).toLowerCase() + primeEndpointWithString("/candidates/${candidate}/${version}/${lowerCaseUname}", "valid") + primeDownloadFor(SERVICE_UP_URL, candidate, version, lowerCaseUname) +} + +And(~/^the candidate "(.*?)" version "(.*?)" is not available for download on "(.*?)"$/) { String candidate, String version, String platform -> + primeEndpointWithString("/candidates/${candidate}/${version}/${UnixUtils.asUname(platform).toLowerCase()}", "invalid") } And(~'^a "([^"]*)" list view is available for consumption$') { String candidate -> @@ -28,11 +40,11 @@ And(~'^a "([^"]*)" list view is available for consumption$') { String candidate } And(~'^the candidate "([^"]*)" version "([^"]*)" is a valid candidate version$') { String candidate, String version -> - primeEndpointWithString("/candidates/${candidate}/${version}", "valid") + primeEndpointWithString("/candidates/${candidate}/${version}/${PLATFORM}", "valid") } And(~'^the candidate "([^"]*)" version "([^"]*)" is not a valid candidate version$') { String candidate, String version -> - primeEndpointWithString("/candidates/${candidate}/${version}", "invalid") + primeEndpointWithString("/candidates/${candidate}/${version}/${PLATFORM}", "invalid") } And(~/^the candidate "(.*?)" has a version list available$/) { String candidate -> diff --git a/src/test/groovy/sdkman/stubs/UnameStub.groovy b/src/test/groovy/sdkman/stubs/UnameStub.groovy new file mode 100644 index 00000000..269805f2 --- /dev/null +++ b/src/test/groovy/sdkman/stubs/UnameStub.groovy @@ -0,0 +1,27 @@ +package sdkman.stubs + +class UnameStub { + + private File file + private uname + + static UnameStub prepareIn(File folder) { + folder.mkdirs() + + def file = new File(folder, "uname") + file.createNewFile() + file.write "#!/bin/bash\n" + file.executable = true + + new UnameStub(file:file) + } + + UnameStub forPlatform(String uname) { + this.uname = uname + this + } + + void build(){ + file << "echo $uname" + } +} diff --git a/src/test/groovy/sdkman/stubs/WebServiceStub.groovy b/src/test/groovy/sdkman/stubs/WebServiceStub.groovy index 22424235..8cf02c22 100644 --- a/src/test/groovy/sdkman/stubs/WebServiceStub.groovy +++ b/src/test/groovy/sdkman/stubs/WebServiceStub.groovy @@ -12,16 +12,9 @@ class WebServiceStub { .withBody(body))) } - static primeEndpointWithBinary(String endpoint, byte[] body) { - stubFor(get(urlEqualTo(endpoint)).willReturn( - aResponse() - .withStatus(200) - .withHeader("Content-Type", "application/octet-stream") - .withBody(body))) - } static primeDownloadFor(String host, String candidate, String version, String platform) { - stubFor(get(urlEqualTo("/broker/download/${candidate}/${version}?platform=${platform}")).willReturn( + stubFor(get(urlEqualTo("/broker/download/${candidate}/${version}/${platform}")).willReturn( aResponse() .withHeader("Location", "${host}/${candidate}-${version}.zip") .withStatus(302))) diff --git a/src/test/groovy/sdkman/support/UnixUtils.groovy b/src/test/groovy/sdkman/support/UnixUtils.groovy index d61eef6e..d3d6ddc3 100644 --- a/src/test/groovy/sdkman/support/UnixUtils.groovy +++ b/src/test/groovy/sdkman/support/UnixUtils.groovy @@ -2,14 +2,24 @@ package sdkman.support class UnixUtils { static getPlatform() { - def osName = System.getProperty("os.name") - switch (osName) { - case "Mac OS X": - "Darwin" + asUname(System.getProperty("os.name")) + } + + static asUname(platform) { + def result + switch (platform) { + case "Mac OSX": + result = "Darwin" + break + case "Linux": + result = "Linux" + break + case "FreeBSD": + result = "FreeBSD" break default: - osName - break + result = platform } + result } } diff --git a/src/test/resources/__files/java-8u101.zip b/src/test/resources/__files/java-8u101.zip new file mode 100644 index 0000000000000000000000000000000000000000..864ab5fee94f6d60b6d7c26520deb647eb3c6dec GIT binary patch literal 588 zcmWIWW@Zs#0D-iwXiqQ$N^mnMFk~f`CF)v~8X6ethlcPnurq23MX&%dh%T+*W?*D_ z!OXw_CIa9Fb)p%>k7`g-W*)>aXP_ZfKn$X>8pgrE!GLU-8qj6Jn1+EI33eUGAQ(n7 zFgWar|EV*V{k?R~J@E29cOpFClb4Qn$O)aW6KAybJkOl;^bHJo#KaKb&CaoL#{W