Esempio n. 1
0
 def test_collect_all_nostore(self):
     (changed_keys, content) = self._call_collect_all(store=False)
     self.assertEqual(set(), changed_keys)
     self.assertThat(content, matchers.IsInstance(dict))
     for collector in cfg.CONF.collectors:
         self.assertIn(collector, content)
         self.assertThat(content[collector], matchers.IsInstance(dict))
Esempio n. 2
0
    def test_collect_local_orders_multiple(self):
        self._setup_test_json(META_DATA, '00test.json')
        self._setup_test_json(META_DATA2, '99test.json')

        # Monkey Patch os.listdir so it _always_ returns the wrong sort
        unpatched_listdir = os.listdir

        def wrong_sort_listdir(path):
            ret = unpatched_listdir(path)
            save_locale = locale.getdefaultlocale()
            locale.setlocale(locale.LC_ALL, 'C')
            bad_sort = sorted(ret, reverse=True)
            locale.setlocale(locale.LC_ALL, save_locale)
            return bad_sort

        self.useFixture(fixtures.MonkeyPatch('os.listdir', wrong_sort_listdir))
        local_md = self._call_collect()

        self.assertThat(local_md, matchers.IsInstance(list))
        self.assertEqual(2, len(local_md))
        self.assertThat(local_md[0], matchers.IsInstance(tuple))

        self.assertEqual('00test.json', local_md[0][0])
        md1 = local_md[0][1]
        self.assertEqual(META_DATA, md1)

        self.assertEqual('99test.json', local_md[1][0])
        md2 = local_md[1][1]
        self.assertEqual(META_DATA2, md2)
Esempio n. 3
0
 def test_setup_logging_logfile(self):
     self.CONF.set_override('console_output_enabled', False)
     del logging.getLogger().handlers[:]
     service._setup_logging()
     for hndler in logging.getLogger().handlers:
         self.assertThat(
             hndler,
             matchers.MatchesAny(
                 matchers.IsInstance(logging.handlers.RotatingFileHandler),
                 matchers.IsInstance(logging.NullHandler)))
Esempio n. 4
0
 def test_setup_logging_console(self):
     self.CONF.set_override('logfile', None)
     del logging.getLogger().handlers[:]
     service._setup_logging()
     for hndler in logging.getLogger().handlers:
         self.assertThat(
             hndler,
             matchers.MatchesAny(
                 matchers.IsInstance(logging.StreamHandler),
                 matchers.IsInstance(logging.NullHandler)))
Esempio n. 5
0
 def test_collect_local_path_nonexist(self):
     cfg.CONF.set_override(name='path',
                           override=['/this/doesnt/exist'],
                           group='local')
     local_md = self._call_collect()
     self.assertThat(local_md, matchers.IsInstance(list))
     self.assertEqual(0, len(local_md))
Esempio n. 6
0
    def test_setup_logging(self):
        del logging.getLogger().handlers[:]
        service._setup_logging()
        self.assertEqual(logging.getLogger().getEffectiveLevel(),
                         logging.INFO)
        self.assertEqual(
            logging.getLogger('tvdbapi_client').getEffectiveLevel(),
            logging.WARNING)

        for hndler in logging.getLogger().handlers:
            self.assertThat(
                hndler,
                matchers.MatchesAny(
                    matchers.IsInstance(logging.handlers.RotatingFileHandler),
                    matchers.IsInstance(logging.StreamHandler),
                    matchers.IsInstance(logging.NullHandler)))
Esempio n. 7
0
    def test_get_functions(self):
        def f():
            pass

        def f_():
            pass

        def f__():
            pass

        def g():
            pass

        context = contexts.Context()
        context2 = context.create_child_context()

        context.register_function(f)
        context.register_function(f_)
        context.register_function(g, exclusive=True)
        context2.register_function(f__)

        functions, is_exclusive = context.get_functions('f')
        self.assertFalse(is_exclusive)
        self.assertIsInstance(functions, set)
        self.assertThat(functions, testtools.matchers.HasLength(2))
        self.assertThat(
            functions,
            matchers.AllMatch(matchers.IsInstance(specs.FunctionDefinition)))
        functions, is_exclusive = context2.get_functions('g')
        self.assertFalse(is_exclusive)
        functions, is_exclusive = context2.get_functions('f')
        self.assertFalse(is_exclusive)
        self.assertThat(functions, testtools.matchers.HasLength(1))
