Пример #1
0
def test_add_to_old_lookup_hash_ignore():
    """
    Test with an already-existing lookup table. Verify new rows with same hash
    are not written to the lookup table. Also ignore some keys when calculating
    the hash, to test that.
    """
    tmpdir = tempfile.gettempdir()
    old_lookup_table = os.path.join(tmpdir, "old.csv")
    tmp_lookup_table = os.path.join(tmpdir, "tmp.csv")
    data = [{"first": "nick", "last": "wallin", "id": "12345", "time": "now"}]
    ignore_keys = ["time"]
    splunk_lookup_table.write_lookup_table(old_lookup_table, tmp_lookup_table, data, "id", ignore_keys)
    assert os.path.exists(old_lookup_table)
    assert not os.path.exists(tmp_lookup_table)
    new_tmp_table = os.path.join(tmpdir, "tmp.csv")
    new_data = [{"first": "nick", "last": "wallin", "id": "12345", "time": "later"}]
    splunk_lookup_table.write_lookup_table(old_lookup_table, new_tmp_table, new_data, "id", ignore_keys)
    assert os.path.exists(old_lookup_table)
    assert not os.path.exists(new_tmp_table)
    with open(old_lookup_table, "r") as new:
        lines = new.readlines()
        assert len(lines) == 2
        assert lines[0].split(",") == ["first", "id", "last", "time\n"]
        assert lines[1].split(",") == ["nick", "12345", "wallin", "now\n"]
    os.remove(old_lookup_table)
Пример #2
0
def test_initial_lookup_time_extra_rows():
    """
    Test that when a lookup table is made for the first time, and a time key
    is provided, that the times are modified as we expect, and later-added
    rows are NOT modified.
    """
    tmpdir = tempfile.gettempdir()
    old_lookup_table = os.path.join(tmpdir, "old.csv")
    tmp_lookup_table = os.path.join(tmpdir, "tmp.csv")
    data = [{"first": "nick", "last": "wallin", "id": "12345", "time": "2015-08-27"}]
    splunk_lookup_table.write_lookup_table(old_lookup_table, tmp_lookup_table, data, "id", [], "time")
    assert os.path.exists(old_lookup_table)
    assert not os.path.exists(tmp_lookup_table)
    new_tmp_table = os.path.join(tmpdir, "tmp.csv")
    new_data = [{"first": "nick", "last": "james", "id": "12345", "time": "2015-08-27"}]
    splunk_lookup_table.write_lookup_table(old_lookup_table, new_tmp_table, new_data, "id", [], "time")
    assert os.path.exists(old_lookup_table)
    assert not os.path.exists(new_tmp_table)
    with open(old_lookup_table, "r") as new:
        lines = new.readlines()
        assert len(lines) == 3
        assert lines[0].split(",") == ["first", "id", "last", "time\n"]
        assert lines[1].split(",") == ["nick", "12345", "wallin", "2000-01-01T00:01:01.000-00:00\n"]
        assert lines[2].split(",") == ["nick", "12345", "james", "2015-08-27\n"]
    os.remove(old_lookup_table)
Пример #3
0
def _run():
    """
    The script's body. Creates/Updates Splunk computer lookup table.
    """
    app_home = common.app_home()

    lookups_dir = os.path.join(app_home, "lookups")
    old_computer_lookup_table = os.path.join(lookups_dir,
                                             "computer_lookup.csv")
    tmp_computer_lookup_table = os.path.join(lookups_dir,
                                             "computer_lookup_tmp.csv")

    if not os.path.exists(lookups_dir):
        os.makedirs(lookups_dir)

    server, _ = common.setup()

    # write computer lookup table
    params = {'active': 'true', 'incBackupUsage': False, 'incHistory': False}
    computer_results = c42api.fetch_computers(server,
                                              params,
                                              insert_schema_version=True)
    splunk_lookup_table.write_lookup_table(old_computer_lookup_table,
                                           tmp_computer_lookup_table,
                                           computer_results, COMPUTER_UID_KEY,
                                           COMPUTER_KEYS_TO_IGNORE, TIME_KEY)
Пример #4
0
def test_temp_file_removed():
    """
    Test that the temp file does not exist after the lookup table is written.
    """
    tmpdir = tempfile.gettempdir()
    old_lookup_table = os.path.join(tmpdir, "old.csv")
    tmp_lookup_table = os.path.join(tmpdir, "tmp.csv")
    data = [{"first": "nick", "last": "wallin", "id": "12345"}]
    splunk_lookup_table.write_lookup_table(old_lookup_table, tmp_lookup_table, data, "id", [])
    assert os.path.exists(old_lookup_table)
    assert not os.path.exists(tmp_lookup_table)
    os.remove(old_lookup_table)
Пример #5
0
def test_new_lookup_table():
    """
    Test with no old lookup table. Simple verify of json written to the
    new lookup table.
    """
    tmpdir = tempfile.gettempdir()
    old_lookup_table = os.path.join(tmpdir, "old.csv")
    tmp_lookup_table = os.path.join(tmpdir, "tmp.csv")
    data = [{"first": "nick", "last": "wallin", "id": "12345"}]
    splunk_lookup_table.write_lookup_table(old_lookup_table, tmp_lookup_table, data, "id", [])
    assert os.path.exists(old_lookup_table)
    assert not os.path.exists(tmp_lookup_table)
    with open(old_lookup_table, "r") as new:
        lines = new.readlines()
        assert len(lines) == 2
        assert lines[0].split(",") == ["first", "id", "last\n"]
        assert lines[1].split(",") == ["nick", "12345", "wallin\n"]
    os.remove(old_lookup_table)
