Ejemplo n.º 1
0
    def test_reserve_manually(self):
        with session.begin():
            system = data_setup.create_system(status=SystemStatus.manual)
            user = data_setup.create_user()
            system.reserve_manually('TESTING', user=user)
        json_out = run_client(
            ['bkr', 'system-status', system.fqdn, '--format', 'json'])
        json_out = loads(json_out)
        current_reservation = json_out['current_reservation']
        self.assertEqual(current_reservation.get('user_name'), unicode(user))
        self.assertEqual(json_out.get('current_loan'), None)
        self.assertEqual(json_out.get('condition'), '%s' % SystemStatus.manual)

        # Human friendly output
        human_out = run_client(['bkr', 'system-status', system.fqdn])
        expected_out = textwrap.dedent('''\
            Condition: %s
            Current reservation:
                User: %s
            Current loan: None''' % (
            system.status,
            system.user,
        ))
        human_out = human_out.rstrip('\n')
        self.assertEqual(human_out, expected_out, human_out)
Ejemplo n.º 2
0
    def test_list_policy(self):

        # print the policies as a list
        out = run_client(['bkr', 'policy-list', self.system.fqdn])
        expected_output = self.gen_expected_pretty_table(
            (['control_system', 'X', self.group.group_name, 'No'],
             ['control_system', self.user2.user_name, 'X', 'No'],
             ['edit_system', self.user1.user_name, 'X', 'No'],
             ['view', 'X', 'X', 'Yes'])) + '\n'
        self.assertEquals(out, expected_output)

        # For the second system
        out = run_client(['bkr', 'policy-list', self.system_public.fqdn,
                          '--format','tabular'])
        expected_output = self.gen_expected_pretty_table(
            (['control_system', 'X', 'X', 'Yes'],
             ['view', 'X', 'X', 'Yes'])) + '\n'

        self.assertEquals(out, expected_output)

        # print the policies as JSON object
        out = run_client(['bkr', 'policy-list', self.system.fqdn,
                          '--format','json'])
        out = json.loads(out)
        self.assertEquals(len(out['rules']), 4)
Ejemplo n.º 3
0
    def test_list_policy(self):

        # print the policies as a list
        out = run_client(['bkr', 'policy-list', self.system.fqdn])
        expected_output = self.gen_expected_pretty_table(
            (['control_system', 'X', self.group.group_name, 'No'], [
                'control_system', self.user2.user_name, 'X', 'No'
            ], ['edit_system', self.user1.user_name, 'X', 'No'
                ], ['view', 'X', 'X', 'Yes'])) + '\n'
        self.assertEquals(out, expected_output)

        # For the second system
        out = run_client([
            'bkr', 'policy-list', self.system_public.fqdn, '--format',
            'tabular'
        ])
        expected_output = self.gen_expected_pretty_table(
            (['control_system', 'X', 'X', 'Yes'], ['view', 'X', 'X', 'Yes'
                                                   ])) + '\n'

        self.assertEquals(out, expected_output)

        # print the policies as JSON object
        out = run_client(
            ['bkr', 'policy-list', self.system.fqdn, '--format', 'json'])
        out = json.loads(out)
        self.assertEquals(len(out['rules']), 4)
Ejemplo n.º 4
0
    def test_list_policy(self):

        # print the policies as a list
        out = run_client(['bkr', 'policy-list', self.system.fqdn])
        expected_output = self.gen_expected_pretty_table(
            (['control_system', 'X', self.group.group_name, 'No'], [
                'control_system', self.user2.user_name, 'X', 'No'
            ], ['edit_system', self.user1.user_name, 'X', 'No'
                ], ['view', 'X', 'X', 'Yes'])) + '\n'
        self.assertEquals(out, expected_output)

        # For the second system
        out = run_client([
            'bkr', 'policy-list', self.system_public.fqdn, '--format',
            'tabular'
        ])
        expected_output = self.gen_expected_pretty_table(
            (['control_system', 'X', 'X', 'Yes'], ['view', 'X', 'X', 'Yes'
                                                   ])) + '\n'

        self.assertEquals(out, expected_output)

        # print the policies as JSON object
        out = run_client(
            ['bkr', 'policy-list', self.system.fqdn, '--format', 'json'])
        out = json.loads(out)
        self.assertEquals(len(out['rules']), 4)

        # change active policy
        with session.begin():
            self.system.active_access_policy = self.pool.access_policy
        out = run_client(
            ['bkr', 'policy-list', self.system.fqdn, '--format', 'json'])
        out = json.loads(out)
        self.assertEquals(len(out['rules']), 2)

        # --custom should return the custom access policy rules
        out = run_client(['bkr', 'policy-list', self.system.fqdn, '--custom'])
        expected_output = self.gen_expected_pretty_table(
            (['control_system', 'X', self.group.group_name, 'No'], [
                'control_system', self.user2.user_name, 'X', 'No'
            ], ['edit_system', self.user1.user_name, 'X', 'No'
                ], ['view', 'X', 'X', 'Yes'])) + '\n'
        self.assertEquals(out, expected_output)
