Beispiel #1
0
    def test_not_found(self):
        with self.app.app_context():
            manager = managers.SharedManager()
            instance = manager.create_instance('databasenotexist')

            self.assertRaises(psycopg2.ProgrammingError, instance.drop_user,
                              '127.0.0.1')
Beispiel #2
0
    def test_status(self):
        rv = self.client.get('/resources/databasenotexist/status',
                             headers=self.headers)
        self.assertEqual(rv.status_code, 404)

        with self.app.app_context():
            manager = managers.SharedManager()
            manager.create_instance('databasenotexist')

        rv = self.client.get('/resources/databasenotexist/status',
                             headers=self.headers)
        self.assertEqual(rv.status_code, 204)

        admin = self.app.config['SHARED_ADMIN']
        self.app.config['SHARED_ADMIN'] = admin * 2
        rv = self.client.get('/resources/databasenotexist/status',
                             headers=self.headers)
        self.assertEqual(rv.status_code, 500)

        self.app.config['SHARED_ADMIN'] = admin
        db = self.create_db()
        with db.transaction() as cursor:
            cursor.execute("UPDATE instance SET state='pending'")
        rv = self.client.get('/resources/databasenotexist/status',
                             headers=self.headers)
        self.assertEqual(rv.status_code, 202)

        with db.transaction() as cursor:
            cursor.execute("UPDATE instance SET state='error'")
        rv = self.client.get('/resources/databasenotexist/status',
                             headers=self.headers)
        self.assertEqual(rv.status_code, 500)
Beispiel #3
0
    def test_unbind_app_500(self):
        with self.app.app_context():
            manager = managers.SharedManager()
            manager.create_instance('databasenotexist')

        # the database exists but not the role
        # tsuru's api flow set this to 500 but not 404
        rv = self.client.delete('/resources/databasenotexist/bind-app',
                                data={'app-host': '127.0.0.1'},
                                headers={
                                    'Content-Type':
                                    'application/x-www-form-urlencoded',
                                    'Authorization':
                                    self.headers['Authorization']
                                })
        self.assertEqual(rv.status_code, 500)

        db = self.create_db()
        with db.transaction() as cursor:
            cursor.execute("UPDATE instance SET state='pending'")
        rv = self.client.delete('/resources/databasenotexist/bind-app',
                                data={'app-host': '127.0.0.1'},
                                headers={
                                    'Content-Type':
                                    'application/x-www-form-urlencoded',
                                    'Authorization':
                                    self.headers['Authorization']
                                })
        self.assertEqual(rv.status_code, 500)
Beispiel #4
0
    def test_destroy_200(self):
        with self.app.app_context():
            manager = managers.SharedManager()
            manager.create_instance('databasenotexist')

        rv = self.client.delete('/resources/databasenotexist',
                                headers=self.headers)
        self.assertEqual(rv.status_code, 200)
Beispiel #5
0
    def test_create_500(self):
        with self.app.app_context():
            manager = managers.SharedManager()
            manager.create_instance('databasenotexist')

        rv = self.client.post('/resources',
                              data={'name': 'databasenotexist'},
                              headers=self.headers)
        self.assertEqual(rv.status_code, 500)
        self.assertTrue('already exists' in rv.data)
Beispiel #6
0
    def test_success(self):
        with self.app.app_context():
            manager = managers.SharedManager()
            manager.create_instance('databasenotexist')

        db = self.create_db()
        with db.transaction() as cursor:
            cursor.execute('SELECT name, state, plan FROM instance')
            self.assertEqual(cursor.fetchall(),
                             [('databasenotexist', 'running', 'shared')])
Beispiel #7
0
    def test_bind_app_500(self):
        with self.app.app_context():
            manager = managers.SharedManager()
            ins = manager.create_instance('databasenotexist')
            ins.create_user('127.0.0.1')

        rv = self.client.post('/resources/databasenotexist/bind-app',
                              data={'app-host': '127.0.0.1'},
                              headers=self.headers)
        self.assertEqual(rv.status_code, 500)
        self.assertEqual(rv.data.strip(),
                         'role "databaseno90ae84" already exists')
Beispiel #8
0
    def test_bind_app_412(self):
        with self.app.app_context():
            manager = managers.SharedManager()
            manager.create_instance('databasenotexist')

        db = self.create_db()
        with db.transaction() as cursor:
            cursor.execute("UPDATE instance SET state='pending'")
        rv = self.client.post('/resources/databasenotexist/bind-app',
                              data={'app-host': '127.0.0.1'},
                              headers=self.headers)
        self.assertEqual(rv.status_code, 412)
Beispiel #9
0
    def test_success(self):
        with self.app.app_context():
            manager = managers.SharedManager()
            manager.create_instance('databasenotexist')

            s = storage.InstanceStorage()
            instance = s.instance_by_name('databasenotexist')

            user, password = instance.create_user('127.0.0.1')
            self.assertEqual(user, 'databaseno90ae84')
            self.assertEqual(password,
                             '59e325e93f6a8aa81e6bfb270c819ccfaaf1e30a')
