Example #1
0
 def test_k2hr3useragent_construct_conf_validation_error(self):
     """Checks if the url in conf is invalid."""
     self._conf.k2hr3.api_url = ''  # self.conf instantiates in every setUp.
     with self.assertRaises(_K2hr3UserAgentError) as cm:
         _K2hr3UserAgent(self._conf)
     the_exception = cm.exception
     self.assertEqual('a valid url is expected, not ',
                      '{}'.format(the_exception))
Example #2
0
    def test_k2hr3useragent_construct_conf_is_str(self):
        """Checks if the __init__'s conf is not K2hr3Conf object."""
        conf = 'invalid_param'
        with self.assertRaises(_K2hr3UserAgentError) as cm:
            _K2hr3UserAgent(conf)

        the_exception = cm.exception
        self.assertEqual(
            the_exception.msg,
            'conf is a K2hr3Conf instance, not {}'.format(type(conf)))
Example #3
0
    def test_k2hr3useragent_send(self):
        """Checks if send() works correctly."""
        url = 'https://localhost/v1/role'
        instance_id = '12345678-1234-5678-1234-567812345678'
        ips = ['127.0.0.1', '127.0.0.2']
        # params
        params = {'extra': 'openstack-auto-v1'}
        params['host'] = json.dumps(ips)
        params['cuk'] = instance_id
        headers = {
            'User-Agent':
            'Python-k2hr3_ua/{}.{}'.format(sys.version_info[0],
                                           sys.version_info[1])
        }
        method = 'DELETE'

        # Patch to _K2hr3UserAgent._send() method which is expected to return True if success.
        agent = _K2hr3UserAgent(self._conf)
        agent.url = url
        agent.instance_id = instance_id
        agent.ips = ips
        result = agent.send()

        self.assertEqual(result, True)
        # Ensure values are as expected at runtime.
        self.mock_method_agent.assert_called_once_with(url, params, headers,
                                                       method)
Example #4
0
 def test_k2hr3useragent_error_readonly(self):
     """Checks if error is readonly."""
     with self.assertRaises(AttributeError) as cm:
         agent = _K2hr3UserAgent(self._conf)
         agent.error = 'i am broken'
     the_exception = cm.exception
     self.assertEqual("can't set attribute", '{}'.format(the_exception))
Example #5
0
 def test_k2hr3useragent_params_readonly(self):
     """Checks if params is readonly."""
     with self.assertRaises(AttributeError) as cm:
         agent = _K2hr3UserAgent(self._conf)
         new_params = {'newkey': 'value'}
         agent.params = new_params
     the_exception = cm.exception
     self.assertEqual("can't set attribute", '{}'.format(the_exception))
Example #6
0
 def test_k2hr3useragent_ips_setter_value_error_1(self):
     """Checks if the ips setter works."""
     invalid_ip = None
     with self.assertRaises(_K2hr3UserAgentError) as cm:
         agent = _K2hr3UserAgent(self._conf)
         agent.ips = invalid_ip
     the_exception = cm.exception
     self.assertEqual('ips must be list or str, not {}'.format(invalid_ip),
                      '{}'.format(the_exception))
Example #7
0
 def test_k2hr3useragent_url_setter_value_error_3(self):
     """Checks if the url setter works."""
     invalid_domain = 'example.comm'
     invalid_url = 'http://{}/v1/role/'.format(invalid_domain)
     with self.assertRaises(_K2hr3UserAgentError) as cm:
         agent = _K2hr3UserAgent(self._conf)
         agent.url = invalid_url
     the_exception = cm.exception
     self.assertRegex('{}'.format(the_exception), '^unresolved domain, {}.*$'.format(invalid_domain))
Example #8
0
 def test_k2hr3useragent_allow_self_signed_cert(self):
     """Checks if the allow_self_signed_cert setter works."""
     agent = _K2hr3UserAgent(self._conf)
     allow_self_signed_cert = True
     agent.allow_self_signed_cert = allow_self_signed_cert
     self.assertEqual(True, agent.allow_self_signed_cert)
     # make sure boolean type object is immutable.
     allow_self_signed_cert = False
     self.assertEqual(True, agent.allow_self_signed_cert)
Example #9
0
 def test_k2hr3useragent_headers(self):
     """Checks if headers."""
     agent = _K2hr3UserAgent(self._conf)
     headers = {
         'User-Agent':
         'Python-k2hr3_ua/{}.{}'.format(sys.version_info[0],
                                        sys.version_info[1])
     }
     self.assertEqual(agent.headers, headers)
