Skip to content

Commit

Permalink
test(integration): add more integration tests (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
Silas Boyd-Wickizer committed May 23, 2019
1 parent 5149309 commit 1d3948a
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 4 deletions.
33 changes: 29 additions & 4 deletions test-integration/basic.js
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')
})
})
12 changes: 12 additions & 0 deletions test-integration/busybox-pod.yaml
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
21 changes: 21 additions & 0 deletions test-integration/common.js
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
}
58 changes: 58 additions & 0 deletions test-integration/stream.js
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)
})
})
})
})

0 comments on commit 1d3948a

Please sign in to comment.