Skip to content

Commit 8350c3b

Browse files
telpirionNimJay
authored andcommitted
test: adds cleanup function for tests (#28)
* feat: adds cleanup function for tests
1 parent 3b87355 commit 8350c3b

8 files changed

Lines changed: 110 additions & 3 deletions

File tree

game-servers/snippets/get_realm.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ function main(
4545
console.log(`Realm name: ${realm.name}`);
4646
console.log(`Realm description: ${realm.description}`);
4747
console.log(`Realm time zone: ${realm.timeZone}`);
48+
49+
const createTime = realm.createTime;
50+
const createDate = new Date(createTime.seconds * 1000);
51+
52+
console.log(`Realm created on: ${createDate.toLocaleDateString()}`);
4853
// [END cloud_game_servers_get_realm]
4954
}
5055

game-servers/snippets/list_realms.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ function main(projectId = 'YOUR_PROJECT_ID', location = 'LOCATION_ID') {
3838
for (const realm of results.realms) {
3939
console.log(`Realm name: ${realm.name}`);
4040
console.log(`Realm description: ${realm.description}`);
41-
console.log(`Realm time zone: ${realm.timeZone}\n`);
41+
console.log(`Realm time zone: ${realm.timeZone}`);
42+
43+
const createTime = realm.createTime;
44+
const createDate = new Date(createTime.seconds * 1000);
45+
console.log(`Realm created on: ${createDate.toLocaleDateString()}\n`);
4246
}
4347
// [END cloud_game_servers_list_realms]
4448
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
const {GameServerClustersServiceClient} = require('@google-cloud/game-servers');
16+
const {RealmsServiceClient} = require('@google-cloud/game-servers');
17+
18+
const gameServerClusterClient = new GameServerClustersServiceClient();
19+
const realmsClient = new RealmsServiceClient();
20+
21+
/**
22+
* Utility function for removing unneeded realms from project.
23+
*/
24+
module.exports = async () => {
25+
const projectId = await realmsClient.getProjectId();
26+
const location = 'us-central1';
27+
28+
const request = {
29+
parent: `projects/${projectId}/locations/${location}`,
30+
};
31+
32+
const MAX_REALM_LIFESPAN = 3600000; // One hour (in milliseconds)
33+
const NOW = new Date();
34+
35+
const [results] = await realmsClient.listRealms(request);
36+
for (const realm of results.realms) {
37+
// Check age of realm. If older than maximum life span, delete.
38+
const ageOfRealm = new Date(realm.createTime.seconds * 1000);
39+
if (NOW - ageOfRealm > MAX_REALM_LIFESPAN) {
40+
// First, delete all clusters associated with this realm.
41+
const clustersRequest = {
42+
parent: realm.name,
43+
};
44+
45+
const clustersResult = await gameServerClusterClient.listGameServerClusters(
46+
clustersRequest
47+
);
48+
const [clusters] = clustersResult;
49+
for (const cluster of clusters.gameServerClusters) {
50+
const deleteClusterRequest = {
51+
name: cluster.name,
52+
};
53+
54+
const [
55+
deleteClusterOperation,
56+
] = await gameServerClusterClient.deleteGameServerCluster(
57+
deleteClusterRequest
58+
);
59+
await deleteClusterOperation.promise();
60+
}
61+
62+
// Then delete the realm itself.
63+
const realmDeleteRequest = {
64+
name: realm.name,
65+
};
66+
67+
const [deleteRealmOperation] = await realmsClient.deleteRealm(
68+
realmDeleteRequest
69+
);
70+
await deleteRealmOperation.promise();
71+
}
72+
}
73+
};

game-servers/snippets/test/create_realm.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
'use strict';
1616

1717
const {assert} = require('chai');
18-
const {describe, it, after} = require('mocha');
18+
const cleanup = require('./clean.js');
19+
const {describe, before, it, after} = require('mocha');
1920
const {RealmsServiceClient} = require('@google-cloud/game-servers');
2021

2122
const cp = require('child_process');
@@ -29,6 +30,10 @@ describe('Game Servers Create Realm Test', () => {
2930
const client = new RealmsServiceClient();
3031
let realmId;
3132

33+
before(async () => {
34+
await cleanup();
35+
});
36+
3237
it('should create a realm', async () => {
3338
const projectId = await client.getProjectId();
3439
realmId = `test-${uuid.v4()}`;

game-servers/snippets/test/delete_realm.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'use strict';
1616

1717
const {assert} = require('chai');
18+
const cleanup = require('./clean.js');
1819
const {describe, it, before} = require('mocha');
1920
const {RealmsServiceClient} = require('@google-cloud/game-servers');
2021

@@ -31,6 +32,10 @@ describe('Game Servers Delete Realm Test', () => {
3132

3233
before(async () => {
3334
const projectId = await client.getProjectId();
35+
36+
// Clean up any stray realms
37+
await cleanup();
38+
3439
realmId = `test-${uuid.v4()}`;
3540

3641
await client.createRealm({

game-servers/snippets/test/get_realm.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'use strict';
1616

1717
const {assert} = require('chai');
18+
const cleanup = require('./clean.js');
1819
const {describe, it, before, after} = require('mocha');
1920
const {RealmsServiceClient} = require('@google-cloud/game-servers');
2021

@@ -31,6 +32,10 @@ describe('Game Servers Get Realms Test', () => {
3132

3233
before(async () => {
3334
const projectId = await client.getProjectId();
35+
36+
// Clean up any stray realms
37+
await cleanup();
38+
3439
realmId = `test-${uuid.v4()}`;
3540

3641
await client.createRealm({

game-servers/snippets/test/list_realms.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'use strict';
1616

1717
const {assert} = require('chai');
18+
const cleanup = require('./clean.js');
1819
const {describe, it, before, after} = require('mocha');
1920
const {RealmsServiceClient} = require('@google-cloud/game-servers');
2021

@@ -31,6 +32,10 @@ describe('Game Servers List Realms Test', () => {
3132

3233
before(async () => {
3334
const projectId = await client.getProjectId();
35+
36+
// Clean up any stray realms
37+
await cleanup();
38+
3439
realmId = `test-${uuid.v4()}`;
3540

3641
await client.createRealm({

game-servers/snippets/test/quickstart.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
'use strict';
1616

1717
const {assert} = require('chai');
18-
const {describe, it, after} = require('mocha');
18+
const cleanup = require('./clean.js');
19+
const {describe, it, before, after} = require('mocha');
1920
const {RealmsServiceClient} = require('@google-cloud/game-servers');
2021

2122
const cp = require('child_process');
@@ -29,6 +30,10 @@ describe('Game Servers Quickstart Test', () => {
2930
const client = new RealmsServiceClient();
3031
let realmId;
3132

33+
before(async () => {
34+
await cleanup();
35+
});
36+
3237
it('should create a realm', async () => {
3338
const projectId = await client.getProjectId();
3439
realmId = `test-${uuid.v4()}`;

0 commit comments

Comments
 (0)