Beispiel #1
0
    def setUp(self):
        """Create and configure a new app instance for each test."""
        # create the app with common test config
        self.app = create_app('test')
        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()
        self.headers = {
            "Authorization":
            f"Bearer {create_access_token('00000000-0000-0000-0000-000000000001')}"
        }

        db.create_all()
        set_initial()

        self.soft = Software(id='aaaaaaaa-1234-5678-1234-56781234aaa1',
                             name='test',
                             version='1',
                             filename='file')
        self.soft2 = Software(id='aaaaaaaa-1234-5678-1234-56781234aaa2',
                              name='test',
                              version='2',
                              filename='file')
        self.ssa = SoftwareServerAssociation(software=self.soft,
                                             server=Server.get_current(),
                                             path='/root')
        db.session.add_all([self.soft, self.soft2, self.ssa])
        db.session.commit()
    def test_execute_send_software(self, mock_post, mock_get):
        at = ActionTemplate.query.get('00000000-0000-0000-000a-000000000001')
        soft = Software(name='test', version=1, filename='test.zip')
        node1 = Server('nodeA', port=5000)
        node2 = Server('nodeB', port=5000)
        ssa1 = SoftwareServerAssociation(software=soft, server=node1, path='/')
        ssa2 = SoftwareServerAssociation(software=soft, server=node2, path='/')
        ssa3 = SoftwareServerAssociation(software=soft,
                                         server=self.s1,
                                         path='/')
        db.session.add_all([soft, node1, node2, ssa1, ssa2, ssa3])

        mock_post.return_value = Response(msg={'transfer_id': 1},
                                          code=at.expected_rc)
        mock_get.return_value = Response(msg={
            "route_list": [{
                "cost": 0,
                "destination_id": f"{node1.id}",
            }, {
                "cost": 1,
                "destination_id": f"{self.s1.id}",
            }],
        },
                                         code=200,
                                         server=node2)

        ro = NativeSoftwareSendOperation(code,
                                         expected_stdout=at.expected_stdout,
                                         expected_stderr=at.expected_stderr,
                                         expected_rc=at.expected_rc)

        cp = ro._execute(dict(input=dict(software=soft.id,
                                         server=node2.id,
                                         dest_path='dest',
                                         chunk_size=20,
                                         max_senders=2)),
                         timeout=None,
                         context=self.context)

        mock_post.assert_called_once_with(node1,
                                          'api_1_0.send',
                                          json=dict(software_id=str(soft.id),
                                                    dest_server_id=str(
                                                        node2.id),
                                                    background=False,
                                                    include_transfer_data=True,
                                                    force=True,
                                                    dest_path='dest',
                                                    chunk_size=20,
                                                    max_senders=2),
                                          timeout=None,
                                          identity=ROOT)
        self.assertTrue(cp.success)
        self.assertEqual(flask.json.dumps(mock_post.return_value.msg),
                         cp.stdout)
Beispiel #3
0
    def setUp(self) -> None:
        super().setUp()

        self.source_path = '/software'
        self.filename = 'filename.zip'
        self.content = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
        self.size = len(self.content)
        self.checksum = hashlib.md5(self.content).hexdigest()

        self.soft = Software(name='test_software',
                             version=1,
                             filename='software.zip',
                             size=self.size,
                             checksum=self.checksum)

        self.ssa = SoftwareServerAssociation(software=self.soft,
                                             server=self.s1,
                                             path=self.source_path)

        self.dest_path = '/dest_repo'

        db.session.add(self.soft)
        db.session.add(self.ssa)
        db.session.commit()

        self.setUpPyfakefs()
        self.fs.create_dir(self.source_path)
        self.fs.create_dir(self.dest_path)
        self.fs.create_file(os.path.join(self.source_path, self.filename),
                            contents=self.content)
        self.fs.create_file(os.path.join(self.source_path, self.soft.filename),
                            contents=self.content)
        self.app.config['DEBUG'] = False
    def test_execute_send_software_error(self, mock_post, mock_get):
        at = ActionTemplate.query.get('00000000-0000-0000-000a-000000000001')
        soft = Software(name='test', version=1, filename='test.zip')
        node1 = Server('nodeA', port=5000)
        ssa1 = SoftwareServerAssociation(software=soft, server=node1, path='/')
        db.session.add_all([soft, node1, ssa1])

        mock_post.return_value = Response(msg={'error': 'message'}, code=400)
        mock_get.return_value = Response(code=400)

        ro = NativeSoftwareSendOperation(code,
                                         expected_stdout=at.expected_stdout,
                                         expected_stderr=at.expected_stderr,
                                         expected_rc=at.expected_rc)

        cp = ro._execute(
            dict(input=dict(software=str(soft.id), server=str(node1.id))),
            timeout=10,
            context=self.context)

        mock_post.assert_called_once_with(node1,
                                          'api_1_0.send',
                                          json=dict(software_id=str(soft.id),
                                                    dest_server_id=str(
                                                        node1.id),
                                                    background=False,
                                                    include_transfer_data=True,
                                                    force=True),
                                          timeout=10,
                                          identity=ROOT)
        self.assertFalse(cp.success)
        self.assertEqual(flask.json.dumps(mock_post.return_value.msg),
                         cp.stdout)
