Skip to content

Commit

Permalink
tests:os: Use writeConfigJsonProp helper function
Browse files Browse the repository at this point in the history
Use the existing helper function to write changes to config.json.

Change-type: patch
  • Loading branch information
klutchell committed Sep 16, 2024
1 parent 743309f commit 8fb56a3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 88 deletions.
79 changes: 9 additions & 70 deletions tests/suites/cloud/tests/ssh-auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,76 +21,15 @@ const { join, dirname } = require("path");
const { homedir } = require("os");
const fse = require("fs-extra");

const setConfig = async (test, that, target, key, value) => {

return test.test(`Update or delete ${key} in config.json`, t =>
t.resolves(
that.waitForServiceState(
'config-json.service',
'inactive',
target
),
'Should wait for config-json.service to be inactive'
).then(() => {
if (value == null) {
return t.resolves(
that.cloud.executeCommandInHostOS(
[
`tmp=$(mktemp)`,
`&&`, `jq`, `"del(.${key})"`, `/mnt/boot/config.json`,
`>`, `$tmp`, `&&`, `mv`, `"$tmp"`, `/mnt/boot/config.json`
].join(' '),
target
), `Should delete ${key} from config.json`
)
} else {
if (typeof(value) == 'string') {
value = `"${value}"`
} else {
value = JSON.stringify(value);
}

return t.resolves(
that.cloud.executeCommandInHostOS(
[
`tmp=$(mktemp)`,
`&&`, `jq`, `'.${key}=${value}'`, `/mnt/boot/config.json`,
`>`, `$tmp`, `&&`, `mv`, `"$tmp"`, `/mnt/boot/config.json`
].join(' '),
target
), `Should set ${key} to ${value.substring(24) ? value.replace(value.substring(24), '...') : value} in config.json`
)
}
}).then(() => {
// avoid hitting 'start request repeated too quickly'
return t.resolves(
that.cloud.executeCommandInHostOS(
'systemctl reset-failed config-json.service',
target
), `Should reset start counter of config-json.service`
);
}).then(() => {
return t.resolves(
that.waitForServiceState(
'config-json.service',
'inactive',
target
),
'Should wait for config-json.service to be inactive'
)
})
);
}

module.exports = {
title: 'SSH authentication test',
tests: [
{
title: 'SSH authentication in production mode',
run: async function(test) {
return setConfig(test, this, this.balena.uuid, 'developmentMode', false)
return this.systemd.writeConfigJsonProp(test, 'developmentMode', false, this.balena.uuid)
.then(() => {
return setConfig(test, this, this.balena.uuid, 'os.sshKeys');
return this.systemd.writeConfigJsonProp(test, 'os.sshKeys', null, this.balena.uuid)
}).then(() => {
return test.resolves(
this.waitForServiceState(
Expand Down Expand Up @@ -122,7 +61,7 @@ module.exports = {
);
});
}).then(async () => {
return setConfig(test, this, this.balena.uuid, 'os.sshKeys', [this.context.get().sshKey.pubKey.trim()]);
return this.systemd.writeConfigJsonProp(test, 'os.sshKeys', [this.context.get().sshKey.pubKey.trim()], this.balena.uuid);
}).then(async () => {
let result;
await this.utils.waitUntil(
Expand All @@ -137,7 +76,7 @@ module.exports = {
"Local SSH authentication with custom keys is allowed in production mode"
);
}).then(async () => {
await setConfig(test, this, this.balena.uuid, 'os.sshKeys');
await this.systemd.writeConfigJsonProp(test, 'os.sshKeys', null, this.balena.uuid);
}).then(async () => {
let result;
let config = {};
Expand Down Expand Up @@ -177,9 +116,9 @@ module.exports = {
const customKey = await keygen({
location: customSshPath,
});
return setConfig(test, this, this.balena.uuid, 'developmentMode', true)
return this.systemd.writeConfigJsonProp(test, 'developmentMode', true, this.balena.uuid)
.then(() => {
return setConfig(test, this, this.balena.uuid, 'os.sshKeys');
return this.systemd.writeConfigJsonProp(test, 'os.sshKeys', null, this.balena.uuid);
}).then(() => {
return test.resolves(
this.waitForServiceState(
Expand All @@ -203,7 +142,7 @@ module.exports = {
"Local SSH authentication without custom keys is allowed in development mode"
)
}).then(() => {
return setConfig(test, this, this.balena.uuid, 'os.sshKeys', [customKey.pubKey.trim()]);
return this.systemd.writeConfigJsonProp(test, 'os.sshKeys', [customKey.pubKey.trim()], this.balena.uuid);
}).then(() => {
return test.resolves(
this.waitForServiceState(
Expand All @@ -228,7 +167,7 @@ module.exports = {
this.worker.addSSHKey(this.context.get().sshKeyPath);
})
.then(() => {
return setConfig(test, this, this.balena.uuid, 'os.sshKeys', [this.context.get().sshKey.pubKey.trim()]);
return this.systemd.writeConfigJsonProp(test, 'os.sshKeys', [this.context.get().sshKey.pubKey.trim()], this.balena.uuid);
});
}).then(async () => {
let result;
Expand All @@ -244,7 +183,7 @@ module.exports = {
"Local SSH authentication with custom keys is allowed in development mode"
)
}).then(async () => {
return setConfig(test, this, this.balena.uuid, 'os.sshKeys');
return this.systemd.writeConfigJsonProp(test, 'os.sshKeys', null, this.balena.uuid);
}).then(async () => {
let result;
let config = {};
Expand Down
21 changes: 3 additions & 18 deletions tests/suites/os/tests/engine-socket/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ module.exports = {
let ip = await this.worker.ip(this.link)
const docker = new Docker({host: `http://${ip}`, port: 2375})
test.comment(`Setting system in development mode...`)
await this.context
.get()
.worker.executeCommandInHostOS(
`tmp=$(mktemp)&&cat /mnt/boot/config.json | jq '.developmentMode="true"' > $tmp&&mv "$tmp" /mnt/boot/config.json`,
this.link,
);
await this.systemd.writeConfigJsonProp(test, 'developmentMode', true, this.link);
test.comment(`Waiting for engine to restart...`)
await this.utils.waitUntil(async () => {
return (
Expand Down Expand Up @@ -69,12 +64,7 @@ module.exports = {
let ip = await this.worker.ip(this.link)
const docker = new Docker({host: `http://${ip}`, port: 2375})
test.comment(`Setting system in production mode...`)
await this.context
.get()
.worker.executeCommandInHostOS(
`tmp=$(mktemp)&&cat /mnt/boot/config.json | jq '.developmentMode="false"' > $tmp&&mv "$tmp" /mnt/boot/config.json`,
this.link,
);
await this.systemd.writeConfigJsonProp(test, 'developmentMode', false, this.link);
test.comment(`Waiting for engine to restart...`)
await this.utils.waitUntil(async () => {
return (
Expand All @@ -97,12 +87,7 @@ module.exports = {
"Engine socket should not be exposed in production images"
);
test.comment(`Leaving system in development mode...`)
await this.context
.get()
.worker.executeCommandInHostOS(
`tmp=$(mktemp)&&cat /mnt/boot/config.json | jq '.developmentMode="true"' > $tmp&&mv "$tmp" /mnt/boot/config.json`,
this.link,
);
await this.systemd.writeConfigJsonProp(test, 'developmentMode', true, this.link);
},
},
],
Expand Down

0 comments on commit 8fb56a3

Please sign in to comment.