Esempio n. 1
0
 def test_create_database_canonicalizes_the_name_of_the_database(self):
     settings.SHARED_SERVER = "127.0.0.1"
     request = RequestFactory().post("/", {"name": "foo-bar"})
     response = CreateDatabase().post(request)
     instances_filter = Instance.objects.filter(
         name=canonicalize_db_name("foo-bar"))
     exists = instances_filter.exists()
     sql = "DROP DATABASE IF EXISTS {0}"
     self.cursor.execute(sql.format(canonicalize_db_name("foo-bar")))
     instances_filter[0].delete()
     self.assertEqual(201, response.status_code)
     self.assertTrue(exists)
 def test_create_database_canonicalizes_the_name_of_the_database(self):
     settings.SHARED_SERVER = "127.0.0.1"
     request = RequestFactory().post("/", {"name": "foo-bar"})
     response = CreateDatabase().post(request)
     instances_filter = Instance.objects.filter(
         name=canonicalize_db_name("foo-bar")
     )
     exists = instances_filter.exists()
     sql = "DROP DATABASE IF EXISTS {0}"
     self.cursor.execute(sql.format(canonicalize_db_name("foo-bar")))
     instances_filter[0].delete()
     self.assertEqual(201, response.status_code)
     self.assertTrue(exists)
Esempio n. 3
0
 def test_create_user_canonicalizes_database_name(self):
     instance = Instance.objects.create(
         name=canonicalize_db_name("some-db"),
         shared=True,
         state="running",
     )
     settings.SHARED_SERVER = "localhost"
     request = RequestFactory().post("/", {"unit-host": "someurl.com"})
     response = CreateUser.as_view()(request, "some-db")
     instance.delete()
     self.assertEqual(201, response.status_code)
Esempio n. 4
0
 def delete(self, request, name, *args, **kwargs):
     name = canonicalize_db_name(name)
     try:
         instance = Instance.objects.get(name=name)
     except Instance.DoesNotExist:
         return HttpResponse("Instance not found.", status=404)
     db = instance.db_manager()
     try:
         db.drop_user(name, None)
     except Exception, e:
         return HttpResponse(e.args[-1], status=500)
Esempio n. 5
0
 def delete(self, request, name, *args, **kwargs):
     name = canonicalize_db_name(name)
     try:
         instance = Instance.objects.get(name=name)
     except Instance.DoesNotExist:
         return HttpResponse("Instance not found.", status=404)
     db = instance.db_manager()
     try:
         db.drop_user(name, None)
     except Exception, e:
         return HttpResponse(e.args[-1], status=500)
Esempio n. 6
0
 def test_create_user_canonicalizes_database_name(self):
     instance = Instance.objects.create(
         name=canonicalize_db_name("some-db"),
         shared=True,
         state="running",
     )
     settings.SHARED_SERVER = "localhost"
     request = RequestFactory().post("/", {"unit-host": "someurl.com"})
     response = CreateUser.as_view()(request, "some-db")
     instance.delete()
     self.assertEqual(201, response.status_code)
Esempio n. 7
0
 def post(self, request):
     if not "name" in request.POST:
         return HttpResponse("App name is missing", status=500)
     name = request.POST.get("name")
     if not name:
         return HttpResponse("App name is empty", status=500)
     instance = Instance(name=canonicalize_db_name(name))
     try:
         create_database(instance, self._client)
     except Exception as e:
         return HttpResponse(e.args[-1], status=500)
     return HttpResponse("", status=201)
Esempio n. 8
0
 def post(self, request, name, *args, **kwargs):
     name = canonicalize_db_name(name)
     try:
         instance = Instance.objects.get(name=name)
     except Instance.DoesNotExist:
         return HttpResponse("Instance not found", status=404)
     if instance.state != "running":
         msg = u"You can't bind to this instance because it's not running."
         return HttpResponse(msg, status=412)
     db = instance.db_manager()
     try:
         username, password = db.create_user(name, None)
     except Exception, e:
         return HttpResponse(e.args[-1], status=500)
Esempio n. 9
0
 def post(self, request, name, *args, **kwargs):
     name = canonicalize_db_name(name)
     try:
         instance = Instance.objects.get(name=name)
     except Instance.DoesNotExist:
         return HttpResponse("Instance not found", status=404)
     if instance.state != "running":
         msg = u"You can't bind to this instance because it's not running."
         return HttpResponse(msg, status=412)
     db = instance.db_manager()
     try:
         username, password = db.create_user(name, None)
     except Exception, e:
         return HttpResponse(e.args[-1], status=500)
Esempio n. 10
0
    def test_drop_database_that_needs_name_canonicalization(self):
        settings.SHARED_SERVER = "127.0.0.1"
        canonical_name = canonicalize_db_name("xu-xu")
        Instance.objects.create(name=canonical_name, shared=True)
        db = DatabaseManager("xu-xu", settings.SHARED_SERVER)
        db.create_database()

        view = DropDatabase()
        request = RequestFactory().delete("/xu-xu")
        resp = view.delete(request, "xu-xu")
        self.assertEqual(200, resp.status_code)
        sql = "select SCHEMA_NAME from information_schema.SCHEMATA " +\
              "where SCHEMA_NAME = '{0}'"
        self.cursor.execute(sql.format(canonical_name))
        row = self.cursor.fetchone()
        self.assertIsNone(row)
