예제 #1
0
    def _databroker():
        mongo_box = MongoBox()
        try:
            mongo_box.start()
            mongo_client = mongo_box.client()
            mongo_host, mongo_port = mongo_client.address
            mongo_uri = f"mongodb://{mongo_host}:{mongo_port}"
            catalog_descriptor_path = tmp_path / Path("mad.yml")
            with open(catalog_descriptor_path, "w") as f:
                f.write(f"""\
sources:
  mad:
    description: Made up beamline
    driver: "bluesky-mongo-normalized-catalog"
    container: catalog
    args:
      metadatastore_db: {mongo_uri}
      asset_registry_db: {mongo_uri}
      handler_registry:
        NPY_SEQ: ophyd.sim.NumpySeqHandler
    metadata:
      beamline: "00-ID"
""")

            yield intake.open_catalog(catalog_descriptor_path)
        finally:
            mongo_box.stop()
예제 #2
0
    def test_keep_db_path(self):
        db_path = tempfile.mkdtemp()
        box = MongoBox(db_path=db_path)
        box.start()
        box.stop()

        self.assertTrue(os.path.exists(db_path))
        shutil.rmtree(db_path)
예제 #3
0
def mongo_db():
    db_path = tempfile.mkdtemp(dir="/dev/shm")
    mongobox = MongoBox(db_path=db_path)
    port_envvar = DEFAULT_PORT_ENVVAR

    mongobox.start()
    os.environ[port_envvar] = str(mongobox.port)

    yield mongobox

    mongobox.stop()
    del os.environ[port_envvar]
    shutil.rmtree(db_path)
예제 #4
0
    def test_replset(self):
        box = MongoBox(replset="repl0")
        box.start()

        client = box.client()
        
        # initiate the replSet
        config = {'_id': 'repl0', 'members':
            [
                {'_id': 0, 'host': '%s:%s' %('127.0.0.1', box.port)}
            ]
                  }

        client.admin.command("replSetInitiate", config)
        setconfig = client.admin.command("replSetGetConfig")
        self.assertEqual(setconfig['config']['_id'], 'repl0')
예제 #5
0
class TestAuthorization(unittest.TestCase):

    dummy_user = {
        'username': '******',
        'password': '******',
        'rights': ['bb', 'of']
    }

    def setUp(self):

        self.box = MongoBox()
        self.box.start()
        db = self.box.client().matomat
        db.users.insert(self.dummy_user)

        self.sut = Authorization(db, MicroMock.get_log_mock())

    def tearDown(self):
        self.box.stop()

    def test_should_set_logged_in_user(self):
        self.assertTrue(self.sut.login('DummyUser', 'dummypassword'))
        self.assertTrue(self.dummy_user == self.sut.currentUser)

    def test_should_not_login_wrong_username(self):
        self.assertFalse(self.sut.login('wrongUser', 'dummypassword'))
        self.assertTrue(self.sut.currentUser is None)

    def test_should_not_login_wrong_password(self):
        self.assertFalse(self.sut.login('DummyUser', 'wrongPassword'))
        self.assertTrue(self.sut.currentUser is None)

    def test_sould_return_true_on_has_right(self):
        self.sut.login('DummyUser', 'dummypassword')
        self.assertTrue(self.sut.user_has_right('bb'))

    def test_sould_return_false_on_has_not_right(self):
        self.sut.login('DummyUser', 'dummypassword')
        self.assertFalse(self.sut.user_has_right('omg'))

    def test_main_menu_should_not_contain_prohibited_Entries(self):
        menu = self.sut.create_main_menu()
        self.assertTrue(len(menu) == 1 and menu[0].key == MenuKey.quit)

    def test_main_menu_should_contain_quit(self):
        menu = self.sut.create_main_menu()
        self.assertTrue(any(item.key == MenuKey.quit for item in menu))
예제 #6
0
class TestLoadJSON(unittest.TestCase):
    def setUp(self):
        self.box = MongoBox()
        self.box.start()
        self.coll = get_collection('db1', 'coll1',
                                   ('localhost', self.box.port))

    def test_get_collection_returns_mongo_collection(self):
        self.assertEqual(self.coll.count(), 0)

    def test_insert_adds_request_to_collection(self):
        req = {'Tumblesniff': 'Bogwort'}
        insert(self.coll, req)
        self.assertEqual(self.coll.find_one(), req)

    def tearDown(self):
        self.box.stop()