Ejemplo n.º 5
0
    def test_list_policy(self):

        # print the policies as a list
        out = run_client(['bkr', 'policy-list', self.system.fqdn])
        expected_output = self.gen_expected_pretty_table(
            (['control_system', 'X', self.group.group_name, 'No'],
             ['control_system', self.user2.user_name, 'X', 'No'],
             ['edit_system', self.user1.user_name, 'X', 'No'],
             ['view', 'X', 'X', 'Yes'])) + '\n'
        self.assertEquals(out, expected_output)

        # For the second system
        out = run_client(['bkr', 'policy-list', self.system_public.fqdn,
                          '--format','tabular'])
        expected_output = self.gen_expected_pretty_table(
            (['control_system', 'X', 'X', 'Yes'],
             ['view', 'X', 'X', 'Yes'])) + '\n'

        self.assertEquals(out, expected_output)

        # print the policies as JSON object
        out = run_client(['bkr', 'policy-list', self.system.fqdn,
                          '--format','json'])
        out = json.loads(out)
        self.assertEquals(len(out['rules']), 4)

        # change active policy
        with session.begin():
            self.system.active_access_policy = self.pool.access_policy
        out = run_client(['bkr', 'policy-list', self.system.fqdn,
                          '--format','json'])
        out = json.loads(out)
        self.assertEquals(len(out['rules']), 2)

        # --custom should return the custom access policy rules
        out = run_client(['bkr', 'policy-list', self.system.fqdn, '--custom'])
        expected_output = self.gen_expected_pretty_table(
            (['control_system', 'X', self.group.group_name, 'No'],
             ['control_system', self.user2.user_name, 'X', 'No'],
             ['edit_system', self.user1.user_name, 'X', 'No'],
             ['view', 'X', 'X', 'Yes'])) + '\n'
        self.assertEquals(out, expected_output)
Ejemplo n.º 6
0
    def run(self, *args, **kwargs):

        if len(args) != 1:
            self.parser.error('Exactly one system fqdn must be given')
        fqdn = args[0]

        rules_mine = kwargs.get('mine', None)
        rules_user = kwargs.get('user', None)
        rules_group = kwargs.get('group', None)

        # only one or none of the filtering criteria must be specified
        if len(filter(lambda x: x, [rules_mine, rules_user, rules_group])) > 1:
            self.parser.error('Only one filtering criteria allowed')

        # build the query string for filtering, if any
        query_string = {}
        if rules_mine:
            query_string['mine'] = True
        elif rules_user:
            query_string['user'] = rules_user
        elif rules_group:
            query_string['group'] = rules_group

        self.set_hub(**kwargs)
        requests_session = self.requests_session()

        if kwargs.get('custom', False):
            rules_url = 'systems/%s/access-policy' % urllib.quote(fqdn, '')
        else:
            rules_url = 'systems/%s/active-access-policy/' % urllib.quote(
                fqdn, '')
        res = requests_session.get(rules_url, params=query_string)
        res.raise_for_status()

        if kwargs['format'] == 'json':
            print res.text
        else:
            policy_dict = json.loads(res.text)
            # setup table
            table = PrettyTable(['Permission', 'User', 'Group', 'Everybody'])
            for rule in policy_dict['rules']:
                everybody_humanreadble = 'Yes' if rule['everybody'] else 'No'
                table.add_row([
                    col if col else 'X' for col in [
                        rule['permission'], rule['user'], rule['group'],
                        everybody_humanreadble
                    ]
                ])
            print table.get_string(sortby='Permission')
