예제 #1
0
 def test_create_user_generates_username_for_dbs_grater_than_16_chars(self):
     db = DatabaseManager("usernamegreaterthan16")
     username, password = db.create_user("usernamegreaterthan16", "%")
     sql = "select User, Host FROM mysql.user " +\
           "WHERE User like 'usernamegrea%' AND Host='%'"
     self.cursor.execute(sql)
     row = self.cursor.fetchone()
     self.assertEqual("usernamegrea", row[0][:12])
     db = DatabaseManager(row[0])
     db.drop_user(username, "%")
예제 #2
0
def export(request, name):
    host = request.GET.get("service_host", "localhost")
    try:
        db = DatabaseManager(name, host)
        return HttpResponse(db.export())
    except subprocess.CalledProcessError, e:
        return HttpResponse(e.output.split(":")[-1].strip(), status=500)
예제 #3
0
 def test_create_user_in_shared_instance_with_public_host(self):
     settings.SHARED_SERVER = "localhost"
     settings.SHARED_SERVER_PUBLIC_HOST = "10.10.10.10"
     instance = Instance.objects.create(
         name="inside_out",
         shared=True,
         state="running",
     )
     try:
         request = RequestFactory().post("/", {"unit-host": "192.168.1.10"})
         response = CreateUser.as_view()(request, "inside_out")
         self.assertEqual(201, response.status_code)
         content = json.loads(response.content)
         expected = {
             u"MYSQL_HOST": u"10.10.10.10",
             u"MYSQL_PORT": u"3306",
             u"MYSQL_DATABASE_NAME": "inside_out",
             u"MYSQL_USER": u"inside_out",
             u"MYSQL_PASSWORD": content["MYSQL_PASSWORD"],
         }
         self.assertEqual(expected, content)
         sql = "select User, Host FROM mysql.user " +\
               "WHERE User='******' AND Host='%'"
         self.cursor.execute(sql)
         row = self.cursor.fetchone()
         self.assertIsNotNone(row)
     finally:
         db = DatabaseManager("inside_out")
         db.drop_user("inside_out", "%")
         instance.delete()
예제 #4
0
 def test_create_user(self):
     instance = Instance.objects.create(
         name="ciclops",
         host="127.0.0.1",
         ec2_id="i-009",
         state="running",
     )
     try:
         request = RequestFactory().post("/", {"unit-host": "192.168.1.1"})
         response = CreateUser.as_view()(request, "ciclops")
         self.assertEqual(201, response.status_code)
         content = json.loads(response.content)
         expected = {
             u"MYSQL_HOST": u"127.0.0.1",
             u"MYSQL_PORT": u"3306",
             u"MYSQL_DATABASE_NAME": "ciclops",
             u"MYSQL_USER": u"ciclops",
             u"MYSQL_PASSWORD": content["MYSQL_PASSWORD"],
         }
         self.assertDictEqual(expected, content)
         sql = "select User, Host FROM mysql.user " +\
               "WHERE User='******' AND Host='%'"
         self.cursor.execute(sql)
         row = self.cursor.fetchone()
         self.assertEqual("ciclops", row[0])
     finally:
         db = DatabaseManager("ciclops")
         db.drop_user("ciclops", "%")
         instance.delete()
예제 #5
0
 def test_init_should_canonicalize_name_property(self):
     db = DatabaseManager(
         name="foo-bar",
         host=settings.SHARED_SERVER,
         user=settings.SHARED_USER,
         password=settings.SHARED_PASSWORD,
     )
     self.assertRegexpMatches(db.name, "^foo_bar.*$")
예제 #6
0
 def test_create(self):
     db = DatabaseManager("newdatabase")
     db.create_database()
     sql = "select SCHEMA_NAME from information_schema.SCHEMATA " +\
           "where SCHEMA_NAME = 'newdatabase'"
     self.cursor.execute(sql)
     row = self.cursor.fetchone()
     self.assertEqual("newdatabase", row[0])
     db.drop_database()
예제 #7
0
 def test_drop(self):
     db = DatabaseManager("otherdatabase")
     db.create_database()
     db.drop_database()
     sql = "select SCHEMA_NAME from information_schema.SCHEMATA where " +\
           "SCHEMA_NAME = 'otherdatabase'"
     self.cursor.execute(sql)
     row = self.cursor.fetchone()
     self.assertFalse(row)
예제 #8
0
 def test_drop_user(self):
     db = DatabaseManager("magneto")
     db.create_user("magneto", "%")
     db.drop_user("magneto", "%")
     sql = "select User, Host FROM mysql.user " +\
           "WHERE User='******' AND Host='%'"
     self.cursor.execute(sql)
     row = self.cursor.fetchone()
     self.assertFalse(row)