Beispiel #5
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()
Beispiel #6
0
 def fill_database(self):
     soft = Software(name='test_software',
                     version=1,
                     filename=self.filename,
                     size=self.size,
                     checksum=self.checksum,
                     id=self.soft_id)
     ssa = SoftwareServerAssociation(software=soft,
                                     server_id=self.SERVER1,
                                     path=self.source_path)
     db.session.add_all([soft, ssa])
Beispiel #7
0
def set_software_server(soft, server, path, recalculate_data=False):
    file = os.path.join(path, soft.filename)
    if not os.path.exists(file):
        raise errors.FileNotFound(file)

    if soft.size != os.path.getsize(file):
        return errors.GenericError(f"file is not of specified size",
                                   file=file,
                                   size=soft.size)
    if soft.checksum != md5(file):
        return errors.GenericError(f"checksum error on file", file=file)

    return SoftwareServerAssociation(software=soft, server=server, path=path)
    def fill_database(self):
        soft = Software(name="python",
                        version="3.8.3",
                        family="programming",
                        filename=self.filename,
                        size=self.size,
                        checksum=self.checksum,
                        id=self.SOFTWARE,
                        last_modified_at=defaults.INITIAL_DATEMARK.strftime(
                            defaults.DATEMARK_FORMAT))

        ssa = SoftwareServerAssociation(
            software_id=self.SOFTWARE,
            server_id=self.SERVER1,
            path=self.source_folder,
            last_modified_at=defaults.INITIAL_DATEMARK.strftime(
                defaults.DATEMARK_FORMAT))

        o = Orchestration(name="install python 3.8.3",
                          version=1,
                          description="installs python 3.8.3",
                          id=self.ORCH,
                          last_modified_at=defaults.INITIAL_DATEMARK.strftime(
                              defaults.DATEMARK_FORMAT))

        s1 = o.add_step(id=self.STEP1,
                        action_template=ActionTemplate.query.get(
                            ActionTemplate.SEND_SOFTWARE),
                        undo=False,
                        undo_on_error=False,
                        schema={
                            "mapping": {
                                "software": self.SOFTWARE,
                                "server": {
                                    "from": "env.server_id"
                                }
                            }
                        },
                        last_modified_at=defaults.INITIAL_DATEMARK.strftime(
                            defaults.DATEMARK_FORMAT))
        o.add_step(id=self.STEP2,
                   undo=False,
                   undo_on_error=False,
                   parents=[s1],
                   code="{{input.file}}",
                   action_type="SHELL",
                   expected_rc=0,
                   last_modified_at=defaults.INITIAL_DATEMARK.strftime(
                       defaults.DATEMARK_FORMAT))
        db.session.add_all([soft, ssa, o])
Beispiel #9
0
    def test_from_json_new(self):
        ssa_json = dict(software_id='aaaaaaaa-1234-5678-1234-56781234aaa2',
                        server_id=str(Server.get_current().id),
                        path='/root',
                        last_modified_at=defaults.INITIAL_DATEMARK.strftime(
                            defaults.DATEMARK_FORMAT))

        smashed = SoftwareServerAssociation.from_json(ssa_json)

        self.assertEqual(defaults.INITIAL_DATEMARK, smashed.last_modified_at)
        self.assertIsNotNone(smashed.last_modified_at)
        self.assertEqual("/root", smashed.path)
        self.assertIsNotNone(smashed.path)
        self.assertEqual(Server.get_current(), smashed.server)
        self.assertIsNotNone(smashed.server)
        self.assertEqual(self.soft2, smashed.software)
        self.assertIsNotNone(smashed.software)
Beispiel #10
0
    def put(self, software_id):
        json = request.get_json()

        soft = Software.query.get_or_raise(software_id)

        # delete all associations
        for ssa in soft.ssas:
            ssa.delete()

        for ssa_json in json:
            server = Server.query.get_or_raise(ssa_json['server_id'])
            ssa = SoftwareServerAssociation(software=soft,
                                            server=server,
                                            path=ssa_json['path'])
            db.session.add(ssa)

        db.session.commit()
        return {}, 204
    def test_execute_send_software_no_destination_server(self):
        at = ActionTemplate.query.get('00000000-0000-0000-000a-000000000001')
        soft = Software(name='test', version='1', filename='test.zip')
        soft2 = Software(name='test', version='2', filename='test.zip')
        node1 = Server('nodeA', port=5000)
        ssa1 = SoftwareServerAssociation(software=soft2,
                                         server=node1,
                                         path='/')
        db.session.add_all([soft, soft2, node1, ssa1])

        ro = NativeSoftwareSendOperation(code,
                                         expected_stdout=at.expected_stdout,
                                         expected_stderr=at.expected_stderr,
                                         expected_rc=at.expected_rc)

        cp = ro._execute(dict(input=dict(software='test', server='a')),
                         context=self.context)

        self.assertFalse(cp.success)
        self.assertEqual(f"destination server 'a' not found", cp.stderr)
