Skip to content

Commit

Permalink
fix(tests): use unique label for tests (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkwlui authored and JustinBeckwith committed Oct 16, 2018
1 parent d2f0903 commit 9596120
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 4 deletions.
29 changes: 29 additions & 0 deletions .kokoro/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Copyright 2018 Google LLC
#
# Licensed 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
#
# https://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.

set -xeo pipefail

export NPM_CONFIG_PREFIX=/home/node/.npm-global

# Setup service account credentials.
export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/spanner-key.json
export GCLOUD_PROJECT=long-door-651

cd $(dirname $0)/..

npm install

npm run cleanup
27 changes: 27 additions & 0 deletions .kokoro/release/cleanup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Format: //devtools/kokoro/config/proto/build.proto

# Build logs will be here
action {
define_artifacts {
regex: "**/*sponge_log.xml"
}
}

# Download trampoline resources.
gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline"

# Download resources for system tests (service account key, etc.)
gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/google-cloud-nodejs"

# Use the trampoline script to run in docker.
build_file: "nodejs-spanner/.kokoro/trampoline.sh"

# Configure the docker image for kokoro-trampoline.
env_vars: {
key: "TRAMPOLINE_IMAGE"
value: "gcr.io/cloud-devrel-kokoro-resources/node:8-user"
}
env_vars: {
key: "TRAMPOLINE_BUILD_FILE"
value: "github/nodejs-spanner/.kokoro/cleanup.sh"
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@
"cover": "nyc --reporter=lcov mocha test/*.js && nyc report",
"docs": "jsdoc -c .jsdoc.js",
"generate-scaffolding": "repo-tools generate all && repo-tools generate lib_samples_readme -l samples/ --config ../.cloud-repo-tools.json",
"lint": "eslint src/ samples/ system-test/ test/",
"lint": "eslint src/ samples/ system-test/ test/ scripts/",
"prettier": "prettier --write benchmark/*.js src/*.js src/*/*.js src/admin/database/*/*.js src/admin/instance/*/*.js samples/*.js samples/*/*.js samples/*/*/*.js test/*.js test/*/*.js system-test/*.js system-test/*/*.js",
"samples-test": "cd samples/ && npm link ../ && npm test && cd ../",
"system-test": "mocha system-test/*.js --timeout 600000",
"cleanup": "mocha scripts/cleanup.js --timeout 30000",
"test-no-cover": "mocha test/*.js",
"test": "npm run cover",
"ycsb": "node ./benchmark/ycsb.js run -P ./benchmark/workloada -p table=usertable -p cloudspanner.instance=ycsb-instance -p operationcount=100 -p cloudspanner.database=ycsb",
Expand Down
5 changes: 5 additions & 0 deletions scripts/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
env:
mocha: true
rules:
node/no-unsupported-features/es-syntax: off
67 changes: 67 additions & 0 deletions scripts/cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*!
* Copyright 2018 Google LLC. All Rights Reserved.
*
* Licensed 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.
*/

const {Spanner} = require('../');
const async = require('async');

const spanner = new Spanner({projectId: process.env.GCLOUD_PROJECT});

// Delete instances that are 1 hour old.
const STALE_THRESHOLD = 60 * 60;

const CURRENT_DATE = new Date();

async function deleteStaleInstances(labelFilter) {
const [instances] = await spanner.getInstances({});

const filtered = instances.filter(instance => {
return labelFilter(instance.metadata.labels);
});

return new Promise((resolve, reject) => {
async.eachLimit(
filtered,
5,
(instance, callback) => {
setTimeout(() => {
instance.delete(callback);
}, 500); // Delay allows the instance and its databases to fully clear.
},
(err, res) => {
if (err) return reject(err);
resolve(res);
}
);
});
}

describe('Clean up', () => {
it('should clean up stale instances', async () => {
// Remove instances with label { created: Date } that's older than STALE_THRESHOLD
const labelFilter = labels => {
if (labels.created) {
const creationDate = new Date(labels.created * 1000);
return (
CURRENT_DATE.valueOf() - creationDate.valueOf() >
STALE_THRESHOLD * 1000
);
}
return false;
};

await deleteStaleInstances(labelFilter);
});
});
14 changes: 11 additions & 3 deletions system-test/spanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,24 @@ const uuid = require('uuid');
const {Spanner} = require('../');

const PREFIX = 'gcloud-tests-';
const RUN_ID = uuid
.v1()
.split('-')
.shift(); // get a short uuid
const LABEL = `gcloud-tests-${RUN_ID}`;
const spanner = new Spanner({projectId: process.env.GCLOUD_PROJECT});

const CURRENT_TIME = Math.round(Date.now() / 1000).toString();

describe('Spanner', () => {
const instance = spanner.instance(generateName('instance'));

const INSTANCE_CONFIG = {
config: 'regional-us-central1',
nodes: 1,
labels: {
'gcloud-tests': 'true',
[LABEL]: 'true',
created: CURRENT_TIME,
},
};

Expand Down Expand Up @@ -3184,7 +3192,7 @@ describe('Spanner', () => {
keys: ['k1'],
columns: ALL_COLUMNS,
gaxOptions: {
timeout: 10,
timeout: 1,
},
};

Expand Down Expand Up @@ -4337,7 +4345,7 @@ function execAfterOperationComplete(callback) {
function deleteTestInstances(done) {
spanner.getInstances(
{
filter: 'labels.gcloud-tests:true',
filter: `labels.${LABEL}:true`,
},
(err, instances) => {
if (err) {
Expand Down

0 comments on commit 9596120

Please sign in to comment.