tests.routers.test_config

tests/routers/test_config.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""
Tests for config management routes
"""

import filecmp
import os
import tarfile
from pathlib import Path
from shutil import rmtree

from fastapi.testclient import TestClient
from requests.models import Response

from serverctl_deployd.main import app
from tests.fakes.fake_config_directory import (MOCK_CONF_DIRPATH,
                                               MOCK_CONF_FILE_CONTENT,
                                               MOCK_CONF_FILEPATH,
                                               MOCK_CONF_NEW_CONTENT,
                                               MOCK_FILE_HASH,
                                               make_mock_config_dir)

client = TestClient(app)


def test_validate_bucket() -> None:
    """Test config bucket validation"""
    make_mock_config_dir()

    # Valid request
    response: Response = client.post(
        "/config/buckets/",
        json={
            "directory_path": MOCK_CONF_DIRPATH,
            "update_command": "echo updated",
            "ignore_patterns": ["leave*"]
        })
    assert response.status_code == 200
    assert response.json() == {
        "mock.conf": MOCK_FILE_HASH
    }

    # Unsuccessful or invalid command
    response = client.post(
        "/config/buckets/",
        json={
            "directory_path": MOCK_CONF_DIRPATH,
            "update_command": "invalid command",
            "ignore_patterns": ["leave*"]
        })
    assert response.status_code == 500
    assert response.json() == {
        "detail": "Internal server error"
    }

    rmtree(MOCK_CONF_DIRPATH)


def test_list_filenames() -> None:
    """Test for file name list route i.e /files"""
    make_mock_config_dir()

    # Valid request
    response: Response = client.post(
        "/config/buckets/files",
        json={
            "directory_path": MOCK_CONF_DIRPATH,
            "ignore_patterns": ["leave*"]
        })
    assert response.status_code == 200
    assert response.json() == ["mock.conf"]

    rmtree(MOCK_CONF_DIRPATH)


def test_get_hashes() -> None:
    """Test for the get hashes route i.e /check"""
    make_mock_config_dir()

    # Valid request
    response: Response = client.post(
        "/config/buckets/check",
        json={
            "directory_path": MOCK_CONF_DIRPATH,
            "ignore_patterns": ["leave*"]
        })
    assert response.status_code == 200
    assert response.json() == {
        "mock.conf": MOCK_FILE_HASH
    }

    rmtree(MOCK_CONF_DIRPATH)


def test_get_file() -> None:
    """Test for getting config file"""
    make_mock_config_dir()

    # Valid request
    response: Response = client.get(
        "/config/buckets/file",
        params={"file_path": MOCK_CONF_FILEPATH}
    )
    assert response.status_code == 200
    assert response.content.decode() == MOCK_CONF_FILE_CONTENT

    rmtree(MOCK_CONF_DIRPATH)


def test_update_file() -> None:
    """Test for updating config file"""
    make_mock_config_dir()

    with open(
        MOCK_CONF_DIRPATH + "new_file.conf",
        'w', encoding="utf8"
    ) as new_file:
        new_file.write(MOCK_CONF_NEW_CONTENT)

    with open(
        MOCK_CONF_DIRPATH + "new_file.conf",
        'r', encoding="utf8"
    ) as new_file:
        # Valid request
        response: Response = client.put(
            "/config/buckets/file",
            params={"file_path": MOCK_CONF_FILEPATH},
            data={"update_command": "echo updated"},
            files={"new_file": new_file},
        )
        assert response.status_code == 200
        assert response.content.decode() == MOCK_CONF_NEW_CONTENT

        # Unsuccessful or invalid command
        response = client.put(
            "/config/buckets/file",
            params={"file_path": MOCK_CONF_FILEPATH},
            data={"update_command": "invalid command"},
            files={"new_file": new_file},
        )
        assert response.status_code == 500
        assert response.json() == {
            "detail": "Internal server error"
        }

    rmtree(MOCK_CONF_DIRPATH)


def test_delete_file() -> None:
    """Test for deleting config file"""
    make_mock_config_dir()

    with open(
        MOCK_CONF_DIRPATH + "delete_this.conf",
        'w', encoding="utf8"
    ) as new_file:
        new_file.write(MOCK_CONF_FILE_CONTENT)

    # Valid request
    response: Response = client.delete(
        "/config/buckets/file",
        params={
            "file_path": MOCK_CONF_DIRPATH + "delete_this.conf"
        },
        json={"update_command": "echo updated"}
    )
    assert response.status_code == 204
    assert not Path(MOCK_CONF_DIRPATH + "delete_this.conf").is_file()

    with open(
        MOCK_CONF_DIRPATH + "delete_this.conf",
        'w', encoding="utf8"
    ) as new_file:
        new_file.write(MOCK_CONF_FILE_CONTENT)

    # Unsuccessful or invalid command
    response = client.delete(
        "/config/buckets/file",
        params={
            "file_path": MOCK_CONF_DIRPATH + "delete_this.conf"
        },
        json={"update_command": "invalid command"}
    )
    assert response.status_code == 500
    assert response.json() == {
        "detail": "Internal server error"
    }

    rmtree(MOCK_CONF_DIRPATH)


def test_get_tar_archive() -> None:
    """Test for getting tar archive backup and verifying its contents"""
    make_mock_config_dir()

    response: Response = client.post(
        "/config/buckets/backup",
        json={"directory_path": MOCK_CONF_DIRPATH}
    )
    assert response.status_code == 200

    with open("tests/fakes/mock_conf.tar.gz", "wb") as bin_file:
        bin_file.write(response.content)
    backup_path = Path("tests/fakes/backup_conf")
    backup_path.mkdir(exist_ok=True)
    with tarfile.open("tests/fakes/mock_conf.tar.gz", "r|gz") as tar_file:
        tar_file.extractall(path=backup_path)
    _, mismatch, error = filecmp.cmpfiles(
        backup_path,
        MOCK_CONF_DIRPATH,
        ["mock.conf", "leave_this.conf"]
    )
    assert mismatch == []
    assert error == []

    os.remove("tests/fakes/mock_conf.tar.gz")
    rmtree(backup_path)
    rmtree(MOCK_CONF_DIRPATH)