import 'package:podman/podman.dart'; import 'package:test/test.dart'; import 'support/fake_podman_transport.dart'; void main() { group('PodmanClient network API', () { test('checks whether a network exists', () async { final transport = FakePodmanTransport() ..enqueue( method: HttpMethod.get, path: '/v5.0.0/libpod/networks/groupware/exists', statusCode: 204, ) ..enqueue( method: HttpMethod.get, path: '/v5.0.0/libpod/networks/missing/exists', statusCode: 404, ); final client = PodmanClient(transport: transport); expect(await client.networkExists('groupware'), isTrue); expect(await client.networkExists('missing'), isFalse); transport.expectNoPending(); }); test('lists networks', () async { final transport = FakePodmanTransport() ..enqueue( method: HttpMethod.get, path: '/v5.0.0/libpod/networks/json', responseBody: [ { 'id': 'net-1', 'name': 'groupware', 'driver': 'bridge', 'internal': false, 'dns_enabled': true, 'subnets': [ { 'subnet': '10.40.0.0/24', 'gateway': '10.40.0.1', }, ], 'labels': {'stack': 'groupware'}, }, ], ); final client = PodmanClient(transport: transport); final networks = await client.listNetworks(); expect(networks, hasLength(1)); expect(networks.first.id, 'net-1'); expect(networks.first.name, 'groupware'); expect(networks.first.driver, 'bridge'); expect(networks.first.labels['stack'], 'groupware'); expect(networks.first.subnets.first.subnet, '10.40.0.0/24'); transport.expectNoPending(); }); test('supports create, inspect, connect, disconnect, and remove', () async { final transport = FakePodmanTransport() ..enqueue( method: HttpMethod.post, path: '/v5.0.0/libpod/networks/create', body: const { 'name': 'groupware', 'driver': 'bridge', 'internal': false, 'dns_enabled': true, 'labels': {'stack': 'groupware'}, 'subnets': [ { 'subnet': '10.40.0.0/24', 'gateway': '10.40.0.1', }, ], }, statusCode: 200, responseBody: { 'id': 'net-1', 'name': 'groupware', 'driver': 'bridge', 'internal': false, 'dns_enabled': true, 'labels': {'stack': 'groupware'}, 'subnets': [ { 'subnet': '10.40.0.0/24', 'gateway': '10.40.0.1', }, ], }, ) ..enqueue( method: HttpMethod.get, path: '/v5.0.0/libpod/networks/groupware/json', responseBody: { 'id': 'net-1', 'name': 'groupware', 'driver': 'bridge', 'internal': false, 'dns_enabled': true, }, ) ..enqueue( method: HttpMethod.post, path: '/v5.0.0/libpod/networks/groupware/connect', body: const { 'Container': 'orchestrator', 'Aliases': ['orch'], }, statusCode: 200, ) ..enqueue( method: HttpMethod.post, path: '/v5.0.0/libpod/networks/groupware/disconnect', body: const { 'Container': 'orchestrator', 'Force': true, }, statusCode: 200, ) ..enqueue( method: HttpMethod.delete, path: '/v5.0.0/libpod/networks/groupware', statusCode: 200, ); final client = PodmanClient(transport: transport); final created = await client.createNetwork( const NetworkCreateOptions( name: 'groupware', internal: false, dnsEnabled: true, labels: {'stack': 'groupware'}, subnet: '10.40.0.0/24', gateway: '10.40.0.1', ), ); expect(created.id, 'net-1'); final inspected = await client.inspectNetwork('groupware'); expect(inspected.name, 'groupware'); await client.connectNetwork( 'groupware', const NetworkConnectOptions( container: 'orchestrator', aliases: ['orch'], ), ); await client.disconnectNetwork( 'groupware', container: 'orchestrator', force: true, ); await client.removeNetwork('groupware'); transport.expectNoPending(); }); test('supports network update and prune endpoints', () async { final transport = FakePodmanTransport() ..enqueue( method: HttpMethod.post, path: '/v5.0.0/libpod/networks/groupware/update', body: const { 'adddnsservers': ['1.1.1.1'], 'removednsservers': ['9.9.9.9'], }, statusCode: 204, ) ..enqueue( method: HttpMethod.post, path: '/v5.0.0/libpod/networks/prune', queryParameters: const >{ 'filters': ['{"until":["24h"]}'], }, responseBody: const [ {'Name': 'groupware-old'}, { 'Name': 'groupware-in-use', 'Error': 'network is in use', }, ], ); final client = PodmanClient(transport: transport); await client.updateNetwork( 'groupware', const NetworkUpdateOptions( addDnsServers: ['1.1.1.1'], removeDnsServers: ['9.9.9.9'], ), ); final reports = await client.pruneNetworks( options: const NetworkPruneOptions( filters: >{ 'until': ['24h'], }, ), ); expect(reports, hasLength(2)); expect(reports.first.name, 'groupware-old'); expect(reports.last.error, contains('in use')); transport.expectNoPending(); }); }); }