2019-12-05 17:09:02 +00:00
|
|
|
/**
|
|
|
|
* @license
|
2020-01-15 19:41:47 +00:00
|
|
|
* Copyright 2019-2020 Balena Ltd.
|
2019-12-05 17:09:02 +00:00
|
|
|
*
|
|
|
|
* 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';
|
2021-09-09 00:22:32 +00:00
|
|
|
import { stripIndent } from '../../../build/utils/lazy';
|
2019-12-05 17:09:02 +00:00
|
|
|
|
2021-06-25 23:42:07 +00:00
|
|
|
import { BalenaAPIMock } from '../../nock/balena-api-mock';
|
2019-12-05 17:09:02 +00:00
|
|
|
import { runCommand } from '../../helpers';
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
describe('balena envs', function () {
|
2019-12-05 17:09:02 +00:00
|
|
|
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();
|
2020-01-15 19:41:47 +00:00
|
|
|
api.expectGetWhoAmI({ optional: true, persist: true });
|
|
|
|
api.expectGetMixpanel({ optional: true });
|
2019-12-04 18:04:57 +00:00
|
|
|
// Random device UUID used to frustrate _.memoize() in utils/cloud.ts
|
2020-06-15 22:53:07 +00:00
|
|
|
fullUUID = require('crypto').randomBytes(16).toString('hex');
|
2019-12-04 18:04:57 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
it('should successfully list env vars for a test fleet', async () => {
|
2020-12-07 13:58:23 +00:00
|
|
|
api.expectGetApplication();
|
|
|
|
api.expectGetAppEnvVars();
|
|
|
|
api.expectGetAppServiceVars();
|
|
|
|
|
2021-09-08 16:48:16 +00:00
|
|
|
const { out, err } = await runCommand(`envs -f ${appName}`);
|
2020-12-07 13:58:23 +00:00
|
|
|
|
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
2021-12-19 20:50:03 +00:00
|
|
|
ID NAME VALUE FLEET SERVICE
|
|
|
|
120110 svar1 svar1-value gh_user/testApp service1
|
|
|
|
120111 svar2 svar2-value gh_user/testApp service2
|
|
|
|
120101 var1 var1-val gh_user/testApp *
|
|
|
|
120102 var2 22 gh_user/testApp *
|
2020-12-07 13:58:23 +00:00
|
|
|
` + '\n',
|
|
|
|
);
|
2021-09-08 16:48:16 +00:00
|
|
|
expect(err.join('')).to.equal('');
|
2019-12-05 17:09:02 +00:00
|
|
|
});
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
it('should successfully list config vars for a test fleet', async () => {
|
2020-01-15 19:41:47 +00:00
|
|
|
api.expectGetApplication();
|
|
|
|
api.expectGetAppConfigVars();
|
2019-12-04 18:04:57 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
const { out, err } = await runCommand(`envs -f ${appName} --config`);
|
2019-12-04 18:04:57 +00:00
|
|
|
|
2020-12-07 13:58:23 +00:00
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
2021-07-15 13:41:38 +00:00
|
|
|
ID NAME VALUE FLEET
|
2021-12-19 20:50:03 +00:00
|
|
|
120300 RESIN_SUPERVISOR_NATIVE_LOGGER false gh_user/testApp
|
2020-12-07 13:58:23 +00:00
|
|
|
` + '\n',
|
|
|
|
);
|
2020-05-29 07:27:11 +00:00
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
it('should successfully list config vars for a test fleet (JSON output)', async () => {
|
2020-01-15 19:41:47 +00:00
|
|
|
api.expectGetApplication();
|
|
|
|
api.expectGetAppConfigVars();
|
2019-12-04 18:04:57 +00:00
|
|
|
|
2021-09-08 16:48:16 +00:00
|
|
|
const { out, err } = await runCommand(`envs -cjf ${appName}`);
|
2019-12-04 18:04:57 +00:00
|
|
|
|
2020-12-07 13:58:23 +00:00
|
|
|
expect(JSON.parse(out.join(''))).to.deep.equal([
|
|
|
|
{
|
2021-12-20 01:12:13 +00:00
|
|
|
fleet: 'gh_user/testApp',
|
2020-12-07 13:58:23 +00:00
|
|
|
id: 120300,
|
|
|
|
name: 'RESIN_SUPERVISOR_NATIVE_LOGGER',
|
|
|
|
value: 'false',
|
|
|
|
},
|
|
|
|
]);
|
2019-12-04 18:04:57 +00:00
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
it('should successfully list service variables for a test fleet (-s flag)', async () => {
|
2019-12-04 18:04:57 +00:00
|
|
|
const serviceName = 'service2';
|
2020-01-15 19:41:47 +00:00
|
|
|
api.expectGetService({ serviceName });
|
|
|
|
api.expectGetApplication();
|
2020-12-07 13:58:23 +00:00
|
|
|
api.expectGetAppEnvVars();
|
2020-01-15 19:41:47 +00:00
|
|
|
api.expectGetAppServiceVars();
|
2019-12-04 18:04:57 +00:00
|
|
|
|
|
|
|
const { out, err } = await runCommand(
|
2021-09-08 16:48:16 +00:00
|
|
|
`envs -f ${appName} -s ${serviceName}`,
|
2019-12-04 18:04:57 +00:00
|
|
|
);
|
|
|
|
|
2020-12-07 13:58:23 +00:00
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
2021-12-19 20:50:03 +00:00
|
|
|
ID NAME VALUE FLEET SERVICE
|
|
|
|
120111 svar2 svar2-value gh_user/testApp service2
|
|
|
|
120101 var1 var1-val gh_user/testApp *
|
|
|
|
120102 var2 22 gh_user/testApp *
|
2020-12-07 13:58:23 +00:00
|
|
|
` + '\n',
|
|
|
|
);
|
2021-09-08 16:48:16 +00:00
|
|
|
expect(err.join('')).to.equal('');
|
2019-12-04 18:04:57 +00:00
|
|
|
});
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
it('should successfully list env and service vars for a test fleet (-s flags)', async () => {
|
2020-12-07 13:58:23 +00:00
|
|
|
const serviceName = 'service1';
|
|
|
|
api.expectGetService({ serviceName });
|
|
|
|
api.expectGetApplication();
|
|
|
|
api.expectGetAppEnvVars();
|
|
|
|
api.expectGetAppServiceVars();
|
2019-12-04 18:04:57 +00:00
|
|
|
|
2020-12-07 13:58:23 +00:00
|
|
|
const { out, err } = await runCommand(
|
2021-09-08 16:48:16 +00:00
|
|
|
`envs -f ${appName} -s ${serviceName}`,
|
2020-12-07 13:58:23 +00:00
|
|
|
);
|
2019-12-04 18:04:57 +00:00
|
|
|
|
2020-12-07 13:58:23 +00:00
|
|
|
expect(out.join('')).to.equal(
|
|
|
|
stripIndent`
|
2021-12-19 20:50:03 +00:00
|
|
|
ID NAME VALUE FLEET SERVICE
|
|
|
|
120110 svar1 svar1-value gh_user/testApp ${serviceName}
|
|
|
|
120101 var1 var1-val gh_user/testApp *
|
|
|
|
120102 var2 22 gh_user/testApp *
|
2020-05-29 07:27:11 +00:00
|
|
|
` + '\n',
|
2020-12-07 13:58:23 +00:00
|
|
|
);
|
2021-09-08 16:48:16 +00:00
|
|
|
expect(err.join('')).to.equal('');
|
2020-12-07 13:58:23 +00:00
|
|
|
});
|
2019-12-04 18:04:57 +00:00
|
|
|
|
|
|
|
it('should successfully list env variables for a test device', async () => {
|
2020-01-15 19:41:47 +00:00
|
|
|
api.expectGetDevice({ fullUUID });
|
|
|
|
api.expectGetDeviceEnvVars();
|
2020-12-07 13:58:23 +00:00
|
|
|
api.expectGetApplication();
|
|
|
|
api.expectGetAppEnvVars();
|
|
|
|
api.expectGetAppServiceVars();
|
|
|
|
api.expectGetDeviceServiceVars();
|
2019-12-05 17:09:02 +00:00
|
|
|
|
2020-05-29 07:27:11 +00:00
|
|
|
const uuid = shortUUID;
|
2021-09-08 16:48:16 +00:00
|
|
|
const result = await runCommand(`envs -d ${uuid}`);
|
|
|
|
let { out } = result;
|
|
|
|
let expected =
|
2020-12-07 13:58:23 +00:00
|
|
|
stripIndent`
|
2021-12-19 20:50:03 +00:00
|
|
|
ID NAME VALUE FLEET DEVICE SERVICE
|
|
|
|
120110 svar1 svar1-value org/test * service1
|
|
|
|
120111 svar2 svar2-value org/test * service2
|
|
|
|
120120 svar3 svar3-value org/test ${uuid} service1
|
|
|
|
120121 svar4 svar4-value org/test ${uuid} service2
|
|
|
|
120101 var1 var1-val org/test * *
|
|
|
|
120102 var2 22 org/test * *
|
|
|
|
120203 var3 var3-val org/test ${uuid} *
|
|
|
|
120204 var4 44 org/test ${uuid} *
|
2021-09-08 16:48:16 +00:00
|
|
|
` + '\n';
|
2021-12-19 20:50:03 +00:00
|
|
|
|
|
|
|
out = out.map((l) => l.replace(/ +/g, ' '));
|
|
|
|
expected = expected.replace(/ +/g, ' ');
|
|
|
|
|
2021-09-08 16:48:16 +00:00
|
|
|
expect(out.join('')).to.equal(expected);
|
2019-12-05 17:09:02 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
it('should successfully list env variables for a test device (JSON output)', async () => {
|
2020-01-15 19:41:47 +00:00
|
|
|
api.expectGetDevice({ fullUUID });
|
|
|
|
api.expectGetDeviceEnvVars();
|
2020-12-07 13:58:23 +00:00
|
|
|
api.expectGetApplication();
|
|
|
|
api.expectGetAppEnvVars();
|
|
|
|
api.expectGetAppServiceVars();
|
|
|
|
api.expectGetDeviceServiceVars();
|
2019-12-05 17:09:02 +00:00
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
const { out, err } = await runCommand(`envs -jd ${shortUUID}`);
|
2021-12-19 20:50:03 +00:00
|
|
|
const expected = `[
|
2021-12-20 01:12:13 +00:00
|
|
|
{ "id": 120101, "fleet": "org/test", "deviceUUID": "*", "name": "var1", "value": "var1-val", "serviceName": "*" },
|
|
|
|
{ "id": 120102, "fleet": "org/test", "deviceUUID": "*", "name": "var2", "value": "22", "serviceName": "*" },
|
|
|
|
{ "id": 120110, "fleet": "org/test", "deviceUUID": "*", "name": "svar1", "value": "svar1-value", "serviceName": "service1" },
|
|
|
|
{ "id": 120111, "fleet": "org/test", "deviceUUID": "*", "name": "svar2", "value": "svar2-value", "serviceName": "service2" },
|
|
|
|
{ "id": 120120, "fleet": "org/test", "deviceUUID": "${fullUUID}", "name": "svar3", "value": "svar3-value", "serviceName": "service1" },
|
|
|
|
{ "id": 120121, "fleet": "org/test", "deviceUUID": "${fullUUID}", "name": "svar4", "value": "svar4-value", "serviceName": "service2" },
|
|
|
|
{ "id": 120203, "fleet": "org/test", "deviceUUID": "${fullUUID}", "name": "var3", "value": "var3-val", "serviceName": "*" },
|
|
|
|
{ "id": 120204, "fleet": "org/test", "deviceUUID": "${fullUUID}", "name": "var4", "value": "44", "serviceName": "*" }
|
2021-09-08 16:48:16 +00:00
|
|
|
]`;
|
2021-12-19 20:50:03 +00:00
|
|
|
|
2021-09-08 16:48:16 +00:00
|
|
|
expect(JSON.parse(out.join(''))).to.deep.equal(JSON.parse(expected));
|
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 variables for a test device', async () => {
|
2020-01-15 19:41:47 +00:00
|
|
|
api.expectGetDevice({ fullUUID });
|
|
|
|
api.expectGetDeviceConfigVars();
|
2020-12-07 13:58:23 +00:00
|
|
|
api.expectGetApplication();
|
|
|
|
api.expectGetAppConfigVars();
|
2019-12-05 17:09:02 +00:00
|
|
|
|
2021-09-08 16:48:16 +00:00
|
|
|
const result = await runCommand(`envs -d ${shortUUID} --config`);
|
|
|
|
let { out } = result;
|
|
|
|
let expected =
|
2020-12-07 13:58:23 +00:00
|
|
|
stripIndent`
|
2021-12-19 20:50:03 +00:00
|
|
|
ID NAME VALUE FLEET DEVICE
|
|
|
|
120300 RESIN_SUPERVISOR_NATIVE_LOGGER false org/test *
|
|
|
|
120400 RESIN_SUPERVISOR_POLL_INTERVAL 900900 org/test ${shortUUID}
|
2021-09-08 16:48:16 +00:00
|
|
|
` + '\n';
|
2021-12-19 20:50:03 +00:00
|
|
|
|
|
|
|
out = out.map((l) => l.replace(/ +/g, ' '));
|
|
|
|
expected = expected.replace(/ +/g, ' ');
|
|
|
|
|
2021-09-08 16:48:16 +00:00
|
|
|
expect(out.join('')).to.equal(expected);
|
2019-12-05 17:09:02 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
it('should successfully list service variables for a test device (-s flag)', async () => {
|
|
|
|
const serviceName = 'service2';
|
2020-01-15 19:41:47 +00:00
|
|
|
api.expectGetService({ serviceName });
|
|
|
|
api.expectGetApplication();
|
|
|
|
api.expectGetDevice({ fullUUID });
|
|
|
|
api.expectGetDeviceServiceVars();
|
2020-12-07 13:58:23 +00:00
|
|
|
api.expectGetAppEnvVars();
|
|
|
|
api.expectGetAppServiceVars();
|
|
|
|
api.expectGetDeviceEnvVars();
|
2019-12-05 17:09:02 +00:00
|
|
|
|
2020-05-29 07:27:11 +00:00
|
|
|
const uuid = shortUUID;
|
2021-09-08 16:48:16 +00:00
|
|
|
const result = await runCommand(`envs -d ${uuid} -s ${serviceName}`);
|
|
|
|
let { out } = result;
|
|
|
|
let expected =
|
2020-12-07 13:58:23 +00:00
|
|
|
stripIndent`
|
2021-12-19 20:50:03 +00:00
|
|
|
ID NAME VALUE FLEET DEVICE SERVICE
|
|
|
|
120111 svar2 svar2-value org/test * service2
|
|
|
|
120121 svar4 svar4-value org/test ${uuid} service2
|
|
|
|
120101 var1 var1-val org/test * *
|
|
|
|
120102 var2 22 org/test * *
|
|
|
|
120203 var3 var3-val org/test ${uuid} *
|
|
|
|
120204 var4 44 org/test ${uuid} *
|
2021-09-08 16:48:16 +00:00
|
|
|
` + '\n';
|
2021-12-19 20:50:03 +00:00
|
|
|
|
|
|
|
out = out.map((l) => l.replace(/ +/g, ' '));
|
|
|
|
expected = expected.replace(/ +/g, ' ');
|
|
|
|
|
2021-09-08 16:48:16 +00:00
|
|
|
expect(out.join('')).to.equal(expected);
|
2019-12-04 18:04:57 +00:00
|
|
|
});
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
it('should successfully list env and service variables for a test device (unknown fleet)', async () => {
|
2020-01-15 19:41:47 +00:00
|
|
|
api.expectGetDevice({ fullUUID, inaccessibleApp: true });
|
|
|
|
api.expectGetDeviceEnvVars();
|
|
|
|
api.expectGetDeviceServiceVars();
|
2019-12-04 18:04:57 +00:00
|
|
|
|
|
|
|
const uuid = shortUUID;
|
2021-09-08 16:48:16 +00:00
|
|
|
const result = await runCommand(`envs -d ${uuid}`);
|
|
|
|
let { out } = result;
|
|
|
|
let expected =
|
2019-12-04 18:04:57 +00:00
|
|
|
stripIndent`
|
2021-12-19 20:50:03 +00:00
|
|
|
ID NAME VALUE FLEET 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} *
|
2021-09-08 16:48:16 +00:00
|
|
|
` + '\n';
|
2021-12-19 20:50:03 +00:00
|
|
|
|
|
|
|
out = out.map((l) => l.replace(/ +/g, ' '));
|
|
|
|
expected = expected.replace(/ +/g, ' ');
|
|
|
|
|
2021-09-08 16:48:16 +00:00
|
|
|
expect(out.join('')).to.equal(expected);
|
2019-12-04 18:04:57 +00:00
|
|
|
});
|
|
|
|
|
2020-12-07 13:58:23 +00:00
|
|
|
it('should successfully list env and service vars for a test device (-s flags)', async () => {
|
|
|
|
const serviceName = 'service1';
|
|
|
|
api.expectGetService({ serviceName });
|
|
|
|
api.expectGetApplication();
|
|
|
|
api.expectGetAppEnvVars();
|
|
|
|
api.expectGetAppServiceVars();
|
|
|
|
api.expectGetDevice({ fullUUID });
|
|
|
|
api.expectGetDeviceEnvVars();
|
|
|
|
api.expectGetDeviceServiceVars();
|
|
|
|
|
|
|
|
const uuid = shortUUID;
|
2021-09-08 16:48:16 +00:00
|
|
|
const result = await runCommand(`envs -d ${uuid} -s ${serviceName}`);
|
|
|
|
let { out } = result;
|
|
|
|
let expected =
|
2020-12-07 13:58:23 +00:00
|
|
|
stripIndent`
|
2021-12-19 20:50:03 +00:00
|
|
|
ID NAME VALUE FLEET DEVICE SERVICE
|
|
|
|
120110 svar1 svar1-value org/test * ${serviceName}
|
|
|
|
120120 svar3 svar3-value org/test ${uuid} ${serviceName}
|
|
|
|
120101 var1 var1-val org/test * *
|
|
|
|
120102 var2 22 org/test * *
|
|
|
|
120203 var3 var3-val org/test ${uuid} *
|
|
|
|
120204 var4 44 org/test ${uuid} *
|
2021-09-08 16:48:16 +00:00
|
|
|
` + '\n';
|
2021-12-19 20:50:03 +00:00
|
|
|
|
|
|
|
out = out.map((l) => l.replace(/ +/g, ' '));
|
|
|
|
expected = expected.replace(/ +/g, ' ');
|
|
|
|
|
2021-09-08 16:48:16 +00:00
|
|
|
expect(out.join('')).to.equal(expected);
|
2020-12-07 13:58:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully list env and service vars for a test device (-js flags)', async () => {
|
|
|
|
const serviceName = 'service1';
|
|
|
|
api.expectGetService({ serviceName });
|
|
|
|
api.expectGetApplication();
|
|
|
|
api.expectGetAppEnvVars();
|
|
|
|
api.expectGetAppServiceVars();
|
|
|
|
api.expectGetDevice({ fullUUID });
|
|
|
|
api.expectGetDeviceEnvVars();
|
|
|
|
api.expectGetDeviceServiceVars();
|
|
|
|
|
|
|
|
const { out, err } = await runCommand(
|
|
|
|
`envs -d ${shortUUID} -js ${serviceName}`,
|
|
|
|
);
|
2021-12-19 20:50:03 +00:00
|
|
|
const expected = `[
|
2021-12-20 01:12:13 +00:00
|
|
|
{ "id": 120101, "fleet": "org/test", "deviceUUID": "*", "name": "var1", "value": "var1-val", "serviceName": "*" },
|
|
|
|
{ "id": 120102, "fleet": "org/test", "deviceUUID": "*", "name": "var2", "value": "22", "serviceName": "*" },
|
|
|
|
{ "id": 120110, "fleet": "org/test", "deviceUUID": "*", "name": "svar1", "value": "svar1-value", "serviceName": "${serviceName}" },
|
|
|
|
{ "id": 120120, "fleet": "org/test", "deviceUUID": "${fullUUID}", "name": "svar3", "value": "svar3-value", "serviceName": "${serviceName}" },
|
|
|
|
{ "id": 120203, "fleet": "org/test", "deviceUUID": "${fullUUID}", "name": "var3", "value": "var3-val", "serviceName": "*" },
|
|
|
|
{ "id": 120204, "fleet": "org/test", "deviceUUID": "${fullUUID}", "name": "var4", "value": "44", "serviceName": "*" }
|
2021-09-08 16:48:16 +00:00
|
|
|
]`;
|
2021-12-19 20:50:03 +00:00
|
|
|
|
2021-09-08 16:48:16 +00:00
|
|
|
expect(JSON.parse(out.join(''))).to.deep.equal(JSON.parse(expected));
|
2020-12-07 13:58:23 +00:00
|
|
|
expect(err.join('')).to.equal('');
|
|
|
|
});
|
2019-12-05 17:09:02 +00:00
|
|
|
});
|