test_cli.py 1.45 KB
Newer Older
David Trudgian's avatar
David Trudgian committed
1
import pytest
David Trudgian's avatar
David Trudgian committed
2
import json
3
import socket
David Trudgian's avatar
David Trudgian committed
4
from click.testing import CliRunner
David Trudgian's avatar
David Trudgian committed
5
from clair_singularity.cli import cli
David Trudgian's avatar
David Trudgian committed
6

David Trudgian's avatar
David Trudgian committed
7
from .test_image import testimage
David Trudgian's avatar
David Trudgian committed
8

David Trudgian's avatar
David Trudgian committed
9

10
11
12
MY_IP = socket.gethostbyname(socket.gethostname())


David Trudgian's avatar
David Trudgian committed
13
14
15
16
@pytest.fixture
def runner():
    return CliRunner()

17

18
def test_help(runner):
David Trudgian's avatar
David Trudgian committed
19
20
21
    result = runner.invoke(cli, ['--help'])
    assert 'Usage:' in result.output

David Trudgian's avatar
David Trudgian committed
22

David Trudgian's avatar
David Trudgian committed
23
def test_full_json(runner, testimage):
David Trudgian's avatar
David Trudgian committed
24
    result = runner.invoke(cli,
25
                           ['--quiet', '--json-output', '--bind-ip', MY_IP, '--bind-port', '8081', '--clair-uri',
David Trudgian's avatar
David Trudgian committed
26
                            'http://clair:6060', testimage])
David Trudgian's avatar
David Trudgian committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    output = json.loads(result.output)

    # Using the shub://396 image and the 2017-08-21 clair db...
    # There are 62 features in the container scan, and 14 have vulnerabilities
    assert 'Layer' in output
    assert 'Features' in output['Layer']
    assert len(output['Layer']['Features']) == 62
    features_with_vuln = 0
    for feature in output['Layer']['Features']:
        if 'Vulnerabilities' in feature:
            features_with_vuln = features_with_vuln + 1
    assert features_with_vuln == 14


def test_full_text(runner, testimage):
42
    result = runner.invoke(cli, ['--quiet', '--bind-ip', MY_IP, '--bind-port', '8082', '--clair-uri',
David Trudgian's avatar
David Trudgian committed
43
                                 'http://clair:6060', testimage])
David Trudgian's avatar
David Trudgian committed
44
45
    # Check we do have some CVEs we expect reported here
    assert 'bash - 4.3-14ubuntu1.1' in result.output
David Trudgian's avatar
David Trudgian committed
46
    assert 'CVE-2016-9401' in result.output