def test_parse_fail(self): # chronologist humor: '1752-09-05T12:00:00.000000-0000' also not # handled correctly by python for my locale. for v in [ '1752-9-5', '1752-09-05', ]: with self.assertRaises(ValueError): podman.datetime_parse(v)
def test_parse(self): expected = datetime.datetime.strptime( '2018-05-08T14:12:53.797795-0700', '%Y-%m-%dT%H:%M:%S.%f%z') for v in [ '2018-05-08T14:12:53.797795191-07:00', '2018-05-08T14:12:53.797795-07:00', '2018-05-08T14:12:53.797795-0700', '2018-05-08 14:12:53.797795191 -0700 MST' ]: actual = podman.datetime_parse(v) self.assertEqual(actual, expected) podman.datetime_parse(datetime.datetime.now().isoformat())
def list(self): """List containers.""" if self._args.all: ictnrs = self.client.containers.list() else: ictnrs = filter( lambda c: podman.FoldedString(c['status']) == 'running', self.client.containers.list()) # TODO: Verify sorting on dates and size ctnrs = sorted(ictnrs, key=operator.attrgetter(self._args.sort)) if not ctnrs: return rows = list() for ctnr in ctnrs: fields = dict(ctnr) fields.update({ 'command': ' '.join(ctnr.command), 'createdat': humanize.naturaldate(podman.datetime_parse(ctnr.createdat)), }) rows.append(fields) with Report(self.columns, heading=self._args.heading) as report: report.layout(rows, self.columns.keys(), truncate=self._args.truncate) for row in rows: report.row(**row)
def list(self): """List containers.""" # TODO: Verify sorting on dates and size ctnrs = sorted( self.client.containers.list(), key=operator.attrgetter(self._args.sort)) if not ctnrs: return rows = list() for ctnr in ctnrs: fields = dict(ctnr) fields.update({ 'command': ' '.join(ctnr.command), 'createdat': humanize.naturaldate(podman.datetime_parse(ctnr.createdat)), }) if self._args.truncate: fields.update({'image': ctnr.image[-30:]}) rows.append(fields) with Report(self.columns, heading=self._args.heading) as report: report.layout( rows, self.columns.keys(), truncate=self._args.truncate) for row in rows: report.row(**row)
def history(self): """Report image history.""" rows = list() for ident in self._args.image: for details in self.client.images.get(ident).history(): fields = dict(details._asdict()) if self._args.human: fields.update({ 'size': humanize.naturalsize(details.size), 'created': humanize.naturaldate( podman.datetime_parse(details.created)), }) del fields['tags'] rows.append(fields) if self._args.quiet: for row in rows: ident = row['id'][:12] if self._args.truncate else row['id'] print(ident) elif self._args.format == 'json': print(json.dumps(rows, indent=2), flush=True) else: with Report(self.columns, heading=self._args.heading) as report: report.layout(rows, self.columns.keys(), truncate=self._args.truncate) for row in rows: report.row(**row)
def test_build(self): path = os.path.join(self.tmpdir, 'ctnr', 'Dockerfile') img, logs = self.pclient.images.build( dockerfile=[path], tags=['alpine-unittest'], ) self.assertIsNotNone(img) self.assertIn('localhost/alpine-unittest:latest', img.repoTags) self.assertLess(podman.datetime_parse(img.created), datetime.now(timezone.utc)) self.assertTrue(logs)
def test_build(self): path = os.path.join(self.tmpdir, 'ctnr', 'Dockerfile') img, logs = self.pclient.images.build( dockerfile=[path], tags=['alpine-unittest'], ) self.assertIsNotNone(img) self.assertIn('localhost/alpine-unittest:latest', img.repoTags) self.assertLess( podman.datetime_parse(img.created), datetime.now(timezone.utc)) self.assertTrue(logs)
def test_parse(self): expected = datetime.datetime.strptime( '2018-05-08T14:12:53.797795-0700', '%Y-%m-%dT%H:%M:%S.%f%z') for v in [ '2018-05-08T14:12:53.797795191-07:00', '2018-05-08T14:12:53.797795-07:00', '2018-05-08T14:12:53.797795-0700', '2018-05-08 14:12:53.797795191 -0700 MST', ]: actual = podman.datetime_parse(v) self.assertEqual(actual, expected) expected = datetime.datetime.strptime( '2018-05-08T14:12:53.797795-0000', '%Y-%m-%dT%H:%M:%S.%f%z') for v in [ '2018-05-08T14:12:53.797795191Z', '2018-05-08T14:12:53.797795191z', ]: actual = podman.datetime_parse(v) self.assertEqual(actual, expected) actual = podman.datetime_parse(datetime.datetime.now().isoformat()) self.assertIsNotNone(actual)
def test_build(self): path = os.path.join(self.tmpdir, "ctnr", "Dockerfile") os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, "w") as stream: stream.write("FROM alpine") builder = self.pclient.images.build(containerfiles=[path], tags=["alpine-unittest"]) self.assertIsNotNone(builder) *_, last_element = builder() # drain the builder generator # Each element from builder is a tuple (line, image) img = last_element[1] self.assertIsNotNone(img) self.assertIn("localhost/alpine-unittest:latest", img.repoTags) self.assertLess(podman.datetime_parse(img.created), datetime.now(timezone.utc))
def list(self): """List images.""" images = sorted( self.client.images.list(), key=operator.attrgetter(self._args.sort)) if not images: return rows = list() for image in images: fields = dict(image) fields.update({ 'created': humanize.naturaldate(podman.datetime_parse(image.created)), 'size': humanize.naturalsize(int(image.size)), 'repoDigests': ' '.join(image.repoDigests), }) for r in image.repoTags: name, tag = r.split(':', 1) fields.update({ 'name': name, 'tag': tag, }) rows.append(fields) if not self._args.digests: del self.columns['repoDigests'] with Report(self.columns, heading=self._args.heading) as report: report.layout( rows, self.columns.keys(), truncate=self._args.truncate) for row in rows: report.row(**row)
def list(self): """List images.""" images = sorted(self.client.images.list(), key=operator.attrgetter(self._args.sort)) if not images: return rows = list() for image in images: fields = dict(image) fields.update({ 'created': humanize.naturaldate(podman.datetime_parse(image.created)), 'size': humanize.naturalsize(int(image.size)), 'repoDigests': ' '.join(image.repoDigests), }) for r in image.repoTags: name, tag = r.split(':', 1) fields.update({ 'name': name, 'tag': tag, }) rows.append(fields) if not self._args.digests: del self.columns['repoDigests'] with Report(self.columns, heading=self._args.heading) as report: report.layout(rows, self.columns.keys(), truncate=self._args.truncate) for row in rows: report.row(**row)
#!/usr/bin/env python3 """Example: Show all containers created since midnight.""" from datetime import datetime, time, timezone import podman print('{}\n'.format(__doc__)) midnight = datetime.combine(datetime.today(), time.min, tzinfo=timezone.utc) with podman.Client() as client: for c in client.containers.list(): created_at = podman.datetime_parse(c.createdat) if created_at > midnight: print('{}: image: {} createdAt: {}'.format( c.id[:12], c.image[:32], podman.datetime_format(created_at)))
def test_format(self): expected = '2018-05-08T18:24:52.753227-07:00' dt = podman.datetime_parse(expected) actual = podman.datetime_format(dt) self.assertEqual(actual, expected)
def test_parse_fail(self): for v in [ 'There is no time here.', ]: with self.assertRaises(ValueError): podman.datetime_parse(v)
def test_inspect(self): actual = self.alpine_ctnr.inspect() self.assertEqual(actual.id, self.alpine_ctnr.id) self.assertEqual(datetime_parse(actual.created), datetime_parse(self.alpine_ctnr.createdat))