99from yaml import load , SafeLoader
1010
1111import cachet_url_monitor .status
12+ from cachet_url_monitor .client import CachetClient
13+ import cachet_url_monitor .exceptions
1214
1315sys .modules ['logging' ] = mock .Mock ()
1416from cachet_url_monitor .configuration import Configuration
1517import os
1618
1719
1820class ConfigurationTest (unittest .TestCase ):
19- @mock .patch .dict (os .environ , {'CACHET_TOKEN' : 'token2' })
21+ client : CachetClient
22+ configuration : Configuration
23+
2024 def setUp (self ):
2125 def getLogger (name ):
2226 self .mock_logger = mock .Mock ()
2327 return self .mock_logger
2428
2529 sys .modules ['logging' ].getLogger = getLogger
26-
27- # def get(url, headers):
28- # get_return = mock.Mock()
29- # get_return.ok = True
30- # get_return.json = mock.Mock()
31- # get_return.json.return_value = {'data': {'status': 1, 'default_value': 0.5}}
32- # return get_return
33- #
34- # sys.modules['requests'].get = get
35-
30+ self .client = mock .Mock ()
31+ # We set the initial status to OPERATIONAL.
32+ self .client .get_component_status .return_value = cachet_url_monitor .status .ComponentStatus .OPERATIONAL
3633 self .configuration = Configuration (
37- load (open (os .path .join (os .path .dirname (__file__ ), 'configs/config.yml' ), 'rt' ), SafeLoader ), 0 )
38- # sys.modules['requests'].Timeout = Timeout
39- # sys.modules['requests'].ConnectionError = ConnectionError
40- # sys.modules['requests'].HTTPError = HTTPError
34+ load (open (os .path .join (os .path .dirname (__file__ ), 'configs/config.yml' ), 'rt' ), SafeLoader ), 0 , self .client ,
35+ 'token2' )
4136
4237 def test_init (self ):
4338 self .assertEqual (len (self .configuration .data ), 2 , 'Number of root elements in config.yml is incorrect' )
4439 self .assertEqual (len (self .configuration .expectations ), 3 , 'Number of expectations read from file is incorrect' )
4540 self .assertDictEqual (self .configuration .headers , {'X-Cachet-Token' : 'token2' }, 'Header was not set correctly' )
46- self .assertEqual (self .configuration .api_url , 'https://demo.cachethq.io/api/v1' ,
47- 'Cachet API URL was set incorrectly' )
4841 self .assertDictEqual (self .configuration .endpoint_header , {'SOME-HEADER' : 'SOME-VALUE' }, 'Header is incorrect' )
4942
5043 @requests_mock .mock ()
@@ -98,31 +91,49 @@ def test_evaluate_with_http_error(self, m):
9891 'Component status set incorrectly' )
9992 self .mock_logger .exception .assert_called_with ('Unexpected HTTP response' )
10093
101- @requests_mock .mock ()
102- def test_push_status (self , m ):
103- m .put ('https://demo.cachethq.io/api/v1/components/1?id=1&status=1' , headers = {'X-Cachet-Token' : 'token2' })
104- self .assertEqual (self .configuration .status , cachet_url_monitor .status .ComponentStatus .OPERATIONAL ,
105- 'Incorrect component update parameters' )
94+ def test_push_status (self ):
95+ self .client .get_component_status .return_value = cachet_url_monitor .status .ComponentStatus .OPERATIONAL
96+ push_status_response = mock .Mock ()
97+ self .client .push_status .return_value = push_status_response
98+ push_status_response .ok = True
99+ self .configuration .status = cachet_url_monitor .status .ComponentStatus .PARTIAL_OUTAGE
100+
106101 self .configuration .push_status ()
107102
108- @requests_mock .mock ()
109- def test_push_status_with_failure (self , m ):
110- m .put ('https://demo.cachethq.io/api/v1/components/1?id=1&status=1' , headers = {'X-Cachet-Token' : 'token2' },
111- status_code = 400 )
112- self .assertEqual (self .configuration .status , cachet_url_monitor .status .ComponentStatus .OPERATIONAL ,
113- 'Incorrect component update parameters' )
103+ self .client .push_status .assert_called_once_with (1 , cachet_url_monitor .status .ComponentStatus .OPERATIONAL )
104+
105+ def test_push_status_with_failure (self ):
106+ self .client .get_component_status .return_value = cachet_url_monitor .status .ComponentStatus .OPERATIONAL
107+ push_status_response = mock .Mock ()
108+ self .client .push_status .return_value = push_status_response
109+ push_status_response .ok = False
110+ self .configuration .status = cachet_url_monitor .status .ComponentStatus .PARTIAL_OUTAGE
111+
112+ self .configuration .push_status ()
113+
114+ self .client .push_status .assert_called_once_with (1 , cachet_url_monitor .status .ComponentStatus .OPERATIONAL )
115+
116+ def test_push_status_same_status (self ):
117+ self .client .get_component_status .return_value = cachet_url_monitor .status .ComponentStatus .OPERATIONAL
118+ self .configuration .status = cachet_url_monitor .status .ComponentStatus .OPERATIONAL
119+
114120 self .configuration .push_status ()
115121
122+ self .client .push_status .assert_not_called ()
123+
116124
117125class ConfigurationMultipleUrlTest (unittest .TestCase ):
118126 @mock .patch .dict (os .environ , {'CACHET_TOKEN' : 'token2' })
119127 def setUp (self ):
120128 config_yaml = load (open (os .path .join (os .path .dirname (__file__ ), 'configs/config_multiple_urls.yml' ), 'rt' ),
121129 SafeLoader )
130+ self .client = []
122131 self .configuration = []
123132
124133 for index in range (len (config_yaml ['endpoints' ])):
125- self .configuration .append (Configuration (config_yaml , index ))
134+ client = mock .Mock ()
135+ self .client .append (client )
136+ self .configuration .append (Configuration (config_yaml , index , client , 'token2' ))
126137
127138 def test_init (self ):
128139 expected_method = ['GET' , 'POST' ]
@@ -133,8 +144,6 @@ def test_init(self):
133144 self .assertEqual (len (config .data ), 2 , 'Number of root elements in config.yml is incorrect' )
134145 self .assertEqual (len (config .expectations ), 1 , 'Number of expectations read from file is incorrect' )
135146 self .assertDictEqual (config .headers , {'X-Cachet-Token' : 'token2' }, 'Header was not set correctly' )
136- self .assertEqual (config .api_url , 'https://demo.cachethq.io/api/v1' ,
137- 'Cachet API URL was set incorrectly' )
138147
139148 self .assertEqual (expected_method [index ], config .endpoint_method )
140149 self .assertEqual (expected_url [index ], config .endpoint_url )
@@ -146,4 +155,4 @@ def test_init(self):
146155 with pytest .raises (cachet_url_monitor .configuration .ConfigurationValidationError ):
147156 self .configuration = Configuration (
148157 load (open (os .path .join (os .path .dirname (__file__ ), 'configs/config_invalid_type.yml' ), 'rt' ),
149- SafeLoader ), 0 )
158+ SafeLoader ), 0 , mock . Mock (), 'token2' )
0 commit comments