feat(host): Add DockerHost version & image-prune APIs, extend network creation options, return exec inspect info, and improve image import/store and streaming

This commit is contained in:
2025-11-25 05:18:48 +00:00
parent 889b017d4f
commit b86601c939
10 changed files with 670 additions and 18 deletions

View File

@@ -318,6 +318,119 @@ tap.test('should complete container tests', async () => {
console.log('Container streaming tests completed');
});
// NEW FEATURES TESTS (v5.1.0)
// Test 1: Network creation with custom driver and IPAM
tap.test('should create bridge network with custom IPAM config', async () => {
const network = await testDockerHost.createNetwork({
Name: 'test-bridge-network',
Driver: 'bridge',
IPAM: {
Config: [{
Subnet: '172.20.0.0/16',
Gateway: '172.20.0.1',
}]
},
Labels: { testLabel: 'v5.1.0' },
});
expect(network).toBeInstanceOf(docker.DockerNetwork);
expect(network.Name).toEqual('test-bridge-network');
expect(network.Driver).toEqual('bridge');
console.log('Created bridge network:', network.Name, 'with driver:', network.Driver);
await network.remove();
});
// Test 2: getVersion() returns proper Docker daemon info
tap.test('should get Docker daemon version information', async () => {
const version = await testDockerHost.getVersion();
expect(version).toBeInstanceOf(Object);
expect(typeof version.Version).toEqual('string');
expect(typeof version.ApiVersion).toEqual('string');
expect(typeof version.Os).toEqual('string');
expect(typeof version.Arch).toEqual('string');
console.log('Docker version:', version.Version, 'API version:', version.ApiVersion);
});
// Test 3: pruneImages() functionality
tap.test('should prune dangling images', async () => {
const result = await testDockerHost.pruneImages({ dangling: true });
expect(result).toBeInstanceOf(Object);
expect(result).toHaveProperty('ImagesDeleted');
expect(result).toHaveProperty('SpaceReclaimed');
expect(Array.isArray(result.ImagesDeleted)).toEqual(true);
expect(typeof result.SpaceReclaimed).toEqual('number');
console.log('Pruned images. Space reclaimed:', result.SpaceReclaimed, 'bytes');
});
// Test 4: exec() inspect() returns exit codes
tap.test('should get exit code from exec command', async (tools) => {
const done = tools.defer();
// Execute a successful command (exit code 0)
const { stream, close, inspect } = await testContainer.exec('echo "test successful"', {
tty: false,
attachStdout: true,
attachStderr: true,
});
stream.on('end', async () => {
// Give Docker a moment to finalize the exec state
await tools.delayFor(500);
const info = await inspect();
expect(info).toBeInstanceOf(Object);
expect(typeof info.ExitCode).toEqual('number');
expect(info.ExitCode).toEqual(0); // Success
expect(typeof info.Running).toEqual('boolean');
expect(info.Running).toEqual(false); // Should be done
expect(typeof info.ContainerID).toEqual('string');
console.log('Exec inspect - ExitCode:', info.ExitCode, 'Running:', info.Running);
await close();
done.resolve();
});
stream.on('error', async (error) => {
console.error('Exec error:', error);
await close();
done.resolve();
});
await done.promise;
});
tap.test('should get non-zero exit code from failed exec command', async (tools) => {
const done = tools.defer();
// Execute a command that fails (exit code 1)
const { stream, close, inspect } = await testContainer.exec('exit 1', {
tty: false,
attachStdout: true,
attachStderr: true,
});
stream.on('end', async () => {
// Give Docker a moment to finalize the exec state
await tools.delayFor(500);
const info = await inspect();
expect(info.ExitCode).toEqual(1); // Failure
expect(info.Running).toEqual(false);
console.log('Exec inspect (failed command) - ExitCode:', info.ExitCode);
await close();
done.resolve();
});
stream.on('error', async (error) => {
console.error('Exec error:', error);
await close();
done.resolve();
});
await done.promise;
});
tap.test('cleanup', async () => {
await testDockerHost.stop();
});