Esempio n. 8
0
 def test_main_no_command(self):
     fake_args = [
         'os-collect-config',
         '--config-file',
         '/dev/null',
         '--cfn-metadata-url',
         'http://192.0.2.1:8000/v1/',
         '--cfn-stack-name',
         'foo',
         '--cfn-path',
         'foo.Metadata',
         '--cfn-access-key-id',
         '0123456789ABCDEF',
         '--cfn-secret-access-key',
         'FEDCBA9876543210',
     ]
     fake_metadata = _setup_heat_local_metadata(self)
     fake_args.append('--heat_local-path')
     fake_args.append(fake_metadata)
     output = self.useFixture(fixtures.StringStream('stdout'))
     self.useFixture(fixtures.MonkeyPatch('sys.stdout', output.stream))
     self._call_main(fake_args)
     out_struct = json.loads(output.getDetails()['stdout'].as_text())
     self.assertThat(out_struct, matchers.IsInstance(dict))
     self.assertIn('ec2', out_struct)
     self.assertIn('cfn', out_struct)
    def test_single_fail(self):
        """
        A single incoming message with a single file path.
        """
        sut = MetadataExtractor('exiftool', ('-some', '-arg'))

        error = RuntimeError('boom!')

        sut.peer = Mock(spec=ExiftoolProtocol)
        sut.peer.execute.return_value = defer.fail(error)

        insert = {
            'inserts': ['a'],
            'deletes': [],
            'data': {
                'a': {
                    'path': '/path/to/file.jpg',
                }
            }
        }
        send = Mock(spec=Scheduler.send)
        result = sut(insert, send)
        self.assertEquals(send.call_count, 0)
        sut.peer.execute.assert_called_once_with(
            b'-some', b'-arg', b'-j', b'-charset', b'exiftool=UTF-8',
            b'-charset', b'filename=UTF-8', b'/path/to/file.jpg')

        failure_matcher = matchers.AfterPreprocessing(
            lambda f: f.value, matchers.IsInstance(MetadataExtractorError))
        assert_that(result, twistedsupport.failed(failure_matcher))
Esempio n. 10
0
    def test_collect_local(self):
        self._setup_test_json(META_DATA)
        local_md = self._call_collect()

        self.assertThat(local_md, matchers.IsInstance(list))
        self.assertEqual(1, len(local_md))
        self.assertThat(local_md[0], matchers.IsInstance(tuple))
        self.assertEqual(2, len(local_md[0]))
        self.assertEqual('test.json', local_md[0][0])

        only_md = local_md[0][1]
        self.assertThat(only_md, matchers.IsInstance(dict))

        for k in ('localstrA', 'localint9', 'localmap_xy'):
            self.assertIn(k, only_md)
            self.assertEqual(only_md[k], META_DATA[k])

        self.assertEqual('', self.log.output)
Esempio n. 11
0
 def _validate_object(self, obj):
     for attr in BASE_EXPECTED_FIELDS:
         if attr.endswith('_at'):
             self.assertThat(
                 obj[attr],
                 matchers.MatchesAny(matchers.Is(None),
                                     matchers.IsInstance(str)))
         else:
             self.assertIsInstance(obj[attr], bool)
Esempio n. 12
0
 def test_collect_cfn_sub_path(self):
     cfg.CONF.cfn.path = ['foo.Metadata.map_ab']
     cfn_collect = cfn.Collector(requests_impl=FakeRequests(self))
     content = cfn_collect.collect()
     self.assertThat(content, matchers.IsInstance(list))
     self.assertEqual('cfn', content[0][0])
     content = content[0][1]
     self.assertIn(u'b', content)
     self.assertEqual(u'banana', content[u'b'])
Esempio n. 13
0
 def test_collect_all_ec2_unavailable(self):
     collector_kwargs_map = {
         'ec2': {'requests_impl': test_ec2.FakeFailRequests},
         'cfn': {'requests_impl': test_cfn.FakeRequests(self)}
     }
     (changed_keys, content) = self._call_collect_all(
         store=False, collector_kwargs_map=collector_kwargs_map)
     self.assertEqual(set(), changed_keys)
     self.assertThat(content, matchers.IsInstance(dict))
     self.assertNotIn('ec2', content)