class TestLoadJSON(unittest.TestCase):

    def setUp(self):
        self.box = MongoBox()
        self.box.start()
        self.coll = get_collection('db1', 'coll1', ('localhost', self.box.port))

    def test_get_collection_returns_mongo_collection(self):
        self.assertEqual(self.coll.count(), 0)

    def test_insert_adds_request_to_collection(self):
        req = {'Tumblesniff': 'Bogwort'}
        insert(self.coll, req)
        self.assertEqual(self.coll.find_one(), req)

    def tearDown(self):
        self.box.stop()
예제 #8
0
    def test_can_run_mongo(self):
        box = MongoBox()
        box.start()

        db_path = box.db_path

        self.assertTrue(box.running())
        self.assertIsNotNone(box.port)

        client = box.client()
        self.assertTrue(client.alive())

        box.stop()

        self.assertFalse(box.running())
        self.assertFalse(client.alive())
        self.assertFalse(os.path.exists(db_path))
예제 #9
0
    def test_replset(self):
        box = MongoBox(replset="repl0")
        box.start()

        client = box.client()

        # initiate the replSet
        config = {
            '_id': 'repl0',
            'members': [{
                '_id': 0,
                'host': '%s:%s' % ('127.0.0.1', box.port)
            }]
        }

        client.admin.command("replSetInitiate", config)
        setconfig = client.admin.command("replSetGetConfig")
        self.assertEqual(setconfig['config']['_id'], 'repl0')
예제 #10
0
 def test_auth(self):
     box = MongoBox(auth=True)
     box.start()
     
     client = box.client()
     client['admin'].add_user('foo','bar')
     self.assertRaises(OperationFailure, client['test'].add_user, 'test','test')
     client['admin'].authenticate('foo', 'bar')
     try:
         client['test'].add_user('test','test')
     except OperationFailure:
         self.fail("add_user() operation unexpectedly failed")
     
     client = box.client()
     self.assertRaises(OperationFailure, client['test'].collection_names)
     client['admin'].authenticate('foo', 'bar')
     try:
         client['test'].collection_names()
     except OperationFailure:
         self.fail("collection_names() operation unexpectedly failed")
예제 #11
0
    def test_auth(self):
        box = MongoBox(auth=True)
        box.start()

        client = box.client()
        client['admin'].add_user('foo', 'bar')
        self.assertRaises(OperationFailure, client['test'].add_user, 'test', 'test')
        client['admin'].authenticate('foo', 'bar')

        try:
            client['test'].add_user('test', 'test')
        except OperationFailure:
            self.fail("add_user() operation unexpectedly failed")

        client = box.client()
        self.assertRaises(OperationFailure, client['test'].collection_names)
        client['admin'].authenticate('foo', 'bar')
        try:
            client['test'].collection_names()
        except OperationFailure:
            self.fail("collection_names() operation unexpectedly failed")
예제 #12
0
    def test_can_run_mongo(self):
        box = MongoBox()
        box.start()

        db_path = box.db_path

        self.assertTrue(box.running())
        self.assertIsNotNone(box.port)

        client = box.client()
        try:
            client.list_databases()
        except:
            self.fail('Cannot list databases')

        box.stop()

        self.assertFalse(box.running())
        self.assertFalse(os.path.exists(db_path))

        with self.assertRaises(AutoReconnect):
            client.list_databases()
예제 #13
0
class MongoTemporaryInstance(object):

    _instance = None

    @classmethod
    def get_instance(cls):
        if cls._instance is None:
            cls._instance = cls()
            atexit.register(cls._instance.shutdown)

        return cls._instance

    def __init__(self):
        self._box = MongoBox()
        self._box.start()

    @property
    def conn(self):
        return self._box.client

    def shutdown(self):  # pragma: no cover
        self._box.stop()
예제 #14
0
class MongoTemporaryInstance(object):

    _instance = None

    @classmethod
    def get_instance(cls):
        if cls._instance is None:
            cls._instance = cls()
            atexit.register(cls._instance.shutdown)

        return cls._instance

    def __init__(self):
        self._box = MongoBox()
        self._box.start()

    @property
    def conn(self):
        return self._box.client

    def shutdown(self):  # pragma: no cover
        self._box.stop()
