-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tests): use unique label for tests (#367)
- Loading branch information
1 parent
d2f0903
commit 9596120
Showing
6 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters