Skip to content

Commit

Permalink
Merge pull request #2 from imranismail/chore/change-download-path
Browse files Browse the repository at this point in the history
Support new download path for kustomize
  • Loading branch information
imranismail authored Nov 12, 2019
2 parents 01c23dd + 9d368a1 commit ee04d50
Show file tree
Hide file tree
Showing 1,454 changed files with 204,578 additions and 235,302 deletions.
61 changes: 35 additions & 26 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ process.env['RUNNER_TEMP'] = tempDir;

import * as installer from '../src/installer';

const IS_WINDOWS = process.platform === 'win32';
const IS_WINDOWS = os.platform() === 'win32';

describe('installer tests', () => {
beforeAll(async () => {
Expand All @@ -24,9 +24,9 @@ describe('installer tests', () => {
await io.rmRF(tempDir);
}, 100000);

it('Acquires version of kustomize if no matching version is installed', async () => {
await installer.getKustomize('3.1.0');
const kustomizeDir = path.join(toolDir, 'kustomize', '3.1.0', os.arch());
it('Acquires kustomize version 3.2.0 successfully', async () => {
await installer.getKustomize('3.2.0');
const kustomizeDir = path.join(toolDir, 'kustomize', '3.2.0', os.arch());

expect(fs.existsSync(`${kustomizeDir}.complete`)).toBe(true);

Expand All @@ -37,6 +37,32 @@ describe('installer tests', () => {
}
}, 100000);

it ('Acquires kustomize version 3.2.1 successfully', async () => {
await installer.getKustomize('3.2.1');
const kustomizeDir = path.join(toolDir, 'kustomize', '3.2.1', os.arch());

expect(fs.existsSync(`${kustomizeDir}.complete`)).toBe(true);

if (IS_WINDOWS) {
expect(fs.existsSync(path.join(kustomizeDir, 'kustomize.exe'))).toBe(true);
} else {
expect(fs.existsSync(path.join(kustomizeDir, 'kustomize'))).toBe(true);
}
}, 100000)

it ('Acquires kustomize version 3.3.0 successfully', async () => {
await installer.getKustomize('3.3.0');
const kustomizeDir = path.join(toolDir, 'kustomize', '3.3.0', os.arch());

expect(fs.existsSync(`${kustomizeDir}.complete`)).toBe(true);

if (IS_WINDOWS) {
expect(fs.existsSync(path.join(kustomizeDir, 'kustomize.exe'))).toBe(true);
} else {
expect(fs.existsSync(path.join(kustomizeDir, 'kustomize'))).toBe(true);
}
}, 100000)

it('Throws if no location contains correct kustomize version', async () => {
let thrown = false;

Expand All @@ -45,6 +71,7 @@ describe('installer tests', () => {
} catch {
thrown = true;
}

expect(thrown).toBe(true);
});

Expand All @@ -60,33 +87,15 @@ describe('installer tests', () => {
return;
});

it('Doesnt use version of kustomize that was only partially installed in cache', async () => {
const kustomizeDir: string = path.join(toolDir, 'kustomize', '3.3.0', os.arch());

await io.mkdirP(kustomizeDir);

let thrown = false;

try {
await installer.getKustomize('3.3.0');
} catch {
thrown = true;
}

expect(thrown).toBe(true);

return;
});

it('Resolves semantic versions of kustomize installed in cache', async () => {
const kustomizeDir: string = path.join(toolDir, 'kustomize', '3.4.0', os.arch());
const kustomizeDir: string = path.join(toolDir, 'kustomize', '3.0.0', os.arch());

await io.mkdirP(kustomizeDir);

fs.writeFileSync(`${kustomizeDir}.complete`, 'hello');

await installer.getKustomize('3.4.0');
await installer.getKustomize('3');
await installer.getKustomize('3.x');
await installer.getKustomize('3.0.0');
// await installer.getKustomize('3.0');
await installer.getKustomize('3.0');
});
});
51 changes: 36 additions & 15 deletions lib/installer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Expand All @@ -23,7 +24,6 @@ const restm = __importStar(require("typed-rest-client/RestClient"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const semver = __importStar(require("semver"));
const fs = __importStar(require("fs"));
let osPlat = os.platform();
let osArch = os.arch();
if (!tempDirectory) {
Expand Down Expand Up @@ -99,15 +99,15 @@ function queryLatestMatch(versionSpec) {
let kustomizeVersions = (yield rest.get(dataUrl)).result || [];
kustomizeVersions.forEach((kustomizeVersion) => {
if (kustomizeVersion.assets.some(asset => asset.name.includes(dataFileName))) {
versions.push(kustomizeVersion.name);
let version = semver.clean(kustomizeVersion.name);
if (version != null) {
versions.push(version);
}
}
});
// get the latest version that matches the version spec
let version = evaluateVersions(versions, versionSpec);
return version;
return evaluateVersions(versions, versionSpec);
});
}
// TODO - should we just export this from @actions/tool-cache? Lifted directly from there
function evaluateVersions(versions, versionSpec) {
let version = '';
core.debug(`evaluating ${versions.length} versions`);
Expand Down Expand Up @@ -136,33 +136,54 @@ function evaluateVersions(versions, versionSpec) {
function acquireKustomize(version) {
return __awaiter(this, void 0, void 0, function* () {
version = semver.clean(version) || '';
let fileName = `kustomize_${version}`;
let downloadUrl;
let downloadPath;
if (semver.gte(version, "3.3.0")) {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v${version}/kustomize_v${version}_%{os}_%{arch}.tar.gz`;
}
else if (semver.gte(version, "3.2.1")) {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v${version}/kustomize_kustomize.v${version}_%{os}_%{arch}`;
}
else {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/v${version}/kustomize_${version}_%{os}_%{arch}`;
}
switch (osPlat) {
case 'win32':
if (semver.lte(version, "3.2.1"))
throw new Error(`Unexpected OS '${osPlat}'`);
downloadUrl = downloadUrl.replace('%{os}', 'windows');
if (semver.lt(version, "3.3.0"))
downloadUrl = `${downloadUrl}.exe`;
break;
case 'linux':
case 'darwin':
case 'win32':
fileName = `${fileName}_${osPlat}`;
downloadUrl = downloadUrl.replace('%{os}', osPlat);
break;
default:
throw new Error(`Unexpected OS '${osPlat}'`);
}
switch (osArch) {
case 'x64':
fileName = `${fileName}_amd64`;
downloadUrl = downloadUrl.replace('%{arch}', 'amd64');
break;
default:
fileName = `${fileName}_${osArch}`;
throw new Error(`Unexpected Arch '${osArch}'`);
}
let downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/v${version}/${fileName}`;
let downloadPath;
try {
downloadPath = yield tc.downloadTool(downloadUrl);
}
catch (err) {
core.debug(err);
throw `Failed to download version ${version}: ${err}`;
}
fs.chmodSync(downloadPath, 0o755);
let toolPath = downloadPath;
if (downloadUrl.endsWith('.tar.gz')) {
let extPath = yield tc.extractTar(downloadPath);
toolPath = path.join(extPath, "kustomize");
}
if (osPlat == "win32") {
toolPath = `${toolPath}.exe`;
}
return yield tc.cacheFile(downloadPath, 'kustomize', 'kustomize', version);
});
}
3 changes: 2 additions & 1 deletion lib/setup-kustomize.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 0 additions & 7 deletions node_modules/@actions/core/LICENSE.md

This file was deleted.

Loading

0 comments on commit ee04d50

Please sign in to comment.