Exemplo n.º 1
0
    def test_ceres_finder(self):
        test_dir = join(settings.CERES_DIR)

        def create_ceres(metric):
            if not isdir(test_dir):
                os.makedirs(test_dir)
            tree = ceres.CeresTree(test_dir)

            options = {}
            tree.createNode(metric, **options)

            tree.store(metric, [(1, 60)])

        def wipe_ceres():
            try:
                shutil.rmtree(test_dir)
            except OSError:
                pass

        self.addCleanup(wipe_ceres)

        create_ceres('foo')
        create_ceres('foo.bar.baz')
        create_ceres('bar.baz.foo')

        finder = get_finders('graphite.finders.ceres.CeresFinder')[0]

        nodes = finder.find_nodes(FindQuery('foo', None, None))
        self.assertEqual(len(list(nodes)), 1)

        nodes = finder.find_nodes(FindQuery('foo.bar.baz', None, None))
        self.assertEqual(len(list(nodes)), 1)

        # No data in the expected time period
        nodes = finder.find_nodes(FindQuery('foo.bar.baz', 10000, 10060))
        self.assertEqual(len(list(nodes)), 0)

        nodes = finder.find_nodes(FindQuery('foo.bar', None, None))
        self.assertEqual(len(list(nodes)), 1)

        nodes = finder.find_nodes(FindQuery('*.ba?.{baz,foo}', None, None))
        self.assertEqual(len(list(nodes)), 2)

        # Search for something that isn't valid Ceres content
        fh = open(join(test_dir, 'foo', 'blah'), 'wb')
        fh.close()
        nodes = finder.find_nodes(FindQuery('foo.blah', None, None))
        self.assertEqual(len(list(nodes)), 0)

        # get index
        result = finder.get_index({})
        self.assertEqual(result, ['bar.baz.foo', 'foo', 'foo.bar.baz'])
Exemplo n.º 2
0
    def test_fetch(self):
        disabled_finder = get_finders('tests.test_storage.DisabledFinder')[0]
        legacy_finder = get_finders('tests.test_storage.LegacyFinder')[0]
        test_finder = get_finders('tests.test_storage.TestFinder')[0]
        remote_finder = get_finders('tests.test_storage.RemoteFinder')[0]

        store = Store(
            finders=[
                disabled_finder, legacy_finder, test_finder, remote_finder
            ],
            tagdb=get_tagdb('graphite.tags.localdatabase.LocalDatabaseTagDB'))

        # tagb is properly initialized
        self.assertIsInstance(store.tagdb, LocalDatabaseTagDB)

        # get all enabled finders
        finders = store.get_finders()
        self.assertEqual(list(finders),
                         [legacy_finder, test_finder, remote_finder])

        # get only local finders
        finders = store.get_finders(local=True)
        self.assertEqual(list(finders), [legacy_finder, test_finder])

        # fetch with empty patterns
        result = store.fetch([], 1, 2, 3, {})
        self.assertEqual(result, [])

        # fetch
        result = store.fetch(['a.**'], 1, 2, 3, {})
        self.assertEqual(len(result), 3)
        result.sort(key=lambda node: node['name'])
        self.assertEqual(result[0]['name'], 'a.b.c.d')
        self.assertEqual(result[0]['pathExpression'], 'a.**')
        self.assertEqual(result[1]['name'], 'a.b.c.d')
        self.assertEqual(result[1]['pathExpression'], 'a.**')
        self.assertEqual(result[2]['name'], 'a.b.c.e')
        self.assertEqual(result[2]['pathExpression'], 'a.**')
Exemplo n.º 3
0
    def test_legacy_finder(self):
        store = Store(finders=get_finders('tests.test_finders.LegacyFinder'))
        nodes = list(store.find("foo"))
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].path, 'foo')

        nodes = list(store.find('bar.*'))
        self.assertEqual(len(nodes), 10)
        node = nodes[0]
        self.assertEqual(node.path.split('.')[0], 'bar')

        time_info, series = node.fetch(100, 200)
        self.assertEqual(time_info, (100, 200, 10))
        self.assertEqual(len(series), 10)
