Skip to content

Commit

Permalink
Merge pull request #3511 from balena-os/klutchell-patch-1
Browse files Browse the repository at this point in the history
tests:os: Use writeConfigJsonProp helper function
  • Loading branch information
flowzone-app[bot] authored Sep 17, 2024
2 parents 466cedd + b44642c commit 81bd280
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 95 deletions.
83 changes: 76 additions & 7 deletions tests/suites/cloud/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,76 @@ module.exports = {
120,
250,
);
}
},
/**
* Write or remove a property from config.json in the boot partition
* @param {string} test Current test instance to append results
* @param {string} key Object key to update, dot separated
* @param {string} value New value, can be string, or object, or null|undefined to remove
* @param {string} target The address of the target device
*
* @category helper
*/
writeConfigJsonProp: async function (test, key, value, target) {

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

return t.resolves(
this.worker.executeCommandInHostOS(
[
`tmp=$(mktemp)`,
`&&`, `jq`, `'.${key}=${value}'`, `/mnt/boot/config.json`,
`>`, `$tmp`, `&&`, `mv`, `"$tmp"`, `/mnt/boot/config.json`
].join(' '),
target
), `Should write ${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(
this.worker.executeCommandInHostOS(
'systemctl reset-failed config-json.service',
target
), `Should reset start counter of config-json.service`
);
}).then(() => {
return t.resolves(
this.waitForServiceState(
'config-json.service',
'inactive',
target
),
'Should wait for config-json.service to be inactive'
)
})
);
}
});

// Network definitions - these are given to the testbot via the config sent via the config.js
Expand All @@ -176,7 +245,7 @@ module.exports = {
this.suite.options.balenaOS.network.wired = {
nat: true,
};
}
}
else {
// device has wifi, use wifi hotspot to connect to DUT
delete this.suite.options.balenaOS.network.wired;
Expand All @@ -196,7 +265,7 @@ module.exports = {
psk: `${this.suite.options.id}_psk`,
nat: true,
};
}
}
else {
// no wifi on DUT
delete this.suite.options.balenaOS.network.wireless;
Expand Down Expand Up @@ -312,7 +381,7 @@ module.exports = {
this.balena.application,
this.balena.uuid
);

// Add registered device's id and api key to config.json
config.deviceApiKey = deviceRegInfo.api_key;
config.deviceId = deviceRegInfo.id;
Expand Down Expand Up @@ -390,7 +459,7 @@ module.exports = {
//re - enable port forwarding in case something flaked between us disabling it and re-enabling it
await this.worker.executeCommandInWorker('sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"');
});

await this.worker.on();

// create tunnels
Expand All @@ -413,11 +482,11 @@ module.exports = {
`Device ${this.link} be reachable over local SSH connection`
)

await test.resolves(
await test.resolves(
this.waitForServiceState('balena', 'active', this.link),
'balena Engine should be running and healthy'
)

// we want to waitUntil here as the supervisor may take some time to come online.
await test.resolves(
this.utils.waitUntil(async () => {
Expand Down
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.writeConfigJsonProp(test, 'developmentMode', false, this.balena.uuid)
.then(() => {
return setConfig(test, this, this.balena.uuid, 'os.sshKeys');
return this.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.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.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.writeConfigJsonProp(test, 'developmentMode', true, this.balena.uuid)
.then(() => {
return setConfig(test, this, this.balena.uuid, 'os.sshKeys');
return this.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.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.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.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 81bd280

Please sign in to comment.