Example #1
0
    def test_update_servers(self):
        """Testing update server records"""
        self._config = configuration.TestConfig()
        self._db = ServersDB(self._config)

        test_counter = 0

        for word in self._test_words:
            server_data = {}

            test_counter += 1

            server_data['host'] = "{}{}.{}{}".format(word, word, word, word)

            record = self._db.get_servers('host', server_data['host'])

            if len(record) == 1:
                _data = record[0].get_data()
                _data['location'] = "{}{}-{}{}".format(word, word, word, word)
                new_data = Server(_data)
                self._db.update_server(new_data)

                updated = self._db.get_servers('host', server_data['host'])

                self.assertEqual(new_data.get_data(), updated[0].get_data())

            if test_counter == self._max_tests:
                break
Example #2
0
    def setUp(self):
        """Setting up Dynamic Hosts Test"""
        with open(
                os.path.join(os.path.dirname(os.path.realpath(__file__)),
                             "words.json")) as f:
            self._test_words = json.load(f)

        self._config = configuration.DevConfig()
        self._db = ServersDB(self._config)

        test_counter = 0
        """Let's create a test DB"""
        if os.path.isfile(self._config.get_db_file()):
            os.remove(self._config.get_db_file())
            time.sleep(2.5)  # sleep time in seconds

        for word in self._test_words:
            env = choice(['dev', 'itg', 'pro'])
            role = choice(['app', 'db', 'web', 'zoo'])
            server_data = {}
            test_counter += 1
            server_data['host'] = "{}{}.{}{}".format(word, word, word, word)
            server_data['environment'] = env
            server_data['role'] = role
            server_data['location'] = "{}.{}-{}.{}".format(
                word, word, word, word)

            if len(self._db.get_servers('host', server_data['host'])) == 0:
                new_server = Server(server_data)
                self._db.add_new_server(new_server)

            if test_counter == self._max_tests:
                break
Example #3
0
    def test_add_servers(self):
        """Testing add new server records"""
        self._config = configuration.TestConfig()
        self._db = ServersDB(self._config)

        test_counter = 0

        for word in self._test_words:
            env = choice(['dev', 'itg', 'pro'])
            role = choice(['app', 'db', 'web', 'zoo'])
            server_data = {}
            test_counter += 1
            server_data['host'] = "{}{}.{}{}.net".format(word, word, word, word)
            server_data['environment'] = env
            server_data['role'] = role
            server_data['location'] = "{}.{}-{}.{}".format(word, word, word, word)

            new_server = Server(server_data)

            self._db.add_new_server(new_server)

            inserted = self._db.get_servers('host', server_data['host'])

            self.assertEqual(inserted[0].get_data(), new_server.get_data())

            if test_counter == self._max_tests:
                break
Example #4
0
    def test_add_servers_exceptions(self):
        """Testing exceptions when adding new server records"""
        self._config = configuration.TestConfig()
        self._db = ServersDB(self._config)

        test_counter = 0

        for word in self._test_words:
            test_counter += 1
            host = "{}{}.{}{}".format(word, word, word, word)

            record = self._db.get_servers('host', host)

            if len(record) == 1:
                with self.assertRaises(Exception) as context:
                    self._db.add_new_server(record[0])

                self.assertTrue('Duplicated data' in str(context.exception))

            if test_counter == self._max_tests:
                break
Example #5
0
    def test_delete_server(self):
        """Testing delete server records"""
        self._config = configuration.TestConfig()
        self._db = ServersDB(self._config)

        test_counter = 0

        for word in self._test_words:
            test_counter += 1
            host = "{}{}.{}{}".format(word, word, word, word)

            record = self._db.get_servers('host', host)

            if len(record) == 1:
                self._db.delete_server(record[0])

                erased = self._db.get_servers('host', host)

                self.assertEqual(0, len(erased))

            if test_counter == 10:
                break
Example #6
0
    def __init__(self, config):
        self._config = config

        self._db = ServersDB(self._config)

        if self._config.verbose > 0:
            self._log.log_verbose("----- Dynamic hosts configuration -----")
            self._log.log_verbose(" Client.......: {}".format(
                self._config.client))
            self._log.log_verbose(" Environment..: {}".format(
                self._config.environment))
            self._log.log_verbose(" Role.........: {}".format(
                self._config.role))
            self._log.log_verbose(" Location.....: {}".format(
                self._config.location))
