|
1 |
| -/** |
2 |
| - * © Copyright IBM Corporation 2016 |
3 |
| - * |
4 |
| - * |
5 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
6 |
| - * you may not use this file except in compliance with the License. |
7 |
| - * You may obtain a copy of the License at |
8 |
| - * |
9 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
10 |
| - * |
11 |
| - * Unless required by applicable law or agreed to in writing, software |
12 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
13 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
| - * See the License for the specific language governing permissions and |
15 |
| - * limitations under the License. |
16 |
| - **/ |
17 |
| -const exec = require('child_process').exec; |
18 |
| -const assert = require('chai').assert; |
19 |
| -const net = require('net'); |
20 |
| - |
21 |
| -// Pre-requisites for running this test: |
22 |
| -// * Docker network created |
23 |
| -// * Env. variable called DOCKER_NETWORK with the name of the network |
24 |
| -// * Env. variable called DOCKER_IMAGE with the name of the image to test |
25 |
| - |
26 |
| -const DOCKER_NETWORK = process.env.DOCKER_NETWORK; |
27 |
| -const DOCKER_IMAGE = process.env.DOCKER_IMAGE; |
28 |
| - |
29 |
| -describe('MQ Docker sample', function() { |
30 |
| - describe('when launching container', function () { |
31 |
| - this.timeout(3000); |
32 |
| - it('should display the license when LICENSE=view', function (done) { |
33 |
| - console.log(`docker run --rm --env LICENSE=view ${DOCKER_IMAGE}`); |
34 |
| - exec(`docker run --rm --env LICENSE=view ${DOCKER_IMAGE}`, function (err, stdout, stderr) { |
35 |
| - assert.equal(err.code, 1); |
36 |
| - assert.isTrue(stdout.includes("terms")); |
37 |
| - done(); |
38 |
| - }); |
39 |
| - }); |
40 |
| - it('should fail if LICENSE is not set', function (done) { |
41 |
| - exec(`docker run --rm ${DOCKER_IMAGE}`, function (err, stdout, stderr) { |
42 |
| - assert.equal(err.code, 1); |
43 |
| - done(); |
44 |
| - }); |
45 |
| - }); |
46 |
| - it('should fail if MQ_QMGR_NAME is not set', function (done) { |
47 |
| - exec(`docker run --rm --env LICENSE=accept ${DOCKER_IMAGE}`, function (err, stdout, stderr) { |
48 |
| - assert.equal(err.code, 1); |
49 |
| - assert.isTrue(stderr.includes("ERROR")); |
50 |
| - done(); |
51 |
| - }); |
52 |
| - }); |
53 |
| - }); |
54 |
| - |
55 |
| - describe('with running container', function() { |
56 |
| - var containerId = null; |
57 |
| - var containerAddr = null; |
58 |
| - const QMGR_NAME = "foo"; |
59 |
| - |
60 |
| - beforeEach(function(done) { |
61 |
| - this.timeout(10000); |
62 |
| - exec(`docker run -d --env LICENSE=accept --env MQ_QMGR_NAME=${QMGR_NAME} --net ${DOCKER_NETWORK} ${DOCKER_IMAGE}`, function (err, stdout, stderr) { |
63 |
| - if (err) throw err; |
64 |
| - containerId = stdout.trim(); |
65 |
| - // Run dspmq every second, until the queue manager comes up |
66 |
| - let timer = setInterval(function() { |
67 |
| - exec(`docker exec ${containerId} dspmq -n`, function (err, stdout, stderr) { |
68 |
| - if (err) throw err; |
69 |
| - if (stdout && stdout.includes("RUNNING")) { |
70 |
| - // Queue manager is up, so clear the timer |
71 |
| - clearInterval(timer); |
72 |
| - exec(`docker inspect --format '{{ .NetworkSettings.Networks.${DOCKER_NETWORK}.IPAddress }}' ${containerId}`, function (err, stdout, stderr) { |
73 |
| - if (err) throw err; |
74 |
| - containerAddr = stdout.trim(); |
75 |
| - done(); |
76 |
| - }); |
77 |
| - } |
78 |
| - }); |
79 |
| - }, 1000); |
80 |
| - |
81 |
| - }); |
82 |
| - }); |
83 |
| - |
84 |
| - afterEach(function(done) { |
85 |
| - exec(`docker rm --force ${containerId}`, function (err, stdout, stderr) { |
86 |
| - done(); |
87 |
| - }); |
88 |
| - }); |
89 |
| - |
90 |
| - it('should be listening on port 1414 on the Docker network', function (done) { |
91 |
| - const client = net.connect({host: containerAddr, port: 1414}, () => { |
92 |
| - client.on('close', done); |
93 |
| - client.end(); |
94 |
| - }); |
95 |
| - }); |
96 |
| - }); |
97 |
| -}); |
| 1 | +/** |
| 2 | + * © Copyright IBM Corporation 2016 |
| 3 | + * |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + **/ |
| 17 | +const assert = require('chai').assert; |
| 18 | +const exec = require('child_process').exec; |
| 19 | +const fs = require('fs'); |
| 20 | +const net = require('net'); |
| 21 | + |
| 22 | +// Pre-requisites for running this test: |
| 23 | +// * Docker network created |
| 24 | +// * Env. variable called DOCKER_NETWORK with the name of the network |
| 25 | +// * Env. variable called DOCKER_IMAGE with the name of the image to test |
| 26 | + |
| 27 | +const DOCKER_NETWORK = process.env.DOCKER_NETWORK; |
| 28 | +const DOCKER_IMAGE = process.env.DOCKER_IMAGE; |
| 29 | +const QMGR_NAME = "qm1"; |
| 30 | +const VOLUME_PREFIX = process.env.TEMP_DIR + "/tmp"; |
| 31 | + |
| 32 | +describe('MQ Docker sample', function() { |
| 33 | + describe('when launching container', function () { |
| 34 | + this.timeout(3000); |
| 35 | + it('should display the license when LICENSE=view', function (done) { |
| 36 | + exec(`docker run --rm --env LICENSE=view ${DOCKER_IMAGE}`, function (err, stdout, stderr) { |
| 37 | + assert.equal(err.code, 1); |
| 38 | + assert.isTrue(stdout.includes("terms")); |
| 39 | + done(); |
| 40 | + }); |
| 41 | + }); |
| 42 | + it('should fail if LICENSE is not set', function (done) { |
| 43 | + exec(`docker run --rm ${DOCKER_IMAGE}`, function (err, stdout, stderr) { |
| 44 | + assert.equal(err.code, 1); |
| 45 | + done(); |
| 46 | + }); |
| 47 | + }); |
| 48 | + it('should fail if MQ_QMGR_NAME is not set', function (done) { |
| 49 | + exec(`docker run --rm --env LICENSE=accept ${DOCKER_IMAGE}`, function (err, stdout, stderr) { |
| 50 | + assert.equal(err.code, 1); |
| 51 | + assert.isTrue(stderr.includes("ERROR")); |
| 52 | + done(); |
| 53 | + }); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + // Utility function to run a container and wait until MQ starts |
| 58 | + let runContainer = function(options) { |
| 59 | + return new Promise((resolve, reject) => { |
| 60 | + let cmd = `docker run -d --env LICENSE=accept --env MQ_QMGR_NAME=${QMGR_NAME} --net ${DOCKER_NETWORK} ${options} ${DOCKER_IMAGE}`; |
| 61 | + exec(cmd, function (err, stdout, stderr) { |
| 62 | + if (err) reject(err); |
| 63 | + let containerId = stdout.trim(); |
| 64 | + // Run dspmq every second, until the queue manager comes up |
| 65 | + let timer = setInterval(function() { |
| 66 | + exec(`docker exec ${containerId} dspmq -n`, function (err, stdout, stderr) { |
| 67 | + if (err) reject(err); |
| 68 | + if (stdout && stdout.includes("RUNNING")) { |
| 69 | + // Queue manager is up, so clear the timer |
| 70 | + clearInterval(timer); |
| 71 | + exec(`docker inspect --format '{{ .NetworkSettings.Networks.${DOCKER_NETWORK}.IPAddress }}' ${containerId}`, function (err, stdout, stderr) { |
| 72 | + if (err) reject(err); |
| 73 | + resolve({id: containerId, addr: stdout.trim()}); |
| 74 | + }); |
| 75 | + } |
| 76 | + }); |
| 77 | + }, 1000); |
| 78 | + }); |
| 79 | + }); |
| 80 | + }; |
| 81 | + |
| 82 | + describe('with running container', function() { |
| 83 | + let container = null; |
| 84 | + |
| 85 | + describe('and implicit volume', function() { |
| 86 | + before(function() { |
| 87 | + this.timeout(20000); |
| 88 | + return runContainer("") |
| 89 | + .then((details) => { |
| 90 | + container = details; |
| 91 | + }); |
| 92 | + }); |
| 93 | + it('should be listening on port 1414 on the Docker network', function (done) { |
| 94 | + const client = net.connect({host: container.addr, port: 1414}, () => { |
| 95 | + client.on('close', done); |
| 96 | + client.end(); |
| 97 | + }); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('and an empty bind-mounted volume', function() { |
| 102 | + let volumeDir = null; |
| 103 | + before(function() { |
| 104 | + volumeDir = fs.mkdtempSync(VOLUME_PREFIX); |
| 105 | + }); |
| 106 | + |
| 107 | + before(function() { |
| 108 | + this.timeout(20000); |
| 109 | + return runContainer(`--volume ${volumeDir}:/var/mqm`) |
| 110 | + .then((details) => { |
| 111 | + container = details; |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + after(function(done) { |
| 116 | + exec(`rm -rf ${volumeDir}`, function (err, stdout, stderr) { |
| 117 | + if (err) throw err; |
| 118 | + done(); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should be listening on port 1414 on the Docker network', function (done) { |
| 123 | + const client = net.connect({host: container.addr, port: 1414}, () => { |
| 124 | + client.on('close', done); |
| 125 | + client.end(); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + // This can happen if the entire volume directory is actually a filesystem. |
| 131 | + // See https://github.com/ibm-messaging/mq-docker/issues/29 |
| 132 | + describe('and a non-empty bind-mounted volume', function() { |
| 133 | + let volumeDir = null; |
| 134 | + before(function() { |
| 135 | + volumeDir = fs.mkdtempSync(VOLUME_PREFIX); |
| 136 | + fs.writeFileSync(`${volumeDir}/foo.txt`, 'Hello world'); |
| 137 | + }); |
| 138 | + |
| 139 | + before(function() { |
| 140 | + this.timeout(20000); |
| 141 | + return runContainer(`--volume ${volumeDir}:/var/mqm`) |
| 142 | + .then((details) => { |
| 143 | + container = details; |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + after(function(done) { |
| 148 | + exec(`rm -rf ${volumeDir}`, function (err, stdout, stderr) { |
| 149 | + if (err) throw err; |
| 150 | + done(); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should be listening on port 1414 on the Docker network', function (done) { |
| 155 | + const client = net.connect({host: container.addr, port: 1414}, () => { |
| 156 | + client.on('close', done); |
| 157 | + client.end(); |
| 158 | + }); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + afterEach(function(done) { |
| 163 | + exec(`docker rm --force ${container.id}`, function (err, stdout, stderr) { |
| 164 | + done(); |
| 165 | + }); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments