コード例 #1
0
def test_checks_api(dcos_api_session: DcosApiSession) -> None:
    """
    Test the checks API at /system/checks/
    This will test that all checks run on all agents return a normal status. A
    failure in this test may be an indicator that some unrelated component
    failed and dcos-checks functioned properly.
    """
    checks_uri = '/system/checks/v1/'
    # Test that we can list and run node and cluster checks on a master, agent, and public agent.
    check_nodes = []
    for nodes in [
            dcos_api_session.masters, dcos_api_session.slaves,
            dcos_api_session.public_slaves
    ]:
        if nodes:
            check_nodes.append(random.choice(nodes))
    logging.info('Testing %s on these nodes: %s', checks_uri,
                 ', '.join(check_nodes))

    for node in check_nodes:
        for check_type in ['node', 'cluster']:
            uri = '{}{}/'.format(checks_uri, check_type)
            logging.info('Testing %s on %s', uri, node)

            # List checks
            r = dcos_api_session.get(uri, node=node)
            assert r.status_code == 200
            checks = r.json()
            assert isinstance(checks, dict)

            # Run checks
            r = dcos_api_session.post(uri, node=node)
            assert r.status_code == 200
            results = r.json()
            assert isinstance(results, dict)

            # check that the returned statuses of each check is 0
            expected_status = {c: 0 for c in checks.keys()}
            response_status = {
                c: v['status']
                for c, v in results['checks'].items()
            }

            # print out the response for debugging
            logging.info('Response: {}'.format(results))
            assert expected_status == response_status

            # check that overall status is also 0
            assert results['status'] == 0
コード例 #2
0
def reserved_disk(dcos_api_session: DcosApiSession) -> Generator:
    """
    Set up an agent with one disk in a role.

    Reserve a chunk of `disk` resources on an agent for a role, and the
    remaining resources to another role. With that a framework in the first
    role will only be offered `disk` resources.
    """

    # Setup.

    def principal() -> str:
        is_enterprise = os.getenv('DCOS_ENTERPRISE', 'false').lower() == 'true'

        if is_enterprise:
            uid = dcos_api_session.auth_user.uid  # type: str
            return uid
        else:
            return 'reserved_disk_fixture_principal'

    dcos_api_session.principal = principal()

    # Keep track of all reservations we created so we can clean them up on
    # teardown or on error paths.
    reserved_resources = []

    try:
        # Get the ID of a private agent. We some assume that resources on that
        # agent are unreserved.
        r = dcos_api_session.get('/mesos/slaves')
        assert r.status_code == 200, r.text
        response = json.loads(r.text)
        slaves = [
            slave['id'] for slave in response['slaves']
            if 'public_ip' not in slave['attributes']
        ]
        assert slaves, 'Could not find any private agents'
        slave_id = slaves[0]

        # Create a unique role to reserve the disk to. The test framework should
        # register in this role.
        dcos_api_session.role = 'disk-' + uuid.uuid4().hex

        resources1 = {
            'agent_id': {
                'value': slave_id
            },
            'resources': [{
                'type':
                'SCALAR',
                'name':
                'disk',
                'reservations': [{
                    'type': 'DYNAMIC',
                    'role': dcos_api_session.role,
                    'principal': dcos_api_session.principal,
                }],
                'scalar': {
                    'value': 32
                }
            }]
        }

        request = {
            'type': 'RESERVE_RESOURCES',
            'reserve_resources': resources1
        }
        r = dcos_api_session.post('/mesos/api/v1', json=request)
        assert r.status_code == 202, r.text

        reserved_resources.append(resources1)

        # Reserve the remaining agent resources for another role. We let the Mesos
        # master perform the calculation of the unreserved resources on the agent
        # which requires another query.
        r = dcos_api_session.get('/mesos/slaves')
        assert r.status_code == 200, r.text
        response = json.loads(r.text)

        unreserved = [
            slave['unreserved_resources_full'] for slave in response['slaves']
            if slave['id'] == slave_id
        ]
        assert len(unreserved) == 1
        unreserved = unreserved[0]
        another_role = uuid.uuid4().hex
        for resource in unreserved:
            resource['reservations'] = [{
                'type':
                'DYNAMIC',
                'role':
                another_role,
                'principal':
                dcos_api_session.principal,
            }]
            resource.pop('role')

        resources2 = copy.deepcopy(resources1)
        resources2['resources'] = unreserved
        request = {
            'type': 'RESERVE_RESOURCES',
            'reserve_resources': resources2
        }
        r = dcos_api_session.post('/mesos/api/v1', json=request)
        assert r.status_code == 202, r.text

        reserved_resources.append(resources2)

        yield dcos_api_session

    finally:
        # Teardown.
        #
        # Remove all reservations this fixture has created in reverse order.
        for resources in reversed(reserved_resources):
            request = {
                'type': 'UNRESERVE_RESOURCES',
                'unreserve_resources': resources
            }
            r = dcos_api_session.post('/mesos/api/v1', json=request)
            assert r.status_code == 202, r.text