summaryrefslogtreecommitdiff
path: root/tests/unit/errors_test.py
blob: 2134f86f04a0aa7068e13fdfaf46396597754a07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import unittest

import requests

from docker.errors import (APIError, ContainerError, DockerException,
                           create_unexpected_kwargs_error,
                           create_api_error_from_http_exception)
from .fake_api import FAKE_CONTAINER_ID, FAKE_IMAGE_ID
from .fake_api_client import make_fake_client


class APIErrorTest(unittest.TestCase):
    def test_api_error_is_caught_by_dockerexception(self):
        try:
            raise APIError("this should be caught by DockerException")
        except DockerException:
            pass

    def test_status_code_200(self):
        """The status_code property is present with 200 response."""
        resp = requests.Response()
        resp.status_code = 200
        err = APIError('', response=resp)
        assert err.status_code == 200

    def test_status_code_400(self):
        """The status_code property is present with 400 response."""
        resp = requests.Response()
        resp.status_code = 400
        err = APIError('', response=resp)
        assert err.status_code == 400

    def test_status_code_500(self):
        """The status_code property is present with 500 response."""
        resp = requests.Response()
        resp.status_code = 500
        err = APIError('', response=resp)
        assert err.status_code == 500

    def test_is_server_error_200(self):
        """Report not server error on 200 response."""
        resp = requests.Response()
        resp.status_code = 200
        err = APIError('', response=resp)
        assert err.is_server_error() is False

    def test_is_server_error_300(self):
        """Report not server error on 300 response."""
        resp = requests.Response()
        resp.status_code = 300
        err = APIError('', response=resp)
        assert err.is_server_error() is False

    def test_is_server_error_400(self):
        """Report not server error on 400 response."""
        resp = requests.Response()
        resp.status_code = 400
        err = APIError('', response=resp)
        assert err.is_server_error() is False

    def test_is_server_error_500(self):
        """Report server error on 500 response."""
        resp = requests.Response()
        resp.status_code = 500
        err = APIError('', response=resp)
        assert err.is_server_error() is True

    def test_is_client_error_500(self):
        """Report not client error on 500 response."""
        resp = requests.Response()
        resp.status_code = 500
        err = APIError('', response=resp)
        assert err.is_client_error() is False

    def test_is_client_error_400(self):
        """Report client error on 400 response."""
        resp = requests.Response()
        resp.status_code = 400
        err = APIError('', response=resp)
        assert err.is_client_error() is True

    def test_is_error_300(self):
        """Report no error on 300 response."""
        resp = requests.Response()
        resp.status_code = 300
        err = APIError('', response=resp)
        assert err.is_error() is False

    def test_is_error_400(self):
        """Report error on 400 response."""
        resp = requests.Response()
        resp.status_code = 400
        err = APIError('', response=resp)
        assert err.is_error() is True

    def test_is_error_500(self):
        """Report error on 500 response."""
        resp = requests.Response()
        resp.status_code = 500
        err = APIError('', response=resp)
        assert err.is_error() is True

    def test_create_error_from_exception(self):
            resp = requests.Response()
            resp.status_code = 500
            err = APIError('')
            try:
                resp.raise_for_status()
            except requests.exceptions.HTTPError as e:
                try:
                    create_api_error_from_http_exception(e)
                except APIError as e:
                    err = e
            assert err.is_server_error() is True


class ContainerErrorTest(unittest.TestCase):
    def test_container_without_stderr(self):
        """The massage does not contain stderr"""
        client = make_fake_client()
        container = client.containers.get(FAKE_CONTAINER_ID)
        command = "echo Hello World"
        exit_status = 42
        image = FAKE_IMAGE_ID
        stderr = None

        err = ContainerError(container, exit_status, command, image, stderr)
        msg = ("Command '{}' in image '{}' returned non-zero exit status {}"
               ).format(command, image, exit_status, stderr)
        assert str(err) == msg

    def test_container_with_stderr(self):
        """The massage contains stderr"""
        client = make_fake_client()
        container = client.containers.get(FAKE_CONTAINER_ID)
        command = "echo Hello World"
        exit_status = 42
        image = FAKE_IMAGE_ID
        stderr = "Something went wrong"

        err = ContainerError(container, exit_status, command, image, stderr)
        msg = ("Command '{}' in image '{}' returned non-zero exit status {}: "
               "{}").format(command, image, exit_status, stderr)
        assert str(err) == msg


class CreateUnexpectedKwargsErrorTest(unittest.TestCase):
    def test_create_unexpected_kwargs_error_single(self):
        e = create_unexpected_kwargs_error('f', {'foo': 'bar'})
        assert str(e) == "f() got an unexpected keyword argument 'foo'"

    def test_create_unexpected_kwargs_error_multiple(self):
        e = create_unexpected_kwargs_error('f', {'foo': 'bar', 'baz': 'bosh'})
        assert str(e) == "f() got unexpected keyword arguments 'baz', 'foo'"