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
|
import copy
import json
try:
import unittest2 as unittest
except ImportError:
import unittest
import os
from mock import patch, Mock
import requests
from leap.base import config as baseconfig
from leap.base.constants import DEFAULT_PROVIDER_DEFINITION
from leap.eip import checks as eipchecks
from leap.eip import constants as eipconstants
from leap.eip import exceptions as eipexceptions
from leap.testing.basetest import BaseLeapTest
class EIPCheckTest(BaseLeapTest):
__name__ = "eip_check_tests"
def setUp(self):
pass
def tearDown(self):
pass
# test methods are there, and can be called from run_all
def test_checker_should_implement_check_methods(self):
checker = eipchecks.EIPChecker()
self.assertTrue(hasattr(checker, "check_default_eipconfig"),
"missing meth")
self.assertTrue(hasattr(checker, "check_is_there_default_provider"),
"missing meth")
self.assertTrue(hasattr(checker, "fetch_definition"), "missing meth")
self.assertTrue(hasattr(checker, "fetch_eip_config"), "missing meth")
self.assertTrue(hasattr(checker, "check_complete_eip_config"),
"missing meth")
self.assertTrue(hasattr(checker, "ping_gateway"), "missing meth")
def test_checker_should_actually_call_all_tests(self):
checker = eipchecks.EIPChecker()
mc = Mock()
checker.run_all(checker=mc)
self.assertTrue(mc.check_default_eipconfig.called, "not called")
self.assertTrue(mc.check_is_there_default_provider.called,
"not called")
self.assertTrue(mc.fetch_definition.called,
"not called")
self.assertTrue(mc.fetch_eip_config.called,
"not called")
self.assertTrue(mc.check_complete_eip_config.called,
"not called")
self.assertTrue(mc.ping_gateway.called,
"not called")
# test individual check methods
def test_check_default_eipconfig(self):
checker = eipchecks.EIPChecker()
# no eip config (empty home)
eipconfig = baseconfig.get_config_file(eipconstants.EIP_CONFIG)
self.assertFalse(os.path.isfile(eipconfig))
checker.check_default_eipconfig()
# we've written one, so it should be there.
self.assertTrue(os.path.isfile(eipconfig))
with open(eipconfig, 'rb') as fp:
deserialized = json.load(fp)
self.assertEqual(deserialized,
eipconstants.EIP_SAMPLE_JSON)
# TODO: when new JSONConfig class is in place, we shold
# run validation methods.
def test_check_is_there_default_provider(self):
checker = eipchecks.EIPChecker()
# we do dump a sample eip config, but lacking a
# default provider entry.
# This error will be possible catched in a different
# place, when JSONConfig does validation of required fields.
sampleconfig = copy.copy(eipconstants.EIP_SAMPLE_JSON)
# blank out default_provider
sampleconfig['provider'] = None
eipcfg_path = checker._get_default_eipconfig_path()
with open(eipcfg_path, 'w') as fp:
json.dump(sampleconfig, fp)
with self.assertRaises(eipexceptions.EIPMissingDefaultProvider):
checker.check_is_there_default_provider()
sampleconfig = eipconstants.EIP_SAMPLE_JSON
eipcfg_path = checker._get_default_eipconfig_path()
with open(eipcfg_path, 'w') as fp:
json.dump(sampleconfig, fp)
self.assertTrue(checker.check_is_there_default_provider())
def test_fetch_definition(self):
with patch.object(requests, "get") as mocked_get:
mocked_get.return_value.status_code = 200
mocked_get.return_value.json = DEFAULT_PROVIDER_DEFINITION
checker = eipchecks.EIPChecker(fetcher=requests)
sampleconfig = eipconstants.EIP_SAMPLE_JSON
checker.fetch_definition(config=sampleconfig)
# XXX TODO check for ConnectionError, HTTPError, InvalidUrl
# (and proper EIPExceptions are raised).
# Look at base.test_config.
if __name__ == "__main__":
unittest.main()
|