Example #10
0
 def test_k2hr3useragent_ips_setter_value_error_4(self):
     """Checks if the ips setter works."""
     invalid_ips = [{}, ['1']]
     with self.assertRaises(_K2hr3UserAgentError) as cm:
         agent = _K2hr3UserAgent(self._conf)
         agent.ips = invalid_ips
     the_exception = cm.exception
     msg = 'ip must be str, not {}'.format(invalid_ips[0])
     self.assertEqual(msg, '{}'.format(the_exception))
Example #11
0
 def test_k2hr3useragent_instance_id_setter_value_error_2(self):
     """Checks if the uuid setter works."""
     with self.assertRaises(_K2hr3UserAgentError) as cm:
         agent = _K2hr3UserAgent(self._conf)
         invalid_id = '12345678-1234-5678-1234-56781234567'  # drops the last byte.
         agent.instance_id = invalid_id
     the_exception = cm.exception
     self.assertEqual(
         'Invalid UUID, {} badly formed hexadecimal UUID string'.format(
             invalid_id), '{}'.format(the_exception))
Example #12
0
 def test_k2hr3useragent_url_setter_value_error_1(self):
     """Checks if the url setter works."""
     invalid_url = 'http:/localhost/v1/role/'
     with self.assertRaises(_K2hr3UserAgentError) as cm:
         agent = _K2hr3UserAgent(self._conf)
         agent.url = invalid_url
     the_exception = cm.exception
     self.assertEqual(
         'scheme should contain ://, not {}'.format(invalid_url),
         '{}'.format(the_exception))
Example #13
0
 def test_k2hr3useragent_allow_self_signed_cert_error_1(self):
     """Checks if the allow_self_signed_cert setter works."""
     with self.assertRaises(_K2hr3UserAgentError) as cm:
         agent = _K2hr3UserAgent(self._conf)
         allow_self_signed_cert = None
         agent.allow_self_signed_cert = allow_self_signed_cert
     the_exception = cm.exception
     self.assertEqual(
         'Boolean value expected, not {}'.format(allow_self_signed_cert),
         '{}'.format(the_exception))
Example #14
0
 def test_k2hr3useragent_method_setter_value_error_1(self):
     """Checks if the method setter works."""
     invalid_method = []
     with self.assertRaises(_K2hr3UserAgentError) as cm:
         agent = _K2hr3UserAgent(self._conf)
         agent.method = invalid_method
     the_exception = cm.exception
     self.assertEqual(
         'method should be string, not {}'.format(invalid_method),
         '{}'.format(the_exception))
Example #15
0
 def test_k2hr3useragent_instance_id_setter_value_error_1(self):
     """Checks if the uuid setter works."""
     with self.assertRaises(_K2hr3UserAgentError) as cm:
         agent = _K2hr3UserAgent(self._conf)
         invalid_id = None
         agent.instance_id = invalid_id
     the_exception = cm.exception
     self.assertEqual(
         'Please pass UUID as a string, not {}'.format(invalid_id),
         '{}'.format(the_exception))
Example #16
0
 def test_k2hr3useragent_ips_setter_value_error_3(self):
     """Checks if the ips setter works."""
     invalid_ip = ':::'
     with self.assertRaises(_K2hr3UserAgentError) as cm:
         agent = _K2hr3UserAgent(self._conf)
         agent.ips = invalid_ip
     the_exception = cm.exception
     msg = 'illegal IP address string passed to inet_pton'
     self.assertEqual(
         'ip must be valid string, not {} {}'.format(invalid_ip, msg),
         '{}'.format(the_exception))
Example #17
0
 def test_k2hr3useragent_headers_readonly(self):
     """Checks if headers is readonly."""
     with self.assertRaises(AttributeError) as cm:
         agent = _K2hr3UserAgent(self._conf)
         new_headers = {
             'User-Agent':
             'Python-k2hr3_ua/{}.{}'.format(sys.version_info[0],
                                            sys.version_info[1])
         }
         agent.headers = new_headers
     the_exception = cm.exception
     self.assertEqual("can't set attribute", '{}'.format(the_exception))