Example #7
0
class TestDynamicHosts(unittest.TestCase):
    _db = None
    _config = None
    _test_words = ''
    _max_tests = 500

    def setUp(self):
        """Setting up Dynamic Hosts Test"""
        with open(
                os.path.join(os.path.dirname(os.path.realpath(__file__)),
                             "words.json")) as f:
            self._test_words = json.load(f)

        self._config = configuration.DevConfig()
        self._db = ServersDB(self._config)

        test_counter = 0
        """Let's create a test DB"""
        if os.path.isfile(self._config.get_db_file()):
            os.remove(self._config.get_db_file())
            time.sleep(2.5)  # sleep time in seconds

        for word in self._test_words:
            env = choice(['dev', 'itg', 'pro'])
            role = choice(['app', 'db', 'web', 'zoo'])
            server_data = {}
            test_counter += 1
            server_data['host'] = "{}{}.{}{}".format(word, word, word, word)
            server_data['environment'] = env
            server_data['role'] = role
            server_data['location'] = "{}.{}-{}.{}".format(
                word, word, word, word)

            if len(self._db.get_servers('host', server_data['host'])) == 0:
                new_server = Server(server_data)
                self._db.add_new_server(new_server)

            if test_counter == self._max_tests:
                break

    def tearDown(self):
        """Tear down Dynamic Host Test"""
        if os.path.isfile(self._config.get_db_file()):
            os.remove(self._config.get_db_file())
            time.sleep(2.5)  # sleep time in seconds

        self._config = None

    def test_dynamic_hosts_f1(self):
        """Testing only hosts that belong to the DEV environment"""
        os.environ["THE_ENVIRONMENT"] = "dev"
        self._config = configuration.DevConfig()
        dh = DynamicHosts(self._config)
        hosts_list = dh.get_list()

        self.assertEqual("dev", dh.get_environment)

        for host in hosts_list['all']['hosts']:
            find_host = self._db.get_servers('host', host)
            self.assertEqual(1, len(find_host))
            self.assertEqual("dev", find_host[0].get_data()["environment"])

    def test_dynamic_hosts_f2(self):
        """Testing only hosts that belong to the ITG environment"""
        os.environ["THE_ENVIRONMENT"] = "itg"
        self._config = configuration.DevConfig()
        dh = DynamicHosts(self._config)
        hosts_list = dh.get_list()

        self.assertEqual("itg", dh.get_environment)

        for host in hosts_list['all']['hosts']:
            find_host = self._db.get_servers('host', host)
            self.assertEqual(1, len(find_host))
            self.assertEqual("itg", find_host[0].get_data()["environment"])

    def test_dynamic_hosts_f3(self):
        """Testing only hosts that belong to the PRO environment"""
        os.environ["THE_ENVIRONMENT"] = "pro"
        self._config = configuration.DevConfig()
        dh = DynamicHosts(self._config)
        hosts_list = dh.get_list()

        self.assertEqual("pro", dh.get_environment)

        for host in hosts_list['all']['hosts']:
            find_host = self._db.get_servers('host', host)
            self.assertEqual(1, len(find_host))
            self.assertEqual("pro", find_host[0].get_data()["environment"])

    def test_dynamic_hosts_f4(self):
        """Testing only hosts that belong to the APP role"""
        os.environ["THE_ROLE"] = "app"
        self._config = configuration.DevConfig()
        dh = DynamicHosts(self._config)
        hosts_list = dh.get_list()

        self.assertEqual("app", dh.get_role)

        for host in hosts_list['all']['hosts']:
            find_host = self._db.get_servers('host', host)
            self.assertEqual(1, len(find_host))
            self.assertEqual("app", find_host[0].get_data()["role"])

    def test_dynamic_hosts_f5(self):
        """Testing only hosts that belong to the DB role"""
        os.environ["THE_ROLE"] = "db"
        self._config = configuration.DevConfig()
        dh = DynamicHosts(self._config)
        hosts_list = dh.get_list()

        self.assertEqual("db", dh.get_role)

        for host in hosts_list['all']['hosts']:
            find_host = self._db.get_servers('host', host)
            self.assertEqual(1, len(find_host))
            self.assertEqual("db", find_host[0].get_data()["role"])

    def test_dynamic_hosts_f6(self):
        """Testing only hosts that belong to the WEB role"""
        os.environ["THE_ROLE"] = "web"
        self._config = configuration.DevConfig()
        dh = DynamicHosts(self._config)
        hosts_list = dh.get_list()

        self.assertEqual("web", dh.get_role)

        for host in hosts_list['all']['hosts']:
            find_host = self._db.get_servers('host', host)
            self.assertEqual(1, len(find_host))
            self.assertEqual("web", find_host[0].get_data()["role"])

    def test_dynamic_hosts_f7(self):
        """Testing only hosts that belong to the ZOO role"""
        os.environ["THE_ROLE"] = "zoo"
        self._config = configuration.DevConfig()
        dh = DynamicHosts(self._config)
        hosts_list = dh.get_list()

        self.assertEqual("zoo", dh.get_role)

        for host in hosts_list['all']['hosts']:
            find_host = self._db.get_servers('host', host)
            self.assertEqual(1, len(find_host))
            self.assertEqual("zoo", find_host[0].get_data()["role"])

    def test_dynamic_hosts_f1_f4(self):
        """Testing only hosts that belong to the dev environment and APP role"""
        os.environ["THE_ENVIRONMENT"] = "dev"
        os.environ["THE_ROLE"] = "app"
        self._config = configuration.DevConfig()
        dh = DynamicHosts(self._config)
        hosts_list = dh.get_list()

        self.assertEqual("dev", dh.get_environment)
        self.assertEqual("app", dh.get_role)

        for host in hosts_list['all']['hosts']:
            find_host = self._db.get_servers('host', host)
            self.assertEqual(1, len(find_host))
            self.assertEqual("dev", find_host[0].get_data()["environment"])
            self.assertEqual("app", find_host[0].get_data()["role"])

    def test_dynamic_hosts_f2_f5(self):
        """Testing only hosts that belong to the ITG environment and DB role"""
        os.environ["THE_ENVIRONMENT"] = "itg"
        os.environ["THE_ROLE"] = "db"
        self._config = configuration.DevConfig()
        dh = DynamicHosts(self._config)
        hosts_list = dh.get_list()

        self.assertEqual("itg", dh.get_environment)
        self.assertEqual("db", dh.get_role)

        for host in hosts_list['all']['hosts']:
            find_host = self._db.get_servers('host', host)
            self.assertEqual(1, len(find_host))
            self.assertEqual("itg", find_host[0].get_data()["environment"])
            self.assertEqual("db", find_host[0].get_data()["role"])

    def test_dynamic_hosts_f3_f6(self):
        """Testing only hosts that belong to the PRO environment and WEB role"""
        os.environ["THE_ENVIRONMENT"] = "pro"
        os.environ["THE_ROLE"] = "web"
        self._config = configuration.DevConfig()
        dh = DynamicHosts(self._config)
        hosts_list = dh.get_list()

        self.assertEqual("pro", dh.get_environment)
        self.assertEqual("web", dh.get_role)

        for host in hosts_list['all']['hosts']:
            find_host = self._db.get_servers('host', host)
            self.assertEqual(1, len(find_host))
            self.assertEqual("pro", find_host[0].get_data()["environment"])
            self.assertEqual("web", find_host[0].get_data()["role"])
