Example #1
0
class BasicTest(unittest.TestCase):
    """Basic integration test to demonstrate use of service tester.
    
    """
    def setUp(self):
        self.service = Service()
        self.assertTrue(self.service.setup_with_test_account())

    def tearDown(self):
        self.service.teardown()

    def test_basic(self):
        print("Hello world")
class BatchRunnerTest(unittest.TestCase):
    """Integration test of batch running.
    
    """
    def setUp(self):
        self.service = Service()
        self.assertTrue(self.service.setup_with_test_account())

    def tearDown(self):
        self.service.teardown()

    def make_batch(self, state_vec, start_time, end_time):
        start_time_str = start_time.isoformat() + 'Z'
        end_time_str = end_time.isoformat() + 'Z'

        propagation_params = PropagationParams({
            'start_time':
            start_time_str,
            'end_time':
            end_time_str,
            'step_size':
            0,
            'project_uuid':
            self.service.get_working_project().get_uuid(),
            'description':
            'Created by test at ' + start_time_str
        })
        opm_params = OpmParams({
            'epoch': start_time_str,
            'state_vector': state_vec,
            'mass': 500.5,
            'solar_rad_area': 25.2,
            'solar_rad_coeff': 1.2,
            'drag_area': 33.3,
            'drag_coeff': 2.5,
            'originator': 'Test',
            'object_name': 'TestObj',
            'object_id': 'TestObjId',
        })

        return Batch2(propagation_params, opm_params)

    def test_backwards_and_forwards(self):
        now = datetime.datetime.now()
        later = now + datetime.timedelta(10 * 365)  # 10 years

        state_vec = [
            130347560.13690618, -74407287.6018632, -35247598.541470632,
            23.935241263310683, 27.146279819258538, 10.346605942591514
        ]

        print("Starting at %s" % (state_vec))

        print("Propagating forward from %s to %s" % (now, later))
        batch = self.make_batch(state_vec, now, later)
        runner = BatchRunManager(self.service.get_batches_module(), [batch])
        runner.run()
        forward_end_state = batch.get_results().get_end_state_vector()
        print("Final state at %s" % forward_end_state)

        print("Propagating backward from %s to %s" % (later, now))
        batch = self.make_batch(forward_end_state, later, now)
        runner = BatchRunManager(self.service.get_batches_module(), [batch])
        runner.run()
        backwards_end_state = batch.get_results().get_end_state_vector()
        print("Final state at %s" % backwards_end_state)

        difference = np.subtract(state_vec, backwards_end_state)
        print("Difference is %s" % difference)

        npt.assert_allclose(difference[0:3], [0, 0, 0], rtol=0, atol=1e-3)
        npt.assert_allclose(difference[3:6], [0, 0, 0], rtol=0, atol=1e-10)
class StmPropagationModuleTest(unittest.TestCase):
    """Basic integration test to demonstrate use of service tester.
    
    """
    def setUp(self):
        self.service = Service()
        self.assertTrue(self.service.setup_with_test_account())
        self.stm_module = StmPropagationModule(
            self.service.get_batches_module())

    def tearDown(self):
        self.service.teardown()

    def test_basic_stm(self):
        state_vec = [
            130347560.13690618, -74407287.6018632, -35247598.541470632,
            23.935241263310683, 27.146279819258538, 10.346605942591514
        ]

        start_time = datetime.datetime(2017, 10, 4, 0, 0, 0, 123456)
        end_time = datetime.datetime(2018, 10, 4, 0, 0, 0, 123456)

        propagation_params = PropagationParams({
            'start_time':
            start_time.isoformat() + 'Z',
            'end_time':
            end_time.isoformat() + 'Z',
            'project_uuid':
            self.service.get_working_project().get_uuid(),
            'description':
            'Created by test at ' + start_time.isoformat() + 'Z'
        })

        opm_params = OpmParams({
            'epoch': start_time.isoformat() + 'Z',
            'state_vector': state_vec,
        })

        end_state, stm = self.stm_module.run_stm_propagation(
            propagation_params, opm_params)

        # Taken from printed output of ../state_stm_propagation.py
        expected_end_state = np.array([
            -37523497.931654416, 492950622.8491298, 204482176.63445434,
            -11.336957217854795, 7.18499733419028, 3.3597496059480085
        ])
        expected_stm = np.matrix(
            [[
                9.70874844e+00, -1.21563565e+00, -9.26967637e-01,
                5.34214567e+07, 1.64329953e+07, 5.30094892e+06
            ],
             [
                 7.11171945e+00, -3.24202476e+00, -5.93038128e-01,
                 3.90278376e+07, 3.82420496e+07, 9.62761631e+06
             ],
             [
                 2.50503331e+00, -3.00334152e-01, -2.62144498e+00,
                 1.46131045e+07, 1.04218322e+07, 1.53347450e+07
             ],
             [
                 2.14264136e-07, -2.82295666e-08, -2.23357566e-08,
                 1.33259336e+00, 6.98930318e-01, 2.41824966e-01
             ],
             [
                 4.69172199e-07, -2.03571494e-07, -6.32223023e-08,
                 2.52995851e+00, 2.04570983e+00, 7.47014439e-01
             ],
             [
                 1.82661672e-07, -4.57388872e-08, -1.15455121e-07,
                 9.96459361e-01, 8.11376173e-01, 3.16765622e-01
             ]])

        npt.assert_allclose(expected_end_state,
                            np.array(end_state),
                            rtol=1e-8,
                            atol=0)
        npt.assert_allclose(expected_stm.getA(), stm.getA(), rtol=1e-8, atol=0)