Example #18
0
    def __call_r3api(self, params: Dict[str, Any]) -> str:
        """Calls the r3api.

        :returns: NotificationResult.REQUEUE if failed to call the r3api.
                  Otherwise NotificationResult.HANDLED.
        :rtype: str
        """
        assert [
            isinstance(params, dict),
            isinstance(self._conf, K2hr3Conf),
        ]

        try:
            agent = _K2hr3UserAgent(self._conf)
            agent.instance_id = params.get('cuk', None)
            if params.get('ips', None):
                agent.ips = params.get('ips', None)
            if agent.send():
                LOG.debug('ok sent. %s code, %s', agent.instance_id, agent.code)
                return NotificationResult.HANDLED  # type: ignore
            LOG.error('no sent. %s error %s', agent.instance_id, agent.error)
            if self._conf.k2hr3.requeue_on_error is True:
                LOG.warning('requeuing %s', agent.instance_id)
                return NotificationResult.REQUEUE  # type: ignore
            LOG.warning('handled %s, even if an error occurred.', agent.instance_id)
            return NotificationResult.HANDLED  # type: ignore
        except _K2hr3UserAgentError as error:
            LOG.error('k2hr3 exception %s', error)
            if self._conf.k2hr3.requeue_on_error is True:
                LOG.warning('requeuing the msg')
                return NotificationResult.REQUEUE  # type: ignore
            LOG.warning('handled the msg even if an error occurred.')
            return NotificationResult.HANDLED  # type: ignore
        except Exception as error:
            # Note:
            # unknown exception should be handled by upstream caller.
            LOG.error('unknown exception. upstream caller catch this %s', error)
            raise
Example #19
0
 def test_k2hr3useragent_repr(self):
     """Represent a _K2hr3UserAgent instance."""
     agent = _K2hr3UserAgent(self._conf)
     # Note: The order of _error and _code is unknown!
     self.assertRegex(repr(agent), '<_K2hr3UserAgent _.*')
Example #20
0
 def test_k2hr3useragent_params(self):
     """Checks if params."""
     agent = _K2hr3UserAgent(self._conf)
     params = {'extra': 'openstack-auto-v1'}
     self.assertEqual(agent.params, params)
Example #21
0
 def test_k2hr3useragent_method_setter(self):
     """Checks if the method setter works."""
     agent = _K2hr3UserAgent(self._conf)
     method = 'GET'
     agent.method = method
     self.assertEqual(method, agent.method)
Example #22
0
 def test_k2hr3useragent_code(self):
     """Checks if the code."""
     agent = _K2hr3UserAgent(self._conf)
     self.assertEqual(agent.code, -1)
Example #23
0
 def test_k2hr3useragent_construct(self):
     """Creates a K2hr3UserAgent instance."""
     agent = _K2hr3UserAgent(self._conf)
     self.assertIsInstance(agent, _K2hr3UserAgent)
     self.assertEqual(agent._url, 'https://localhost/v1/role')
Example #24
0
 def test_k2hr3useragent_url(self):
     """Checks if url is valid."""
     agent = _K2hr3UserAgent(self._conf)
     url = 'https://localhost/v1/role'
     agent._url = url
     self.assertEqual(agent.url, agent.url)
Example #25
0
 def test_k2hr3useragent_url_setter(self):
     """Checks if the url setter works."""
     agent = _K2hr3UserAgent(self._conf)
     url = 'https://localhost/v1/role'
     agent.url = url
     self.assertEqual(url, agent.url)
Example #26
0
 def test_k2hr3useragent_ips_setter_list(self):
     """Checks if the ips setter works."""
     agent = _K2hr3UserAgent(self._conf)
     ips = ['127.0.0.1']
     agent.ips = ips
     self.assertEqual(ips, agent.ips)
Example #27
0
 def test_k2hr3httpresponse_str(self):
     """Stringfy a _K2hr3UserAgent instance."""
     agent = _K2hr3UserAgent(self._conf)
     # Note: The order of _error and _code is unknown!
     self.assertRegex(str(agent), '<_K2hr3UserAgent _.*')
Example #28
0
 def test_k2hr3useragent_method(self):
     """Checks if method is valid."""
     agent = _K2hr3UserAgent(self._conf)
     method = 'GET'
     agent._method = method
     self.assertEqual(agent.method, agent.method)
Example #29
0
 def test_k2hr3useragent_instance_id_setter(self):
     """Checks if the ips setter works."""
     agent = _K2hr3UserAgent(self._conf)
     instance_id = '12345678-1234-5678-1234-567812345678'
     agent.instance_id = instance_id
     self.assertEqual(instance_id, agent.instance_id)
Example #30
0
 def test_k2hr3useragent_error(self):
     """Checks if errors."""
     agent = _K2hr3UserAgent(self._conf)
     self.assertEqual(agent.error, '')