Пример #1
0
    def test_from_to_json_with_gate(self, mock_get_now, mock_uuid):
        mock_get_now.return_value = get_now()
        mock_uuid.return_value = '22cd859d-ee91-4079-a112-000000000002'
        s = Server('server2', id='22cd859d-ee91-4079-a112-000000000001')

        gate = mock.MagicMock()
        gate.to_json.return_value = {'server_id': 1, 'server': 'n1'}

        type(s).gates = mock.PropertyMock(return_value=[gate])

        self.assertDictEqual(
            {
                'id':
                '22cd859d-ee91-4079-a112-000000000001',
                'name':
                'server2',
                'granules': [],
                'gates': [{}],
                'created_on':
                mock_get_now.return_value.strftime(defaults.DATETIME_FORMAT),
                '_old_name':
                None,
                'deleted':
                False
            }, s.to_json(add_gates=True))
        db.session.add(s)
        db.session.commit()

        s_json = s.to_json(add_gates=True)
        smashed = Server.from_json(s_json)

        self.assertIs(s, smashed)
        self.assertEqual(s.id, smashed.id)
        self.assertEqual(s.name, smashed.name)
        self.assertEqual(s.granules, smashed.granules)
        self.assertEqual(s.last_modified_at, smashed.last_modified_at)
        self.assertListEqual(s.gates, smashed.gates)

        # from new Server
        db.session.remove()
        db.drop_all()
        db.create_all()

        with patch('dimensigon.domain.entities.server.Gate.from_json'
                   ) as mock_gate_from_json:
            smashed = Server.from_json(s_json)

            self.assertEqual(s.id, smashed.id)
            self.assertEqual(s.name, smashed.name)
            self.assertEqual(s.granules, smashed.granules)
            self.assertEqual(s.last_modified_at, smashed.last_modified_at)
            self.assertEqual(1, len(smashed.gates))
            mock_gate_from_json.assert_called_once()
Пример #2
0
    def test_to_from_json(self, mock_uuid, mock_get_now):
        now = get_now()
        mock_get_now.return_value = now
        mock_uuid.side_effect = [
            '22cd859d-ee91-4079-a112-000000000001',
            '22cd859d-ee91-4079-a112-000000000002',
            '22cd859d-ee91-4079-a112-000000000003'
        ]

        s = Server('server',
                   dns_or_ip='dns',
                   gates=[('gdns', 6000)],
                   created_on=now)
        self.assertDictEqual(
            {
                'id': '22cd859d-ee91-4079-a112-000000000001',
                'name': 'server',
                'granules': [],
                'created_on': now.strftime(defaults.DATETIME_FORMAT),
                'deleted': False,
                '_old_name': None
            }, s.to_json())

        self.assertDictEqual(
            {
                'id': '22cd859d-ee91-4079-a112-000000000001',
                'name': 'server',
                'granules': [],
                'created_on': now.strftime(defaults.DATETIME_FORMAT),
                'ignore_on_lock': False,
            }, s.to_json(no_delete=True, add_ignore=True))
        db.session.add(s)
        db.session.commit()
        db.session.remove()
        del s
        s = Server.query.get('22cd859d-ee91-4079-a112-000000000001')
        self.assertDictEqual(
            {
                'id': '22cd859d-ee91-4079-a112-000000000001',
                'name': 'server',
                'granules': [],
                'last_modified_at': now.strftime(defaults.DATEMARK_FORMAT),
                'created_on': now.strftime(defaults.DATETIME_FORMAT),
            }, s.to_json(no_delete=True))

        smashed = Server.from_json(s.to_json())

        self.assertIs(s, smashed)
        self.assertEqual(s.id, smashed.id)
        self.assertEqual(s.name, smashed.name)
        self.assertEqual(s.granules, smashed.granules)
        self.assertEqual(s.last_modified_at, smashed.last_modified_at)
    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)