예제 #15
0
    def test_can_run_mongo(self):
        box = MongoBox()
        box.start()

        db_path = box.db_path

        self.assertTrue(box.running())
        self.assertIsNotNone(box.port)

        client = box.client()
        try:
            client.list_databases()
        except:
            self.fail('Cannot list databases')

        box.stop()

        self.assertFalse(box.running())
        self.assertFalse(os.path.exists(db_path))

        with self.assertRaises(AutoReconnect):
            client.list_databases()
예제 #16
0
    def test_auth(self):
        box = MongoBox(auth=True)
        box.start()

        client = box.client()
        client['admin'].command('createUser', 'foo', pwd='bar', roles=['root'])
        with self.assertRaises(OperationFailure):
            client['test'].command('createUser', 'test', pwd='test', roles=[])
        client['admin'].authenticate('foo', 'bar')

        try:
            client['test'].command('createUser', 'test', pwd='test', roles=[])
        except OperationFailure:
            self.fail("createUser() operation unexpectedly failed")

        client = box.client()
        self.assertRaises(OperationFailure, client['test'].collection_names)
        client['admin'].authenticate('foo', 'bar')
        try:
            client['test'].collection_names()
        except OperationFailure:
            self.fail("collection_names() operation unexpectedly failed")

        box.stop()
예제 #17
0
def run(args=None):
    print('\nStarting mongo servers')
    args = args or []
    db_path, db_path_auth = _get_mongo_paths()
    params = dict(mongod_bin='mongod', db_path=db_path, port=MONGO_PORT)
    mongobox = MongoBox(**params)
    params.update(dict(auth=True, db_path=db_path_auth, port=MONGO_AUTH_PORT))
    mongoboxAuth = MongoBox(**params)
    status = statusAuth = False
    try:
        # start server
        print(' - server...', end=' '), sys.stdout.flush()
        mongobox.start()
        status = True
        print('OK')

        # start auth server
        print(' - server auth...', end=' '), sys.stdout.flush()
        mongoboxAuth.start()
        print('OK')
        statusAuth = True
        _create_admin()
        print('\n'), sys.stdout.flush()

        pytest.main(*args)
    finally:
        # stop servers
        if status:
            print('\n\nStoping mongo servers:')
            print(' - server...', end=' '), sys.stdout.flush()
            mongobox.stop()
            print('OK')
            if statusAuth:
                print(' - server auth...', end=' '), sys.stdout.flush()
                mongoboxAuth.stop()
                print('OK')
예제 #18
0
    def test_auth(self):
        box = MongoBox(auth=True)
        box.start()

        client = box.client()
        client['admin'].command('createUser', 'foo', pwd='bar', roles=['root'])
        with self.assertRaises(OperationFailure):
            client['test'].command('createUser', 'test', pwd='test', roles=[])
        client['admin'].authenticate('foo', 'bar')

        try:
            client['test'].command('createUser', 'test', pwd='test', roles=[])
        except OperationFailure:
            self.fail("createUser() operation unexpectedly failed")

        client = box.client()
        self.assertRaises(OperationFailure, client['test'].collection_names)
        client['admin'].authenticate('foo', 'bar')
        try:
            client['test'].collection_names()
        except OperationFailure:
            self.fail("collection_names() operation unexpectedly failed")

        box.stop()
