Exemplo n.º 1
0
def test_duplicate(session):
    EventType.create(session, "foo", "bar", "This is a test")

    with pytest.raises(IntegrityError):
        EventType.create(session, "foo", "bar", "Desc ignored")

    EventType.create(session, "foo", "bar2", "This is second test")
    EventType.create(session, "foo2", "bar", "This is second test")
Exemplo n.º 2
0
def test_duplicate(session):
    EventType.create(session, "foo", "bar", "This is a test")

    with pytest.raises(IntegrityError):
        EventType.create(session, "foo", "bar", "Desc ignored")

    EventType.create(session, "foo", "bar2", "This is second test")
    EventType.create(session, "foo2", "bar", "This is second test")
Exemplo n.º 3
0
def test_required(session):
    EventType.create(session, "foo", "bar", "This is a test")
    EventType.create(session, "foo", "bar2")

    with pytest.raises(exc.ValidationError):
        EventType.create(session, "foo", None)

    with pytest.raises(exc.ValidationError):
        EventType.create(session, None, "bar")
Exemplo n.º 4
0
def test_required(session):
    EventType.create(session, "foo", "bar", "This is a test")
    EventType.create(session, "foo", "bar2")

    with pytest.raises(exc.ValidationError):
        EventType.create(session, "foo", None)

    with pytest.raises(exc.ValidationError):
        EventType.create(session, None, "bar")
Exemplo n.º 5
0
def test_creation(session):
    EventType.create(session, "foo", "bar", "This is a test")
    session.commit()

    event_types = session.query(EventType).all()

    assert len(event_types) == 1
    assert event_types[0].id == 1
    assert event_types[0].category == "foo"
    assert event_types[0].state == "bar"
    assert event_types[0].description == "This is a test"
    
    event_type = EventType.get_event_type(session, "foo", "bar")
    assert event_type.id == 1
    assert event_type.category == "foo"
    assert event_type.state == "bar"
    assert event_type.description == "This is a test"

    assert event_type.href('/test') == '/test/eventtypes/1'
Exemplo n.º 6
0
def test_creation(session):
    EventType.create(session, "foo", "bar", "This is a test")
    session.commit()

    event_types = session.query(EventType).all()

    assert len(event_types) == 1
    assert event_types[0].id == 1
    assert event_types[0].category == "foo"
    assert event_types[0].state == "bar"
    assert event_types[0].description == "This is a test"

    event_type = EventType.get_event_type(session, "foo", "bar")
    assert event_type.id == 1
    assert event_type.category == "foo"
    assert event_type.state == "bar"
    assert event_type.description == "This is a test"

    assert event_type.href('/test') == '/test/eventtypes/1'
Exemplo n.º 7
0
def test_get_latest_events(session):
    event_type1 = EventType.create(session, "foo", "bar", "test type 1")
    event_type2 = EventType.create(session, "foo", "baz", "test type 2")

    host1 = Host.create(session, "server1")
    host2 = Host.create(session, "server2")

    Event.create(session, host1, "testman", event_type1)
    Event.create(session, host1, "testman", event_type2)
    Event.create(session, host2, "testman", event_type1)
    Event.create(session, host1, "testman", event_type1)
    Event.create(session, host1, "testman", event_type2)
    last_type2 = Event.create(session, host2, "testman", event_type2)
    last_type1 = Event.create(session, host2, "testman", event_type1)

    events1 = event_type1.get_latest_events().all()
    events2 = event_type2.get_latest_events().all()

    assert len(events1) == 4
    assert len(events2) == 3

    assert events1[0] == last_type1
    assert events2[0] == last_type2
Exemplo n.º 8
0
def test_get_latest_events(session):
    event_type1 = EventType.create(session, "foo", "bar", "test type 1")
    event_type2 = EventType.create(session, "foo", "baz", "test type 2")

    host1 = Host.create(session, "server1")
    host2 = Host.create(session, "server2")

    Event.create(session, host1, "testman", event_type1)
    Event.create(session, host1, "testman", event_type2)
    Event.create(session, host2, "testman", event_type1)
    Event.create(session, host1, "testman", event_type1)
    Event.create(session, host1, "testman", event_type2)
    last_type2 = Event.create(session, host2, "testman", event_type2)
    last_type1 = Event.create(session, host2, "testman", event_type1)

    events1 = event_type1.get_latest_events().all()
    events2 = event_type2.get_latest_events().all()

    assert len(events1) == 4
    assert len(events2) == 3

    assert events1[0] == last_type1
    assert events2[0] == last_type2