Beispiel #10
0
    def test_already_exists(self):
        db = self.create_db()
        with db.autocommit() as cursor:
            cursor.execute('CREATE ROLE databaseno90ae84')
        with self.app.app_context():
            manager = managers.SharedManager()
            manager.create_instance('databasenotexist')

            s = storage.InstanceStorage()
            instance = s.instance_by_name('databasenotexist')

            self.assertRaises(psycopg2.ProgrammingError, instance.create_user,
                              '127.0.0.1')
Beispiel #11
0
    def test_success(self):
        user = self.user
        dbname = self.database

        # Connection to the database using app's user credentials
        self.database = 'databasenotexist'
        self.user = '******'
        user_db = self.create_db(
            dbname='databasenotexist',
            user='******',
            password='******')

        # Connection to the same db instance but logging in as main user
        # This will be used to check the table we create is still there
        # when the app is unbound
        self.user = user
        group_db = self.create_db()

        # Reset the username and db name so setUp and tearDown methods work
        self.user = user
        self.database = dbname

        with self.app.app_context():
            manager = managers.SharedManager()
            instance = manager.create_instance('databasenotexist')
            instance.create_user('127.0.0.1')

            # Let's simulate the app creating some objects in the database
            # This could be a Django/Flask app running migrations, for instance
            with user_db.autocommit() as cursor:
                cursor.execute('CREATE TABLE article ('
                               'article_id bigserial primary key, '
                               'article_name varchar(20) NOT NULL, '
                               'article_desc text NOT NULL, '
                               'date_added timestamp default NULL)')
                cursor.execute('INSERT INTO article(article_id, article_name, '
                               'article_desc, date_added) VALUES '
                               "(1, 'hello', 'world world world', NOW())")

            instance.drop_user('127.0.0.1')

        with group_db.transaction() as cursor:
            # The role created after binding the app should be gone
            cursor.execute(
                "SELECT * FROM pg_roles WHERE rolname = 'databaseno90ae84'")
            self.assertEqual(cursor.fetchall(), [])
            # The table created by the app should be still here
            # containing all data
            cursor.execute("SELECT * FROM article")
            self.assertEqual(len(cursor.fetchall()), 1)
Beispiel #12
0
    def test_unbind_app_200(self):
        with self.app.app_context():
            manager = managers.SharedManager()
            ins = manager.create_instance('databasenotexist')

            ins.create_user('127.0.0.1')
        rv = self.client.delete('/resources/databasenotexist/bind-app',
                                data={'app-host': '127.0.0.1'},
                                headers={
                                    'Content-Type':
                                    'application/x-www-form-urlencoded',
                                    'Authorization':
                                    self.headers['Authorization']
                                })
        self.assertEqual(rv.status_code, 200)
Beispiel #13
0
    def test_bind_app_201(self):
        with self.app.app_context():
            manager = managers.SharedManager()
            manager.create_instance('databasenotexist')

        rv = self.client.post('/resources/databasenotexist/bind-app',
                              data={'app-host': '127.0.0.1'},
                              headers=self.headers)
        self.assertEqual(rv.status_code, 201)
        self.assertEqual(
            json.loads(rv.data), {
                'PG_DATABASE': 'databasenotexist',
                'PG_HOST': 'db.example.com',
                'PG_PASSWORD': '******',
                'PG_PORT': '5432',
                'PG_USER': '******'
            })
Beispiel #14
0
    def test_already_exists(self):
        db = self.create_db()
        manager = managers.SharedManager()

        with db.transaction() as cursor:
            cursor.execute("INSERT INTO instance (name, state, plan) VALUES "
                           "('databasenotexist', 'running', 'shared')")
        with self.app.app_context():
            self.assertRaises(managers.InstanceAlreadyExists,
                              manager.create_instance, 'databasenotexist')

        with db.autocommit() as cursor:
            cursor.execute('CREATE ROLE databaseno_group')
            cursor.execute('CREATE DATABASE databasenotexist '
                           'OWNER databaseno_group')
        with self.app.app_context():
            self.assertRaises(managers.InstanceAlreadyExists,
                              manager.create_instance, 'databasenotexist')
Beispiel #15
0
    def test_success(self):
        db = self.create_db()

        with db.transaction() as cursor:
            cursor.execute(
                "INSERT INTO instance (name, state, plan) VALUES "
                "('databasenotexist', 'running', 'shared')")
        with db.autocommit() as cursor:
            cursor.execute('CREATE ROLE databaseno_group')
            cursor.execute('CREATE DATABASE databasenotexist '
                           'OWNER databaseno_group')
            cursor.execute('CREATE ROLE databaseno90ae84 '
                           'IN GROUP databaseno_group')
        with self.app.app_context():
            manager = managers.SharedManager()
            manager.delete_instance(models.Instance(name='databasenotexist', plan='shared'))

        db = self.create_db()
        with db.transaction() as cursor:
            cursor.execute('SELECT * FROM instance')
            self.assertEqual(cursor.fetchall(), [])
            cursor.execute("SELECT * FROM pg_roles WHERE rolname ='databaseno90ae84'")
            self.assertEqual(cursor.fetchall(), [])
Beispiel #16
0
 def test_not_exist(self):
     with self.app.app_context():
         manager = managers.SharedManager()
         self.assertRaises(
             storage.InstanceNotFound, manager.delete_instance,
             models.Instance(name='databasenotexist', plan='shared'))