Exemplo n.º 1
0
    def test_catalog(self, mock_now, mock_get):
        mock_now.return_value = dt.datetime(2019, 4, 2, tzinfo=dt.timezone.utc)
        mock_get.return_value = [('ActionTemplate', ActionTemplate)]

        at1 = ActionTemplate(name='ActionTest1', version=1, action_type=ActionType.ORCHESTRATION, code='')
        db.session.add(at1)
        db.session.commit()

        resp = self.client.get(
            url_for('api_1_0.catalog',
                    data_mark=dt.datetime(2019, 4, 1, tzinfo=dt.timezone.utc).strftime(defaults.DATEMARK_FORMAT)),
            headers=self.auth.header)

        self.assertDictEqual({'ActionTemplate': [at1.to_json()]},
                             resp.get_json())

        mock_now.return_value = dt.datetime(2019, 4, 2, 1, tzinfo=dt.timezone.utc)
        at2 = ActionTemplate(name='ActionTest2', version=1, action_type=ActionType.ORCHESTRATION, code='')
        db.session.add(at2)
        db.session.commit()

        resp = self.client.get(
            url_for('api_1_0.catalog',
                    data_mark=dt.datetime(2019, 4, 2, tzinfo=dt.timezone.utc).strftime(defaults.DATEMARK_FORMAT)),
            headers=self.auth.header)

        self.assertDictEqual({'ActionTemplate': [at2.to_json()]},
                             resp.get_json())
Exemplo n.º 2
0
    def setUp(self, mocked_now):
        self.initials = dict(self.initials)
        self.initials.update(action_template=False)
        mocked_now.return_value = now1
        super().setUp()
        with self.app2_context:
            mocked_now.return_value = now2
            soft = Software(id='aaaaaaaa-1234-5678-1234-56781234aaa1', name='test', version='1', filename='file')
            at = ActionTemplate(id='aaaaaaaa-1234-5678-1234-56781234aaa2', name='mkdir', version=1,
                                action_type=ActionType.SHELL, code='mkdir {dir}')
            db.session.add_all([soft, at])
            db.session.commit()
            mocked_now.return_value = now3
            ssa = SoftwareServerAssociation(software=soft, server=Server.get_current(), path='/root')
            db.session.add(ssa)
            db.session.commit()
            self.soft_json = soft.to_json()
            self.at_json = at.to_json()
            self.catalog = fetch_catalog(now1)

        self.mock_queue = mock.Mock()
        self.mock_dm = mock.Mock()
        self.mock_dm.flask_app = self.app
        self.mock_dm.engine = db.engine
        self.mock_dm.manager.dict.return_value = dict()
        self.mock_dm.server_id = self.s1.id

        self.cm = CatalogManager("Catalog", startup_event=threading.Event(), shutdown_event=threading.Event(),
                                 publish_q=self.mock_queue, event_q=None, dimensigon=self.mock_dm)
        db.session.commit()
Exemplo n.º 3
0
class TestActionTemplate(TestDimensigonBase):
    def setUp(self) -> None:
        self.initials = dict(self.initials)
        self.initials.update(action_template=False)
        super().setUp()

    def fill_database(self):

        self.at1 = ActionTemplate(id="aaaaaaaa-1234-5678-1234-56781234aaa1",
                                  action_type=ActionType.SHELL,
                                  code="mkdir {dir}",
                                  last_modified_at=defaults.INITIAL_DATEMARK,
                                  name="mkdir",
                                  version=1)

        self.at2 = ActionTemplate(id="aaaaaaaa-1234-5678-1234-56781234aaa2",
                                  action_type=ActionType.SHELL,
                                  code="rmdir {dir}",
                                  last_modified_at=defaults.INITIAL_DATEMARK,
                                  expected_stdout='output',
                                  expected_stderr='err',
                                  expected_rc=0,
                                  name="rmdir",
                                  system_kwargs={'kwarg1': 1},
                                  pre_process='pre_process',
                                  post_process='post_process',
                                  version=1)

        with bypass_datamark_update():
            db.session.add_all([self.at1, self.at2])
            db.session.commit()

    def test_action_template_list(self):
        response = self.client.get(url_for('api_1_0.actiontemplatelist'),
                                   headers=self.auth.header)

        self.assertListEqual(
            [self.at1.to_json(), self.at2.to_json()], response.get_json())

    def test_action_template(self):
        response = self.client.get(url_for(
            'api_1_0.actiontemplateresource',
            action_template_id="aaaaaaaa-1234-5678-1234-56781234aaa1"),
                                   headers=self.auth.header)

        self.assertDictEqual(self.at1.to_json(), response.get_json())
    def test_upgrade_catalog_catalog_mismatch(self, mock_lock, mock_entities):
        mock_lock.return_value.__enter__.return_value = 'applicant'
        mock_entities.return_value = [('ActionTemplate', ActionTemplate),
                                      ('Server', Server)]

        s = Server('server',
                   last_modified_at=dt.datetime(2019,
                                                4,
                                                1,
                                                tzinfo=dt.timezone.utc),
                   port=8000)
        Route(s, cost=0)

        at1 = ActionTemplate(id='aaaaaaaa-1234-5678-1234-56781234aaa1',
                             name='mkdir',
                             version=1,
                             action_type=ActionType.SHELL,
                             code='mkdir {dir}',
                             expected_output=None,
                             expected_rc=None,
                             system_kwargs={},
                             last_modified_at=dt.datetime(
                                 2019, 4, 1, tzinfo=dt.timezone.utc))

        at2 = ActionTemplate(id='aaaaaaaa-1234-5678-1234-56781234aaa2',
                             name='rmdir',
                             version=1,
                             action_type=ActionType.SHELL,
                             code='rmdir {dir}',
                             expected_output=None,
                             expected_rc=None,
                             system_kwargs={},
                             last_modified_at=dt.datetime(
                                 2019, 4, 2, tzinfo=dt.timezone.utc))

        responses.add(
            method='GET',
            url=re.compile('^' + s.url(
                'api_1_0.catalog', data_mark='12345').replace('12345', '')),
            json={"ActionTemplate": [at1.to_json(),
                                     at2.to_json()]})

        with self.assertRaises(errors.CatalogMismatch):
            upgrade_catalog_from_server(s)