Exemplo n.º 9
0
def test_complex_chaining3(sample_data2):
    """This test works on testing some complex chaining of fates:

    ET: sys-audit, sys-needed, sys-ready, sys-complete, reboot-needed,
        reboot-complete, puppet-restart

    Fates:
    system-maintenance-audit => system-maintenance-needed
        => system-maintenance-ready => system-maintenance-completed

    system-maintenance-audit => system-maintenance-needed
        => system-reboot-completed

    system-reboot-needed => system-reboot-completed => puppet-restart

    Quests:
    Bravo: servers need sys-needed
    Charlie: servers need reboot-needed
    """
    event_types = sample_data2.query(EventType).all()
    assert len(event_types) == 7

    fates = sample_data2.query(Fate).all()
    assert len(fates) == 6

    hosts = [sample_data2.query(Host).get(1)]

    target_time1 = datetime.now() + timedelta(days=2)
    target_time2 = datetime.now() + timedelta(days=7)

    print "\nbravo quest"
    bravo_quest = Quest.create(
        sample_data2, "testman", hosts,
        EventType.get_event_type(sample_data2, "system-maintenance", "audit"),
        target_time1,
        description="System maintenance is needed"
    )

    print "\ncharlie quest"
    charlie_quest = Quest.create(
        sample_data2, "testman", hosts,
        EventType.get_event_type(sample_data2, "system-reboot", "needed"),
        target_time2,
        description="Systems need a reboot"
    )
    assert bravo_quest
    assert charlie_quest
    assert len(bravo_quest.labors) == 1
    assert len(charlie_quest.labors) == 1
    assert bravo_quest.completion_time is None
    assert charlie_quest.completion_time is None

    assert len(hosts[0].events) == 2

    # Now we want to progress the bravo quest by throwing the sys-maint needed event
    print "\nevent 1"
    event1 = Event.create(
        sample_data2, hosts[0], "system",
        EventType.get_event_type(sample_data2, "system-maintenance", "needed")
    )
    assert bravo_quest.get_open_labors().all()[0].creation_event == event1
    assert bravo_quest.get_open_labors().all()[0].starting_labor_id == hosts[0].events[0].id
    assert len(bravo_quest.labors) == 2

    # Now we progress the bravo quest again by throwing sys-maint-ready
    print "\nevent 1b"
    event1b = Event.create(
        sample_data2, hosts[0], "system",
        EventType.get_event_type(sample_data2, "system-maintenance", "ready")
    )
    assert bravo_quest.get_open_labors().all()[0].creation_event == event1b
    assert bravo_quest.get_open_labors().all()[0].starting_labor_id == hosts[0].events[0].id
    assert len(bravo_quest.labors) == 3

    # Now we progress the charlie quest with the system-reboot-completed event
    # but bravo quest stays put
    print "\nevent 2"
    event2 = Event.create(
        sample_data2, hosts[0], "system",
        EventType.get_event_type(sample_data2, "system-reboot", "completed")
    )
    assert bravo_quest.completion_time is None
    assert len(bravo_quest.labors) == 3
    assert charlie_quest.get_open_labors().all()[0].creation_event == event2
    assert charlie_quest.get_open_labors().all()[0].starting_labor_id == hosts[0].events[1].id
    assert charlie_quest.completion_time is None
    assert len(charlie_quest.labors) == 2

    # since the previous event progressed the workflow, this one below
    # shouldn't create a labor since system-reboot-completed labors are
    # intermediates
    print "\nevent 3"
    event3 = Event.create(
        sample_data2, hosts[0], "system",
        EventType.get_event_type(sample_data2, "system-reboot", "completed")
    )

    # we will have a 6th event here due to event3
    assert len(hosts[0].events) == 6

    assert len(charlie_quest.labors) == 2
    assert len(charlie_quest.get_open_labors().all()) == 1

    assert charlie_quest.get_open_labors().all()[0].creation_event == event2
    assert charlie_quest.get_open_labors().all()[0].quest == charlie_quest

    assert len(sample_data2.query(Labor).all()) == 5
