Skip to content

Commit

Permalink
Mccalluc/continue network (#8)
Browse files Browse the repository at this point in the history
* Call supermethod, so we can support optionflags same as always

* Make assertions only against docker or kompatible

* Checkpoint: tests for kompatible pass

* Clarify all the differences bewteen docker and kompatible. Tests pass locally on python3

* Hash key order veries, so break out the test
  • Loading branch information
mccalluc authored Oct 15, 2018
1 parent aeb2e9d commit 1ae99f3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
43 changes: 31 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ and the following examples will work, although in the first case
it's Docker containers starting up,
and in the latter it's Kubernetes pods.

(These examples ignore differences in string handling between Python 2 and 3,
but differences between `docker` and `kompatible` are highlighted.)

## "Hello World!": Run, list, remove

```
Expand Down Expand Up @@ -49,12 +52,24 @@ and in the latter it's Kubernetes pods.
## Container properties

```
>>> assert c.id
>>> assert c.image
>>> 'ID: ' + c.id
'ID: ...' # docker
'ID: ...-...-...-...' # kompatible
>>> c.image
<Image: 'alpine:...'> # docker
'alpine' # kompatible
>>> c.labels
{'foo': 'bar'}
>>> assert c.short_id
>>> assert c.status
>>> 'ID: ' + c.short_id
'ID: ...' # docker
'ID: ...-...' # kompatible
>>> c.status
'exited' # docker
{...} # kompatible
```

Expand All @@ -68,16 +83,20 @@ and in the latter it's Kubernetes pods.
... ports={'80/tcp': None},
... detach=True
... )
#>>> container_from_run.attrs['NetworkSettings']['Ports']
# not TODO?: docker: {}; kompatible: initialized
>>> container_from_run.attrs['NetworkSettings']['Ports']
{} # docker
{'80/tcp': [{...}]} # kompatible
>>> container_from_get = client.containers.get('nginx')
>>> port_info = container_from_get.attrs['NetworkSettings']['Ports']['80/tcp']
# TODO: docker: random port; kompatible: None
>>> assert 'HostIp' in port_info[0]
>>> assert 'HostPort' in port_info[0]
>>> attrs = container_from_get.attrs['NetworkSettings']['Ports']['80/tcp']
>>> attrs
[{...}]
>>> repr(attrs[0]['HostIp'])
"'0.0.0.0'" # docker
'None' # kompatible
>>> repr(attrs[0]['HostPort'])
"'...'" # docker
'None' # kompatible
>>> container_from_get.remove(force=True, v=True)
Expand Down
38 changes: 31 additions & 7 deletions doctest_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,63 @@
import doctest
from re import sub

IGNORE_DOCKER = doctest.register_optionflag('IGNORE_DOCKER')
IGNORE_KOMPATIBLE = doctest.register_optionflag('IGNORE_KOMPATIBLE')


class UnfussyOutputChecker(doctest.OutputChecker):
def check_output(self, want, got, optionflags):
if optionflags:
raise Exception('options are not supported')
# Ignore differences in strings between 2 and 3:
got = sub(r"\bu'", "'", got) # python 2
got = sub(r"\bb'", "'", got) # python 3
return want == got

# Allow different assertions for docker and kompatible:
if optionflags & IGNORE_KOMPATIBLE:
want = sub(r'.+# kompatible\s+', '', want)
if optionflags & IGNORE_DOCKER:
want = sub(r'.+# docker\s+', '', want)
want = sub(r'\s+# (docker|kompatible)', '', want)

# Handle any other optionflags
return doctest.OutputChecker.check_output(self, want, got, optionflags)


def get_sdk():
def get_args():
parser = argparse.ArgumentParser(description='Run doc tests')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--docker', action='store_true')
group.add_argument('--kompatible', action='store_true')
return parser.parse_args()

args = parser.parse_args()

def get_sdk(args):
if args.docker:
import docker as sdk
else:
import kompatible as sdk
return sdk


def get_options(args):
if args.docker:
return IGNORE_KOMPATIBLE
else:
return IGNORE_DOCKER


def main():
filename = 'README.md'
with open(filename) as f:
readme = f.read()

args = get_args()
examples = doctest.DocTestParser().get_doctest(
string=readme, globs={'sdk': get_sdk()},
string=readme, globs={'sdk': get_sdk(args)},
name=filename, filename=None, lineno=0)

runner = doctest.DocTestRunner(checker=UnfussyOutputChecker())
runner = doctest.DocTestRunner(
checker=UnfussyOutputChecker(),
optionflags=get_options(args) | doctest.ELLIPSIS)
runner.run(examples)
(failed, attempted) = runner.summarize()
if failed > 0:
Expand Down

0 comments on commit 1ae99f3

Please sign in to comment.