Beispiel #12
0
    def test_to_from_json(self):
        ssa_json = self.ssa.to_json()
        ssa_json['path'] = '/new_root'

        smashed = SoftwareServerAssociation.from_json(ssa_json)

        self.assertEqual("/new_root", smashed.path)
        self.assertIsNotNone(smashed.path)
        self.assertEqual(Server.get_current(), smashed.server)
        self.assertIsNotNone(smashed.server)
        self.assertEqual(self.soft, smashed.software)
        self.assertIsNotNone(smashed.software)
        self.assertEqual(self.ssa.last_modified_at, smashed.last_modified_at)
        self.assertIsNotNone(smashed.last_modified_at)

        db.session.commit()

        del smashed

        ssa = SoftwareServerAssociation.query.get(
            ('aaaaaaaa-1234-5678-1234-56781234aaa1', Server.get_current().id))
        self.assertEqual("/new_root", ssa.path)
Beispiel #13
0
    def setUp(self):
        """Create and configure a new app instance for each test."""
        # create the app with common test config
        super().setUp()

        self.source_path = '/software'
        self.filename = 'filename.zip'
        self.content = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
        self.size = len(self.content)
        self.checksum = hashlib.md5(self.content).hexdigest()
        self.dest_path = '/dest_repo'

        self.soft = Software(name='test_software',
                             version=1,
                             filename=self.filename,
                             size=self.size,
                             checksum=self.checksum)
        self.ssa = SoftwareServerAssociation(software=self.soft,
                                             server=self.s1,
                                             path=self.source_path)
        self.node2 = Server('node2', port=5000)

        db.session.add_all([self.soft, self.ssa, self.node2])
        db.session.commit()
Beispiel #14
0
class TestApi(TestCase):
    def setUp(self):
        """Create and configure a new app instance for each test."""
        # create the app with common test config
        self.app = create_app('test')
        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()
        self.headers = {
            "Authorization":
            f"Bearer {create_access_token('00000000-0000-0000-0000-000000000001')}"
        }

        db.create_all()
        set_initial()

        self.soft = Software(id='aaaaaaaa-1234-5678-1234-56781234aaa1',
                             name='test',
                             version='1',
                             filename='file')
        self.soft2 = Software(id='aaaaaaaa-1234-5678-1234-56781234aaa2',
                              name='test',
                              version='2',
                              filename='file')
        self.ssa = SoftwareServerAssociation(software=self.soft,
                                             server=Server.get_current(),
                                             path='/root')
        db.session.add_all([self.soft, self.soft2, self.ssa])
        db.session.commit()

    def tearDown(self) -> None:
        db.session.remove()
        db.drop_all()
        self.app_context.pop()

    def test_to_from_json(self):
        ssa_json = self.ssa.to_json()
        ssa_json['path'] = '/new_root'

        smashed = SoftwareServerAssociation.from_json(ssa_json)

        self.assertEqual("/new_root", smashed.path)
        self.assertIsNotNone(smashed.path)
        self.assertEqual(Server.get_current(), smashed.server)
        self.assertIsNotNone(smashed.server)
        self.assertEqual(self.soft, smashed.software)
        self.assertIsNotNone(smashed.software)
        self.assertEqual(self.ssa.last_modified_at, smashed.last_modified_at)
        self.assertIsNotNone(smashed.last_modified_at)

        db.session.commit()

        del smashed

        ssa = SoftwareServerAssociation.query.get(
            ('aaaaaaaa-1234-5678-1234-56781234aaa1', Server.get_current().id))
        self.assertEqual("/new_root", ssa.path)

    def test_from_json_new(self):
        ssa_json = dict(software_id='aaaaaaaa-1234-5678-1234-56781234aaa2',
                        server_id=str(Server.get_current().id),
                        path='/root',
                        last_modified_at=defaults.INITIAL_DATEMARK.strftime(
                            defaults.DATEMARK_FORMAT))

        smashed = SoftwareServerAssociation.from_json(ssa_json)

        self.assertEqual(defaults.INITIAL_DATEMARK, smashed.last_modified_at)
        self.assertIsNotNone(smashed.last_modified_at)
        self.assertEqual("/root", smashed.path)
        self.assertIsNotNone(smashed.path)
        self.assertEqual(Server.get_current(), smashed.server)
        self.assertIsNotNone(smashed.server)
        self.assertEqual(self.soft2, smashed.software)
        self.assertIsNotNone(smashed.software)