Exemplo n.º 10
0
def test_complex_chaining1(sample_data2):
    """This test works on testing some complex chainging fates:

    ET: sys-audit, sys-needed, sys-ready, sys-complete, reboot-needed, reboot-complete, puppet-restart

    Fates:
    sys-audit => sys-needed
    sys-needed => sys-ready
    sys-ready => sys-complete
    sys-needed => reboot-complete
    reboot-needed => reboot-complete
    reboot-complete => puppet-restart

    Quests:
    Alpha: servers need sys-audit; on sys-needed event, add only 1 labor for sys-needed and add to quest
    """
    event_types = sample_data2.query(EventType).all()
    assert len(event_types) == 7

    fates = sample_data2.query(Fate).all()
    assert len(fates) == 6

    hosts = [
        sample_data2.query(Host).filter(
            Host.hostname == 'example.dropbox.com'
        ).one(),
        sample_data2.query(Host).filter(
            Host.hostname == 'test.dropbox.com'
        ).one(),
    ]

    target_time = datetime.now() + timedelta(days=2)

    quest = Quest.create(
        sample_data2, "testman", hosts,
        EventType.get_event_type(sample_data2, "system-maintenance", "audit"),
        target_time,
        description="Servers need audit"
    )

    assert quest
    assert len(quest.get_open_labors().all()) == 2

    # now we fire the system-maintenance needed event
    found_hosts = sample_data2.query(Host).filter(
        Host.hostname.in_(["example.dropbox.com", "test.dropbox.com"])
    ).all()
    assert len(found_hosts) == 2
    event1 = Event.create(
        sample_data2, found_hosts[0], "system",
        EventType.get_event_type(sample_data2, "system-maintenance", "needed")
    )
    event2 = Event.create(
        sample_data2, found_hosts[1], "system",
        EventType.get_event_type(sample_data2, "system-maintenance", "needed")
    )

    assert len(quest.labors) == 4
    assert len(quest.get_open_labors().all()) == 2
    assert quest.completion_time is None

    labor1 = quest.get_open_labors().all()[0]
    labor2 = quest.get_open_labors().all()[1]

    assert labor1.host == hosts[0]
    assert labor2.host == hosts[1]
    assert labor1.creation_event == event1
    assert labor2.creation_event == event2