Esempio n. 14
0
 def assertNotIsInstance(self, obj, cls, msg=None):
     """Python < v2.7 compatibility.  Assert 'not isinstance(obj, cls)."""
     try:
         f = super(_BaseTestCase, self).assertNotIsInstance
     except AttributeError:
         self.assertThat(obj,
                         matchers.Not(matchers.IsInstance(cls)),
                         message=msg or '')
     else:
         f(obj, cls, msg=msg)
Esempio n. 15
0
 def test_collect_all_store_alt_order(self):
     # Ensure different than default
     new_list = list(reversed(cfg.CONF.collectors))
     (changed_keys, paths) = self._call_collect_all(
         store=True, collectors=new_list)
     self.assertEqual(set(cfg.CONF.collectors), changed_keys)
     self.assertThat(paths, matchers.IsInstance(list))
     expected_paths = [
         os.path.join(self.cache_dir.path, '%s.json' % collector)
         for collector in new_list]
     self.assertEqual(expected_paths, paths)
Esempio n. 16
0
 def match(self, matchee):
     checks = []
     checks.append(matchers.IsInstance(MultiCheck))
     checks.append(
         matchers.AfterPreprocessing(operator.attrgetter('strategy'),
                                     matchers.Is(self.strategy)))
     checks.append(
         matchers.AfterPreprocessing(
             operator.attrgetter('subchecks'),
             matchers.MatchesListwise(self.subchecks)))
     return matchers.MatchesAll(*checks).match(matchee)
Esempio n. 17
0
 def test_collect_cfn_software_config(self):
     cfn_md = cfn.Collector(
         requests_impl=FakeRequestsSoftwareConfig(self)).collect()
     self.assertThat(cfn_md, matchers.IsInstance(list))
     self.assertEqual('cfn', cfn_md[0][0])
     cfn_config = cfn_md[0][1]
     self.assertThat(cfn_config, matchers.IsInstance(dict))
     self.assertEqual(set(['old-style', 'deployments']),
                      set(cfn_config.keys()))
     self.assertIn('deployments', cfn_config)
     self.assertThat(cfn_config['deployments'], matchers.IsInstance(list))
     self.assertEqual(4, len(cfn_config['deployments']))
     deployment = cfn_config['deployments'][0]
     self.assertIn('inputs', deployment)
     self.assertThat(deployment['inputs'], matchers.IsInstance(list))
     self.assertEqual(1, len(deployment['inputs']))
     self.assertEqual('dep-name1', cfn_md[1][0])
     self.assertEqual('value1', cfn_md[1][1]['config1'])
     self.assertEqual('dep-name2', cfn_md[2][0])
     self.assertEqual('value2', cfn_md[2][1]['config2'])
Esempio n. 18
0
    def test_collect_cfn(self):
        cfn_md = cfn.Collector(requests_impl=FakeRequests(self)).collect()
        self.assertThat(cfn_md, matchers.IsInstance(list))
        self.assertEqual('cfn', cfn_md[0][0])
        cfn_md = cfn_md[0][1]

        for k in ('int1', 'strfoo', 'map_ab'):
            self.assertIn(k, cfn_md)
            self.assertEqual(cfn_md[k], META_DATA[k])

        self.assertEqual('', self.log.output)
Esempio n. 19
0
 def test_collect_heat(self):
     heat_md = heat.Collector(
         keystoneclient=FakeKeystoneClient(self),
         heatclient=FakeHeatClientSoftwareConfig(self),
         discover_class=FakeKeystoneDiscover).collect()
     self.assertThat(heat_md, matchers.IsInstance(list))
     self.assertEqual(2, len(heat_md))
     self.assertEqual('heat', heat_md[0][0])
     self.assertEqual(SOFTWARE_CONFIG_DATA['deployments'],
                      heat_md[0][1]['deployments'])
     self.assertEqual(('dep-name1', {'config1': 'value1'}), heat_md[1])