Exemplo n.º 5
0
    def test_to_from_json(self):
        now = defaults.INITIAL_DATEMARK
        at = ActionTemplate(id='aaaaaaaa-1234-5678-1234-56781234aaa1',
                            name='ActionTest2',
                            version=1,
                            action_type=ActionType.ORCHESTRATION,
                            code='test code',
                            last_modified_at=now)

        db.session.add(at)

        at_json = at.to_json()

        self.assertDictEqual(
            dict(id='aaaaaaaa-1234-5678-1234-56781234aaa1',
                 name='ActionTest2',
                 version=1,
                 action_type='ORCHESTRATION',
                 code='test code',
                 last_modified_at=now.strftime(defaults.DATEMARK_FORMAT)),
            at_json)

        at_json['name'] = "ChangedAction"

        smashed = ActionTemplate.from_json(at_json)

        self.assertEqual(at.id, smashed.id)
        self.assertIsNotNone(smashed.id)
        self.assertEqual("ChangedAction", smashed.name)
        self.assertIsNotNone(smashed.name)
        self.assertEqual(at.version, smashed.version)
        self.assertIsNotNone(smashed.version)
        self.assertEqual(at.action_type, smashed.action_type)
        self.assertIsNotNone(smashed.action_type)
        self.assertEqual(at.code, smashed.code)
        self.assertIsNotNone(smashed.code)
        self.assertEqual(at.last_modified_at, smashed.last_modified_at)
        self.assertIsNotNone(smashed.last_modified_at)

        db.session.commit()

        del at
        del smashed

        at = ActionTemplate.query.get(at_json['id'])
        self.assertEqual("ChangedAction", at.name)
    def test_upgrade_catalog(self, mock_lock, mock_entities, mock_now,
                             mock_tzlocal):
        mock_lock.return_value.__enter__.return_value = 'applicant'
        mock_entities.return_value = [('ActionTemplate', ActionTemplate),
                                      ('Server', Server), ('Gate', Gate)]
        mock_now.return_value = dt.datetime(2019, 4, 1, tzinfo=dt.timezone.utc)
        mock_tzlocal.return_value = dt.timezone.utc

        at1 = ActionTemplate(id='aaaaaaaa-1234-5678-1234-56781234aaa1',
                             name='mkdir',
                             version=1,
                             action_type=ActionType.SHELL,
                             code='mkdir {dir}',
                             expected_output=None,
                             expected_rc=None,
                             system_kwargs={})
        db.session.add(at1)
        db.session.commit()

        s = Server(id='aaaaaaaa-1234-5678-1234-56781234bbb1',
                   name='server',
                   last_modified_at=dt.datetime(2019,
                                                4,
                                                1,
                                                tzinfo=dt.timezone.utc))
        s_json = s.to_json()
        g = Gate(server=s,
                 port=80,
                 dns='server',
                 last_modified_at=dt.datetime(2019,
                                              4,
                                              1,
                                              tzinfo=dt.timezone.utc))
        g_json = g.to_json()
        Route(s, cost=0)

        at2 = ActionTemplate(id='aaaaaaaa-1234-5678-1234-56781234aaa2',
                             name='rmdir',
                             version=1,
                             action_type=ActionType.SHELL,
                             code='rmdir {dir}',
                             expected_output=None,
                             expected_rc=None,
                             system_kwargs={},
                             last_modified_at=dt.datetime(
                                 2019, 4, 2, tzinfo=dt.timezone.utc))
        at2_json = at2.to_json()
        del at2

        at1_json = at1.to_json()
        del at1
        at1_json['code'] = 'mkdir -p {dir}'
        responses.add(
            method='GET',
            url=re.compile('^' + s.url(
                'api_1_0.catalog', data_mark='12345').replace('12345', '')),
            json={
                "ActionTemplate": [at1_json, at2_json],
                "Server": [s_json],
                "Gate": [g_json]
            })

        upgrade_catalog_from_server(s)

        db.session.expire_all()
        atl = [at.to_json() for at in ActionTemplate.query.all()]

        self.assertListEqual([at1_json, at2_json], atl)

        c = Catalog.query.get('ActionTemplate')
        self.assertEqual(dt.datetime(2019, 4, 2, tzinfo=dt.timezone.utc),
                         c.last_modified_at)

        at1 = ActionTemplate.query.get('aaaaaaaa-1234-5678-1234-56781234aaa1')
        self.assertEqual('mkdir -p {dir}', at.code)