Exemplo n.º 4
0
    def test_legacy_finder(self):
        store = Store(finders=get_finders('tests.test_finders.LegacyFinder'))
        nodes = list(store.find("foo"))
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].path, 'foo')

        nodes = list(store.find('bar.*'))
        self.assertEqual(len(nodes), 10)
        node = nodes[0]
        self.assertEqual(node.path.split('.')[0], 'bar')

        time_info, series = node.fetch(100, 200)
        self.assertEqual(time_info, (100, 200, 10))
        self.assertEqual(len(series), 10)
Exemplo n.º 5
0
  def test_fetch(self):
    disabled_finder = get_finders('tests.test_storage.DisabledFinder')[0]
    legacy_finder = get_finders('tests.test_storage.LegacyFinder')[0]
    test_finder = get_finders('tests.test_storage.TestFinder')[0]
    remote_finder = get_finders('tests.test_storage.RemoteFinder')[0]

    store = Store(
      finders=[disabled_finder, legacy_finder, test_finder, remote_finder],
      tagdb=get_tagdb('graphite.tags.localdatabase.LocalDatabaseTagDB')
    )

    # tagb is properly initialized
    self.assertIsInstance(store.tagdb, LocalDatabaseTagDB)

    # get all enabled finders
    finders = store.get_finders()
    self.assertEqual(list(finders), [legacy_finder, test_finder, remote_finder])

    # get only local finders
    finders = store.get_finders(local=True)
    self.assertEqual(list(finders), [legacy_finder, test_finder])

    # fetch with empty patterns
    result = store.fetch([], 1, 2, 3, {})
    self.assertEqual(result, [])

    # fetch
    result = store.fetch(['a.**'], 1, 2, 3, {})
    self.assertEqual(len(result), 3)
    result.sort(key=lambda node: node['name'])
    self.assertEqual(result[0]['name'], 'a.b.c.d')
    self.assertEqual(result[0]['pathExpression'], 'a.**')
    self.assertEqual(result[1]['name'], 'a.b.c.d')
    self.assertEqual(result[1]['pathExpression'], 'a.**')
    self.assertEqual(result[2]['name'], 'a.b.c.e')
    self.assertEqual(result[2]['pathExpression'], 'a.**')
Exemplo n.º 6
0
    def test_multiple_globstars(self):
        self.addCleanup(self.wipe_whisper)
        store  = Store(finders=get_finders('graphite.finders.standard.StandardFinder'))

        query = "x.**.x.**.x"
        hits = ["x.x.x", "x._.x.x", "x.x._.x", "x._.x._.x", "x._._.x.x", "x.x._._.x"]
        misses = ["x.o.x", "o.x.x", "x.x.o", "o.x.x.x", "x.x.x.o", "o._.x._.x", "x._.o._.x", "x._.x._.o"]
        for path in hits + misses:
            file = join(path.replace(".", os.sep)) + ".wsp"
            self.create_whisper(file)

        paths = [node.path for node in store.find(query, local=True)]
        for hit in hits:
            self.assertIn(hit, paths)
        for miss in misses:
            self.assertNotIn(miss, paths)
Exemplo n.º 7
0
    def test_multiple_globstars(self):
        self.addCleanup(self.wipe_whisper)
        store  = Store(finders=get_finders('graphite.finders.standard.StandardFinder'))

        query = "x.**.x.**.x"
        hits = ["x.x.x", "x._.x.x", "x.x._.x", "x._.x._.x", "x._._.x.x", "x.x._._.x"]
        misses = ["x.o.x", "o.x.x", "x.x.o", "o.x.x.x", "x.x.x.o", "o._.x._.x", "x._.o._.x", "x._.x._.o"]
        for path in hits + misses:
            file = join(path.replace(".", os.sep)) + ".wsp"
            self.create_whisper(file)

        paths = [node.path for node in store.find(query, local=True)]
        for hit in hits:
            self.assertIn(hit, paths)
        for miss in misses:
            self.assertNotIn(miss, paths)
Exemplo n.º 8
0
    def test_standard_finder_tagged_whisper_carbonlink(self):
        try:
            self.create_whisper(join(
              '_tagged',
              'b34',
              '2de',
              # foo;bar=baz2
              'b342defa10cb579981c63ef78be5ac248f681f4bd2c35bc0209d3a7b9eb99346.wsp'
            ))

            finder = get_finders('graphite.finders.standard.StandardFinder')[0]

            nodes = list(finder.find_nodes(FindQuery('foo;bar=baz2', None, None)))
            self.assertEqual(len(nodes), 1)
            self.assertEqual(nodes[0].reader.real_metric_path, 'foo;bar=baz2')

        finally:
            self.wipe_whisper()