Exemplo n.º 11
0
def test_explicit_chain(sample_data2):
    """This test works on testing some complex chaining of fates:

    ET: sys-audit, sys-needed, sys-ready, sys-complete, reboot-needed,
        reboot-complete, puppet-restart

    Fates:
    1: system-maintenance-audit => system-maintenance-needed
        => system-maintenance-ready => system-maintenance-completed

    9: system-maintenance-audit => system-reboot-completed

    Quests:
    Bravo: fate chain 1
    Charlie: fate chain 9
    """
    event_types = sample_data2.query(EventType).all()
    assert len(event_types) == 7

    fates = sample_data2.query(Fate).all()
    assert len(fates) == 10

    hosts = [sample_data2.query(Host).get(1), sample_data2.query(Host).get(2)]

    target_time1 = datetime.now() + timedelta(days=2)
    target_time2 = datetime.now() + timedelta(days=7)

    print "\nbravo quest"
    bravo_quest = Quest.create(
        sample_data2, "testman", [hosts[0]],
        target_time1,
        fate_id=1,
        description="System maintenance is needed"
    )

    print "\ncharlie quest"
    charlie_quest = Quest.create(
        sample_data2, "testman", [hosts[1]],
        target_time2,
        fate_id=9,
        description="Systems maintenance is needed"
    )
    assert bravo_quest
    assert charlie_quest
    assert len(bravo_quest.labors) == 1
    assert bravo_quest.get_open_labors().all()[0].for_owner is True
    assert bravo_quest.get_open_labors().all()[0].for_creator is False
    assert len(charlie_quest.labors) == 1
    assert bravo_quest.completion_time is None
    assert charlie_quest.completion_time is None

    assert len(hosts[0].events) == 1
    assert len(hosts[1].events) == 1

    # Now let's look at the fate ID of the labors and make sure they are correct
    assert bravo_quest.get_open_labors().all()[0].fate_id == 1
    assert charlie_quest.get_open_labors().all()[0].fate_id == 9

    # Now progress the bravo quest by throwing the sys-maint needed event
    # and ensure charlie quest didn't move
    print "\nevent 1"
    event1a = Event.create(
        sample_data2, hosts[0], "system",
        EventType.get_event_type(sample_data2, "system-maintenance", "needed")
    )
    event1b = Event.create(
        sample_data2, hosts[1], "system",
        EventType.get_event_type(sample_data2, "system-maintenance", "needed")
    )
    assert bravo_quest.get_open_labors().all()[0].creation_event == event1a
    assert bravo_quest.get_open_labors().all()[0].starting_labor_id == hosts[0].events[0].id
    assert bravo_quest.get_open_labors().all()[0].for_owner is True
    assert bravo_quest.get_open_labors().all()[0].for_creator is False
    assert bravo_quest.completion_time is None
    assert len(bravo_quest.labors) == 2
    assert charlie_quest.completion_time is None
    assert len(charlie_quest.labors) == 1

    # Throw system-reboot completed event for both hosts and ensure it closes
    # charlie quest but does nothing to bravo quest
    print "\nevent 1"
    event2a = Event.create(
        sample_data2, hosts[0], "system",
        EventType.get_event_type(sample_data2, "puppet", "restart")
    )
    event2b = Event.create(
        sample_data2, hosts[1], "system",
        EventType.get_event_type(sample_data2, "puppet", "restart")
    )
    assert bravo_quest.get_open_labors().all()[0].creation_event == event1a
    assert bravo_quest.get_open_labors().all()[0].starting_labor_id == hosts[0].events[0].id
    assert bravo_quest.get_open_labors().all()[0].for_owner is True
    assert bravo_quest.get_open_labors().all()[0].for_creator is False
    assert bravo_quest.completion_time is None
    assert len(bravo_quest.labors) == 2

    assert charlie_quest.completion_time is not None
    assert len(charlie_quest.labors) == 1
Exemplo n.º 12
0
def test_explicit_chain(sample_data2):
    """This test works on testing some complex chaining of fates:

    ET: sys-audit, sys-needed, sys-ready, sys-complete, reboot-needed,
        reboot-complete, puppet-restart

    Fates:
    1: system-maintenance-audit => system-maintenance-needed
        => system-maintenance-ready => system-maintenance-completed

    9: system-maintenance-audit => system-reboot-completed

    Quests:
    Bravo: fate chain 1
    Charlie: fate chain 9
    """
    event_types = sample_data2.query(EventType).all()
    assert len(event_types) == 7

    fates = sample_data2.query(Fate).all()
    assert len(fates) == 10

    hosts = [sample_data2.query(Host).get(1), sample_data2.query(Host).get(2)]

    target_time1 = datetime.now() + timedelta(days=2)
    target_time2 = datetime.now() + timedelta(days=7)

    print "\nbravo quest"
    bravo_quest = Quest.create(sample_data2,
                               "testman", [hosts[0]],
                               target_time1,
                               fate_id=1,
                               description="System maintenance is needed")

    print "\ncharlie quest"
    charlie_quest = Quest.create(sample_data2,
                                 "testman", [hosts[1]],
                                 target_time2,
                                 fate_id=9,
                                 description="Systems maintenance is needed")
    assert bravo_quest
    assert charlie_quest
    assert len(bravo_quest.labors) == 1
    assert bravo_quest.get_open_labors().all()[0].for_owner is True
    assert bravo_quest.get_open_labors().all()[0].for_creator is False
    assert len(charlie_quest.labors) == 1
    assert bravo_quest.completion_time is None
    assert charlie_quest.completion_time is None

    assert len(hosts[0].events) == 1
    assert len(hosts[1].events) == 1

    # Now let's look at the fate ID of the labors and make sure they are correct
    assert bravo_quest.get_open_labors().all()[0].fate_id == 1
    assert charlie_quest.get_open_labors().all()[0].fate_id == 9

    # Now progress the bravo quest by throwing the sys-maint needed event
    # and ensure charlie quest didn't move
    print "\nevent 1"
    event1a = Event.create(
        sample_data2, hosts[0], "system",
        EventType.get_event_type(sample_data2, "system-maintenance", "needed"))
    event1b = Event.create(
        sample_data2, hosts[1], "system",
        EventType.get_event_type(sample_data2, "system-maintenance", "needed"))
    assert bravo_quest.get_open_labors().all()[0].creation_event == event1a
    assert bravo_quest.get_open_labors().all(
    )[0].starting_labor_id == hosts[0].events[0].id
    assert bravo_quest.get_open_labors().all()[0].for_owner is True
    assert bravo_quest.get_open_labors().all()[0].for_creator is False
    assert bravo_quest.completion_time is None
    assert len(bravo_quest.labors) == 2
    assert charlie_quest.completion_time is None
    assert len(charlie_quest.labors) == 1

    # Throw system-reboot completed event for both hosts and ensure it closes
    # charlie quest but does nothing to bravo quest
    print "\nevent 1"
    event2a = Event.create(
        sample_data2, hosts[0], "system",
        EventType.get_event_type(sample_data2, "puppet", "restart"))
    event2b = Event.create(
        sample_data2, hosts[1], "system",
        EventType.get_event_type(sample_data2, "puppet", "restart"))
    assert bravo_quest.get_open_labors().all()[0].creation_event == event1a
    assert bravo_quest.get_open_labors().all(
    )[0].starting_labor_id == hosts[0].events[0].id
    assert bravo_quest.get_open_labors().all()[0].for_owner is True
    assert bravo_quest.get_open_labors().all()[0].for_creator is False
    assert bravo_quest.completion_time is None
    assert len(bravo_quest.labors) == 2

    assert charlie_quest.completion_time is not None
    assert len(charlie_quest.labors) == 1
