Esempio n. 1
0
 def test_get_maas_version(self, mock_get_url):
     """verify we fetch maas version from api """
     host = '127.0.0.1'
     endpoint = 'http://%s/MAAS/metadata/node-1230348484' % host
     maas_version = {'version': '2.1.5+bzr5596-0ubuntu1'}
     maas_api_version = '2.0'
     mock_get_url.side_effect = iter([
         maas_api_version.encode('utf-8'),
         ("%s" % json.dumps(maas_version)).encode('utf-8'),
     ])
     result = url_helper.get_maas_version(endpoint)
     self.assertEqual(maas_version, result)
     mock_get_url.assert_has_calls([
         mock.call('http://%s/MAAS/api/version/' % host),
         mock.call('http://%s/MAAS/api/%s/version/' %
                   (host, maas_api_version))
     ])
Esempio n. 2
0
 def test_get_maas_version_unsupported(self, mock_get_url, mock_log):
     """return value is None if endpoint returns unsupported MAAS version
     """
     host = '127.0.0.1'
     endpoint = 'http://%s/MAAS/metadata/node-1230348484' % host
     supported = ['1.0', '2.0']
     maas_api_version = 'garbage'
     mock_get_url.side_effect = iter([
         maas_api_version.encode('utf-8'),
     ])
     result = url_helper.get_maas_version(endpoint)
     self.assertIsNone(result)
     self.assertTrue(mock_log.warn.called)
     mock_log.warn.assert_called_with(
         'Endpoint "%s" API version "%s" not in MAAS supported'
         'versions: "%s"', endpoint, maas_api_version, supported)
     mock_get_url.assert_has_calls([
         mock.call('http://%s/MAAS/api/version/' % host),
     ])
Esempio n. 3
0
 def test_get_maas_version_bad_json(self, mock_get_url, mock_log):
     """return value is None if endpoint returns invalid json """
     host = '127.0.0.1'
     endpoint = 'http://%s/MAAS/metadata/node-1230348484' % host
     maas_api_version = '2.0'
     bad_json = '{1237'.encode('utf-8')
     mock_get_url.side_effect = iter([
         maas_api_version.encode('utf-8'),
         bad_json,
     ])
     result = url_helper.get_maas_version(endpoint)
     self.assertIsNone(result)
     self.assertTrue(mock_log.warn.called)
     mock_log.warn.assert_called_with(
         'Failed to load MAAS version result: %s', bad_json)
     mock_get_url.assert_has_calls([
         mock.call('http://%s/MAAS/api/version/' % host),
         mock.call('http://%s/MAAS/api/%s/version/' %
                   (host, maas_api_version))
     ])
Esempio n. 4
0
 def test_get_maas_version_network_error_returns_none(
         self, mock_get_url, mock_log):
     """return value is None if endpoint fails to respond"""
     host = '127.0.0.1'
     endpoint = 'http://%s/MAAS/metadata/node-1230348484' % host
     maas_api_version = '2.0'
     exception = url_helper.UrlError('404')
     mock_get_url.side_effect = iter([
         maas_api_version.encode('utf-8'),
         exception,
     ])
     result = url_helper.get_maas_version(endpoint)
     self.assertIsNone(result)
     self.assertTrue(mock_log.warn.called)
     mock_log.warn.assert_called_with(
         'Failed to query MAAS version via URL: %s', exception)
     mock_get_url.assert_has_calls([
         mock.call('http://%s/MAAS/api/version/' % host),
         mock.call('http://%s/MAAS/api/%s/version/' %
                   (host, maas_api_version))
     ])
Esempio n. 5
0
def handle_pollinate_user_agent(cfg, target):
    """Configure the pollinate user-agent if provided configuration

    pollinate:
        user_agent: false  # disable writing out a user-agent string

    # custom agent key/value pairs
    pollinate:
       user_agent:
          key1: value1
          key2: value2

    No config will result in curtin fetching:
      curtin version
      maas version (via endpoint URL, if present)
    """

    pcfg = cfg.get('pollinate')
    if not isinstance(pcfg, dict):
        pcfg = {'user_agent': {}}

    uacfg = pcfg.get('user_agent', {})
    if uacfg is False:
        return

    # set curtin version
    uacfg['curtin'] = curtin_version.version_string()

    # maas configures a curtin reporting webhook handler with
    # an endpoint URL.  This url is used to query the MAAS REST
    # api to extract the exact maas version.
    maas_reporting = cfg.get('reporting', {}).get('maas', None)
    if maas_reporting:
        endpoint = maas_reporting.get('endpoint')
        maas_version = get_maas_version(endpoint)
        if maas_version:
            uacfg['maas'] = maas_version['version']

    inject_pollinate_user_agent_config(uacfg, target)
Esempio n. 6
0
 def test_get_maas_version_no_endpoint(self):
     """verify we return None with invalid endpoint """
     host = None
     result = url_helper.get_maas_version(host)
     self.assertEqual(None, result)