Ejemplo n.º 7
0
    def run(self, *args, **kwargs):

        if len(args) != 1:
            self.parser.error("Exactly one system fqdn must be given")
        fqdn = args[0]

        rules_mine = kwargs.get("mine", None)
        rules_user = kwargs.get("user", None)
        rules_group = kwargs.get("group", None)

        # only one or none of the filtering criteria must be specified
        if len(filter(lambda x: x, [rules_mine, rules_user, rules_group])) > 1:
            self.parser.error("Only one filtering criteria allowed")

        # build the query string for filtering, if any
        query_string = {}
        if rules_mine:
            query_string["mine"] = True
        elif rules_user:
            query_string["user"] = rules_user
        elif rules_group:
            query_string["group"] = rules_group

        self.set_hub(**kwargs)
        requests_session = self.requests_session()

        if kwargs.get("custom", False):
            rules_url = "systems/%s/access-policy" % urllib.quote(fqdn, "")
        else:
            rules_url = "systems/%s/active-access-policy/" % urllib.quote(fqdn, "")
        res = requests_session.get(rules_url, params=query_string)
        res.raise_for_status()

        if kwargs["format"] == "json":
            print res.text
        else:
            policy_dict = json.loads(res.text)
            # setup table
            table = PrettyTable(["Permission", "User", "Group", "Everybody"])
            for rule in policy_dict["rules"]:
                everybody_humanreadble = "Yes" if rule["everybody"] else "No"
                table.add_row(
                    [
                        col if col else "X"
                        for col in [rule["permission"], rule["user"], rule["group"], everybody_humanreadble]
                    ]
                )
            print table.get_string(sortby="Permission")
Ejemplo n.º 8
0
    def run(self, *args, **kwargs):
        if len(args) != 1:
            self.parser.error('Exactly one system fqdn must be given')
        fqdn = args[0]
        format = kwargs.get('format')
        self.set_hub(**kwargs)
        requests_session = self.requests_session()
        status_url = 'systems/%s/status' % urllib.quote(fqdn, '')
        res = requests_session.get(status_url)
        res.raise_for_status()
        if format == 'json':
            print res.text
        else:
            system_status = loads(res.text)
            condition = system_status.get('condition')
            reservation_details = system_status.get('current_reservation')
            msg = ['Condition: %s' % condition]
            if reservation_details:
                reserved_by = reservation_details.get('user_name')
                recipe_id = reservation_details.get('recipe_id')
                start_time = reservation_details.get('start_time')
                msg.append('Current reservation:')
                if reserved_by:
                    # The '%4s' % '' formatting is to indent the output
                    # and make it easier to read.
                    msg.append('%4sUser: %s' % ('', reserved_by))
                if recipe_id:
                    msg.append('%4sRecipe ID: %s' % ('', recipe_id))
                if start_time:
                    msg.append('%4sStart time: %s' % ('', start_time))
            else:
                msg.append('Current reservation: %s' % None)

            loan_details = system_status.get('current_loan')
            if loan_details:
                loaned_to = loan_details.get('recipient')
                loan_comment = loan_details.get('comment')
                msg.append('Current loan:')
                if loaned_to:
                    msg.append('%4sUser: %s' % ('', loaned_to))
                if loan_comment:
                    msg.append('%4sComment: %s' % ('', loan_comment))
            else:
                msg.append('Current loan: %s' % None)
            print '\n'.join(msg)