Esempio n. 20
0
 def _test_collect_all_store(self, collector_kwargs_map=None,
                             expected_changed=None):
     (changed_keys, paths) = self._call_collect_all(
         store=True, collector_kwargs_map=collector_kwargs_map)
     if expected_changed is None:
         expected_changed = set(['heat_local', 'cfn', 'ec2',
                                 'heat', 'local', 'request', 'zaqar'])
     self.assertEqual(expected_changed, changed_keys)
     self.assertThat(paths, matchers.IsInstance(list))
     for path in paths:
         self.assertTrue(os.path.exists(path))
         self.assertTrue(os.path.exists('%s.orig' % path))
Esempio n. 21
0
 def test_collect_heat(self, mock_url_for, mock___init__):
     mock___init__.return_value = None
     mock_url_for.return_value = cfg.CONF.heat.auth_url
     heat_md = heat.Collector(
         keystoneclient=FakeKeystoneClient(self),
         heatclient=FakeHeatClientSoftwareConfig(self)).collect()
     self.assertThat(heat_md, matchers.IsInstance(list))
     self.assertEqual(2, len(heat_md))
     self.assertEqual('heat', heat_md[0][0])
     self.assertEqual(SOFTWARE_CONFIG_DATA['deployments'],
                      heat_md[0][1]['deployments'])
     self.assertEqual(('dep-name1', {'config1': 'value1'}), heat_md[1])
    def test_collect_heat_local_twice(self):
        with tempfile.NamedTemporaryFile() as md:
            md.write(json.dumps(META_DATA).encode('utf-8'))
            md.flush()
            local_md = self._call_collect(md.name, md.name)

        self.assertThat(local_md, matchers.IsInstance(dict))

        for k in ('localstrA', 'localint9', 'localmap_xy'):
            self.assertIn(k, local_md)
            self.assertEqual(local_md[k], META_DATA[k])

        self.assertEqual('', self.log.output)
Esempio n. 23
0
    def test_collect_zaqar(self, mock_url_for, mock___init__):
        mock___init__.return_value = None
        mock_url_for.return_value = cfg.CONF.zaqar.auth_url
        zaqar_md = zaqar.Collector(
            keystoneclient=FakeKeystoneClient(self, cfg.CONF.zaqar),
            zaqarclient=FakeZaqarClient(self)).collect()
        self.assertThat(zaqar_md, matchers.IsInstance(list))
        self.assertEqual('zaqar', zaqar_md[0][0])
        zaqar_md = zaqar_md[0][1]

        for k in ('int1', 'strfoo', 'map_ab'):
            self.assertIn(k, zaqar_md)
            self.assertEqual(zaqar_md[k], test_heat.META_DATA[k])
Esempio n. 24
0
    def test_collect_request(self):
        req_collect = request.Collector(requests_impl=FakeRequests)
        self.assertIsNone(req_collect.last_modified)
        req_md = req_collect.collect()
        self.assertIsNotNone(req_collect.last_modified)
        self.assertThat(req_md, matchers.IsInstance(list))
        self.assertEqual('request', req_md[0][0])
        req_md = req_md[0][1]

        for k in ('int1', 'strfoo', 'map_ab'):
            self.assertIn(k, req_md)
            self.assertEqual(req_md[k], META_DATA[k])

        self.assertEqual('', self.log.output)
Esempio n. 25
0
 def test_empty_account(self):
     """
     this is an account with no containers and no objects
     """
     # setup expectation
     with SwiftClientStub() as swift_stub:
         swift_stub.with_account('123223')
         # interact
         conn = swiftclient.client.Connection()
         account_info = conn.get_account()
         self.assertThat(account_info, matchers.Not(matchers.Is(None)))
         self.assertThat(len(account_info), matchers.Is(2))
         self.assertThat(account_info, matchers.IsInstance(tuple))
         self.assertThat(account_info[0], matchers.IsInstance(dict))
         self.assertThat(
             account_info[0],
             matchers.KeysEqual('content-length', 'accept-ranges',
                                'x-timestamp', 'x-trans-id', 'date',
                                'x-account-bytes-used',
                                'x-account-container-count', 'content-type',
                                'x-account-object-count'))
         self.assertThat(account_info[1], matchers.IsInstance(list))
         self.assertThat(len(account_info[1]), matchers.Is(0))
