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
|
import mock
import time
from urllib.error import HTTPError, URLError
from django.utils.timezone import now
from datetime import timedelta
from django.test import TestCase
from django.core.management import call_command
from mirrors.tests import create_mirror_url
from mirrors.models import MirrorLog, CheckLocation
class MirrorCheckTest(TestCase):
def setUp(self):
self.mirror_url = create_mirror_url()
def tearDown(self):
self.mirror_url.delete()
@mock.patch('urllib.request.Request')
@mock.patch('urllib.request.urlopen')
def test_invalid(self, urlopen, Request):
urlopen.return_value.read.return_value = 'data'
Request.get_host.return_value = 'archlinux.org'
Request.type.return_value = 'https'
call_command('mirrorcheck')
mirrorlog = MirrorLog.objects.first()
self.assertNotEqual(mirrorlog.error, '')
self.assertEqual(mirrorlog.is_success, False)
@mock.patch('urllib.request.Request')
@mock.patch('urllib.request.urlopen')
def test_valid(self, urlopen, Request):
urlopen.return_value.read.return_value = str(int(time.time()))
Request.get_host.return_value = 'archlinux.org'
Request.type.return_value = 'https'
call_command('mirrorcheck')
mirrorlog = MirrorLog.objects.first()
self.assertEqual(mirrorlog.error, '')
self.assertEqual(mirrorlog.is_success, True)
@mock.patch('urllib.request.Request')
@mock.patch('urllib.request.urlopen')
def test_valid_olddate(self, urlopen, Request):
urlopen.return_value.read.return_value = str(int(time.time()))
Request.get_host.return_value = 'archlinux.org'
Request.type.return_value = 'https'
date = now() - timedelta(days=600)
MirrorLog.objects.create(url=self.mirror_url, check_time=date)
call_command('mirrorcheck')
self.assertEqual(len(MirrorLog.objects.all()), 1)
@mock.patch('urllib.request.Request')
@mock.patch('urllib.request.urlopen')
def test_not_found(self, urlopen, Request):
excp = HTTPError('https://archlinux.org/404.txt', 404, 'Not Found', '', None)
urlopen.return_value.read.side_effect = excp
Request.get_host.return_value = 'archlinux.org'
Request.type.return_value = 'https'
call_command('mirrorcheck')
mirrorlog = MirrorLog.objects.first()
self.assertEqual(mirrorlog.error, str(excp))
self.assertEqual(mirrorlog.is_success, False)
@mock.patch('urllib.request.Request')
@mock.patch('urllib.request.urlopen')
def test_not_found_variant(self, urlopen, Request):
excp = URLError('550 No such file', '550.txt')
urlopen.return_value.read.side_effect = excp
Request.get_host.return_value = 'archlinux.org'
Request.type.return_value = 'https'
call_command('mirrorcheck')
mirrorlog = MirrorLog.objects.first()
self.assertIn(excp.reason, mirrorlog.error)
self.assertEqual(mirrorlog.is_success, False)
def test_checklocation(self):
with self.assertRaises(CheckLocation.DoesNotExist) as e:
call_command('mirrorcheck', '-l', '1')
self.assertEqual('CheckLocation matching query does not exist.', str(e.exception))
def test_checklocation_model(self):
checkloc = CheckLocation.objects.create(hostname='archlinux.org',
source_ip='1.1.1.1')
with mock.patch('mirrors.management.commands.mirrorcheck.logger') as logger:
call_command('mirrorcheck', '-l', '1')
logger.info.assert_called()
checkloc.delete()
|