Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

feat: installTools step #402

Merged
merged 8 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions resources/scripts/install-with-choco.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Abort with non zero exit code on errors
$ErrorActionPreference = "Stop"

Write-Host("Getting latest {0} version for {1} ..." -f $env:TOOL,$env:VERSION)
& choco list $env:TOOL --exact --by-id-only --all | Select-String -Pattern "$env:TOOL $env:VERSION"
$Version = $(choco list $env:TOOL --exact --by-id-only --all) | Select-String -Pattern "$env:TOOL $env:VERSION" | %{$_.ToString().split(" ")[1]} | sort | Select-Object -Last 1

Write-Host("Installing {0} version: {1} ..." -f $env:TOOL,$Version)
& choco install $env:TOOL --no-progress -y --version "$Version"
2 changes: 2 additions & 0 deletions src/test/groovy/ApmBasePipelineTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ class ApmBasePipelineTest extends BasePipelineTest {
f.write(m.body)
println f.toString()
})
helper.registerAllowedMethod('powershell', [Map.class], null)
helper.registerAllowedMethod('powershell', [String.class], null)
helper.registerAllowedMethod('readFile', [Map.class], { '' })
helper.registerAllowedMethod('readJSON', [Map.class], { m ->
return readJSON(m)
Expand Down
87 changes: 87 additions & 0 deletions src/test/groovy/InstallToolsStepTests.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import org.junit.Before
import org.junit.Test
import static org.junit.Assert.assertTrue

class InstallToolsStepTests extends ApmBasePipelineTest {
String scriptName = 'vars/installTools.groovy'

@Override
@Before
void setUp() throws Exception {
super.setUp()
}

@Test
void test_without_arguments() throws Exception {
def script = loadScript(scriptName)
try {
script.call()
} catch(e) {
// NOOP
}
printCallStack()
assertTrue(assertMethodCallContainsPattern('error', 'installTools: missing params'))
assertJobStatusFailure()
}

@Test
void test_with_some_missing_arguments() throws Exception {
def script = loadScript(scriptName)
try {
script.installTool([ tool: 'python3' ])
} catch(e) {
// NOOP
}
printCallStack()
assertTrue(assertMethodCallContainsPattern('error', 'installTools: missing version param'))
assertJobStatusFailure()
}

@Test
void test_install_tool() throws Exception {
def script = loadScript(scriptName)
script.installTool([ tool: 'foo', version: 'x.y.z' ])
printCallStack()
assertTrue(assertMethodCallContainsPattern('echo', "Tool='foo' Version='x.y.z'"))
assertJobStatusSuccess()
}

@Test
void test_install_multiple_tools() throws Exception {
def script = loadScript(scriptName)
script.call([[ tool: 'foo', version: 'x.y.z' ], [ tool: 'bar', version: 'z.y.x' ]])
printCallStack()
assertTrue(assertMethodCallContainsPattern('echo', "Tool='foo' Version='x.y.z'"))
assertTrue(assertMethodCallContainsPattern('echo', "Tool='bar' Version='z.y.x'"))
assertJobStatusSuccess()
}

@Test
void test_install_tool_in_windows() throws Exception {
def script = loadScript(scriptName)
helper.registerAllowedMethod('isUnix', [], { false })
script.installTool([ tool: 'foo', version: 'x.y.z' ])
printCallStack()
assertTrue(assertMethodCallContainsPattern('echo', "Tool='foo' Version='x.y.z'"))
assertTrue(assertMethodCallContainsPattern('withEnv', 'VERSION=x.y.z, TOOL=foo'))
assertTrue(assertMethodCallContainsPattern('powershell', 'Install foo:x.y.z, script=.\\install-with-choco.ps1'))
assertJobStatusSuccess()
}
}
49 changes: 49 additions & 0 deletions vars/installTools.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

/**
This step will install the list of tools

installTools([ [ tool: 'python3', version: '3.5'] ])
installTools([ [ tool: 'python3', version: '3.5'], [tool: 'nodejs', version: '12.0' ] ])
*/

def call(List params = []){
if (params.isEmpty()) {
error("installTools: missing params, please use the format [ [ tool: 'foo', version: 'x.y.z'] , ...] tool param.")
}
params.each { item ->
installTool(item)
}
}

private installTool(Map params) {
def tool = params.containsKey('tool') ? params.tool : error('installTools: missing tool param.')
def version = params.containsKey('version') ? params.version : error('installTools: missing version param.')

echo "Tool='${tool}' Version='${version}'"
if(isUnix()) {
echo 'TBD: install in linux'
} else {
def scriptFile = 'install-with-choco.ps1'
def resourceContent = libraryResource('scripts/install-with-choco.ps1')
writeFile file: scriptFile, text: resourceContent
withEnv(["VERSION=${version}", "TOOL=${tool}"]) {
powershell label: "Install ${tool}:${version}", script: ".\\${scriptFile}"
}
}
}
10 changes: 10 additions & 0 deletions vars/installTools.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This step will install the list of tools

```
installTools([ [ tool: 'python3', version: '3.5'] ])
installTools([ [ tool: 'python3', version: '3.5'], [tool: 'nodejs', version: '12.0' ] ])
```


* tool: The name of the tool to be installed for the default package manager. Mandatory
* version: The version of the tool to be installated. Mandatory.