예제 #9
0
 def test_create__and_drop_user_username_greater_than_16_chars(self):
     db = DatabaseManager("anusernamegreaterthan16")
     db.create_user("anusernamegreaterthan16", "%")
     db.drop_user("anusernamegreaterthan16", "%")
     sql = "select User, Host FROM mysql.user " + \
         "WHERE User LIKE 'anusernamegr%' AND Host='%'"
     self.cursor.execute(sql)
     row = self.cursor.fetchone()
     self.assertFalse(row)
예제 #10
0
 def test_create_user(self):
     db = DatabaseManager("wolverine")
     db.create_user("wolverine", "%")
     sql = "select User, Host FROM mysql.user " +\
           "WHERE User='******' AND Host='%'"
     self.cursor.execute(sql)
     row = self.cursor.fetchone()
     self.assertEqual("wolverine", row[0])
     self.assertEqual("%", row[1])
     db.drop_user("wolverine", "%")
예제 #11
0
 def test_drop_user_from_shared_instance(self):
     settings.SHARED_SERVER = "127.0.0.1"
     instance = Instance.objects.create(name="used", shared=True)
     try:
         db = DatabaseManager("used")
         db.create_user("used", "127.0.0.1")
         request = RequestFactory().delete("/used")
         response = drop_user(request, "used", "127.0.0.1")
         self.assertEqual(200, response.status_code)
         sql = "select User, Host FROM mysql.user " +\
               "WHERE User='******' AND Host='%'"
         self.cursor.execute(sql)
         row = self.cursor.fetchone()
         self.assertIsNone(row)
     finally:
         instance.delete()
예제 #12
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)
예제 #13
0
    def test_drop_user(self):
        instance = Instance.objects.create(name="ciclops", host="127.0.0.1")
        try:
            db = DatabaseManager("ciclops")
            db.create_user("ciclops", "localhost")

            request = RequestFactory().delete("/ciclops")
            response = drop_user(request, "ciclops", "localhost")
            self.assertEqual(200, response.status_code)

            sql = "select User, Host FROM mysql.user " +\
                  "WHERE User='******' AND Host='%'"
            self.cursor.execute(sql)
            row = self.cursor.fetchone()
            self.assertFalse(row)
        finally:
            instance.delete()
예제 #14
0
    def test_export(self):
        db = DatabaseManager("magneto")
        db.create_database()
        db.create_user("magneto", "%")
        self.cursor.execute("create table magneto.foo ( test varchar(255) );")
        expected = """\
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `foo` (
  `test` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
"""
        result = db.export()
        self.assertEqual(expected, result.replace("InnoDB", "MyISAM"))
        db.drop_database()
        db.drop_user("magneto", "%")
예제 #15
0
 def test_create_database_ec2(self):
     try:
         client = mocks.FakeEC2Client()
         t = start_creator(DatabaseManager, client)
         request = RequestFactory().post("/", {"name": "ciclops"})
         view = CreateDatabase()
         view._client = client
         response = view.post(request)
         self.assertEqual(201, response.status_code)
         self.assertEqual("", response.content)
         t.stop()
         sql = "select SCHEMA_NAME from information_schema.SCHEMATA " + \
               "where SCHEMA_NAME = 'ciclops'"
         self.cursor.execute(sql)
         row = self.cursor.fetchone()
         self.assertEqual("ciclops", row[0])
     finally:
         db = DatabaseManager("ciclops")
         db.drop_database()
예제 #16
0
    def test_export_from_a_custom_service_host(self):
        db = DatabaseManager("magneto", host="127.0.0.1")
        db.create_database()
        db.create_user("magneto", "%")
        self.cursor.execute("create table magneto.foo ( test varchar(255) );")
        expected = """\
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `foo` (
  `test` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
"""
        request = RequestFactory().get("/", {"service_host": "127.0.0.1"})
        result = export(request, "magneto")
        self.assertEqual(200, result.status_code)
        self.assertEqual(expected, result.content.replace("InnoDB", "MyISAM"))
        db.drop_database()
        db.drop_user("magneto", "%")
예제 #17
0
 def test_is_up_return_True_if_everything_is_ok_with_the_connection(self):
     db = DatabaseManager("wolverine")
     self.assertTrue(db.is_up())
예제 #18
0
 def test_is_up_return_False_something_is_not_ok_with_the_connection(self):
     db = DatabaseManager("wolverine",
                          host="unknownhost.absolute.impossibru.moc")
     self.assertFalse(db.is_up())
예제 #19
0
 def test_public_host_is_host_when_private_attribute_is_None(self):
     db = DatabaseManager("wolverine", host="localhost")
     self.assertEqual(db.host, db.public_host)
예제 #20
0
 def create_fandango_shared(self):
     Instance.objects.create(name="fandango", shared=True)
     db = DatabaseManager("fandango", settings.SHARED_SERVER)
     db.create_database()
예제 #21
0
 def test_public_host(self):
     db = DatabaseManager("wolverine",
                          host="localhost",
                          public_host="10.10.10.10")
     self.assertEqual(db._public_host, db.public_host)