summaryrefslogtreecommitdiff
path: root/tests/urlfetcher_mock.py
blob: ec1d8cf72485dddc1fbb262e1a96d1972f290f04 (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
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
#

import ftplib
import os
import urllib
from urllib.request import Request

import requests

from virtinst import log

_URLPREFIX = "https://virtinst-testsuite.example/"
_MOCK_TOPDIR = os.path.dirname(__file__) + "/data/urldetect/"


def make_mock_input_url(distro):
    return _URLPREFIX + distro


def _map_mock_url_to_file(url):
    if url.startswith(_URLPREFIX):
        path = url[len(_URLPREFIX):]
        fn = _MOCK_TOPDIR + path
        if not os.path.exists(os.path.dirname(fn)):
            raise RuntimeError("Expected mock file not found: %s" % fn)

    elif url.endswith("treeinfo"):
        # If the url is requesting treeinfo, give a fake treeinfo from
        # our testsuite data
        fn = ("%s/data/fakemedia/fakerhel6tree/.treeinfo" %
                os.path.abspath(os.path.dirname(__file__)))
    else:
        # Otherwise just copy this file
        fn = os.path.abspath(__file__)

    return os.path.abspath(fn)


class _MockRequestsResponse:
    def __init__(self, url):
        log.debug("mocking requests session for url=%s", url)
        fn = _map_mock_url_to_file(url)
        self._content = open(fn).read()
        self.headers = {'content-length': len(self._content)}

    def raise_for_status(self):
        pass
    def iter_content(self, *args, **kwargs):
        dummy = args
        dummy = kwargs
        return [self._content.encode("utf-8")]


class _MockRequestsSession:
    def close(self):
        pass
    def head(self, url, *args, **kwargs):
        dummy = args
        dummy = kwargs
        return _MockRequestsResponse(url)
    def get(self, url, *args, **kwargs):
        dummy = args
        dummy = kwargs
        if "testsuitefail" in url:
            raise RuntimeError("testsuitefail seen, raising mock error")
        return _MockRequestsResponse(url)


class _MockFTPSession:
    def connect(self, *args, **kwargs):
        pass
    def login(self, *args, **kwargs):
        pass
    def voidcmd(self, *args, **kwargs):
        pass
    def quit(self, *args, **kwargs):
        pass
    def size(self, url):
        path = _map_mock_url_to_file(url)
        return os.path.getsize(path)


def _MockUrllibRequest(url):
    url = _map_mock_url_to_file(url)
    url = "file://" + url
    return Request(url)


def setup_mock():
    requests.Session = _MockRequestsSession
    ftplib.FTP = _MockFTPSession
    urllib.request.Request = _MockUrllibRequest