Ejemplo n.º 9
0
    def run(self, *args, **kwargs):
        if len(args) != 1:
            self.parser.error('Exactly one system fqdn must be given')
        fqdn = args[0]
        format = kwargs.get('format')
        self.set_hub(**kwargs)
        requests_session = self.requests_session()
        status_url = 'systems/%s/status' % urllib.quote(fqdn, '')
        res = requests_session.get(status_url)
        res.raise_for_status()
        if format == 'json':
            print res.text
        else:
            system_status = loads(res.text)
            condition = system_status.get('condition')
            reservation_details = system_status.get('current_reservation')
            msg = ['Condition: %s' % condition]
            if reservation_details:
                reserved_by = reservation_details.get('user_name')
                recipe_id = reservation_details.get('recipe_id')
                start_time = reservation_details.get('start_time')
                msg.append('Current reservation:')
                if reserved_by:
                    # The '%4s' % '' formatting is to indent the output
                    # and make it easier to read.
                    msg.append('%4sUser: %s' % ('', reserved_by))
                if recipe_id:
                    msg.append('%4sRecipe ID: %s' % ('', recipe_id))
                if start_time:
                    msg.append('%4sStart time: %s' % ('', start_time))
            else:
                msg.append('Current reservation: %s' % None)

            loan_details = system_status.get('current_loan')
            if loan_details:
                loaned_to = loan_details.get('recipient')
                loan_comment = loan_details.get('comment')
                msg.append('Current loan:')
                if loaned_to:
                    msg.append('%4sUser: %s' % ('', loaned_to))
                if loan_comment:
                    msg.append('%4sComment: %s' % ('', loan_comment))
            else:
                msg.append('Current loan: %s' % None)
            print '\n'.join(msg)
Ejemplo n.º 10
0
    def test_unpopulated_status(self):
        with session.begin():
            system = data_setup.create_system(status=SystemStatus.automated)
        json_out = run_client(
            ['bkr', 'system-status', system.fqdn, '--format', 'json'])
        json_out = loads(json_out)
        self.assertEqual(json_out.get('current_reservation'), None)
        self.assertEqual(json_out.get('current_loan'), None)
        self.assertEqual(json_out.get('condition'), unicode(system.status))

        # Human friendly output
        human_out = run_client(['bkr', 'system-status', system.fqdn])
        expected_out = textwrap.dedent('''\
            Condition: %s
            Current reservation: None
            Current loan: None''' % (system.status))
        human_out = human_out.rstrip('\n')
        self.assertEqual(human_out, expected_out, human_out)
Ejemplo n.º 11
0
    def test_unpopulated_status(self):
        with session.begin():
            system = data_setup.create_system(status=SystemStatus.automated)
        json_out = run_client(['bkr', 'system-status', system.fqdn,
            '--format', 'json'])
        json_out = loads(json_out)
        self.assertEqual(json_out.get('current_reservation'), None)
        self.assertEqual(json_out.get('current_loan'), None)
        self.assertEqual(json_out.get('condition'), unicode(system.status))

        # Human friendly output
        human_out = run_client(['bkr', 'system-status', system.fqdn])
        expected_out = textwrap.dedent('''\
            Condition: %s
            Current reservation: None
            Current loan: None''' % (system.status))
        human_out = human_out.rstrip('\n')
        self.assertEqual(human_out, expected_out, human_out)
Ejemplo n.º 12
0
    def run(self, *args, **kwargs):

        if len(args) != 1:
            self.parser.error('Exactly one system fqdn must be given')
        fqdn = args[0]

        rules_mine = kwargs.get('mine', None)
        rules_user = kwargs.get('user', None)
        rules_group = kwargs.get('group', None)

        # only one or none of the filtering criteria must be specified
        if len(filter(lambda x: x,
                      [rules_mine, rules_user, rules_group])) > 1:
            self.parser.error('Only one filtering criteria allowed')

        # build the query string for filtering, if any
        query_string = {}
        if rules_mine:
            query_string['mine'] = True
        elif rules_user:
            query_string['user' ] = rules_user
        elif rules_group:
            query_string['group'] = rules_group

        self.set_hub(**kwargs)
        if rules_mine:
            self.hub._login()
        requests_session = self.requests_session()
        rules_url = 'systems/%s/access-policy' % urllib.quote(fqdn, '')
        res = requests_session.get(rules_url, params=query_string)
        res.raise_for_status()

        if kwargs['format'] == 'json':
            print res.text
        else:
            policy_dict = json.loads(res.text)
            # setup table
            table = PrettyTable(['Permission', 'User', 'Group', 'Everybody'])
            for rule in policy_dict['rules']:
                everybody_humanreadble = 'Yes' if rule['everybody'] else 'No'
                table.add_row([col if col else 'X' for col in [rule['permission'],
                                                               rule['user'], rule['group'],
                                                               everybody_humanreadble]])
            print table.get_string(sortby='Permission')
