-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(integration): add more integration tests (#480)
- Loading branch information
Silas Boyd-Wickizer
committed
May 23, 2019
1 parent
5149309
commit 1d3948a
Showing
4 changed files
with
120 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 |
---|---|---|
@@ -1,12 +1,37 @@ | ||
/* eslint-disable max-nested-callbacks */ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const expect = require('chai').expect | ||
const fs = require('fs') | ||
const yaml = require('js-yaml') | ||
|
||
const { sleep, waitForPod } = require('./common') | ||
const env = require('./env') | ||
|
||
describe('test-integration/basic', () => { | ||
it('creates and deletes Namespaces', async () => { | ||
await env.setupNamespace() | ||
await env.tearDownNamespace() | ||
let namespace = null | ||
beforeEach(async () => { | ||
namespace = await env.setupNamespace() | ||
}) | ||
afterEach(async () => { | ||
await env.tearDownNamespace(namespace) | ||
}) | ||
|
||
// Tests beforeEach and afterEach | ||
it('creates and deletes Namespaces', async () => { }) | ||
|
||
it('gets Pod logs', async () => { | ||
const manifest = yaml.safeLoad(fs.readFileSync('./test-integration/busybox-pod.yaml')) | ||
const client = await env.getClient() | ||
await client.api.v1.namespaces(namespace).pods.post({ body: manifest }) | ||
await waitForPod({ client, namespace, name: manifest.metadata.name }) | ||
|
||
for (let attempt = 0; attempt < 3; attempt++) { | ||
const result = await client.api.v1.namespaces(namespace).pods(manifest.metadata.name).log.get() | ||
if (result.body && result.body === 'hello\n') return | ||
await sleep(1000) | ||
} | ||
|
||
expect.fail('Did not read expected Pod log result') | ||
}) | ||
}) |
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,12 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: busybox | ||
spec: | ||
containers: | ||
- image: busybox | ||
command: ["sh"] | ||
args: ["-c", "echo hello; sleep 3600"] | ||
imagePullPolicy: IfNotPresent | ||
name: busybox | ||
restartPolicy: Always |
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,21 @@ | ||
function sleep (ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)) | ||
} | ||
|
||
async function waitForPod ({ client, namespace, name, timeout = 10000 }) { | ||
let now = (new Date()).getTime() | ||
const end = now + timeout | ||
while (true) { | ||
const pod = await client.api.v1.namespaces(namespace).pods(name).get() | ||
if (pod.body.status.phase === 'Running') return | ||
if (now > end) throw new Error('timeout') | ||
|
||
await sleep(500) | ||
now = (new Date()).getTime() | ||
} | ||
} | ||
|
||
module.exports = { | ||
sleep, | ||
waitForPod | ||
} |
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,58 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const yaml = require('js-yaml') | ||
|
||
const { waitForPod } = require('./common') | ||
const env = require('./env') | ||
|
||
describe('test-integration/stream', () => { | ||
let namespace = null | ||
beforeEach(async () => { | ||
namespace = await env.setupNamespace() | ||
}) | ||
afterEach(async () => { | ||
await env.tearDownNamespace(namespace) | ||
}) | ||
|
||
it('streams Pod logs', async () => { | ||
const manifest = yaml.safeLoad(fs.readFileSync('./test-integration/busybox-pod.yaml')) | ||
const client = await env.getClient() | ||
await client.api.v1.namespaces(namespace).pods.post({ body: manifest }) | ||
await waitForPod({ client, namespace, name: manifest.metadata.name }) | ||
|
||
const stream = client.api.v1 | ||
.namespaces(namespace) | ||
.pods(manifest.metadata.name) | ||
.log | ||
.getStream() | ||
|
||
await new Promise((resolve, reject) => { | ||
stream.on('data', data => { | ||
if (data.toString() === 'hello\n') return resolve() | ||
reject(new Error(`Unexpected log contents ${data.toString()}`)) | ||
}) | ||
}) | ||
}) | ||
|
||
it('watches Pod events', async () => { | ||
const manifest = yaml.safeLoad(fs.readFileSync('./test-integration/busybox-pod.yaml')) | ||
const client = await env.getClient() | ||
await client.api.v1.namespaces(namespace).pods.post({ body: manifest }) | ||
await waitForPod({ client, namespace, name: manifest.metadata.name }) | ||
|
||
const stream = client.api.v1.watch.namespaces(namespace).pods.getStream() | ||
|
||
await new Promise((resolve, reject) => { | ||
stream.on('data', data => { | ||
stream.abort() | ||
resolve() | ||
}) | ||
stream.on('error', err => { | ||
stream.abort() | ||
reject(err) | ||
}) | ||
}) | ||
}) | ||
}) |