2019-12-05 17:09:02 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2019 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { expect } from 'chai';
|
|
|
|
import { stripIndent } from 'common-tags';
|
|
|
|
|
|
|
|
import { BalenaAPIMock } from '../../balena-api-mock';
|
|
|
|
import { runCommand } from '../../helpers';
|
|
|
|
|
|
|
|
describe('balena envs', function() {
|
|
|
|
const appName = 'test';
|
2019-12-04 18:04:57 +00:00
|
|
|
let fullUUID: string;
|
|
|
|
let shortUUID: string;
|
2019-12-05 17:09:02 +00:00
|
|
|
let api: BalenaAPIMock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
api = new BalenaAPIMock();
|
2019-12-04 18:04:57 +00:00
|
|
|
api.expectWhoAmI(true);
|
|
|
|
api.expectMixpanel();
|
|
|
|
// Random device UUID used to frustrate _.memoize() in utils/cloud.ts
|
|
|
|
fullUUID = require('crypto')
|
|
|
|
.randomBytes(16)
|
|
|
|
.toString('hex');
|
|
|
|
shortUUID = fullUUID.substring(0, 7);
|
2019-12-05 17:09:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
// Check all expected api calls have been made and clean up.
|
|
|
|
api.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully list env vars for a test app', async () => {
|
|
|
|
api.expectTestApp();
|
|
|
|
api.expectAppEnvVars();
|
|
|
|
|
|
|
|
const { out, err } = await runCommand(`envs -a ${appName}`);
|
|
|
|
|
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
|
|
|
ID NAME VALUE
|
|
|
|
120101 var1 var1-val
|
|
|
|
120102 var2 22
|
2019-12-04 18:04:57 +00:00
|
|
|
` + '\n',
|
2019-12-05 17:09:02 +00:00
|
|
|
);
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
it('should successfully list config vars for a test app', async () => {
|
|
|
|
api.expectTestApp();
|
|
|
|
api.expectAppConfigVars();
|
|
|
|
|
|
|
|
const { out, err } = await runCommand(`envs -a ${appName} --config`);
|
|
|
|
|
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
|
|
|
ID NAME VALUE
|
|
|
|
120300 RESIN_SUPERVISOR_NATIVE_LOGGER false
|
|
|
|
` + '\n',
|
|
|
|
);
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully list config vars for a test app (JSON output)', async () => {
|
|
|
|
api.expectTestApp();
|
|
|
|
api.expectAppConfigVars();
|
|
|
|
|
|
|
|
const { out, err } = await runCommand(`envs -cja ${appName}`);
|
|
|
|
|
|
|
|
expect(JSON.parse(out.join(''))).to.deep.equal([
|
|
|
|
{
|
|
|
|
id: 120300,
|
|
|
|
name: 'RESIN_SUPERVISOR_NATIVE_LOGGER',
|
|
|
|
value: 'false',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully list service variables for a test app (-s flag)', async () => {
|
|
|
|
const serviceName = 'service2';
|
|
|
|
api.expectService(serviceName);
|
|
|
|
api.expectTestApp();
|
|
|
|
api.expectAppServiceVars();
|
|
|
|
|
|
|
|
const { out, err } = await runCommand(
|
|
|
|
`envs -a ${appName} -s ${serviceName}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
|
|
|
ID NAME VALUE
|
|
|
|
120111 svar2 svar2-value
|
|
|
|
` + '\n',
|
|
|
|
);
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should produce an empty JSON array when no app service variables exist', async () => {
|
|
|
|
const serviceName = 'nono';
|
|
|
|
api.expectService(serviceName);
|
|
|
|
api.expectTestApp();
|
|
|
|
api.expectAppServiceVars();
|
|
|
|
|
|
|
|
const { out, err } = await runCommand(
|
|
|
|
`envs -a ${appName} -s ${serviceName} -j`,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(out.join('')).to.equal('[]\n');
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully list env and service vars for a test app (--all flag)', async () => {
|
|
|
|
api.expectTestApp();
|
|
|
|
api.expectAppEnvVars();
|
|
|
|
api.expectAppServiceVars();
|
|
|
|
|
|
|
|
const { out, err } = await runCommand(`envs -a ${appName} --all`);
|
|
|
|
|
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
|
|
|
ID NAME VALUE APPLICATION SERVICE
|
|
|
|
120110 svar1 svar1-value test service1
|
|
|
|
120111 svar2 svar2-value test service2
|
|
|
|
120101 var1 var1-val test *
|
|
|
|
120102 var2 22 test *
|
|
|
|
` + '\n',
|
|
|
|
);
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully list env and service vars for a test app (--all -s flags)', async () => {
|
|
|
|
const serviceName = 'service1';
|
|
|
|
api.expectService(serviceName);
|
|
|
|
api.expectTestApp();
|
|
|
|
api.expectAppEnvVars();
|
|
|
|
api.expectAppServiceVars();
|
|
|
|
|
|
|
|
const { out, err } = await runCommand(
|
|
|
|
`envs -a ${appName} --all -s ${serviceName}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
|
|
|
ID NAME VALUE APPLICATION SERVICE
|
|
|
|
120110 svar1 svar1-value test ${serviceName}
|
|
|
|
120101 var1 var1-val test *
|
|
|
|
120102 var2 22 test *
|
|
|
|
` + '\n',
|
|
|
|
);
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully list env variables for a test device', async () => {
|
|
|
|
api.expectTestDevice(fullUUID);
|
2019-12-05 17:09:02 +00:00
|
|
|
api.expectDeviceEnvVars();
|
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
const { out, err } = await runCommand(`envs -d ${shortUUID}`);
|
2019-12-05 17:09:02 +00:00
|
|
|
|
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
|
|
|
ID NAME VALUE
|
|
|
|
120203 var3 var3-val
|
|
|
|
120204 var4 44
|
2019-12-04 18:04:57 +00:00
|
|
|
` + '\n',
|
2019-12-05 17:09:02 +00:00
|
|
|
);
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
it('should successfully list env variables for a test device (JSON output)', async () => {
|
|
|
|
api.expectTestDevice(fullUUID);
|
2019-12-05 17:09:02 +00:00
|
|
|
api.expectDeviceEnvVars();
|
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
const { out, err } = await runCommand(`envs -jd ${shortUUID}`);
|
2019-12-05 17:09:02 +00:00
|
|
|
|
|
|
|
expect(JSON.parse(out.join(''))).to.deep.equal([
|
|
|
|
{
|
|
|
|
id: 120203,
|
|
|
|
name: 'var3',
|
|
|
|
value: 'var3-val',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 120204,
|
|
|
|
name: 'var4',
|
|
|
|
value: '44',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
it('should successfully list config variables for a test device', async () => {
|
|
|
|
api.expectTestDevice(fullUUID);
|
|
|
|
api.expectDeviceConfigVars();
|
2019-12-05 17:09:02 +00:00
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
const { out, err } = await runCommand(`envs -d ${shortUUID} --config`);
|
2019-12-05 17:09:02 +00:00
|
|
|
|
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
|
|
|
ID NAME VALUE
|
2019-12-04 18:04:57 +00:00
|
|
|
120400 RESIN_SUPERVISOR_POLL_INTERVAL 900900
|
|
|
|
` + '\n',
|
2019-12-05 17:09:02 +00:00
|
|
|
);
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
it('should successfully list service variables for a test device (-s flag)', async () => {
|
|
|
|
const serviceName = 'service2';
|
|
|
|
api.expectService(serviceName);
|
2019-12-05 17:09:02 +00:00
|
|
|
api.expectTestApp();
|
2019-12-04 18:04:57 +00:00
|
|
|
api.expectTestDevice(fullUUID);
|
|
|
|
api.expectDeviceServiceVars();
|
2019-12-05 17:09:02 +00:00
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
const { out, err } = await runCommand(
|
|
|
|
`envs -d ${shortUUID} -s ${serviceName}`,
|
|
|
|
);
|
2019-12-05 17:09:02 +00:00
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
|
|
|
ID NAME VALUE
|
|
|
|
120121 svar4 svar4-value
|
|
|
|
` + '\n',
|
|
|
|
);
|
2019-12-05 17:09:02 +00:00
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
it('should produce an empty JSON array when no device service variables exist', async () => {
|
|
|
|
const serviceName = 'nono';
|
|
|
|
api.expectService(serviceName);
|
|
|
|
api.expectTestApp();
|
|
|
|
api.expectTestDevice(fullUUID);
|
|
|
|
api.expectDeviceServiceVars();
|
|
|
|
|
|
|
|
const { out, err } = await runCommand(
|
|
|
|
`envs -d ${shortUUID} -s ${serviceName} -j`,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(out.join('')).to.equal('[]\n');
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully list env and service variables for a test device (--all flag)', async () => {
|
|
|
|
api.expectTestApp();
|
|
|
|
api.expectAppEnvVars();
|
|
|
|
api.expectAppServiceVars();
|
|
|
|
api.expectTestDevice(fullUUID);
|
|
|
|
api.expectDeviceEnvVars();
|
|
|
|
api.expectDeviceServiceVars();
|
2019-12-05 17:09:02 +00:00
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
const uuid = shortUUID;
|
|
|
|
const { out, err } = await runCommand(`envs -d ${uuid} --all`);
|
2019-12-05 17:09:02 +00:00
|
|
|
|
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
2019-12-04 18:04:57 +00:00
|
|
|
ID NAME VALUE APPLICATION DEVICE SERVICE
|
|
|
|
120110 svar1 svar1-value test * service1
|
|
|
|
120111 svar2 svar2-value test * service2
|
|
|
|
120120 svar3 svar3-value test ${uuid} service1
|
|
|
|
120121 svar4 svar4-value test ${uuid} service2
|
|
|
|
120101 var1 var1-val test * *
|
|
|
|
120102 var2 22 test * *
|
|
|
|
120203 var3 var3-val test ${uuid} *
|
|
|
|
120204 var4 44 test ${uuid} *
|
|
|
|
` + '\n',
|
|
|
|
);
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully list env and service variables for a test device (unknown app)', async () => {
|
|
|
|
api.expectTestDevice(fullUUID, true);
|
|
|
|
api.expectDeviceEnvVars();
|
|
|
|
api.expectDeviceServiceVars();
|
|
|
|
|
|
|
|
const uuid = shortUUID;
|
|
|
|
const { out, err } = await runCommand(`envs -d ${uuid} --all`);
|
|
|
|
|
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
|
|
|
ID NAME VALUE APPLICATION DEVICE SERVICE
|
|
|
|
120120 svar3 svar3-value N/A ${uuid} service1
|
|
|
|
120121 svar4 svar4-value N/A ${uuid} service2
|
|
|
|
120203 var3 var3-val N/A ${uuid} *
|
|
|
|
120204 var4 44 N/A ${uuid} *
|
|
|
|
` + '\n',
|
|
|
|
);
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully list env and service vars for a test device (--all -s flags)', async () => {
|
|
|
|
const serviceName = 'service1';
|
|
|
|
api.expectService(serviceName);
|
|
|
|
api.expectTestApp();
|
|
|
|
api.expectAppEnvVars();
|
|
|
|
api.expectAppServiceVars();
|
|
|
|
api.expectTestDevice(fullUUID);
|
|
|
|
api.expectDeviceEnvVars();
|
|
|
|
api.expectDeviceServiceVars();
|
|
|
|
|
|
|
|
const uuid = shortUUID;
|
|
|
|
const { out, err } = await runCommand(
|
|
|
|
`envs -d ${uuid} --all -s ${serviceName}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
|
|
|
ID NAME VALUE APPLICATION DEVICE SERVICE
|
|
|
|
120110 svar1 svar1-value test * ${serviceName}
|
|
|
|
120120 svar3 svar3-value test ${uuid} ${serviceName}
|
|
|
|
120101 var1 var1-val test * *
|
|
|
|
120102 var2 22 test * *
|
|
|
|
120203 var3 var3-val test ${uuid} *
|
|
|
|
120204 var4 44 test ${uuid} *
|
|
|
|
` + '\n',
|
|
|
|
);
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully list env and service vars for a test device (--all -js flags)', async () => {
|
|
|
|
const serviceName = 'service1';
|
|
|
|
api.expectService(serviceName);
|
|
|
|
api.expectTestApp();
|
|
|
|
api.expectAppEnvVars();
|
|
|
|
api.expectAppServiceVars();
|
|
|
|
api.expectTestDevice(fullUUID);
|
|
|
|
api.expectDeviceEnvVars();
|
|
|
|
api.expectDeviceServiceVars();
|
|
|
|
|
|
|
|
const { out, err } = await runCommand(
|
|
|
|
`envs -d ${shortUUID} --all -js ${serviceName}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(JSON.parse(out.join(''))).to.deep.equal(
|
|
|
|
JSON.parse(`[
|
|
|
|
{ "id": 120101, "appName": "test", "deviceUUID": "*", "name": "var1", "value": "var1-val", "serviceName": "*" },
|
|
|
|
{ "id": 120102, "appName": "test", "deviceUUID": "*", "name": "var2", "value": "22", "serviceName": "*" },
|
|
|
|
{ "id": 120110, "appName": "test", "deviceUUID": "*", "name": "svar1", "value": "svar1-value", "serviceName": "${serviceName}" },
|
|
|
|
{ "id": 120120, "appName": "test", "deviceUUID": "${fullUUID}", "name": "svar3", "value": "svar3-value", "serviceName": "${serviceName}" },
|
|
|
|
{ "id": 120203, "appName": "test", "deviceUUID": "${fullUUID}", "name": "var3", "value": "var3-val", "serviceName": "*" },
|
|
|
|
{ "id": 120204, "appName": "test", "deviceUUID": "${fullUUID}", "name": "var4", "value": "44", "serviceName": "*" }
|
|
|
|
]`),
|
2019-12-05 17:09:02 +00:00
|
|
|
);
|
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
});
|