Skip to content

Commit

Permalink
test(integration): add start of integration testing framework (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
Silas Boyd-Wickizer committed May 20, 2019
1 parent 283cc34 commit d9543a1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ Run the unit tests:
npm test
```

The integration tests use the `current-context` in your kubeconfig file. Run the integration tests:

```
npm run test-integration
```

Run integration tests with the `@kubernetes/client-node` backend:

```
KUBERNETES_CLIENT_BACKEND=client-node npm run test-integration
```

## References

* [An Intuitive Node.js Client for the Kubernetes API](https://godaddy.github.io/2018/04/10/an-intuitive-nodejs-client-for-the-kubernetes-api/)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"release": "standard-version --tag-prefix=''",
"test": "standard && mocha 'lib/**/*.test.js' 'backends/**/*.test.js' && npm run test-typings && npm run test-generated",
"test-generated": "scripts/test-generated.sh",
"test-integration": "mocha test-integration/ --timeout 30000",
"test-typings": "tsc --project ./typings"
},
"repository": "godaddy/kubernetes-client",
Expand Down
12 changes: 12 additions & 0 deletions test-integration/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable max-nested-callbacks */
/* eslint-env mocha */
'use strict'

const env = require('./env')

describe('test-integration/basic', () => {
it('creates and deletes Namespaces', async () => {
await env.setupNamespace()
await env.tearDownNamespace()
})
})
59 changes: 59 additions & 0 deletions test-integration/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable max-nested-callbacks */
/* eslint-env mocha */
'use strict'

const k8s = require('@kubernetes/client-node')

const Client = require('../').Client
const Request = require('../backends/request')

const ClientNodeBackend = require('../backends/kubernetes-client-node')

async function getClient () {
if (process.env.KUBERNETES_CLIENT_BACKEND === 'client-node') {
const kubeconfig = new k8s.KubeConfig()
kubeconfig.loadFromDefault()
const backend = new ClientNodeBackend({ client: k8s, kubeconfig })
const client = new Client({ backend, version: '1.13' })
return client
} else {
const backend = new Request(Request.config.fromKubeconfig())
const client = new Client({ backend })
await client.loadSpec()
return client
}
}

let lastNamespace = null

async function setupNamespace (options) {
options = options || {}
const client = options.client || await getClient()

const namespace = `test-${Math.floor(Math.random() * 900000 + 100000)}`
lastNamespace = namespace

await client.api.v1.namespaces.post({
body: {
metadata: {
name: namespace
}
}
})

return namespace
}

async function tearDownNamespace (options) {
options = options || {}
const client = options.clent || await getClient()
const namespace = options.namespace || lastNamespace

await client.api.v1.namespaces(namespace).delete()
}

module.exports = {
getClient,
setupNamespace,
tearDownNamespace
}

0 comments on commit d9543a1

Please sign in to comment.