Esempio n. 11
0
    def test_drop_database_that_needs_name_canonicalization(self):
        settings.SHARED_SERVER = "127.0.0.1"
        canonical_name = canonicalize_db_name("xu-xu")
        Instance.objects.create(name=canonical_name, shared=True)
        db = DatabaseManager("xu-xu", settings.SHARED_SERVER)
        db.create_database()

        view = DropDatabase()
        request = RequestFactory().delete("/xu-xu")
        resp = view.delete(request, "xu-xu")
        self.assertEqual(200, resp.status_code)
        sql = "select SCHEMA_NAME from information_schema.SCHEMATA " +\
              "where SCHEMA_NAME = '{0}'"
        self.cursor.execute(sql.format(canonical_name))
        row = self.cursor.fetchone()
        self.assertIsNone(row)
Esempio n. 12
0
 def test_create_database_canonicalizes_name(self):
     settings.SHARED_SERVER = "127.0.0.1"
     instance = Instance(name="invalid-db-name", ec2_id="i-681")
     canonical_name = canonicalize_db_name(instance.name)
     try:
         create_database(instance)
         sql = "select SCHEMA_NAME from information_schema.SCHEMATA " +\
               "where SCHEMA_NAME = '{0}'"
         self.cursor.execute(sql.format(canonical_name))
         row = self.cursor.fetchone()
         self.assertIsNotNone(row)
         self.assertEqual(canonical_name, row[0])
         self.assertIsNotNone(instance.pk)
     finally:
         sql = "DROP DATABASE IF EXISTS {0}"
         self.cursor.execute(sql.format(canonical_name))
         instance.delete()
Esempio n. 13
0
 def delete(self, request, name, *args, **kwargs):
     name = canonicalize_db_name(name)
     try:
         instance = Instance.objects.get(name=name)
     except Instance.DoesNotExist:
         msg = "Can't drop database '%s'; database doesn't exist" % name
         return HttpResponse(msg, status=404)
     if instance.shared:
         db = instance.db_manager()
         db.drop_database()
     elif self._client.unauthorize(instance) and \
             self._client.terminate(instance):
         pass
     else:
         return HttpResponse("Failed to terminate the instance.",
                             status=500)
     instance.delete()
     return HttpResponse("", status=200)
Esempio n. 14
0
 def test_create_database_canonicalizes_name(self):
     settings.SHARED_SERVER = "127.0.0.1"
     instance = Instance(
         name="invalid-db-name",
         ec2_id="i-681"
     )
     canonical_name = canonicalize_db_name(instance.name)
     try:
         create_database(instance)
         sql = "select SCHEMA_NAME from information_schema.SCHEMATA " +\
               "where SCHEMA_NAME = '{0}'"
         self.cursor.execute(sql.format(canonical_name))
         row = self.cursor.fetchone()
         self.assertIsNotNone(row)
         self.assertEqual(canonical_name, row[0])
         self.assertIsNotNone(instance.pk)
     finally:
         sql = "DROP DATABASE IF EXISTS {0}"
         self.cursor.execute(sql.format(canonical_name))
         instance.delete()
Esempio n. 15
0
 def test_canonicalize_db_name_replaces_whitespaces_with_underline(self):
     canonicalized_name = canonicalize_db_name(" foo ")
     expected = "_foo_{0}".format(hashlib.sha1(" foo ").hexdigest()[:10])
     self.assertEqual(canonicalized_name, expected)
Esempio n. 16
0
 def test_canonicalize_db_name_do_nothing_when_called_twice(self):
     canonicalized_name = canonicalize_db_name(
         canonicalize_db_name(" foo "))
     expected = "_foo_{0}".format(hashlib.sha1(" foo ").hexdigest()[:10])
     self.assertEqual(canonicalized_name, expected)
Esempio n. 17
0
 def test_canonicalize_db_name_replaces_whitespaces_with_underline(self):
     canonicalized_name = canonicalize_db_name(" foo ")
     expected = "_foo_{0}".format(hashlib.sha1(" foo ").hexdigest()[:10])
     self.assertEqual(canonicalized_name, expected)
Esempio n. 18
0
 def test_canonicalize_db_name_dont_change_strings_without_dashes(self):
     canonicalized_name = canonicalize_db_name("foo_bar")
     self.assertEqual("foo_bar", canonicalized_name)
Esempio n. 19
0
 def test_canonicalize_db_name_do_nothing_when_called_twice(self):
     canonicalized_name = canonicalize_db_name(
         canonicalize_db_name(" foo ")
     )
     expected = "_foo_{0}".format(hashlib.sha1(" foo ").hexdigest()[:10])
     self.assertEqual(canonicalized_name, expected)
Esempio n. 20
0
 def test_canonicalize_db_name_dont_change_strings_without_dashes(self):
     canonicalized_name = canonicalize_db_name("foo_bar")
     self.assertEqual("foo_bar", canonicalized_name)