class BatchRunnerTest(unittest.TestCase):
    """Integration test of batch running.
    
    """
    def setUp(self):
        self.service = Service()
        self.assertTrue(self.service.setup_with_test_account())

    def tearDown(self):
        self.service.teardown()

    def new_dummy_batch(self, days_to_propagate):
        if (days_to_propagate > 36500):
            print(
                "Server has trouble handling propagation durations longer than 100 years. Try something smaller."
            )
            return

        now = datetime.datetime.utcnow()
        later = now + datetime.timedelta(days_to_propagate)

        propagation_params = PropagationParams({
            'start_time':
            now.isoformat() + 'Z',
            'end_time':
            later.isoformat() + 'Z',
            'step_size':
            60 * 60,  # 1 hour.
            'project_uuid':
            self.service.get_working_project().get_uuid(),
            'description':
            'Created by test at ' + str(now) + 'Z'
        })

        state_vec = [
            130347560.13690618, -74407287.6018632, -35247598.541470632,
            23.935241263310683, 27.146279819258538, 10.346605942591514
        ]
        opm_params = OpmParams({
            'epoch': now.isoformat() + 'Z',
            'state_vector': state_vec,
            'mass': 500.5,
            'solar_rad_area': 25.2,
            'solar_rad_coeff': 1.2,
            'drag_area': 33.3,
            'drag_coeff': 2.5,
            'originator': 'Test',
            'object_name': 'TestObj',
            'object_id': 'TestObjId',
        })

        return Batch2(propagation_params, opm_params)

    def test_small_batch(self):
        num_batches = 3

        duration = 365  # 1 year
        batches = [self.new_dummy_batch(duration) for i in range(num_batches)]

        runner = BatchRunManager(self.service.get_batches_module(), batches)
        runner.run()
        statuses = runner.get_latest_statuses()
        self.assertEqual(0, len(statuses['PENDING']))
        self.assertEqual(0, len(statuses['RUNNING']))
        self.assertEqual(num_batches, len(statuses['COMPLETED']))
        self.assertEqual(0, len(statuses['FAILED']))

        end_state_vec = [
            -37535741.96415495, 492953227.1713997, 204483503.94517875,
            -11.337510170756701, 7.185009462698965, 3.3597614338244766
        ]
        for batch in batches:
            npt.assert_allclose(end_state_vec,
                                batch.get_results().get_end_state_vector(),
                                rtol=1e-5,
                                atol=0)
