podman/test/podman_client_local_test.dart

78 lines
2.3 KiB
Dart

import 'dart:async';
import 'package:podman/podman.dart';
import 'package:test/test.dart';
import 'support/local_test_config.dart';
const String _helloWorldImage = 'docker.io/library/hello-world:latest';
void main() {
final skip = useLocalPodmanTests ? false : localTestsSkipReason;
group('PodmanClient local API', tags: const <String>['local'], () {
test('reads version and info from local podman', skip: skip, () async {
final client = createLocalTestClient();
addTearDown(client.close);
final version = await client.version();
final info = await client.info();
expect(version.serverVersion, isNotEmpty);
expect(info.hostOs, isNotEmpty);
});
test('pulls and runs hello-world', skip: skip, () async {
final client = createLocalTestClient();
String? containerId;
final containerName = _uniqueContainerName();
try {
if (!await client.imageExists(_helloWorldImage)) {
await client.pull(_helloWorldImage, quiet: true);
}
containerId = await client.run(
RunOptions(image: _helloWorldImage, name: containerName),
);
await _waitForContainerExit(client, containerId);
final logs = await client.logs(containerId, tail: 200);
expect(logs.toLowerCase(), contains('hello from'));
} finally {
final cleanupTarget = containerId ?? containerName;
await client.removeContainer(
cleanupTarget,
force: true,
removeVolumes: true,
ignoreMissing: true,
);
await client.close();
}
});
});
}
Future<void> _waitForContainerExit(
PodmanClient client,
String container, {
Duration timeout = const Duration(seconds: 20),
Duration pollInterval = const Duration(milliseconds: 250),
}) async {
final deadline = DateTime.now().add(timeout);
while (DateTime.now().isBefore(deadline)) {
final details = await client.inspectContainer(container);
final state = details.state.toLowerCase();
if (state == 'exited' || state == 'stopped') {
return;
}
await Future<void>.delayed(pollInterval);
}
fail('Timed out waiting for container `$container` to exit.');
}
String _uniqueContainerName() =>
'podman_local_hello_world_${DateTime.now().millisecondsSinceEpoch}';