Пример #6
0
def test_old_file_not_overwritten():
    """
    Test that if no data is returned from the API call, the old lookup table
    remains the same.
    """
    tmpdir = tempfile.gettempdir()
    old_lookup_table = os.path.join(tmpdir, "old.csv")
    tmp_lookup_table = os.path.join(tmpdir, "tmp.csv")
    with open(old_lookup_table, "w") as out:
        out.write("this is only a test")
    data = []
    splunk_lookup_table.write_lookup_table(old_lookup_table, tmp_lookup_table, data, "id", [])
    assert os.path.exists(old_lookup_table)
    assert not os.path.exists(tmp_lookup_table)
    with open(old_lookup_table, "r") as old:
        for line in old:
            assert line == "this is only a test"
    os.remove(old_lookup_table)
Пример #7
0
def _run():
    """
    The script's body. Creates/Updates Splunk user lookup table.
    """
    app_home = common.app_home()

    lookups_dir = os.path.join(app_home, "lookups")
    old_user_lookup_table = os.path.join(lookups_dir, "user_lookup.csv")
    tmp_user_lookup_table = os.path.join(lookups_dir, "user_lookup_tmp.csv")

    if not os.path.exists(lookups_dir):
        os.makedirs(lookups_dir)

    server, _ = common.setup()

    # write user lookup table
    user_results = c42api.fetch_users(server)
    splunk_lookup_table.write_lookup_table(old_user_lookup_table, tmp_user_lookup_table, user_results,
                                           USER_UID_KEY, USER_KEYS_TO_IGNORE, TIME_KEY)
Пример #8
0
def _run():
    """
    The script's body. Creates/Updates Splunk computer lookup table.
    """
    app_home = common.app_home()

    lookups_dir = os.path.join(app_home, "lookups")
    old_computer_lookup_table = os.path.join(lookups_dir, "computer_lookup.csv")
    tmp_computer_lookup_table = os.path.join(lookups_dir, "computer_lookup_tmp.csv")

    if not os.path.exists(lookups_dir):
        os.makedirs(lookups_dir)

    server, _ = common.setup()

    # write computer lookup table
    params = {'active': 'true',
              'incBackupUsage': False,
              'incHistory': False}
    computer_results = c42api.fetch_computers(server, params, insert_schema_version=True)
    splunk_lookup_table.write_lookup_table(old_computer_lookup_table, tmp_computer_lookup_table, computer_results,
                                           COMPUTER_UID_KEY, COMPUTER_KEYS_TO_IGNORE, TIME_KEY)
Пример #9
0
def test_add_to_old_lookup_hash():
    """
    Test with an already-existing lookup table. Verify new rows with same hash
    are not written to the lookup table.
    """
    tmpdir = tempfile.gettempdir()
    old_lookup_table = os.path.join(tmpdir, "old.csv")
    tmp_lookup_table = os.path.join(tmpdir, "tmp.csv")
    data = [{"first": "nick", "last": "wallin", "id": "12345"}]
    splunk_lookup_table.write_lookup_table(old_lookup_table, tmp_lookup_table, data, "id", [])
    assert os.path.exists(old_lookup_table)
    assert not os.path.exists(tmp_lookup_table)
    new_tmp_table = os.path.join(tmpdir, "tmp.csv")
    new_data = [{"first": "nick", "last": "wallin", "id": "12345"}]
    splunk_lookup_table.write_lookup_table(old_lookup_table, new_tmp_table, new_data, "id", [])
    assert os.path.exists(old_lookup_table)
    assert not os.path.exists(new_tmp_table)
    with open(old_lookup_table, "r") as new:
        lines = new.readlines()
        assert len(lines) == 2
        assert lines[0].split(",") == ["first", "id", "last\n"]
        assert lines[1].split(",") == ["nick", "12345", "wallin\n"]
    os.remove(old_lookup_table)
Пример #10
0
def test_add_old_lookup_extra_column_lookup():
    """
    Test adding a row to the lookup table, when there's a column in the old
    lookup table that isn't in the json data
    """
    tmpdir = tempfile.gettempdir()
    old_lookup_table = os.path.join(tmpdir, "old.csv")
    tmp_lookup_table = os.path.join(tmpdir, "tmp.csv")
    data = [{"first": "nick", "last": "wallin", "id": "12345", "time": "now"}]
    splunk_lookup_table.write_lookup_table(old_lookup_table, tmp_lookup_table, data, "id", [])
    assert os.path.exists(old_lookup_table)
    assert not os.path.exists(tmp_lookup_table)
    new_tmp_table = os.path.join(tmpdir, "tmp.csv")
    new_data = [{"first": "nick", "last": "wallin", "id": "12345"}]
    splunk_lookup_table.write_lookup_table(old_lookup_table, new_tmp_table, new_data, "id", [])
    assert os.path.exists(old_lookup_table)
    assert not os.path.exists(new_tmp_table)
    with open(old_lookup_table, "r") as new:
        lines = new.readlines()
        assert len(lines) == 3
        assert lines[0].split(",") == ["first", "id", "last", "time\n"]
        assert lines[1].split(",") == ["nick", "12345", "wallin", "now\n"]
        assert lines[2].split(",") == ["nick", "12345", "wallin", "\n"]
    os.remove(old_lookup_table)