Esempio n. 26
0
    def test_collect_gcore(self):

        gcore_md = gcore.Collector(client=self.client).collect()
        self.assertThat(gcore_md, matchers.IsInstance(list))
        self.assertEqual('gcore', gcore_md[0][0])
        gcore_md = gcore_md[0][1]

        for k in ('int1', 'strfoo', 'map_ab'):
            self.assertIn(k, gcore_md)
            self.assertEqual(gcore_md[k], META_DATA[k])

        # level setting for urllib3.connectionpool.
        self.assertTrue(self.log.output == '' or self.log.output
                        == 'Starting new HTTP connection (1): 192.0.2.1\n')
Esempio n. 27
0
 def test_collect_zaqar_deployments(self, mock_url_for, mock___init__):
     mock___init__.return_value = None
     mock_url_for.return_value = cfg.CONF.zaqar.auth_url
     zaqar_md = zaqar.Collector(
         keystoneclient=FakeKeystoneClient(self, cfg.CONF.zaqar),
         zaqarclient=FakeZaqarClientSoftwareConfig(self),
         discover_class=test_heat.FakeKeystoneDiscover).collect()
     self.assertThat(zaqar_md, matchers.IsInstance(list))
     self.assertEqual('zaqar', zaqar_md[0][0])
     self.assertEqual(2, len(zaqar_md))
     self.assertEqual('zaqar', zaqar_md[0][0])
     self.assertEqual(test_heat.SOFTWARE_CONFIG_DATA['deployments'],
                      zaqar_md[0][1]['deployments'])
     self.assertEqual(('dep-name1', {'config1': 'value1'}), zaqar_md[1])
Esempio n. 28
0
    def test_collect_heat(self, mock_url_for, mock___init__):
        mock___init__.return_value = None
        mock_url_for.return_value = cfg.CONF.heat.auth_url
        heat_md = heat.Collector(keystoneclient=FakeKeystoneClient(self),
                                 heatclient=FakeHeatClient(self)).collect()
        self.assertThat(heat_md, matchers.IsInstance(list))
        self.assertEqual('heat', heat_md[0][0])
        heat_md = heat_md[0][1]

        for k in ('int1', 'strfoo', 'map_ab'):
            self.assertIn(k, heat_md)
            self.assertEqual(heat_md[k], META_DATA[k])

        # FIXME(yanyanhu): Temporary hack to deal with possible log
        # level setting for urllib3.connectionpool.
        self.assertTrue(self.log.output == '' or self.log.output
                        == 'Starting new HTTP connection (1): 127.0.0.1\n')
Esempio n. 29
0
    def test_collect_heat(self):
        heat_md = heat.Collector(
            keystoneclient=FakeKeystoneClient(self),
            heatclient=FakeHeatClient(self),
            discover_class=FakeKeystoneDiscover).collect()
        self.assertThat(heat_md, matchers.IsInstance(list))
        self.assertEqual('heat', heat_md[0][0])
        heat_md = heat_md[0][1]

        for k in ('int1', 'strfoo', 'map_ab'):
            self.assertIn(k, heat_md)
            self.assertEqual(heat_md[k], META_DATA[k])

        # FIXME(yanyanhu): Temporary hack to deal with possible log
        # level setting for urllib3.connectionpool.
        self.assertTrue(self.log.output == '' or self.log.output
                        == 'Starting new HTTP connection (1): 192.0.2.1\n')
Esempio n. 30
0
    def test_collect_zaqar_websocket_recv(self, mock_url_for, mock___init__,
                                          mock_transport):
        mock___init__.return_value = None
        mock_url_for.return_value = cfg.CONF.zaqar.auth_url
        ws = FakeZaqarWebsocketClient({}, messages={}, testcase=self)
        mock_transport.return_value = ws
        conf = config_fixture.Config()
        self.useFixture(conf)
        conf.config(group='zaqar', use_websockets=True)
        zaqar_md = zaqar.Collector(keystoneclient=FakeKeystoneClientWebsocket(
            self, cfg.CONF.zaqar), ).collect()
        self.assertThat(zaqar_md, matchers.IsInstance(list))
        self.assertEqual('zaqar', zaqar_md[0][0])
        zaqar_md = zaqar_md[0][1]

        for k in ('int1', 'strfoo', 'map_ab'):
            self.assertIn(k, zaqar_md)
            self.assertEqual(zaqar_md[k], test_heat.META_DATA[k])