Beispiel #1
0
def test_bad_machine_id():
    with mock.patch.object(util.sys, "exit") as mock_exit:
        with open('/tmp/testmachineid', 'w') as _file:
            _file.write("this_is_bad")
        util.generate_machine_id(destination_file='/tmp/testmachineid')
    assert mock_exit.call_args[0][0] == constants.sig_kill_bad
    os.remove('/tmp/testmachineid')
Beispiel #2
0
def test_generate_machine_id():
    machine_id_regex = re.match('\w{8}-\w{4}-\w{4}-\w{4}-\w{12}',
                                util.generate_machine_id(destination_file='/tmp/testmachineid'))
    assert machine_id_regex.group(0) is not None
    with open('/tmp/testmachineid', 'r') as _file:
        machine_id = _file.read()
    assert util.generate_machine_id(destination_file='/tmp/testmachineid') == machine_id
    os.remove('/tmp/testmachineid')
Beispiel #3
0
def test_generate_machine_id():
    orig_dir = InsightsConstants.insights_ansible_facts_dir
    InsightsConstants.insights_ansible_facts_dir = tempfile.mkdtemp()
    InsightsConstants.insights_ansible_machine_id_file = os.path.join(
        InsightsConstants.insights_ansible_facts_dir, "ansible_machine_id.fact")
    machine_id_regex = re.match('\w{8}-\w{4}-\w{4}-\w{4}-\w{12}',
                                util.generate_machine_id(destination_file='/tmp/testmachineid'))
    assert machine_id_regex.group(0) is not None
    with open('/tmp/testmachineid', 'r') as _file:
        machine_id = _file.read()
    assert util.generate_machine_id(destination_file='/tmp/testmachineid') == machine_id
    os.remove('/tmp/testmachineid')
    os.remove(InsightsConstants.insights_ansible_machine_id_file)
    os.rmdir(InsightsConstants.insights_ansible_facts_dir)
    InsightsConstants.insights_ansible_facts_dir = orig_dir
    InsightsConstants.insights_ansible_machine_id_file = os.path.join(
        InsightsConstants.insights_ansible_facts_dir, "ansible_machine_id.fact")
Beispiel #4
0
def test_force_reregister():
    config = InsightsConfig(reregister=True)
    client = InsightsClient(config)
    client.connection = FakeConnection(registered=None)
    client.session = True

    # initialize comparisons
    old_machine_id = None
    new_machine_id = None

    # register first
    assert client.register() is True
    for r in constants.registered_files:
        assert os.path.isfile(r) is True

    # get modified time of .registered to ensure it's regenerated
    old_reg_file1_ts = os.path.getmtime(constants.registered_files[0])
    old_reg_file2_ts = os.path.getmtime(constants.registered_files[1])

    old_machine_id = generate_machine_id()

    # wait to allow for timestamp difference
    time.sleep(3)

    # reregister with new machine-id
    client.connection = FakeConnection(registered=True)
    config.reregister = True
    assert client.register() is True

    new_machine_id = generate_machine_id()
    new_reg_file1_ts = os.path.getmtime(constants.registered_files[0])
    new_reg_file2_ts = os.path.getmtime(constants.registered_files[1])

    assert old_machine_id != new_machine_id
    assert old_reg_file1_ts != new_reg_file1_ts
    assert old_reg_file2_ts != new_reg_file2_ts
Beispiel #5
0
def post_update(client, config):
    logger.debug("CONFIG: %s", config)
    if config['status']:
        reg_check = registration_check(client.get_connection())
        for msg in reg_check['messages']:
            logger.info(msg)
        sys.exit(constants.sig_kill_ok)

    # put this first to avoid conflicts with register
    if config['unregister']:
        pconn = client.get_connection()
        if pconn.unregister():
            sys.exit(constants.sig_kill_ok)
        else:
            sys.exit(constants.sig_kill_bad)

    # force-reregister -- remove machine-id files and registration files
    # before trying to register again
    new = False
    if config['reregister']:
        new = True
        config['register'] = True
        delete_registered_file()
        delete_unregistered_file()
        write_to_disk(constants.machine_id_file, delete=True)
    logger.debug('Machine-id: %s', generate_machine_id(new))

    if config['register']:
        registration = client.try_register()
        if registration is None:
            logger.info('Running connection test...')
            client.test_connection()
            sys.exit(constants.sig_kill_bad)
        if (not config['disable_schedule'] and
           get_scheduler(config).set_daily()):
            logger.info('Automatic scheduling for Insights has been enabled.')

    # check registration before doing any uploads
    # only do this if we are not running in container mode
    # Ignore if in offline mode
    if not config["analyze_container"]:
        if not config['register'] and not config['offline']:
            msg, is_registered = client._is_client_registered()
            if not is_registered:
                logger.error(msg)
                sys.exit(constants.sig_kill_bad)