From 1688b117e1fb7715c3879a29496a7562cf57dfca Mon Sep 17 00:00:00 2001 From: Konrad Pabjan Date: Thu, 23 Apr 2020 20:52:53 +0200 Subject: [PATCH] E2E tests for the @actions/artifact package (#421) * End-to-end artifact tests * E2E tests for artifact package --- .github/workflows/artifact-tests.yml | 79 +++++++++++++++++++ .../__tests__/ci-test-action/action.yml | 5 ++ .../__tests__/ci-test-action/index.js | 5 ++ packages/artifact/package-lock.json | 11 +++ packages/artifact/package.json | 3 + scripts/test-artifact-file.sh | 25 ++++++ 6 files changed, 128 insertions(+) create mode 100644 .github/workflows/artifact-tests.yml create mode 100644 packages/artifact/__tests__/ci-test-action/action.yml create mode 100644 packages/artifact/__tests__/ci-test-action/index.js create mode 100755 scripts/test-artifact-file.sh diff --git a/.github/workflows/artifact-tests.yml b/.github/workflows/artifact-tests.yml new file mode 100644 index 00000000..8b45404a --- /dev/null +++ b/.github/workflows/artifact-tests.yml @@ -0,0 +1,79 @@ +name: artifact-unit-tests +on: push + +jobs: + build: + name: Build + + strategy: + matrix: + runs-on: [ubuntu-latest, windows-latest, macOS-latest] + fail-fast: false + + runs-on: ${{ matrix.runs-on }} + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Set Node.js 12.x + uses: actions/setup-node@v1 + with: + node-version: 12.x + + # In order to upload & download artifacts from a shell script, certain env variables need to be set that are only available in the + # node context. This runs a local action that gets and sets the necessary env variables that are needed + - name: Set env variables + uses: ./packages/artifact/__tests__/ci-test-action/ + + # Need root node_modules because certain npm packages like jest are configured for the entire repository and it won't be possible + # without these to just compile the artifacts package + - name: Install root npm packages + run: npm ci + + - name: Compile artifact package + run: | + npm ci + npm run tsc + working-directory: packages/artifact + + - name: Set artifact file contents + run: | + echo "::set-env name=non-gzip-artifact-content::hello" + echo "::set-env name=gzip-artifact-content::Some large amount of text that has a compression ratio that is greater than 100%. If greater than 100%, gzip is used to upload the file" + + - name: Create files that will be uploaded + run: | + mkdir artifact-path + echo ${{ env.non-gzip-artifact-content }} > artifact-path/world.txt + echo ${{ env.gzip-artifact-content }} > artifact-path/gzip.txt + + # We're using node -e to call the functions directly available in the @actions/artifact package + - name: Upload artifacts using uploadArtifact() + run: | + node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().uploadArtifact('my-artifact-1',['artifact-path/world.txt'], '${{ github.workspace }}'))" + node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().uploadArtifact('my-artifact-2',['artifact-path/gzip.txt'], '${{ github.workspace }}'))" + + - name: Download artifacts using downloadArtifact() + run: | + mkdir artifact-1-directory + node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadArtifact('my-artifact-1','artifact-1-directory'))" + mkdir artifact-2-directory + node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadArtifact('my-artifact-2','artifact-2-directory'))" + + - name: Verify downloadArtifact() + shell: bash + run: | + scripts/test-artifact-file.sh "artifact-1-directory/artifact-path/world.txt" "${{ env.non-gzip-artifact-content }}" + scripts/test-artifact-file.sh "artifact-2-directory/artifact-path/gzip.txt" "${{ env.gzip-artifact-content }}" + + - name: Download artifacts using downloadAllArtifacts() + run: | + mkdir multi-artifact-directory + node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadAllArtifacts('multi-artifact-directory'))" + + - name: Verify downloadAllArtifacts() + shell: bash + run: | + scripts/test-artifact-file.sh "multi-artifact-directory/my-artifact-1/artifact-path/world.txt" "${{ env.non-gzip-artifact-content }}" + scripts/test-artifact-file.sh "multi-artifact-directory/my-artifact-2/artifact-path/gzip.txt" "${{ env.gzip-artifact-content }}" \ No newline at end of file diff --git a/packages/artifact/__tests__/ci-test-action/action.yml b/packages/artifact/__tests__/ci-test-action/action.yml new file mode 100644 index 00000000..9f80bce9 --- /dev/null +++ b/packages/artifact/__tests__/ci-test-action/action.yml @@ -0,0 +1,5 @@ +name: 'Set env variables' +description: 'Sets certain env variables so that e2e artifact upload and download can be tested in a shell' +runs: + using: 'node12' + main: 'index.js' \ No newline at end of file diff --git a/packages/artifact/__tests__/ci-test-action/index.js b/packages/artifact/__tests__/ci-test-action/index.js new file mode 100644 index 00000000..e2ff6aea --- /dev/null +++ b/packages/artifact/__tests__/ci-test-action/index.js @@ -0,0 +1,5 @@ +// Certain env variables are not set by default in a shell context and are only available in a node context from a running action +// In order to be able to upload and download artifacts e2e in a shell when running CI tests, we need these env variables set +console.log(`::set-env name=ACTIONS_RUNTIME_URL::${process.env.ACTIONS_RUNTIME_URL}`) +console.log(`::set-env name=ACTIONS_RUNTIME_TOKEN::${process.env.ACTIONS_RUNTIME_TOKEN}`) +console.log(`::set-env name=GITHUB_RUN_ID::${process.env.GITHUB_RUN_ID}`) \ No newline at end of file diff --git a/packages/artifact/package-lock.json b/packages/artifact/package-lock.json index 5e6ea9fc..4cccbe33 100644 --- a/packages/artifact/package-lock.json +++ b/packages/artifact/package-lock.json @@ -4,6 +4,11 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@actions/core": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.3.tgz", + "integrity": "sha512-Wp4xnyokakM45Uuj4WLUxdsa8fJjKVl1fDTsPbTEcTcuu0Nb26IPQbOtjmnfaCPGcaoPOOqId8H9NapZ8gii4w==" + }, "@actions/http-client": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-1.0.7.tgz", @@ -118,6 +123,12 @@ "resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz", "integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==" }, + "typescript": { + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.3.tgz", + "integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==", + "dev": true + }, "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", diff --git a/packages/artifact/package.json b/packages/artifact/package.json index 16f136c4..1068489d 100644 --- a/packages/artifact/package.json +++ b/packages/artifact/package.json @@ -41,5 +41,8 @@ "@types/tmp": "^0.1.0", "tmp": "^0.1.0", "tmp-promise": "^2.0.2" + }, + "devDependencies": { + "typescript": "^3.8.3" } } diff --git a/scripts/test-artifact-file.sh b/scripts/test-artifact-file.sh new file mode 100755 index 00000000..8b90c1ad --- /dev/null +++ b/scripts/test-artifact-file.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +path="$1" +expectedContent="$2" + +if [ "$path" == "" ]; then + echo "File path not provided" + exit 1 +fi + +if [ "$expectedContent" == "" ]; then + echo "Expected file contents not provided" + exit 1 +fi + +if [ ! -f "$path" ]; then + echo "Expected file $path does not exist" + exit 1 +fi + +actualContent=$(cat $path) +if [ "$actualContent" != "$expectedContent" ];then + echo "File contents are not correct, expected $expectedContent, recieved $actualContent" + exit 1 +fi \ No newline at end of file