Esempio n. 1
0
    def test_booting_state(self):
        matrix = [
            (ResourceStatus.Booting, None, BootingState),
            (ResourceStatus.Running, None, IntegrateState),
            (ResourceStatus.Deleted, None, DownState),
            (ResourceStatus.Stopped, None, CleanupState),
            (ResourceStatus.Error, None, CleanupState),
        ]

        self.run_the_matrix(matrix, initial_state=BootingState)

        self.run_side_effects(
            BootingState(),
            self.drone.site_agent.resource_status,
            (TardisAuthError, TardisTimeout, TardisResourceStatusUpdateFailed),
            BootingState,
        )

        self.run_side_effects(
            BootingState(),
            self.drone.site_agent.resource_status,
            (TardisDroneCrashed, ),
            CleanupState,
        )

        # Test draining procedure if cobald sets drone demand to zero
        self.drone.demand = 0.0
        self.drone.state.return_value = BootingState()
        run_async(self.drone.state.return_value.run, self.drone)
        self.assertIsInstance(self.drone.state, CleanupState)
        self.assertEqual(self.drone._supply, 0.0)
Esempio n. 2
0
    def setUpClass(cls):
        cls.test_site_name = "MyGreatTestSite"
        cls.test_machine_type = "MyGreatTestMachineType"
        cls.tables_in_db = {
            "MachineTypes", "Resources", "ResourceStates", "Sites"
        }
        cls.test_resource_attributes = {
            "remote_resource_uuid": "bf85022b-fdd6-42b1-932d-086c288d4755",
            "drone_uuid": f"{cls.test_site_name}-07af52405e",
            "site_name": cls.test_site_name,
            "machine_type": cls.test_machine_type,
            "created": datetime.datetime(2018, 11, 16, 15, 49, 58),
            "updated": datetime.datetime(2018, 11, 16, 15, 49, 58),
        }
        cls.test_updated_resource_attributes = {
            "remote_resource_uuid": "bf85022b-fdd6-42b1-932d-086c288d4755",
            "drone_uuid": f"{cls.test_site_name}-07af52405e",
            "site_name": cls.test_site_name,
            "machine_type": cls.test_machine_type,
            "created": datetime.datetime(2018, 11, 16, 15, 49, 58),
            "updated": datetime.datetime(2018, 11, 16, 15, 50, 58),
        }

        cls.test_get_resources_result = {
            "remote_resource_uuid":
            cls.test_resource_attributes["remote_resource_uuid"],
            "drone_uuid":
            cls.test_resource_attributes["drone_uuid"],
            "state":
            str(BootingState()),
            "created":
            cls.test_resource_attributes["created"],
            "updated":
            cls.test_resource_attributes["updated"],
        }

        cls.test_notify_result = (
            cls.test_resource_attributes["remote_resource_uuid"],
            cls.test_resource_attributes["drone_uuid"],
            str(BootingState()),
            cls.test_resource_attributes["site_name"],
            cls.test_resource_attributes["machine_type"],
            str(cls.test_resource_attributes["created"]),
            str(cls.test_resource_attributes["updated"]),
        )

        cls.test_updated_notify_result = (
            cls.test_updated_resource_attributes["remote_resource_uuid"],
            cls.test_updated_resource_attributes["drone_uuid"],
            str(IntegrateState()),
            cls.test_updated_resource_attributes["site_name"],
            cls.test_updated_resource_attributes["machine_type"],
            str(cls.test_updated_resource_attributes["created"]),
            str(cls.test_updated_resource_attributes["updated"]),
        )

        cls.mock_config_patcher = patch(
            "tardis.plugins.sqliteregistry.Configuration")
        cls.mock_config = cls.mock_config_patcher.start()