class PermissionsTest(unittest.TestCase):
    """Integration test of permissions management.
    
    """
    def _clean_up(self):
        # Clean up all the groups. This should automatically clean up any permissions
        # associated with them.
        groups = self.service.get_groups_module()
        my_groups = groups.get_my_memberships()
        if len(my_groups) > 0:
            print("Cleaning up " + str(len(my_groups)) + " groups...")
        for g in my_groups:
            groups.delete_group(g.get_uuid())

    def setUp(self):
        self.service = Service()
        self.assertTrue(self.service.setup_with_test_account())
        self.me = "*****@*****.**"
        self._clean_up()

    def tearDown(self):
        self.service.teardown()
        self._clean_up()

    def test_permission_management(self):
        permissions = self.service.get_permissions_module()

        # Create three groups to grant permission to.
        groups = self.service.get_groups_module()
        group1 = groups.new_group("name1", "description1")
        group2 = groups.new_group("name2", "description2")
        group3 = groups.new_group("name3", "description3")

        # All permissions lists should be empty.
        pms = permissions.get_group_permissions(group1.get_uuid())
        self.assertEqual(pms, {group1.get_uuid(): []})
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {group2.get_uuid(): []})
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): []})

        # I should have permission to all three groups.
        pms = permissions.get_my_permissions()
        self.assertTrue(
            Permission('ADMIN', 'GROUP', group1.get_uuid()) in pms[''])
        self.assertTrue(
            Permission('ADMIN', 'GROUP', group2.get_uuid()) in pms[''])
        self.assertTrue(
            Permission('ADMIN', 'GROUP', group3.get_uuid()) in pms[''])

        # Add WRITE permission for group1 on group2.
        pm1 = Permission('WRITE', 'GROUP', group2.get_uuid())
        permissions.grant_group_permission(group1.get_uuid(), pm1)

        # group1 should have that permissions listed. Other groups should be unaffected.
        pms = permissions.get_group_permissions(group1.get_uuid())
        self.assertEqual(pms, {group1.get_uuid(): [pm1]})
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {group2.get_uuid(): []})
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): []})

        # Add READ permission for group2 on group3.
        pm2 = Permission('READ', 'GROUP', group3.get_uuid())
        permissions.grant_group_permission(group2.get_uuid(), pm2)

        # That should show up in group2's list.
        pms = permissions.get_group_permissions(group1.get_uuid())
        self.assertEqual(pms, {group1.get_uuid(): [pm1]})
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {group2.get_uuid(): [pm2]})
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): []})

        # Add group1 as a member of group2. Now pm2 should show up transitively in
        # group1's list as well.
        groups.add_group_to_group(group1.get_uuid(), group2.get_uuid())
        pms = permissions.get_group_permissions(group1.get_uuid())
        self.assertEqual(pms, {
            group1.get_uuid(): [pm1],
            group2.get_uuid(): [pm2]
        })
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {group2.get_uuid(): [pm2]})
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): []})

        # Add group2 to group3 and add ADMIN permission for group3 on group1
        # (yes, cycles are OK).
        pm3 = Permission('ADMIN', 'GROUP', group1.get_uuid())
        permissions.grant_group_permission(group3.get_uuid(), pm3)
        groups.add_group_to_group(group2.get_uuid(), group3.get_uuid())

        # pm3 should show up for everybody.
        pms = permissions.get_group_permissions(group1.get_uuid())
        self.assertEqual(
            pms, {
                group1.get_uuid(): [pm1],
                group2.get_uuid(): [pm2],
                group3.get_uuid(): [pm3]
            })
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {
            group2.get_uuid(): [pm2],
            group3.get_uuid(): [pm3]
        })
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): [pm3]})

        # All of these transitive permissions should show up in my permissions.
        pms = permissions.get_my_permissions()
        self.assertEqual(pms[group1.get_uuid()], [pm1])
        self.assertEqual(pms[group2.get_uuid()], [pm2])
        self.assertEqual(pms[group3.get_uuid()], [pm3])

        # Remove pm3, which should be removed from everybody's lists. The lists should
        # still include an item for group3 because membership hasn't changed, just with
        # no permissions directly granted.
        permissions.revoke_group_permission(group3.get_uuid(), pm3)
        pms = permissions.get_group_permissions(group1.get_uuid())
        self.assertEqual(
            pms, {
                group1.get_uuid(): [pm1],
                group2.get_uuid(): [pm2],
                group3.get_uuid(): []
            })
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {
            group2.get_uuid(): [pm2],
            group3.get_uuid(): []
        })
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): []})
        pms = permissions.get_my_permissions()
        self.assertEqual(pms[group1.get_uuid()], [pm1])
        self.assertEqual(pms[group2.get_uuid()], [pm2])
        self.assertEqual(pms[group3.get_uuid()], [])

        # Delete group1. All permissions (pm1) involving group1 should no longer exist.
        groups.delete_group(group1.get_uuid())
        pms = permissions.get_group_permissions(group2.get_uuid())
        self.assertEqual(pms, {
            group2.get_uuid(): [pm2],
            group3.get_uuid(): []
        })  # It's still in group3
        pms = permissions.get_group_permissions(group3.get_uuid())
        self.assertEqual(pms, {group3.get_uuid(): []})
        pms = permissions.get_my_permissions()
        self.assertFalse(group1.get_uuid() in pms)
        self.assertEqual(pms[group2.get_uuid()], [pm2])
        self.assertEqual(pms[group3.get_uuid()], [])

        # Delete group2. This should remove pm2.
        groups.delete_group(group2.get_uuid())
        pms = permissions.get_my_permissions()
        self.assertFalse(group1.get_uuid() in pms)
        self.assertFalse(group2.get_uuid() in pms)
        self.assertEqual(pms[group3.get_uuid()], [])

        # Now grant and revoke a user permission, just to show it doesn't die.
        permissions.grant_user_permission(
            "u1", Permission('READ', 'GROUP', group3.get_uuid()))
        permissions.revoke_user_permission(
            "u1", Permission('READ', 'GROUP', group3.get_uuid()))

        groups.delete_group(group3.get_uuid())

    def test_permission_mismanagement(self):
        permissions = self.service.get_permissions_module()

        # Target object does not exist.
        with self.assertRaises(RuntimeError):
            permissions.grant_user_permission(
                'u1', Permission('READ', 'GROUP', 'wat this is not a group'))

        groups = self.service.get_groups_module()
        group1 = groups.new_group("name1", "description1")

        # Recipient of permission does not exist.
        with self.assertRaises(RuntimeError):
            permissions.grant_group_permission(
                'g1', Permission('READ', 'GROUP', group1.get_uuid()))

        # Target does not exist.
        with self.assertRaises(RuntimeError):
            permissions.grant_group_permission(
                group1.get_uuid(),
                Permission('READ', 'GROUP', 'wat this is not a group'))

        # Finally this works.
        permissions.grant_group_permission(
            group1.get_uuid(), Permission('READ', 'GROUP', group1.get_uuid()))

        # Unable to check authorization (target does not exist).
        with self.assertRaises(RuntimeError):
            permissions.revoke_user_permission(
                "u1", Permission('READ', 'GROUP', 'wat this is not a group'))
        with self.assertRaises(RuntimeError):
            permissions.revoke_group_permission(
                group1.get_uuid(),
                Permission('READ', 'GROUP', 'wat this is not a group'))

        # But this is fine. Deleting something that doesn't exist.
        permissions.revoke_group_permission(
            'not a group', Permission('READ', 'GROUP', group1.get_uuid()))

        # Can't inspect nonexistent things either.
        with self.assertRaises(RuntimeError):
            permissions.get_group_permissions('not a group')

        # Not permitted to inspect other users.
        with self.assertRaises(RuntimeError):
            permissions.get_my_permissions(
                user_superuser_only="some other user")

        groups.delete_group(group1.get_uuid())
