feat(systemd-manager): Support sudo password and root detection in SystemdManager; add user/group support in services and templates; add tests and expand README
This commit is contained in:
85
test/test.smartdaemon.ts
Normal file
85
test/test.smartdaemon.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||||
import * as smartdaemon from '../ts/index.js';
|
||||
|
||||
let testSmartdaemon: smartdaemon.SmartDaemon;
|
||||
|
||||
tap.test('should create an instance of smartdaemon', async () => {
|
||||
testSmartdaemon = new smartdaemon.SmartDaemon();
|
||||
expect(testSmartdaemon).toBeInstanceOf(smartdaemon.SmartDaemon);
|
||||
});
|
||||
|
||||
tap.test('should detect root status correctly', async () => {
|
||||
const isRoot = await testSmartdaemon.systemdManager['checkIsRoot']();
|
||||
console.log(`Running as root: ${isRoot}`);
|
||||
expect(isRoot).toBeTypeofBoolean();
|
||||
});
|
||||
|
||||
tap.test('should create service with user/group properties', async () => {
|
||||
const testService = await smartdaemon.SmartDaemonService.createFromOptions(
|
||||
testSmartdaemon,
|
||||
{
|
||||
name: 'test-service',
|
||||
description: 'A test service',
|
||||
command: 'node test.js',
|
||||
workingDir: '/tmp',
|
||||
version: '1.0.0',
|
||||
user: 'www-data',
|
||||
group: 'www-data'
|
||||
}
|
||||
);
|
||||
|
||||
expect(testService.user).toEqual('www-data');
|
||||
expect(testService.group).toEqual('www-data');
|
||||
});
|
||||
|
||||
tap.test('should generate systemd unit file with User/Group directives', async () => {
|
||||
const testService = await smartdaemon.SmartDaemonService.createFromOptions(
|
||||
testSmartdaemon,
|
||||
{
|
||||
name: 'test-service',
|
||||
description: 'A test service',
|
||||
command: 'node test.js',
|
||||
workingDir: '/tmp',
|
||||
version: '1.0.0',
|
||||
user: 'www-data',
|
||||
group: 'www-data'
|
||||
}
|
||||
);
|
||||
|
||||
const unitFileContent = testSmartdaemon.templateManager.generateUnitFileForService(testService);
|
||||
console.log('Generated unit file:');
|
||||
console.log(unitFileContent);
|
||||
|
||||
expect(unitFileContent).toInclude('User=www-data');
|
||||
expect(unitFileContent).toInclude('Group=www-data');
|
||||
expect(unitFileContent).toInclude('# user: www-data');
|
||||
expect(unitFileContent).toInclude('# group: www-data');
|
||||
});
|
||||
|
||||
tap.test('should handle services without user/group', async () => {
|
||||
const testService = await smartdaemon.SmartDaemonService.createFromOptions(
|
||||
testSmartdaemon,
|
||||
{
|
||||
name: 'test-service-no-user',
|
||||
description: 'A test service without user',
|
||||
command: 'node test.js',
|
||||
workingDir: '/tmp',
|
||||
version: '1.0.0'
|
||||
}
|
||||
);
|
||||
|
||||
const unitFileContent = testSmartdaemon.templateManager.generateUnitFileForService(testService);
|
||||
|
||||
expect(unitFileContent).not.toInclude('User=');
|
||||
expect(unitFileContent).not.toInclude('Group=');
|
||||
});
|
||||
|
||||
tap.test('should create smartdaemon with sudo password option', async () => {
|
||||
const daemonWithPassword = new smartdaemon.SmartDaemon({
|
||||
sudoPassword: 'test-password'
|
||||
});
|
||||
|
||||
expect(daemonWithPassword.systemdManager.sudoPassword).toEqual('test-password');
|
||||
});
|
||||
|
||||
export default tap.start();
|
Reference in New Issue
Block a user