From b075e43e8bbc2964f26b4c4bbd43a0ef14022014 Mon Sep 17 00:00:00 2001 From: Akihiro Harai Date: Fri, 11 Jan 2019 16:57:17 +0900 Subject: [PATCH 1/6] Refactor #3 --- cloudbuild.yaml | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/cloudbuild.yaml b/cloudbuild.yaml index 1638af2..9ba0c8b 100644 --- a/cloudbuild.yaml +++ b/cloudbuild.yaml @@ -17,6 +17,11 @@ steps: gs://$_CACHE_BUCKET/$_GITHUB_REPO/tree/$BRANCH_NAME/$_CACHE_NAME.tar.gz \ /var/tmp/$_CACHE_NAME.tar.gz && \ tar xvzf /var/tmp/$_CACHE_NAME.tar.gz -C $_CACHE_BASEDIR) || exit 0 +- name: gcr.io/cloud-builders/curl + entrypoint: bash + args: + - -c + - mv $_CACHE_BASEDIR/$_CACHE_DIR/gopkg gopath/pkg || exit 0 # Build - name: gcr.io/cloud-builders/go:debian @@ -30,6 +35,13 @@ steps: - GO111MODULE=on # Save cache +- name: gcr.io/cloud-builders/curl + entrypoint: bash + args: + - -c + - | + mkdir -p $_CACHE_BASEDIR/$_CACHE_DIR && \ + mv gopath/pkg $_CACHE_BASEDIR/$_CACHE_DIR/gopkg - name: gcr.io/cloud-builders/gsutil entrypoint: bash args: @@ -118,9 +130,9 @@ artifacts: substitutions: _CACHE_BUCKET: kunoichimarket-cloudbuild-cache _ARTIFACT_BUCKET: kunoichimarket-cloudbuild-artifact - _CACHE_NAME: gopkg - _CACHE_BASEDIR: gopath - _CACHE_DIR: pkg + _CACHE_NAME: cache + _CACHE_BASEDIR: work + _CACHE_DIR: cache _GITHUB_REPO: github.com/kuno1/immortalize _GITHUB_USER: kuno1bot # _ENCRYPTED_GITHUB_TOKEN is generated with the following commands: From 92a2caaa8e6baf38af752d546db30ffb2c9221d2 Mon Sep 17 00:00:00 2001 From: Akihiro Harai Date: Fri, 11 Jan 2019 18:27:56 +0900 Subject: [PATCH 2/6] Add bats-core #3 --- cloudbuild.yaml | 18 ++++++++++++++++++ test/test.bats | 13 +++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 test/test.bats diff --git a/cloudbuild.yaml b/cloudbuild.yaml index 9ba0c8b..f05d830 100644 --- a/cloudbuild.yaml +++ b/cloudbuild.yaml @@ -34,6 +34,23 @@ steps: - PROJECT_ROOT=$_GITHUB_REPO - GO111MODULE=on +# Test +- name: gcr.io/cloud-builders/npm + entrypoint: bash + args: + - -c + - | + mkdir -p $_CACHE_BASEDIR/$_CACHE_DIR && cd $_CACHE_BASEDIR/$_CACHE_DIR + if [ ! -f node_modules/.bin/bats ]; then + echo '{"private": true}' > package.json + npm install bats@$_BATS_VERSION + fi +- name: gcr.io/cloud-builders/npm + entrypoint: bash + args: + - -c + - $_CACHE_BASEDIR/$_CACHE_DIR/node_modules/.bin/bats test/test.bats + # Save cache - name: gcr.io/cloud-builders/curl entrypoint: bash @@ -147,4 +164,5 @@ substitutions: _GITHUB_API: https://api.github.com/repos/kuno1/immortalize _GITHUB_UPLOADS: https://uploads.github.com/repos/kuno1/immortalize _BIN_NAME: immortalize + _BATS_VERSION: 1.1.0 _MAIN_BRANCH: master diff --git a/test/test.bats b/test/test.bats new file mode 100644 index 0000000..597b047 --- /dev/null +++ b/test/test.bats @@ -0,0 +1,13 @@ +#!/usr/bin/env bats + +@test "test1" { + sleep 1 +} + +@test "test2" { + sleep 1 +} + +@test "test3" { + sleep 1 +} From 7ecf6d62467101127e8ff2869c214fa591ef9288 Mon Sep 17 00:00:00 2001 From: Akihiro Harai Date: Sun, 13 Jan 2019 12:02:12 +0900 Subject: [PATCH 3/6] Add tests #3 --- cloudbuild.yaml | 2 +- test-bin/command-one | 24 +++++++++++++++++++++ test-bin/command-zero | 22 +++++++++++++++++++ test/test.bats | 50 ++++++++++++++++++++++++++++++++++++++----- 4 files changed, 92 insertions(+), 6 deletions(-) create mode 100755 test-bin/command-one create mode 100755 test-bin/command-zero diff --git a/cloudbuild.yaml b/cloudbuild.yaml index f05d830..bfe5d0c 100644 --- a/cloudbuild.yaml +++ b/cloudbuild.yaml @@ -49,7 +49,7 @@ steps: entrypoint: bash args: - -c - - $_CACHE_BASEDIR/$_CACHE_DIR/node_modules/.bin/bats test/test.bats + - $_CACHE_BASEDIR/$_CACHE_DIR/node_modules/.bin/bats -t test # Save cache - name: gcr.io/cloud-builders/curl diff --git a/test-bin/command-one b/test-bin/command-one new file mode 100755 index 0000000..9b76edf --- /dev/null +++ b/test-bin/command-one @@ -0,0 +1,24 @@ +#!/bin/bash -eu + +# https://stackoverflow.com/questions/9256644 +trap_with_arg() { + func="$1" ; shift + for sig ; do + # shellcheck disable=SC2064 + trap "$func $sig" "$sig" + done +} + +func_trap() { + >&2 echo "Trapped: $1" + exit 1 +} + +trap_with_arg func_trap INT TERM EXIT + +for i in {1..8}; do + sleep 0.5 + >&2 echo "$i / 8" +done + +exit 1 diff --git a/test-bin/command-zero b/test-bin/command-zero new file mode 100755 index 0000000..2ef405c --- /dev/null +++ b/test-bin/command-zero @@ -0,0 +1,22 @@ +#!/bin/bash -eu + +# https://stackoverflow.com/questions/9256644 +trap_with_arg() { + func="$1" ; shift + for sig ; do + # shellcheck disable=SC2064 + trap "$func $sig" "$sig" + done +} + +func_trap() { + echo "Trapped: $1" + exit 0 +} + +trap_with_arg func_trap INT TERM EXIT + +for i in {1..8}; do + sleep 0.5 + >&2 echo "$i / 8" +done diff --git a/test/test.bats b/test/test.bats index 597b047..ba92c01 100644 --- a/test/test.bats +++ b/test/test.bats @@ -1,13 +1,53 @@ #!/usr/bin/env bats -@test "test1" { - sleep 1 +result () { + >&3 cat work/time + # Floor the time + awk '{split($0,a,"."); print a[1]}' < work/time } -@test "test2" { - sleep 1 +measure () { + # Measure immortalize and redirect stdout/stderr to file descriptor 3. + # Details: + # https://github.com/bats-core/bats-core#file-descriptor-3-read-this-if-bats-hangs + /usr/bin/time -f '%e' -o work/time ./immortalize "$@" >&3 2>&1 & + # Store time's PID + echo "$!" > work/pid +} + +sigterm () { + # Send SIGTERM to immortalize by using time's PID + pkill -P "$(< work/pid)" +} + +# MIN -> COM +@test "MIN -> COM" { + measure -min-lifetime 2 -command test-bin/command-zero + wait + [ "$(result)" == 4 ] +} + +# COM -> MIN +@test "COM -> MIN" { + measure -min-lifetime 6 -command test-bin/command-zero + wait + [ "$(result)" == 4 ] } -@test "test3" { +# SIG -> MIN +@test "SIG -> MIN" { + measure -min-lifetime 2 -command test-bin/command-zero sleep 1 + sigterm + wait + [ "$(result)" == 2 ] +} + +# MIN -> SIG +@test "MIN -> SIG" { + measure -min-lifetime 2 -command test-bin/command-zero + sleep 3 + sigterm + wait + [ "$(result)" == 3 ] } From 3b7bd32c0f7960bb4375bf4d4dd5f0984e724e33 Mon Sep 17 00:00:00 2001 From: Akihiro Harai Date: Sun, 13 Jan 2019 12:50:55 +0900 Subject: [PATCH 4/6] Fix build error #3 --- cloudbuild.yaml | 10 ++++++++++ test/test.bats | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/cloudbuild.yaml b/cloudbuild.yaml index bfe5d0c..c669bc7 100644 --- a/cloudbuild.yaml +++ b/cloudbuild.yaml @@ -45,6 +45,16 @@ steps: echo '{"private": true}' > package.json npm install bats@$_BATS_VERSION fi +- name: gcr.io/cloud-builders/curl + entrypoint: bash + args: + - -c + - | + mkdir -p $_CACHE_BASEDIR/$_CACHE_DIR + if [ ! -f $_CACHE_BASEDIR/$_CACHE_DIR/time ]; then + apt-get update && apt-get install -y time + cp /usr/bin/time $_CACHE_BASEDIR/$_CACHE_DIR/time + fi - name: gcr.io/cloud-builders/npm entrypoint: bash args: diff --git a/test/test.bats b/test/test.bats index ba92c01..6f6d794 100644 --- a/test/test.bats +++ b/test/test.bats @@ -6,11 +6,24 @@ result () { awk '{split($0,a,"."); print a[1]}' < work/time } +time_cmd= + measure () { + if [ "$time_cmd" == '' ]; then + if [ -f /usr/bin/time ]; then + time_cmd=/usr/bin/time + elif [ -f work/cache/time ]; then + time_cmd=work/cache/time + else + >&3 echo 'Error: time command does not exist.' + exit 1 + fi + fi + # Measure immortalize and redirect stdout/stderr to file descriptor 3. # Details: # https://github.com/bats-core/bats-core#file-descriptor-3-read-this-if-bats-hangs - /usr/bin/time -f '%e' -o work/time ./immortalize "$@" >&3 2>&1 & + "$time_cmd" -f '%e' -o work/time ./immortalize "$@" >&3 2>&1 & # Store time's PID echo "$!" > work/pid } From 3b9e04e0649d10ca938d37e63d9199839d6178ce Mon Sep 17 00:00:00 2001 From: Akihiro Harai Date: Sun, 13 Jan 2019 13:22:14 +0900 Subject: [PATCH 5/6] Fix build error #3 --- .gitignore | 1 - .realize.yaml | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 84b1e47..76b5fe4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -/immortalize /work/ diff --git a/.realize.yaml b/.realize.yaml index c00d67f..fb93879 100755 --- a/.realize.yaml +++ b/.realize.yaml @@ -4,6 +4,9 @@ schema: commands: build: status: true + args: + - -o + - work/immortalize watcher: extensions: - go From 521b4746f6269b799245d6de900518124ceb6565 Mon Sep 17 00:00:00 2001 From: Akihiro Harai Date: Sun, 13 Jan 2019 13:23:48 +0900 Subject: [PATCH 6/6] Fix build error #3 --- test/test.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.bats b/test/test.bats index 6f6d794..b54d184 100644 --- a/test/test.bats +++ b/test/test.bats @@ -23,7 +23,7 @@ measure () { # Measure immortalize and redirect stdout/stderr to file descriptor 3. # Details: # https://github.com/bats-core/bats-core#file-descriptor-3-read-this-if-bats-hangs - "$time_cmd" -f '%e' -o work/time ./immortalize "$@" >&3 2>&1 & + "$time_cmd" -f '%e' -o work/time ./work/immortalize "$@" >&3 2>&1 & # Store time's PID echo "$!" > work/pid }