Exemplo n.º 9
0
  def test_get_index(self):
    disabled_finder = DisabledFinder()
    # use get_finders so legacy_finder is patched with get_index
    legacy_finder = get_finders('tests.test_storage.LegacyFinder')[0]
    test_finder = TestFinder()
    remote_finder = RemoteFinder()

    store = Store(
      finders=[disabled_finder, legacy_finder, test_finder, remote_finder],
      tagdb=get_tagdb('graphite.tags.localdatabase.LocalDatabaseTagDB')
    )

    # get index
    result = store.get_index()
    self.assertEqual(result, ['a.b.c.d', 'a.b.c.e'])

    # get local index
    result = store.get_index({'localOnly': True})
    self.assertEqual(result, ['a.b.c.d'])
Exemplo n.º 10
0
    def test_get_index(self):
        disabled_finder = DisabledFinder()
        # use get_finders so legacy_finder is patched with get_index
        legacy_finder = get_finders('tests.test_storage.LegacyFinder')[0]
        test_finder = TestFinder()
        remote_finder = RemoteFinder()

        store = Store(finders=[
            disabled_finder, legacy_finder, test_finder, remote_finder
        ],
                      tagdb='graphite.tags.localdatabase.LocalDatabaseTagDB')

        # get index
        result = store.get_index()
        self.assertEqual(result, ['a.b.c.d', 'a.b.c.e'])

        # get local index
        result = store.get_index({'localOnly': True})
        self.assertEqual(result, ['a.b.c.d'])
Exemplo n.º 11
0
    def test_standard_finder_gzipped_whisper(self, scandir_mock):
        try:
            self.create_whisper('foo.wsp', True)
            self.create_whisper(join('foo', 'bar', 'baz.wsp'), True)
            self.create_whisper(join('bar', 'baz', 'foo.wsp'))
            finder = get_finders('graphite.finders.standard.StandardFinder')[0]

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo', None, None))
            self.assertEqual(len(list(nodes)), 2)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo{}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

        finally:
            scandir_mock.call_count = 0
            self.wipe_whisper()
Exemplo n.º 12
0
    def test_standard_finder_gzipped_whisper(self, scandir_mock):
        try:
            self.create_whisper('foo.wsp', True)
            self.create_whisper(join('foo', 'bar', 'baz.wsp'), True)
            self.create_whisper(join('bar', 'baz', 'foo.wsp'))
            finder = get_finders('graphite.finders.standard.StandardFinder')[0]

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo', None, None))
            self.assertEqual(len(list(nodes)), 2)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo{}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 1)

        finally:
            scandir_mock.call_count = 0
            self.wipe_whisper()