Esempio n. 3
0
    def setUpClass(cls):
        cls.test_site_name = 'MyGreatTestSite'
        cls.test_machine_type = 'MyGreatTestMachineType'
        cls.tables_in_db = {
            'MachineTypes', 'Resources', 'ResourceStates', 'Sites'
        }
        cls.test_resource_attributes = {
            'remote_resource_uuid': 'bf85022b-fdd6-42b1-932d-086c288d4755',
            'drone_uuid': f'{cls.test_site_name}-07af52405e',
            'site_name': cls.test_site_name,
            'machine_type': cls.test_machine_type,
            'created': datetime.datetime(2018, 11, 16, 15, 49, 58),
            'updated': datetime.datetime(2018, 11, 16, 15, 49, 58)
        }
        cls.test_updated_resource_attributes = {
            'remote_resource_uuid': 'bf85022b-fdd6-42b1-932d-086c288d4755',
            'drone_uuid': f'{cls.test_site_name}-07af52405e',
            'site_name': cls.test_site_name,
            'machine_type': cls.test_machine_type,
            'created': datetime.datetime(2018, 11, 16, 15, 49, 58),
            'updated': datetime.datetime(2018, 11, 16, 15, 50, 58)
        }

        cls.test_get_resources_result = {
            'remote_resource_uuid':
            cls.test_resource_attributes['remote_resource_uuid'],
            'drone_uuid':
            cls.test_resource_attributes['drone_uuid'],
            'state':
            str(BootingState()),
            'created':
            cls.test_resource_attributes['created'],
            'updated':
            cls.test_resource_attributes['updated']
        }

        cls.test_notify_result = (
            cls.test_resource_attributes['remote_resource_uuid'],
            cls.test_resource_attributes['drone_uuid'], str(BootingState()),
            cls.test_resource_attributes['site_name'],
            cls.test_resource_attributes['machine_type'],
            str(cls.test_resource_attributes['created']),
            str(cls.test_resource_attributes['updated']))

        cls.test_updated_notify_result = (
            cls.test_updated_resource_attributes['remote_resource_uuid'],
            cls.test_updated_resource_attributes['drone_uuid'],
            str(IntegrateState()),
            cls.test_updated_resource_attributes['site_name'],
            cls.test_updated_resource_attributes['machine_type'],
            str(cls.test_updated_resource_attributes['created']),
            str(cls.test_updated_resource_attributes['updated']))

        cls.mock_config_patcher = patch(
            'tardis.plugins.sqliteregistry.Configuration')
        cls.mock_config = cls.mock_config_patcher.start()
Esempio n. 4
0
    def test_notify(self):
        def fetch_row(db):
            with sqlite3.connect(db) as connection:
                cursor = connection.cursor()
                cursor.execute(
                    """SELECT R.remote_resource_uuid, R.drone_uuid, RS.state,
                S.site_name, MT.machine_type, R.created, R.updated
                FROM Resources R
                JOIN ResourceStates RS ON R.state_id = RS.state_id
                JOIN Sites S ON R.site_id = S.site_id
                JOIN MachineTypes MT ON R.machine_type_id = MT.machine_type_id"""
                )
                return cursor.fetchone()

        registry = SqliteRegistry()
        registry.add_site(self.test_site_name)
        registry.add_machine_types(self.test_site_name, self.test_machine_type)

        run_async(registry.notify, BootingState(),
                  self.test_resource_attributes)

        self.assertEqual(self.test_notify_result, fetch_row(self.test_db))

        run_async(registry.notify, IntegrateState(),
                  self.test_updated_resource_attributes)

        self.assertEqual(self.test_updated_notify_result,
                         fetch_row(self.test_db))

        run_async(registry.notify, DownState(),
                  self.test_updated_resource_attributes)

        self.assertIsNone(fetch_row(self.test_db))
Esempio n. 5
0
    def test_booting_state(self):
        matrix = [(ResourceStatus.Booting, None, BootingState),
                  (ResourceStatus.Running, None, IntegrateState),
                  (ResourceStatus.Deleted, None, DownState),
                  (ResourceStatus.Stopped, None, CleanupState),
                  (ResourceStatus.Error, None, CleanupState)]

        self.run_the_matrix(matrix, initial_state=BootingState)

        self.run_side_effects(
            BootingState(), self.drone.site_agent.resource_status,
            (TardisAuthError, TardisTimeout, TardisResourceStatusUpdateFailed),
            BootingState)

        self.run_side_effects(BootingState(),
                              self.drone.site_agent.resource_status,
                              (TardisDroneCrashed, ), CleanupState)
