Completed
Push — master ( d462aa...30f501 )
by
unknown
11s
created

test_data_fetch.test_file_retreival()   A

Complexity

Conditions 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
import tempfile
2
import filecmp
3
import contextlib
4
import pathlib
5
import sys
6
import subprocess
7
import time
8
9
from org_fedora_oscap import data_fetch
10
11
12
PORT = 8001
13
14
15
@contextlib.contextmanager
16
def serve_directory_in_separate_process(port):
17
    args = [sys.executable, "-m", "http.server", str(port)]
18
    proc = subprocess.Popen(
19
        args,
20
        stdout=subprocess.DEVNULL,
21
        stderr=subprocess.DEVNULL)
22
    # give the server some time to start
23
    time.sleep(0.4)
24
    yield
25
    proc.terminate()
26
    proc.wait()
27
28
29
def test_file_retreival():
30
    filename_to_test = pathlib.Path(__file__)
31
    relative_filename_to_test = filename_to_test.relative_to(pathlib.Path.cwd())
32
33
    temp_file = tempfile.NamedTemporaryFile()
34
    temp_filename = temp_file.name
35
36
    with serve_directory_in_separate_process(PORT):
37
        data_fetch._curl_fetch(
38
            "http://localhost:{}/{}".format(PORT, relative_filename_to_test), temp_filename)
39
40
    assert filecmp.cmp(relative_filename_to_test, temp_filename)
41
42
43
def test_supported_url():
44
    assert data_fetch.can_fetch_from("http://example.com")
45
    assert data_fetch.can_fetch_from("https://example.com")
46
47
48
def test_unsupported_url():
49
    assert not data_fetch.can_fetch_from("aaaaa")
50