Exemplo n.º 13
0
def test_complex_chaining3(sample_data2):
    """This test works on testing some complex chaining of fates:

    ET: sys-audit, sys-needed, sys-ready, sys-complete, reboot-needed,
        reboot-complete, puppet-restart

    Fates:
    system-maintenance-audit => system-maintenance-needed
        => system-maintenance-ready => system-maintenance-completed

    system-maintenance-audit => system-maintenance-needed
        => system-reboot-completed

    system-reboot-needed => system-reboot-completed => puppet-restart

    Quests:
    Bravo: servers need sys-needed
    Charlie: servers need reboot-needed
    """
    event_types = sample_data2.query(EventType).all()
    assert len(event_types) == 7

    fates = sample_data2.query(Fate).all()
    assert len(fates) == 10

    hosts = [sample_data2.query(Host).get(1)]

    target_time1 = datetime.now() + timedelta(days=2)
    target_time2 = datetime.now() + timedelta(days=7)

    print "\nbravo quest"
    bravo_quest = Quest.create(sample_data2,
                               "testman",
                               hosts,
                               target_time1,
                               fate_id=1,
                               description="System maintenance is needed")

    print "\ncharlie quest"
    charlie_quest = Quest.create(sample_data2,
                                 "testman",
                                 hosts,
                                 target_time2,
                                 fate_id=6,
                                 description="Systems need a reboot")
    assert bravo_quest
    assert charlie_quest
    assert len(bravo_quest.labors) == 1
    assert bravo_quest.get_open_labors().all()[0].for_owner is True
    assert bravo_quest.get_open_labors().all()[0].for_creator is False
    assert len(charlie_quest.labors) == 1
    assert bravo_quest.completion_time is None
    assert charlie_quest.completion_time is None

    assert len(hosts[0].events) == 2

    # Now we want to progress the bravo quest by throwing the sys-maint needed event
    print "\nevent 1"
    event1 = Event.create(
        sample_data2, hosts[0], "system",
        EventType.get_event_type(sample_data2, "system-maintenance", "needed"))
    assert bravo_quest.get_open_labors().all()[0].creation_event == event1
    assert bravo_quest.get_open_labors().all(
    )[0].starting_labor_id == hosts[0].events[0].id
    assert bravo_quest.get_open_labors().all()[0].for_owner is True
    assert bravo_quest.get_open_labors().all()[0].for_creator is False
    assert len(bravo_quest.labors) == 2

    # Now we progress the bravo quest again by throwing sys-maint-ready
    print "\nevent 1b"
    event1b = Event.create(
        sample_data2, hosts[0], "system",
        EventType.get_event_type(sample_data2, "system-maintenance", "ready"))
    assert bravo_quest.get_open_labors().all()[0].creation_event == event1b
    assert bravo_quest.get_open_labors().all(
    )[0].starting_labor_id == hosts[0].events[0].id
    assert bravo_quest.get_open_labors().all()[0].for_owner is False
    assert bravo_quest.get_open_labors().all()[0].for_creator is True
    assert len(bravo_quest.labors) == 3

    # Now we progress the charlie quest with the system-reboot-completed event
    # but bravo quest stays put
    print "\nevent 2"
    event2 = Event.create(
        sample_data2, hosts[0], "system",
        EventType.get_event_type(sample_data2, "system-reboot", "completed"))
    assert bravo_quest.completion_time is None
    assert len(bravo_quest.labors) == 3
    assert charlie_quest.get_open_labors().all()[0].creation_event == event2
    assert charlie_quest.get_open_labors().all(
    )[0].starting_labor_id == hosts[0].events[1].id
    assert charlie_quest.completion_time is None
    assert len(charlie_quest.labors) == 2

    # since the previous event progressed the workflow, this one below
    # shouldn't create a labor since system-reboot-completed labors are
    # intermediates
    print "\nevent 3"
    event3 = Event.create(
        sample_data2, hosts[0], "system",
        EventType.get_event_type(sample_data2, "system-reboot", "completed"))

    # we will have a 6th event here due to event3
    assert len(hosts[0].events) == 6

    assert len(charlie_quest.labors) == 2
    assert len(charlie_quest.get_open_labors().all()) == 1

    assert charlie_quest.get_open_labors().all()[0].creation_event == event2
    assert charlie_quest.get_open_labors().all()[0].quest == charlie_quest

    assert len(sample_data2.query(Labor).all()) == 5
