예제 #1
0
def test_enroll_in_operation_mode_already_claimed(tmpdir):
    executor.Locker.LOCKDIR = str(tmpdir)
    agent.INI_PATH = str(tmpdir / 'config.ini')
    with open(agent.INI_PATH, "w") as f:
        f.write("[DEFAULT]\nenroll_token = 123456\nrollback_token = 123456\n")

    mock_mtls = mock.Mock()
    mock_mtls.json = mock.Mock(return_value={'claim_token': '3456', 'claimed': True})
    mock_mtls.return_value = "TestClaimToken"

    with mock.patch('agent.mtls_request') as mtls, \
            mock.patch('agent.logger') as prn:
        mtls.return_value = mock_mtls
        mtls.return_value.__repr__ = _mock_repr
        mtls.return_value.ok = True
        mtls.return_value.status_code = 200
        mtls.return_value.content = {}
        agent.try_enroll_in_operation_mode('deviceid000', True)

        assert mock.call.info('Enroll token found. Trying to automatically enroll the node.') in prn.method_calls
        assert mock.call.debug('\nDASH_ENDPOINT: %s\nWOTT_ENDPOINT: %s\nMTLS_ENDPOINT: %s', 'http://localhost:8000',
                               'http://localhost:8001/api', 'http://localhost:8002/api') in prn.method_calls
        assert mock.call.debug('[RECEIVED] Get Node Claim Info: TestClaimToken') in prn.method_calls
        assert mock.call.info('The node is already claimed. No enrolling required.') in prn.method_calls
        assert mock.call.info('Update config...') in prn.method_calls
        assert len(prn.method_calls) == 5
        with open(agent.INI_PATH) as f:
            assert f.read() == "[DEFAULT]\nrollback_token = 123456\n\n"
예제 #2
0
def test_enroll_in_operation_mode_no_claim_info(tmpdir):   # or server error
    executor.Locker.LOCKDIR = str(tmpdir)
    agent.INI_PATH = str(tmpdir / 'config.ini')
    file_content = "[DEFAULT]\nenroll_token = 123456\nrollback_token = 123456\n"
    with open(agent.INI_PATH, "w") as f:
        f.write(file_content)
    mock_mtls = mock.Mock()
    mock_mtls.json = mock.Mock(return_value={})

    with mock.patch('agent.mtls_request') as mtls, \
            mock.patch('agent.logger') as prn:
        mtls.return_value = mock_mtls
        mtls.return_value.__repr__ = _mock_repr
        mtls.return_value.ok = False
        mtls.return_value.status_code = 400
        mtls.return_value.content = {}
        agent.try_enroll_in_operation_mode('deviceid000', True)

        assert mock.call.info('Enroll token found. Trying to automatically enroll the node.') in prn.method_calls
        assert mock.call.debug('\nDASH_ENDPOINT: %s\nWOTT_ENDPOINT: %s\nMTLS_ENDPOINT: %s', 'http://localhost:8000',
                               'http://localhost:8001/api', 'http://localhost:8002/api') in prn.method_calls
        assert mock.call.error('Did not manage to get claim info from the server.') in prn.method_calls
        assert len(prn.method_calls) == 3
        with open(agent.INI_PATH) as f:
            assert f.read() == file_content
예제 #3
0
def test_enroll_in_operation_mode_no_token(tmpdir):   # or server error
    executor.Locker.LOCKDIR = str(tmpdir)
    file_content = "[DEFAULT]\nrollback_token = 123456\n"
    agent.INI_PATH = str(tmpdir / 'config.ini')
    with open(agent.INI_PATH, "w") as f:
        f.write(file_content)

    with mock.patch('agent.logger') as prn:
        agent.try_enroll_in_operation_mode('deviceid000', True)
        assert len(prn.method_calls) == 0
        with open(agent.INI_PATH) as f:
            assert f.read() == file_content
예제 #4
0
def test_enroll_in_operation_mode_enroll_fail(tmpdir):
    error_content = {
        "key": ["Pairnig-token not found"],
    }
    executor.Locker.LOCKDIR = str(tmpdir)
    file_content = "[DEFAULT]\nenroll_token = 123456\nrollback_token = 123456\n"
    agent.INI_PATH = str(tmpdir / 'config.ini')
    with open(agent.INI_PATH, "w") as f:
        f.write(file_content)

    mock_resp = mock.Mock()
    mock_resp.json = mock.Mock(return_value=error_content)

    mock_mtls = mock.Mock()
    mock_mtls.json = mock.Mock(return_value={'claim_token': '3456', 'claimed': False})
    mock_mtls.return_value = "TestClaimToken"

    with mock.patch('agent.mtls_request') as mtls, \
            mock.patch('requests.post') as req, \
            mock.patch('agent.logger') as prn:
        req.return_value = mock_resp
        req.return_value.ok = False
        req.return_value.status_code = 400
        req.return_value.content = {}
        req.return_value.reason = "Bad Request"
        req.return_value.content = error_content

        mtls.return_value = mock_mtls
        mtls.return_value.__repr__ = _mock_repr
        mtls.return_value.ok = True
        mtls.return_value.status_code = 200
        mtls.return_value.content = {}
        agent.try_enroll_in_operation_mode('deviceid000', True)

        assert mock.call.info('Enroll token found. Trying to automatically enroll the node.') in prn.method_calls
        assert mock.call.debug('\nDASH_ENDPOINT: %s\nWOTT_ENDPOINT: %s\nMTLS_ENDPOINT: %s', 'http://localhost:8000',
                               'http://localhost:8001/api', 'http://localhost:8002/api') in prn.method_calls
        assert mock.call.debug('[RECEIVED] Get Node Claim Info: TestClaimToken') in prn.method_calls
        assert mock.call.error('Failed to enroll node...') in prn.method_calls
        assert mock.call.error('Code:400, Reason:Bad Request') in prn.method_calls
        assert mock.call.error('key : Pairnig-token not found') in prn.method_calls
        assert mock.call.debug('enroll-device :: [RECEIVED] Enroll by token post: 400') in prn.method_calls
        assert mock.call.debug("enroll-device :: [RECEIVED] Enroll by token post: {"
                               "'key': ['Pairnig-token not found']}") in prn.method_calls
        assert mock.call.error('Node enrolling failed. Will try next time.') in prn.method_calls
        assert len(prn.method_calls) == 9
        with open(agent.INI_PATH) as f:
            assert f.read() == file_content