Exemplo n.º 13
0
    def test_standard_finder(self,scandir_mock):
        try:
            self.create_whisper('foo.wsp')
            self.create_whisper(join('foo', 'bar', 'baz.wsp'))
            self.create_whisper(join('bar', 'baz', 'foo.wsp'))
            self.create_whisper(join('_tagged', '9c6', '79b', 'foo;bar=baz.wsp'))
            self.create_whisper(join(
              '_tagged',
              'b34',
              '2de',
              # foo;bar=baz2
              'b342defa10cb579981c63ef78be5ac248f681f4bd2c35bc0209d3a7b9eb99346.wsp'
            ))

            finder = get_finders('graphite.finders.standard.StandardFinder')[0]

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo', None, None))
            self.assertEqual(len(list(nodes)), 2)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('*.ba?.{baz,foo}', None, None))
            self.assertEqual(len(list(nodes)), 2)
            self.assertEqual(scandir_mock.call_count, 4)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('{foo,bar}.{baz,bar}.{baz,foo}', None, None))
            self.assertEqual(len(list(nodes)), 2)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('{foo}.bar.*', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 1)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo.{ba{r,z},baz}.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('{foo,garbage}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('{fo{o}}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo{}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('{fo,ba}{o}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('{fo,ba}{o,o}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('{fo,ba}{o,z}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo;bar=baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo;bar=baz2', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            results = finder.fetch(['foo'], 0, 1)
            self.assertEqual(results, [])

        finally:
            scandir_mock.call_count = 0
            self.wipe_whisper()
Exemplo n.º 14
0
    def test_standard_finder(self, scandir_mock):
        try:
            self.create_whisper('foo.wsp')
            self.create_whisper(join('foo', 'bar', 'baz.wsp'))
            self.create_whisper(join('bar', 'baz', 'foo.wsp'))
            finder = get_finders('graphite.finders.standard.StandardFinder')[0]

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo', None, None))
            self.assertEqual(len(list(nodes)), 2)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('*.ba?.{baz,foo}', None, None))
            self.assertEqual(len(list(nodes)), 2)
            self.assertEqual(scandir_mock.call_count, 5)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(
                FindQuery('{foo,bar}.{baz,bar}.{baz,foo}', None, None))
            self.assertEqual(len(list(nodes)), 2)
            self.assertEqual(scandir_mock.call_count, 5)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('{foo}.bar.*', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 2)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(
                FindQuery('foo.{ba{r,z},baz}.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 1)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(
                FindQuery('{foo,garbage}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 1)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('{fo{o}}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 1)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo{}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 1)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(
                FindQuery('{fo,ba}{o}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 1)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(
                FindQuery('{fo,ba}{o,o}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 1)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(
                FindQuery('{fo,ba}{o,z}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 1)

            results = finder.fetch(['foo'], 0, 1)
            self.assertEqual(results, [])

        finally:
            scandir_mock.call_count = 0
            self.wipe_whisper()
Exemplo n.º 15
0
    def test_ceres_finder(self):
        test_dir = join(settings.CERES_DIR)

        def create_ceres(metric):
            if not isdir(test_dir):
                os.makedirs(test_dir)
            tree = ceres.CeresTree(test_dir)

            options = {}
            tree.createNode(metric, **options)

            tree.store(metric, [(1, 60)])

        def wipe_ceres():
            try:
                shutil.rmtree(test_dir)
            except OSError:
                pass

        self.addCleanup(wipe_ceres)

        create_ceres('foo')
        create_ceres('foo.bar.baz')
        create_ceres('bar.baz.foo')
        create_ceres(
            # foo;bar=baz
            '_tagged.9c6.79b.foo;bar=baz'
        )
        create_ceres(
            # foo;bar=baz2
            '_tagged.b34.2de.b342defa10cb579981c63ef78be5ac248f681f4bd2c35bc0209d3a7b9eb99346'
        )

        finder = get_finders('graphite.finders.ceres.CeresFinder')[0]

        nodes = finder.find_nodes(FindQuery('foo', None, None))
        self.assertEqual(len(list(nodes)), 1)

        nodes = finder.find_nodes(FindQuery('foo.bar.baz', None, None))
        self.assertEqual(len(list(nodes)), 1)

        # No data in the expected time period
        nodes = finder.find_nodes(FindQuery('foo.bar.baz', 10000, 10060))
        self.assertEqual(len(list(nodes)), 0)

        nodes = finder.find_nodes(FindQuery('foo.bar', None, None))
        self.assertEqual(len(list(nodes)), 1)

        nodes = finder.find_nodes(FindQuery('*.ba?.{baz,foo}', None, None))
        self.assertEqual(len(list(nodes)), 2)

        nodes = finder.find_nodes(FindQuery('{bar,foo}.{bar,baz}.{baz,foo}', None, None))
        self.assertEqual(len(list(nodes)), 2)

        nodes = finder.find_nodes(FindQuery('foo;bar=baz', None, None))
        self.assertEqual(len(list(nodes)), 1)

        nodes = finder.find_nodes(FindQuery('foo;bar=baz2', None, None))
        self.assertEqual(len(list(nodes)), 1)

        # Search for something that isn't valid Ceres content
        fh = open(join(test_dir, 'foo', 'blah'), 'wb')
        fh.close()
        nodes = finder.find_nodes(FindQuery('foo.blah', None, None))
        self.assertEqual(len(list(nodes)), 0)

        # get index
        result = finder.get_index({})
        self.assertEqual(result, [
          '_tagged.9c6.79b.foo;bar=baz',
          '_tagged.b34.2de.b342defa10cb579981c63ef78be5ac248f681f4bd2c35bc0209d3a7b9eb99346',
          'bar.baz.foo',
          'foo',
          'foo.bar.baz',
        ])
Exemplo n.º 16
0
    def test_standard_finder(self, scandir_mock):
        try:
            self.create_whisper('foo.wsp')
            self.create_whisper(join('foo', 'bar', 'baz.wsp'))
            self.create_whisper(join('bar', 'baz', 'foo.wsp'))
            self.create_whisper(
                join('_tagged', '9c6', '79b', 'foo;bar=baz.wsp'))
            self.create_whisper(
                join(
                    '_tagged',
                    'b34',
                    '2de',
                    # foo;bar=baz2
                    'b342defa10cb579981c63ef78be5ac248f681f4bd2c35bc0209d3a7b9eb99346.wsp'
                ))

            finder = get_finders('graphite.finders.standard.StandardFinder')[0]

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo', None, None))
            self.assertEqual(len(list(nodes)), 2)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('*.ba?.{baz,foo}', None, None))
            self.assertEqual(len(list(nodes)), 2)
            self.assertEqual(scandir_mock.call_count, 4)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(
                FindQuery('{foo,bar}.{baz,bar}.{baz,foo}', None, None))
            self.assertEqual(len(list(nodes)), 2)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('{foo}.bar.*', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 1)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(
                FindQuery('foo.{ba{r,z},baz}.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(
                FindQuery('{foo,garbage}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('{fo{o}}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo{}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(
                FindQuery('{fo,ba}{o}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(
                FindQuery('{fo,ba}{o,o}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(
                FindQuery('{fo,ba}{o,z}.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo;bar=baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            scandir_mock.call_count = 0
            nodes = finder.find_nodes(FindQuery('foo;bar=baz2', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(scandir_mock.call_count, 0)

            results = finder.fetch(['foo'], 0, 1)
            self.assertEqual(results, [])

        finally:
            scandir_mock.call_count = 0
            self.wipe_whisper()
Exemplo n.º 17
0
    def test_ceres_finder(self):
        test_dir = join(settings.CERES_DIR)

        def create_ceres(metric):
            if not isdir(test_dir):
                os.makedirs(test_dir)
            tree = ceres.CeresTree(test_dir)

            options = {}
            tree.createNode(metric, **options)

            tree.store(metric, [(1, 60)])

        def wipe_ceres():
            try:
                shutil.rmtree(test_dir)
            except OSError:
                pass

        def listdir_mock(d):
            self._listdir_counter += 1
            return self._original_listdir(d)

        try:
            os.listdir = listdir_mock
            create_ceres('foo')
            create_ceres('foo.bar.baz')
            create_ceres('bar.baz.foo')

            finder = get_finders('graphite.finders.ceres.CeresFinder')[0]

            self._listdir_counter = 0
            nodes = finder.find_nodes(FindQuery('foo', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(self._listdir_counter, 1)

            self._listdir_counter = 0
            nodes = finder.find_nodes(FindQuery('foo.bar.baz', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(self._listdir_counter, 1)

            # No data in the expected time period
            self._listdir_counter = 0
            nodes = finder.find_nodes(FindQuery('foo.bar.baz', 10000, 10060))
            self.assertEqual(len(list(nodes)), 0)
            self.assertEqual(self._listdir_counter, 1)

            self._listdir_counter = 0
            nodes = finder.find_nodes(FindQuery('foo.bar', None, None))
            self.assertEqual(len(list(nodes)), 1)
            self.assertEqual(self._listdir_counter, 0)

            self._listdir_counter = 0
            nodes = finder.find_nodes(FindQuery('*.ba?.{baz,foo}', None, None))
            self.assertEqual(len(list(nodes)), 2)
            self.assertEqual(self._listdir_counter, 8)

            # Search for something that isn't valid Ceres content
            fh = open(join(test_dir, 'foo', 'blah'), 'wb')
            fh.close()
            self._listdir_counter = 0
            nodes = finder.find_nodes(FindQuery('foo.blah', None, None))
            self.assertEqual(len(list(nodes)), 0)
            self.assertEqual(self._listdir_counter, 0)

        finally:
            os.listdir = self._original_listdir
            wipe_ceres()