Example #8
0
    def test_prod_instance(self):
        """Testing Production instance"""
        self._config = configuration.ProdConfig()
        self._db = ServersDB(self._config)

        self.assertIsInstance(self._db, ServersDB)
Example #9
0
    def test_test_instance(self):
        """Testing Test instance"""
        self._config = configuration.TestConfig()
        self._db = ServersDB(self._config)

        self.assertIsInstance(self._db, ServersDB)
Example #10
0
    def test_dev_instance(self):
        """Testing Development instance"""
        self._config = configuration.DevConfig()
        self._db = ServersDB(self._config)

        self.assertIsInstance(self._db, ServersDB)
Example #11
0
class TestDatabase(unittest.TestCase):
    _db = None
    _config = None
    _test_words = ''
    _max_tests = 500

    def setUp(self):
        """Setting up DB tests"""
        with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "words.json")) as f:
            self._test_words = json.load(f)

        test_counter = 0

        """Let's create a test DB"""
        self._config = configuration.TestConfig()
        self._db = ServersDB(self._config)

        if os.path.isfile(self._config.get_db_file()):
            os.remove(self._config.get_db_file())
            time.sleep(2.5)  # sleep time in seconds

        for word in self._test_words:
            env = choice(['dev', 'itg', 'pro'])
            role = choice(['app', 'db', 'web', 'zoo'])
            server_data = {}
            test_counter += 1
            server_data['host'] = "{}{}.{}{}".format(word, word, word, word)
            server_data['environment'] = env
            server_data['role'] = role
            server_data['location'] = "{}.{}-{}.{}".format(word, word, word, word)

            if len(self._db.get_servers('host', server_data['host'])) == 0:
                new_server = Server(server_data)
                self._db.add_new_server(new_server)

            if test_counter == self._max_tests:
                break

    def tearDown(self):
        """Cleaning up DB Tests"""
        if self._config and os.path.isfile(self._config.get_db_file()):
            os.remove(self._config.get_db_file())
            time.sleep(2.5)  # sleep time in seconds

        self._db = None
        self._config = None

    def test_dev_instance(self):
        """Testing Development instance"""
        self._config = configuration.DevConfig()
        self._db = ServersDB(self._config)

        self.assertIsInstance(self._db, ServersDB)

    def test_test_instance(self):
        """Testing Test instance"""
        self._config = configuration.TestConfig()
        self._db = ServersDB(self._config)

        self.assertIsInstance(self._db, ServersDB)

    def test_prod_instance(self):
        """Testing Production instance"""
        self._config = configuration.ProdConfig()
        self._db = ServersDB(self._config)

        self.assertIsInstance(self._db, ServersDB)

    def test_server_object(self):
        """Testing ServerDB object"""
        test_counter = 0

        for word in self._test_words:
            env = choice(['dev', 'itg', 'pro'])
            role = choice(['app', 'db', 'web', 'zoo'])
            server_data = {}
            test_counter += 1
            server_data['host'] = "{}{}.{}{}".format(word, word, word, word)
            server_data['environment'] = env
            server_data['role'] = role
            server_data['location'] = "{}.{}-{}.{}".format(word, word, word, word)

            new_server = Server(server_data)

            self.assertIsInstance(new_server, Server)
            self.assertEqual(new_server.get_data(), server_data)

            if test_counter == self._max_tests:
                break

    def test_server_exceptions(self):
        """Testing Server exceptions"""
        test_counter = 0

        for word in self._test_words:
            env = choice(['dev', 'itg', 'pro'])
            server_data = {}
            test_counter += 1
            server_data['host'] = "{}{}.{}{}".format(word, word, word, word)
            server_data['environment'] = env
            server_data['location'] = "{}.{}-{}.{}".format(word, word, word, word)

            with self.assertRaises(Exception) as context:
                Server(server_data)

            self.assertTrue('Invalid data' in str(context.exception))

            if test_counter == self._max_tests:
                break

    def test_add_servers_exceptions(self):
        """Testing exceptions when adding new server records"""
        self._config = configuration.TestConfig()
        self._db = ServersDB(self._config)

        test_counter = 0

        for word in self._test_words:
            test_counter += 1
            host = "{}{}.{}{}".format(word, word, word, word)

            record = self._db.get_servers('host', host)

            if len(record) == 1:
                with self.assertRaises(Exception) as context:
                    self._db.add_new_server(record[0])

                self.assertTrue('Duplicated data' in str(context.exception))

            if test_counter == self._max_tests:
                break

    def test_add_servers(self):
        """Testing add new server records"""
        self._config = configuration.TestConfig()
        self._db = ServersDB(self._config)

        test_counter = 0

        for word in self._test_words:
            env = choice(['dev', 'itg', 'pro'])
            role = choice(['app', 'db', 'web', 'zoo'])
            server_data = {}
            test_counter += 1
            server_data['host'] = "{}{}.{}{}.net".format(word, word, word, word)
            server_data['environment'] = env
            server_data['role'] = role
            server_data['location'] = "{}.{}-{}.{}".format(word, word, word, word)

            new_server = Server(server_data)

            self._db.add_new_server(new_server)

            inserted = self._db.get_servers('host', server_data['host'])

            self.assertEqual(inserted[0].get_data(), new_server.get_data())

            if test_counter == self._max_tests:
                break

    def test_update_servers(self):
        """Testing update server records"""
        self._config = configuration.TestConfig()
        self._db = ServersDB(self._config)

        test_counter = 0

        for word in self._test_words:
            server_data = {}

            test_counter += 1

            server_data['host'] = "{}{}.{}{}".format(word, word, word, word)

            record = self._db.get_servers('host', server_data['host'])

            if len(record) == 1:
                _data = record[0].get_data()
                _data['location'] = "{}{}-{}{}".format(word, word, word, word)
                new_data = Server(_data)
                self._db.update_server(new_data)

                updated = self._db.get_servers('host', server_data['host'])

                self.assertEqual(new_data.get_data(), updated[0].get_data())

            if test_counter == self._max_tests:
                break

    def test_delete_server(self):
        """Testing delete server records"""
        self._config = configuration.TestConfig()
        self._db = ServersDB(self._config)

        test_counter = 0

        for word in self._test_words:
            test_counter += 1
            host = "{}{}.{}{}".format(word, word, word, word)

            record = self._db.get_servers('host', host)

            if len(record) == 1:
                self._db.delete_server(record[0])

                erased = self._db.get_servers('host', host)

                self.assertEqual(0, len(erased))

            if test_counter == 10:
                break