Example #6
0
class ProjectsTest(unittest.TestCase):
    """Integration test of project management.
    
    """
    def setUp(self):
        self.service = Service()
        self.assertTrue(self.service.setup_with_test_account())

    def delete_children(self, project):
        projects = self.service.get_projects_module().get_projects()
        
        # First topographically sort all children of <project> so that we can delete
        # them in dependency-order.
        parent_map = {}
        for p in projects:
            parent_map[p.get_uuid()] = p.get_parent()
        
        children = [project.get_uuid()]
        change = 1
        while change > 0:
            change = 0
            for p in projects:
                if not p.get_uuid() in children and p.get_parent() in children:
                    children.append(p.get_uuid())
                    change = change + 1
        
        children = children[1:]
        if len(children) > 0:
            print("Cleaning up " + str(len(children)) 
                + " children of " + project.get_uuid())
        for p in reversed(children):
            self.service.get_projects_module().delete_project(p)
    
    
    def tearDown(self):
        self.delete_children(self.service.get_working_project())
        self.service.teardown()
        
    def test_projects(self):
        parent = self.service.get_working_project()
        p = self.service.get_projects_module()
        
        projects = p.get_projects()
        self.assertTrue(parent.get_uuid() in [p.get_uuid() for p in projects])
        
        p1 = p.new_project(parent.get_uuid(), 'p1', 'description')
        self.assertTrue(p1.get_uuid() is not None)
        self.assertTrue(p1.get_parent() == parent.get_uuid())
        self.assertTrue(p1.get_name() == 'p1')
        self.assertTrue(p1.get_description() == 'description')
        
        projects = p.get_projects()
        self.assertTrue(parent.get_uuid() in [p.get_uuid() for p in projects])
        self.assertTrue(p1.get_uuid() in [p.get_uuid() for p in projects])
        
        p2 = p.new_project(p1.get_uuid(), 'p2', 'another description')
        self.assertTrue(p2.get_uuid() is not None)
        self.assertTrue(p2.get_parent() == p1.get_uuid())
        
        projects = p.get_projects()
        self.assertTrue(parent.get_uuid() in [p.get_uuid() for p in projects])
        self.assertTrue(p1.get_uuid() in [p.get_uuid() for p in projects])
        self.assertTrue(p2.get_uuid() in [p.get_uuid() for p in projects])
        
        # Can't delete the project before its children are deleted.
        try:
            p.delete_project(p1.get_uuid())
            self.fail("Expected error deleting project that still has children.")
        except RuntimeError:
            pass  # This is expected

        p.delete_project(p2.get_uuid())
        p.delete_project(p1.get_uuid())