Esempio n. 6
0
    def test_get_resources(self):
        registry = SqliteRegistry()
        registry.add_site(self.test_site_name)
        registry.add_machine_types(self.test_site_name, self.test_machine_type)
        run_async(registry.notify, BootingState(),
                  self.test_resource_attributes)

        self.assertListEqual(
            registry.get_resources(site_name=self.test_site_name,
                                   machine_type=self.test_machine_type),
            [self.test_get_resources_result])
Esempio n. 7
0
    def test_insert_resources(self):
        # Database has to be queried multiple times
        # Define inline function to re-use code
        def fetch_all():
            return self.execute_db_query(
                sql_query=
                """SELECT R.remote_resource_uuid, R.drone_uuid, RS.state,
                S.site_name, MT.machine_type, R.created, R.updated
                FROM Resources R
                JOIN ResourceStates RS ON R.state_id = RS.state_id
                JOIN Sites S ON R.site_id = S.site_id
                JOIN MachineTypes MT ON R.machine_type_id = MT.machine_type_id"""
            )

        test_site_names = (self.test_site_name, self.other_test_site_name)
        for site_name in test_site_names:
            self.registry.add_site(site_name)
            self.registry.add_machine_types(site_name, self.test_machine_type)

        bind_parameters = {"state": "RequestState"}
        bind_parameters.update(self.test_resource_attributes)

        run_async(self.registry.insert_resource, bind_parameters)

        self.assertListEqual([self.test_notify_result], fetch_all())

        with self.assertRaises(sqlite3.IntegrityError) as ie:
            run_async(self.registry.insert_resource, bind_parameters)
        self.assertTrue("unique" in str(ie.exception).lower())

        self.assertListEqual([self.test_notify_result], fetch_all())

        # Test same remote_resource_uuids on different sites
        bind_parameters = {"state": "BootingState"}
        bind_parameters.update(self.test_resource_attributes)
        bind_parameters[
            "drone_uuid"] = f"{self.other_test_site_name}-045285abef1"
        bind_parameters["site_name"] = self.other_test_site_name

        run_async(self.registry.insert_resource, bind_parameters)

        other_test_notify_result = (
            self.test_resource_attributes["remote_resource_uuid"],
            f"{self.other_test_site_name}-045285abef1",
            str(BootingState()),
            self.other_test_site_name,
            self.test_resource_attributes["machine_type"],
            str(self.test_resource_attributes["created"]),
            str(self.test_resource_attributes["updated"]),
        )

        self.assertListEqual(
            [self.test_notify_result, other_test_notify_result], fetch_all())
Esempio n. 8
0
    def test_notify(self):
        # Database has to be queried multiple times
        # Define inline function to re-use code
        def fetch_all():
            return self.execute_db_query(
                sql_query=
                """SELECT R.remote_resource_uuid, R.drone_uuid, RS.state,
                S.site_name, MT.machine_type, R.created, R.updated
                FROM Resources R
                JOIN ResourceStates RS ON R.state_id = RS.state_id
                JOIN Sites S ON R.site_id = S.site_id
                JOIN MachineTypes MT ON R.machine_type_id = MT.machine_type_id"""
            )

        self.registry.add_site(self.test_site_name)
        self.registry.add_machine_types(self.test_site_name,
                                        self.test_machine_type)

        run_async(self.registry.notify, RequestState(),
                  self.test_resource_attributes)

        self.assertEqual([self.test_notify_result], fetch_all())

        with self.assertRaises(sqlite3.IntegrityError) as ie:
            run_async(self.registry.notify, RequestState(),
                      self.test_resource_attributes)
        self.assertTrue("unique" in str(ie.exception).lower())

        run_async(
            self.registry.notify,
            BootingState(),
            self.test_updated_resource_attributes,
        )

        self.assertEqual([self.test_updated_notify_result], fetch_all())

        run_async(self.registry.notify, DownState(),
                  self.test_updated_resource_attributes)

        self.assertListEqual([], fetch_all())