Exemplo n.º 14
0
def test_complex_chaining1(sample_data2):
    """This test works on testing some complex chainging fates:

    ET: sys-audit, sys-needed, sys-ready, sys-complete, reboot-needed, reboot-complete, puppet-restart

    Fates:
    sys-audit => sys-needed
    sys-needed => sys-ready
    sys-ready => sys-complete
    sys-needed => reboot-complete
    reboot-needed => reboot-complete
    reboot-complete => puppet-restart

    Quests:
    Alpha: servers need sys-audit; on sys-needed event, add only 1 labor for sys-needed and add to quest
    """
    event_types = sample_data2.query(EventType).all()
    assert len(event_types) == 7

    fates = sample_data2.query(Fate).all()
    assert len(fates) == 10

    hosts = [
        sample_data2.query(Host).filter(
            Host.hostname == 'example.dropbox.com').one(),
        sample_data2.query(Host).filter(
            Host.hostname == 'test.dropbox.com').one(),
    ]

    target_time = datetime.now() + timedelta(days=2)

    quest = Quest.create(sample_data2,
                         "testman",
                         hosts,
                         target_time,
                         fate_id=1,
                         description="Servers need audit")

    assert quest
    assert len(quest.get_open_labors().all()) == 2

    # now we fire the system-maintenance needed event
    found_hosts = sample_data2.query(Host).filter(
        Host.hostname.in_(["example.dropbox.com", "test.dropbox.com"])).all()
    assert len(found_hosts) == 2
    event1 = Event.create(
        sample_data2, found_hosts[0], "system",
        EventType.get_event_type(sample_data2, "system-maintenance", "needed"))
    event2 = Event.create(
        sample_data2, found_hosts[1], "system",
        EventType.get_event_type(sample_data2, "system-maintenance", "needed"))

    assert len(quest.labors) == 4
    assert len(quest.get_open_labors().all()) == 2
    assert quest.completion_time is None

    labor1 = quest.get_open_labors().all()[0]
    labor2 = quest.get_open_labors().all()[1]

    assert labor1.host == hosts[0]
    assert labor2.host == hosts[1]
    assert labor1.for_owner is True
    assert labor1.for_creator is False
    assert labor2.for_owner is True
    assert labor2.for_creator is False
    assert labor1.creation_event == event1
    assert labor2.creation_event == event2