Ejemplo n.º 13
0
    def test_reserve_manually(self):
        with session.begin():
            system = data_setup.create_system(status=SystemStatus.manual)
            user = data_setup.create_user()
            system.reserve_manually('TESTING', user=user)
        json_out = run_client(['bkr', 'system-status', system.fqdn,
            '--format', 'json'])
        json_out = loads(json_out)
        current_reservation = json_out['current_reservation']
        self.assertEqual(current_reservation.get('user_name'), unicode(user))
        self.assertEqual(json_out.get('current_loan'), None)
        self.assertEqual(json_out.get('condition'), '%s' % SystemStatus.manual)

        # Human friendly output
        human_out = run_client(['bkr', 'system-status', system.fqdn])
        expected_out = textwrap.dedent('''\
            Condition: %s
            Current reservation:
                User: %s
            Current loan: None''' % (system.status, system.user,))
        human_out = human_out.rstrip('\n')
        self.assertEqual(human_out, expected_out, human_out)
Ejemplo n.º 14
0
    def test_reserve_with_recipe(self):
        with session.begin():
            recipe = data_setup.create_recipe()
            job = data_setup.create_job_for_recipes([recipe])
            data_setup.mark_job_running(job)
            recipe = Recipe.by_id(recipe.id)
            fqdn = recipe.resource.system.fqdn
            user = recipe.resource.system.user
            recipe.resource.system.loaned = user
            recipe.resource.system.loan_comment = 'Amy, must I jujitsu my ma?'
        system = recipe.resource.system
        # JSON output
        json_out = run_client(
            ['bkr', 'system-status', fqdn, '--format', 'json'])
        json_out = loads(json_out)
        current_reservation = json_out.get('current_reservation')
        self.assertEqual(current_reservation.get('user_name'),
                         unicode(system.user))
        self.assertEqual(current_reservation.get('recipe_id'), '%s' % \
            system.open_reservation.recipe.id)
        self.assertEqual(json_out.get('condition'), '%s' % system.status)

        # Human friendly output
        human_out = run_client(['bkr', 'system-status', fqdn])
        expected_out = textwrap.dedent('''\
            Condition: %s
            Current reservation:
                User: %s
                Recipe ID: %s
                Start time: %s UTC
            Current loan:
                User: %s
                Comment: Amy, must I jujitsu my ma?''' % \
            (system.status, system.user, system.open_reservation.recipe.id,
            system.open_reservation.recipe.start_time.strftime('%Y-%m-%d %H:%M:%S'),
            system.loaned))
        human_out = human_out.rstrip('\n')
        self.assertEqual(human_out, expected_out, human_out)
Ejemplo n.º 15
0
    def test_reserve_with_recipe(self):
        with session.begin():
            recipe = data_setup.create_recipe()
            job = data_setup.create_job_for_recipes([recipe])
            data_setup.mark_job_running(job)
            recipe = Recipe.by_id(recipe.id)
            fqdn = recipe.resource.system.fqdn
            user = recipe.resource.system.user
            recipe.resource.system.loaned = user
            recipe.resource.system.loan_comment = 'Amy, must I jujitsu my ma?'
        system = recipe.resource.system
        # JSON output
        json_out = run_client(['bkr', 'system-status', fqdn, '--format',
            'json'])
        json_out = loads(json_out)
        current_reservation = json_out.get('current_reservation')
        self.assertEqual(current_reservation.get('user_name'),
            unicode(system.user))
        self.assertEqual(current_reservation.get('recipe_id'), '%s' % \
            system.open_reservation.recipe.id)
        self.assertEqual(json_out.get('condition'), '%s' % system.status)

        # Human friendly output
        human_out = run_client(['bkr', 'system-status', fqdn])
        expected_out = textwrap.dedent('''\
            Condition: %s
            Current reservation:
                User: %s
                Recipe ID: %s
                Start time: %s UTC
            Current loan:
                User: %s
                Comment: Amy, must I jujitsu my ma?''' % \
            (system.status, system.user, system.open_reservation.recipe.id,
            system.open_reservation.recipe.start_time.strftime('%Y-%m-%d %H:%M:%S'),
            system.loaned))
        human_out = human_out.rstrip('\n')
        self.assertEqual(human_out, expected_out, human_out)