class TestSummarize(unittest.TestCase):

    def setUp(self):
        self.box = MongoBox()
        self.box.start()
        self.req = self.box.client().db1.req
        self.sum = self.box.client().db1.sum

    def test_add_summary_data_summarizes_data(self):
        self.req.insert([
                        {'handle': 'TEH_FOO'},
                        {'handle': 'TEH_FOO'},
                        {'handle': 'TEH_BAR'}])
        summarize.add_summary_data(self.req, self.sum)
        self.assertEqual(self.sum.find_one(),
                         {'_id': 'Overall', 'type': 'overall', 'size': 2, 'downloads': 3})

    def test_add_summary_map_summarizes_country_data(self):
        self.req.insert([
                        {'country': 'RUS'},
                        {'country': 'RUS'},
                        {'country': 'USA'}])
        summarize.add_summary_map(self.req, self.sum)
        self.assertEqual(self.sum.find_one({'_id': 'Overall'}),
                         {'_id': 'Overall', 'countries': [
                            {'country': 'RUS', 'downloads': 2},
                            {'country': 'USA', 'downloads': 1}
                         ]})

    def test_add_summary_date_summarizes_date_data(self):
        self.req.insert([
                        {'time': datetime(2006, 6, 6)},
                        {'time': datetime(2006, 6, 6)},
                        {'time': datetime(1955, 11, 5)}])
        summarize.add_summary_date(self.req, self.sum)
        self.assertEqual(self.sum.find_one({'_id': 'Overall'}),
                         {'_id': 'Overall', 'dates': [
                            {'date': '1955-11-05', 'downloads': 1},
                            {'date': '2006-06-06', 'downloads': 2}
                         ]})

    def test_add_field_summary_date_summarizes_date(self):
        self.req.insert([
                        {'author': 'Muffincup', 'time': datetime(2006,6,6)},
                        {'author': 'Muffincup', 'time': datetime(2006,6,6)},
                        {'author': "Sir Cup'ncake", 'time': datetime(1955,11,5)},
                        {'author': "Sir Cup'ncake", 'time': datetime(1999,12,31)}])
        summarize.add_field_summary_date(self.req, self.sum, 'author')
        self.assertEqual(self.sum.find_one({'_id': 'Muffincup'}),
                         {'_id': 'Muffincup', 'dates': [
                            {'date': '2006-06-06', 'downloads': 2}
                         ]})
        self.assertEqual(self.sum.find_one({'_id': "Sir Cup'ncake"}),
                         {'_id': "Sir Cup'ncake", 'dates': [
                            {'date': '1955-11-05', 'downloads': 1},
                            {'date': '1999-12-31', 'downloads': 1}
                         ]})

    def test_add_field_summary_map_summarizes_country(self):
        self.req.insert([
                        {'author': 'Muffincup', 'country': 'USA'},
                        {'author': 'Muffincup', 'country': 'USA'},
                        {'author': 'Tinsletoon', 'country': 'GER'},
                        {'author': 'Tinsletoon', 'country': 'FIN'},])
        summarize.add_field_summary_map(self.req, self.sum, 'author')
        self.assertEqual(self.sum.find_one({'_id': 'Muffincup'}),
                         {'_id': 'Muffincup', 'countries': [
                            {'country': 'USA', 'downloads': 2}
                         ]})
        self.assertEqual(self.sum.find_one({'_id': 'Tinsletoon'}),
                         {'_id': 'Tinsletoon', 'countries': [
                            {'country': 'FIN', 'downloads': 1},
                            {'country': 'GER', 'downloads': 1}
                         ]})

    def test_add_field_summary_overall_summarizes_overall_data(self):
        self.req.insert([
                        {'handle': 'LOONYGOONS', 'author': 'Sir Topham Hate'},
                        {'handle': 'LOONYGOONS', 'author': 'Sir Topham Hate'},
                        {'handle': 'LOONYGOONS', 'author': 'Master Bromide'},
                        {'handle': 'STIMBLECON', 'author': 'Sir Topham Hate'}])
        summarize.add_field_summary_overall(self.req, self.sum, 'author')
        self.assertEqual(self.sum.find_one({'_id': 'Sir Topham Hate'}),
                         {'_id': 'Sir Topham Hate',
                          'type': 'author', 'downloads': 3, 'size': 2})
        self.assertEqual(self.sum.find_one({'_id': 'Master Bromide'}),
                         {'_id': 'Master Bromide',
                          'type': 'author', 'downloads': 1, 'size': 1})

    def test_add_author_dlcs_adds_set_of_dlcs(self):
        self.req.insert([
                        {'author': 'Muffincup', 'dlc': 'School of Muffin Science'},
                        {'author': 'Muffincup', 'dlc': 'School of Muffin Science'},
                        {'author': 'Muffincup', 'dlc': 'School of Muffin Design'},
                        {'author': 'Cupcake Mary', 'dlc': 'Dept of Cupcake Research'}])
        summarize.add_author_dlcs(self.req, self.sum)
        self.assertEqual(self.sum.find_one({'_id': 'Muffincup'}),
                         {'_id': 'Muffincup', 'parents': [
                            'School of Muffin Design', 'School of Muffin Science']})
        self.assertEqual(self.sum.find_one({'_id': 'Cupcake Mary'}),
                         {'_id': 'Cupcake Mary', 'parents': [
                            'Dept of Cupcake Research']})

    def test_add_handle_author_adds_set_of_authors(self):
        self.req.insert([
                        {'handle': 'MOONBOTTOM', 'author': 'Lord Fiddleruff'},
                        {'handle': 'MOONBOTTOM', 'author': 'Lady Rumplecuff'},
                        {'handle': 'MOONBOTTOM', 'author': 'Lord Fiddleruff'},
                        {'handle': 'KITTENTOWN', 'author': 'Lady Rumplecuff'}])
        summarize.add_handle_author(self.req, self.sum)
        self.assertEqual(self.sum.find_one({'_id': 'MOONBOTTOM'}),
                         {'_id': 'MOONBOTTOM', 'parents': [
                            'Lady Rumplecuff', 'Lord Fiddleruff']})
        self.assertEqual(self.sum.find_one({'_id': 'KITTENTOWN'}),
                         {'_id': 'KITTENTOWN', 'parents': ['Lady Rumplecuff']})

    def tearDown(self):
        self.box.stop()
class TestSummarize(unittest.TestCase):
    def setUp(self):
        self.box = MongoBox()
        self.box.start()
        self.req = self.box.client().db1.req
        self.sum = self.box.client().db1.sum

    def test_add_summary_data_summarizes_data(self):
        self.req.insert([{
            'handle': 'TEH_FOO'
        }, {
            'handle': 'TEH_FOO'
        }, {
            'handle': 'TEH_BAR'
        }])
        summarize.add_summary_data(self.req, self.sum)
        self.assertEqual(self.sum.find_one(), {
            '_id': 'Overall',
            'type': 'overall',
            'size': 2,
            'downloads': 3
        })

    def test_add_summary_map_summarizes_country_data(self):
        self.req.insert([{
            'country': 'RUS'
        }, {
            'country': 'RUS'
        }, {
            'country': 'USA'
        }])
        summarize.add_summary_map(self.req, self.sum)
        self.assertEqual(
            self.sum.find_one({'_id': 'Overall'}), {
                '_id':
                'Overall',
                'countries': [{
                    'country': 'RUS',
                    'downloads': 2
                }, {
                    'country': 'USA',
                    'downloads': 1
                }]
            })

    def test_add_summary_date_summarizes_date_data(self):
        self.req.insert([{
            'time': datetime(2006, 6, 6)
        }, {
            'time': datetime(2006, 6, 6)
        }, {
            'time': datetime(1955, 11, 5)
        }])
        summarize.add_summary_date(self.req, self.sum)
        self.assertEqual(
            self.sum.find_one({'_id': 'Overall'}), {
                '_id':
                'Overall',
                'dates': [{
                    'date': '1955-11-05',
                    'downloads': 1
                }, {
                    'date': '2006-06-06',
                    'downloads': 2
                }]
            })

    def test_add_field_summary_date_summarizes_date(self):
        self.req.insert([{
            'author': 'Muffincup',
            'time': datetime(2006, 6, 6)
        }, {
            'author': 'Muffincup',
            'time': datetime(2006, 6, 6)
        }, {
            'author': "Sir Cup'ncake",
            'time': datetime(1955, 11, 5)
        }, {
            'author': "Sir Cup'ncake",
            'time': datetime(1999, 12, 31)
        }])
        summarize.add_field_summary_date(self.req, self.sum, 'author')
        self.assertEqual(self.sum.find_one({'_id': 'Muffincup'}), {
            '_id': 'Muffincup',
            'dates': [{
                'date': '2006-06-06',
                'downloads': 2
            }]
        })
        self.assertEqual(
            self.sum.find_one({'_id': "Sir Cup'ncake"}), {
                '_id':
                "Sir Cup'ncake",
                'dates': [{
                    'date': '1955-11-05',
                    'downloads': 1
                }, {
                    'date': '1999-12-31',
                    'downloads': 1
                }]
            })

    def test_add_field_summary_map_summarizes_country(self):
        self.req.insert([
            {
                'author': 'Muffincup',
                'country': 'USA'
            },
            {
                'author': 'Muffincup',
                'country': 'USA'
            },
            {
                'author': 'Tinsletoon',
                'country': 'GER'
            },
            {
                'author': 'Tinsletoon',
                'country': 'FIN'
            },
        ])
        summarize.add_field_summary_map(self.req, self.sum, 'author')
        self.assertEqual(self.sum.find_one({'_id': 'Muffincup'}), {
            '_id': 'Muffincup',
            'countries': [{
                'country': 'USA',
                'downloads': 2
            }]
        })
        self.assertEqual(
            self.sum.find_one({'_id': 'Tinsletoon'}), {
                '_id':
                'Tinsletoon',
                'countries': [{
                    'country': 'FIN',
                    'downloads': 1
                }, {
                    'country': 'GER',
                    'downloads': 1
                }]
            })

    def test_add_field_summary_overall_summarizes_overall_data(self):
        self.req.insert([{
            'handle': 'LOONYGOONS',
            'author': 'Sir Topham Hate'
        }, {
            'handle': 'LOONYGOONS',
            'author': 'Sir Topham Hate'
        }, {
            'handle': 'LOONYGOONS',
            'author': 'Master Bromide'
        }, {
            'handle': 'STIMBLECON',
            'author': 'Sir Topham Hate'
        }])
        summarize.add_field_summary_overall(self.req, self.sum, 'author')
        self.assertEqual(self.sum.find_one({'_id': 'Sir Topham Hate'}), {
            '_id': 'Sir Topham Hate',
            'type': 'author',
            'downloads': 3,
            'size': 2
        })
        self.assertEqual(self.sum.find_one({'_id': 'Master Bromide'}), {
            '_id': 'Master Bromide',
            'type': 'author',
            'downloads': 1,
            'size': 1
        })

    def test_add_author_dlcs_adds_set_of_dlcs(self):
        self.req.insert([{
            'author': 'Muffincup',
            'dlc': 'School of Muffin Science'
        }, {
            'author': 'Muffincup',
            'dlc': 'School of Muffin Science'
        }, {
            'author': 'Muffincup',
            'dlc': 'School of Muffin Design'
        }, {
            'author': 'Cupcake Mary',
            'dlc': 'Dept of Cupcake Research'
        }])
        summarize.add_author_dlcs(self.req, self.sum)
        self.assertEqual(
            self.sum.find_one({'_id': 'Muffincup'}), {
                '_id': 'Muffincup',
                'parents':
                ['School of Muffin Design', 'School of Muffin Science']
            })
        self.assertEqual(self.sum.find_one({'_id': 'Cupcake Mary'}), {
            '_id': 'Cupcake Mary',
            'parents': ['Dept of Cupcake Research']
        })

    def test_add_handle_author_adds_set_of_authors(self):
        self.req.insert([{
            'handle': 'MOONBOTTOM',
            'author': 'Lord Fiddleruff'
        }, {
            'handle': 'MOONBOTTOM',
            'author': 'Lady Rumplecuff'
        }, {
            'handle': 'MOONBOTTOM',
            'author': 'Lord Fiddleruff'
        }, {
            'handle': 'KITTENTOWN',
            'author': 'Lady Rumplecuff'
        }])
        summarize.add_handle_author(self.req, self.sum)
        self.assertEqual(self.sum.find_one({'_id': 'MOONBOTTOM'}), {
            '_id': 'MOONBOTTOM',
            'parents': ['Lady Rumplecuff', 'Lord Fiddleruff']
        })
        self.assertEqual(self.sum.find_one({'_id': 'KITTENTOWN'}), {
            '_id': 'KITTENTOWN',
            'parents': ['Lady Rumplecuff']
        })

    def tearDown(self):
        self.box.stop()
예제 #21
0
from mongobox import MongoBox
import time

# Start up MongoDB with user permissions.
# In a real deployment we would simply connect to an existing,
# separately managed MongoDB deployment.

box = MongoBox(port=27017)
box.start()
client = box.client()

while True:
    time.sleep(1)