Ejemplo n.º 1
0
def test_longer_chain(sample_data2):
    """Test chained labors A->B->C->D"""
    labors = sample_data2.query(Labor).all()
    assert len(labors) == 0

    # system-maintenance audit
    event_type_a = sample_data2.query(EventType).get(1)
    # system-maintenance needed
    event_type_b = sample_data2.query(EventType).get(2)
    # system-maintenance ready
    event_type_c = sample_data2.query(EventType).get(3)
    # system-maintenance completed
    event_type_d = sample_data2.query(EventType).get(4)

    host = sample_data2.query(Host).get(1)

    event_a = Event.create(sample_data2, host, "system", event_type_a)

    # We will aggressively validate the events created only for event A
    event = (
        sample_data2.query(Event)
        .order_by(desc(Event.id)).first()
    )
    assert event == event_a
    assert event.host == host
    assert event.event_type == event_type_a

    labors = Labor.get_open_unacknowledged(sample_data2)
    assert len(labors) == 1
    assert len(host.labors) == 1
    assert labors[0].starting_labor_id is None
    assert labors[0].for_creator is False
    starting_labor_id = labors[0].id

    event_b = Event.create(sample_data2, host, "system", event_type_b)
    labors = Labor.get_open_unacknowledged(sample_data2)
    assert len(labors) == 1
    assert len(host.labors) == 2
    assert labors[0].starting_labor_id == starting_labor_id
    assert labors[0].for_creator is False

    event_c = Event.create(sample_data2, host, "system", event_type_c)
    labors = Labor.get_open_unacknowledged(sample_data2)
    assert len(labors) == 1
    assert len(host.labors) == 3
    assert labors[0].starting_labor_id == starting_labor_id
    assert labors[0].for_creator is True

    # This last event closes the final labor but does not create a new labor
    event_d = Event.create(sample_data2, host, "system", event_type_d)
    labors = Labor.get_open_unacknowledged(sample_data2)
    assert len(labors) == 0
    assert len(host.labors) == 3
Ejemplo n.º 2
0
def test_longer_chain(sample_data2):
    """Test chained labors A->B->C->D"""
    labors = sample_data2.query(Labor).all()
    assert len(labors) == 0

    # system-maintenance audit
    event_type_a = sample_data2.query(EventType).get(1)
    # system-maintenance needed
    event_type_b = sample_data2.query(EventType).get(2)
    # system-maintenance ready
    event_type_c = sample_data2.query(EventType).get(3)
    # system-maintenance completed
    event_type_d = sample_data2.query(EventType).get(4)

    host = sample_data2.query(Host).get(1)

    event_a = Event.create(sample_data2, host, "system", event_type_a)

    # We will aggressively validate the events created only for event A
    event = (sample_data2.query(Event).order_by(desc(Event.id)).first())
    assert event == event_a
    assert event.host == host
    assert event.event_type == event_type_a

    labors = Labor.get_open_unacknowledged(sample_data2)
    assert len(labors) == 1
    assert len(host.labors) == 1
    assert labors[0].starting_labor_id is None
    assert labors[0].for_creator is False
    starting_labor_id = labors[0].id

    event_b = Event.create(sample_data2, host, "system", event_type_b)
    labors = Labor.get_open_unacknowledged(sample_data2)
    assert len(labors) == 1
    assert len(host.labors) == 2
    assert labors[0].starting_labor_id == starting_labor_id
    assert labors[0].for_creator is False

    event_c = Event.create(sample_data2, host, "system", event_type_c)
    labors = Labor.get_open_unacknowledged(sample_data2)
    assert len(labors) == 1
    assert len(host.labors) == 3
    assert labors[0].starting_labor_id == starting_labor_id
    assert labors[0].for_creator is True

    # This last event closes the final labor but does not create a new labor
    event_d = Event.create(sample_data2, host, "system", event_type_d)
    labors = Labor.get_open_unacknowledged(sample_data2)
    assert len(labors) == 0
    assert len(host.labors) == 3
Ejemplo n.º 3
0
def test_creation_without_target(sample_data1):
    hosts = [
        sample_data1.query(Host).filter(
            Host.hostname == 'example.dropbox.com'
        ).one(),
        sample_data1.query(Host).filter(
            Host.hostname == 'test.dropbox.com'
        ).one(),
    ]

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 0

    creation_event_type = (
        sample_data1.query(EventType)
        .filter(EventType.id == 1).first()
    )

    Quest.create(
        sample_data1, "testman", hosts, creation_event_type,
        description="Embark on the long road of maintenance"
    )

    quests = sample_data1.query(Quest).all()

    assert len(quests) == 1
    assert quests[0].embark_time is not None
    assert quests[0].completion_time is None
    assert quests[0].description == "Embark on the long road of maintenance"
    assert quests[0].creator == "testman"
    assert quests[0].target_time == None
    assert len(quests[0].labors) == 2

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 2
Ejemplo n.º 4
0
def test_cannot_start_in_midworkflow(sample_data1):
    """Ensures that intermediate fates do not create labors when no labor
    exists.

    Given a Fate C -> D, and intermediate Fate D -> E,
    Throw event D and ensure Labor D is not created since Labor C does not exist.

    """

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 0

    event_type_d = sample_data1.query(EventType).get(4)
    host = sample_data1.query(Host).get(1)

    Event.create(sample_data1, host, "system", event_type_d)

    event = (
        sample_data1.query(Event)
        .order_by(desc(Event.id)).first()
    )

    assert event.host == host
    assert event.event_type == event_type_d

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 0
Ejemplo n.º 5
0
def test_creation_without_target(sample_data1):
    hosts = [
        sample_data1.query(Host).filter(
            Host.hostname == 'example.dropbox.com').one(),
        sample_data1.query(Host).filter(
            Host.hostname == 'test.dropbox.com').one(),
    ]

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 0

    fate = (sample_data1.query(Fate).get(1))

    Quest.create(sample_data1,
                 "testman",
                 hosts,
                 fate_id=fate.id,
                 description="Embark on the long road of maintenance")

    quests = sample_data1.query(Quest).all()

    assert len(quests) == 1
    assert quests[0].embark_time is not None
    assert quests[0].completion_time is None
    assert quests[0].description == "Embark on the long road of maintenance"
    assert quests[0].creator == "testman"
    assert quests[0].target_time is None
    assert len(quests[0].labors) == 2

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 2
Ejemplo n.º 6
0
def test_quest_preservation(sample_data1):
    """When a quest has labors that chain together, make sure they stay
    attached to the quest.
    """
    hosts = [
        sample_data1.query(Host).filter(
            Host.hostname == 'example.dropbox.com'
        ).one(),
        sample_data1.query(Host).filter(
            Host.hostname == 'test.dropbox.com'
        ).one(),
    ]

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 0

    creation_event_type = (
        sample_data1.query(EventType)
        .filter(EventType.id == 3).first()
    )

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

    Quest.create(
        sample_data1, "testman", hosts, creation_event_type, target_time,
        description="Embark on the long road of maintenance"
    )

    quests = sample_data1.query(Quest).all()

    assert len(quests) == 1
    assert quests[0].embark_time is not None
    assert quests[0].completion_time is None
    assert quests[0].description == "Embark on the long road of maintenance"
    assert quests[0].creator == "testman"
    assert len(quests[0].labors) == 2

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 2

    # now we want to throw events that create the subsequent labors
    found_hosts = sample_data1.query(Host).filter(
        Host.hostname.in_(["example.dropbox.com", "test.dropbox.com"])
    ).all()
    assert len(found_hosts) == 2

    completion_event_type1 = sample_data1.query(EventType).get(4)

    Event.create(
        sample_data1, found_hosts[0], "testdude", completion_event_type1
    )
    Event.create(
        sample_data1, found_hosts[1], "testdude", completion_event_type1
    )

    assert len(quests[0].labors) == 4
    assert len(quests[0].get_open_labors().all()) == 2
Ejemplo n.º 7
0
def test_acknowledge(sample_data1):
    """Test to ensure that acknowledgement correctly flags Labors as such"""

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 0

    fate = sample_data1.query(Fate).get(1)
    host = sample_data1.query(Host).get(1)

    Event.create(sample_data1, host, "system", fate.creation_event_type)

    event = (
        sample_data1.query(Event)
        .order_by(desc(Event.id)).first()
    )

    assert event.host == host
    assert event.event_type == fate.creation_event_type

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 1
    assert labors[0].completion_time is None
    assert labors[0].completion_event is None
    assert labors[0].ack_time is None
    assert labors[0].ack_user is None
    assert labors[0].creation_event == event
    assert labors[0].for_creator is False

    labors[0].acknowledge("testman")

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 1
    assert labors[0].completion_time is None
    assert labors[0].completion_event is None
    assert labors[0].ack_time is not None
    assert labors[0].ack_user == "testman"
    assert labors[0].creation_event == event
    assert labors[0].for_creator is False

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 0
Ejemplo n.º 8
0
def test_quest_preservation(sample_data1):
    """When a quest has labors that chain together, make sure they stay
    attached to the quest.
    """
    hosts = [
        sample_data1.query(Host).filter(
            Host.hostname == 'example.dropbox.com').one(),
        sample_data1.query(Host).filter(
            Host.hostname == 'test.dropbox.com').one(),
    ]

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 0

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

    fate = (sample_data1.query(Fate).get(4))

    Quest.create(sample_data1,
                 "testman",
                 hosts,
                 target_time,
                 fate_id=fate.id,
                 description="Embark on the long road of maintenance")

    quests = sample_data1.query(Quest).all()

    assert len(quests) == 1
    assert quests[0].embark_time is not None
    assert quests[0].completion_time is None
    assert quests[0].description == "Embark on the long road of maintenance"
    assert quests[0].creator == "testman"
    assert len(quests[0].labors) == 2

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 2

    # now we want to throw events that create the subsequent labors
    found_hosts = sample_data1.query(Host).filter(
        Host.hostname.in_(["example.dropbox.com", "test.dropbox.com"])).all()
    assert len(found_hosts) == 2

    completion_event_type1 = sample_data1.query(EventType).get(4)

    Event.create(sample_data1, found_hosts[0], "testdude",
                 completion_event_type1)
    Event.create(sample_data1, found_hosts[1], "testdude",
                 completion_event_type1)

    assert len(quests[0].labors) == 4
    assert len(quests[0].get_open_labors().all()) == 2
Ejemplo n.º 9
0
def test_acknowledge(sample_data1):
    """Test to ensure that acknowledgement correctly flags Labors as such"""

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 0

    fate = sample_data1.query(Fate).get(1)
    host = sample_data1.query(Host).get(1)

    Event.create(sample_data1, host, "system", fate.creation_event_type)

    event = (sample_data1.query(Event).order_by(desc(Event.id)).first())

    assert event.host == host
    assert event.event_type == fate.creation_event_type

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 1
    assert labors[0].completion_time is None
    assert labors[0].completion_event is None
    assert labors[0].ack_time is None
    assert labors[0].ack_user is None
    assert labors[0].creation_event == event
    assert labors[0].for_creator is False

    labors[0].acknowledge("testman")

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 1
    assert labors[0].completion_time is None
    assert labors[0].completion_event is None
    assert labors[0].ack_time is not None
    assert labors[0].ack_user == "testman"
    assert labors[0].creation_event == event
    assert labors[0].for_creator is False

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 0
Ejemplo n.º 10
0
def test_cannot_start_in_midworkflow(sample_data1):
    """Ensures that intermediate fates do not create labors when no labor
    exists.

    Given a Fate C -> D, and intermediate Fate D -> E,
    Throw event D and ensure Labor D is not created since Labor C does not exist.

    """

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 0

    event_type_d = sample_data1.query(EventType).get(4)
    host = sample_data1.query(Host).get(1)

    Event.create(sample_data1, host, "system", event_type_d)

    event = (sample_data1.query(Event).order_by(desc(Event.id)).first())

    assert event.host == host
    assert event.event_type == event_type_d

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 0
Ejemplo n.º 11
0
def test_creation(sample_data1):
    hosts = [
        sample_data1.query(Host).filter(
            Host.hostname == 'example.dropbox.com'
        ).one(),
        sample_data1.query(Host).filter(
            Host.hostname == 'test.dropbox.com'
        ).one(),
    ]

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 0

    creation_event_type = (
        sample_data1.query(EventType)
        .filter(EventType.id == 1).first()
    )

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

    Quest.create(
        sample_data1, "testman", hosts, creation_event_type, target_time,
        description="Embark on the long road of maintenance"
    )

    quests = sample_data1.query(Quest).all()

    assert len(quests) == 1
    assert quests[0].embark_time is not None
    assert quests[0].completion_time is None
    assert quests[0].description == "Embark on the long road of maintenance"
    assert quests[0].creator == "testman"
    assert quests[0].target_time == target_time
    assert len(quests[0].labors) == 2

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 2

    # now we want to test the closing of the quest by throwing events
    # that fulfill the labors

    found_hosts = sample_data1.query(Host).filter(
        Host.hostname.in_(["example.dropbox.com", "test.dropbox.com"])
    ).all()

    assert len(found_hosts) == 2

    completion_event_type = (
        sample_data1.query(EventType)
        .filter(EventType.id == 2).first()
    )

    Event.create(
        sample_data1, found_hosts[0], "testdude", completion_event_type
    )
    Event.create(
        sample_data1, found_hosts[1], "testdude", completion_event_type
    )

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 0

    quests = sample_data1.query(Quest).all()

    assert len(quests) == 1
    assert quests[0].embark_time is not None
    assert quests[0].completion_time is not None
    assert quests[0].description == "Embark on the long road of maintenance"
    assert quests[0].creator == "testman"
    assert len(quests[0].labors) == 2
Ejemplo n.º 12
0
def test_lifecycle_complex2(sample_data1):
    """Test the automatic creation and closing of labors based on Events and Fates.
    This version is a bit more complex in that we make sure unaffiliated labors
    are left untouched.

    Throw event A, creates Labor A.
    Throw event C, creates Labor C.
    Throw event B, closes Labor A, but does nothing to Labor C.
    """
    labors = sample_data1.query(Labor).all()
    assert len(labors) == 0

    fates = sample_data1.query(Fate).all()
    fate1 = fates[0]
    fate2 = fates[1]
    fate4 = fates[3]

    hosts = sample_data1.query(Host).all()
    host1 = hosts[0]
    host2 = hosts[1]

    # Throw event A and C
    Event.create(sample_data1, host1, "system", fate1.creation_event_type)
    Event.create(sample_data1, host2, "system", fate4.creation_event_type)

    event = (
        sample_data1.query(Event)
        .order_by(desc(Event.id)).first()
    )

    assert event.host == host2
    assert event.event_type == fate4.creation_event_type

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 2
    assert labors[0].completion_time is None
    assert labors[0].completion_event is None
    assert labors[0].for_creator is False
    assert labors[1].completion_time is None
    assert labors[1].completion_event is None
    assert labors[1].for_creator is False

    # Throw event B
    Event.create(
        sample_data1, host1, "system", fate2.creation_event_type
    )

    event = (
        sample_data1.query(Event)
        .order_by(desc(Event.id)).first()
    )

    assert event.host == host1
    assert event.event_type == fate2.creation_event_type

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 2
    assert labors[0].completion_time is not None
    assert labors[0].completion_event is not None
    assert labors[0].for_creator is False
    assert labors[1].completion_time is None
    assert labors[1].completion_event is None
    assert labors[1].for_creator is False

    labors = Labor.get_open_labors(sample_data1).all()
    assert len(labors) == 1

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 1
Ejemplo n.º 13
0
def test_mass_creation(sample_data1):
    hosts = [
        sample_data1.query(Host).filter(
            Host.hostname == 'example.dropbox.com'
        ).one(),
        sample_data1.query(Host).filter(
            Host.hostname == 'test.dropbox.com'
        ).one(),
    ]

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 0

    fate = (
        sample_data1.query(Fate).get(1)
    )

    creation_event_type1 = fate.creation_event_type


    target_time1 = datetime.now() + timedelta(days=7)
    target_time2 = datetime.now() + timedelta(days=10)
    target_time3 = datetime.now() + timedelta(days=14)

    Quest.create(
        sample_data1, "testman", hosts, target_time1, fate_id=fate.id,
        description="Embark on the long road of maintenance"
    )
    Quest.create(
        sample_data1, "testman", hosts, target_time2, fate_id=fate.id,
        description="Embark on the longer road of maintenance"
    )
    Quest.create(
        sample_data1, "testman", hosts, target_time3, fate_id=fate.id,
        description="WHEN WILL IT END!!"
    )

    quests = sample_data1.query(Quest).all()

    assert len(quests) == 3
    assert quests[0].embark_time is not None
    assert quests[0].completion_time is None
    assert quests[0].description == "Embark on the long road of maintenance"
    assert quests[0].creator == "testman"
    assert quests[1].embark_time is not None
    assert quests[1].completion_time is None
    assert quests[2].embark_time is not None
    assert quests[2].completion_time is None
    assert len(quests[0].labors) == 2
    assert len(quests[1].labors) == 2
    assert len(quests[2].labors) == 2

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 6

    # now we want to test the closing of the quest by throwing events
    # that fulfill the labors

    found_hosts = sample_data1.query(Host).filter(
        Host.hostname.in_(["example.dropbox.com", "test.dropbox.com"])
    ).all()

    assert len(found_hosts) == 2

    completion_event_type1 = (
        sample_data1.query(EventType)
        .filter(EventType.id == 2).first()
    )

    Event.create(
        sample_data1, found_hosts[0], "testdude", completion_event_type1
    )
    Event.create(
        sample_data1, found_hosts[1], "testdude", completion_event_type1
    )

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 0

    quests = sample_data1.query(Quest).all()

    assert len(quests) == 3
    assert quests[0].embark_time is not None
    assert quests[0].completion_time is not None
    assert quests[1].embark_time is not None
    assert quests[1].completion_time is not None
    assert quests[2].embark_time is not None
    assert quests[2].completion_time is not None
    assert quests[0].description == "Embark on the long road of maintenance"
    assert quests[0].creator == "testman"
    assert len(quests[0].labors) == 2
Ejemplo n.º 14
0
def test_creation(sample_data1):
    hosts = [
        sample_data1.query(Host).filter(
            Host.hostname == 'example.dropbox.com').one(),
        sample_data1.query(Host).filter(
            Host.hostname == 'test.dropbox.com').one(),
    ]

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 0

    fate = (sample_data1.query(Fate).get(1))

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

    Quest.create(sample_data1,
                 "testman",
                 hosts,
                 target_time,
                 fate_id=fate.id,
                 description="Embark on the long road of maintenance")

    quests = sample_data1.query(Quest).all()

    assert len(quests) == 1
    assert quests[0].embark_time is not None
    assert quests[0].completion_time is None
    assert quests[0].description == "Embark on the long road of maintenance"
    assert quests[0].creator == "testman"
    assert quests[0].target_time == target_time
    assert len(quests[0].labors) == 2

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 2

    # now we want to test the closing of the quest by throwing events
    # that fulfill the labors

    found_hosts = sample_data1.query(Host).filter(
        Host.hostname.in_(["example.dropbox.com", "test.dropbox.com"])).all()

    assert len(found_hosts) == 2

    completion_event_type = (sample_data1.query(EventType).filter(
        EventType.id == 2).first())

    Event.create(sample_data1, found_hosts[0], "testdude",
                 completion_event_type)
    Event.create(sample_data1, found_hosts[1], "testdude",
                 completion_event_type)

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 0

    quests = sample_data1.query(Quest).all()

    assert len(quests) == 1
    assert quests[0].embark_time is not None
    assert quests[0].completion_time is not None
    assert quests[0].description == "Embark on the long road of maintenance"
    assert quests[0].creator == "testman"
    assert len(quests[0].labors) == 2
Ejemplo n.º 15
0
def test_lifecycle_complex2(sample_data1):
    """Test the automatic creation and closing of labors based on Events and Fates.
    This version is a bit more complex in that we make sure unaffiliated labors
    are left untouched.

    Throw event A, creates Labor A.
    Throw event C, creates Labor C.
    Throw event B, closes Labor A, but does nothing to Labor C.
    """
    labors = sample_data1.query(Labor).all()
    assert len(labors) == 0

    fates = sample_data1.query(Fate).all()
    fate1 = fates[0]
    fate2 = fates[1]
    fate4 = fates[3]

    hosts = sample_data1.query(Host).all()
    host1 = hosts[0]
    host2 = hosts[1]

    # Throw event A and C
    Event.create(sample_data1, host1, "system", fate1.creation_event_type)
    Event.create(sample_data1, host2, "system", fate4.creation_event_type)

    event = (sample_data1.query(Event).order_by(desc(Event.id)).first())

    assert event.host == host2
    assert event.event_type == fate4.creation_event_type

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 2
    assert labors[0].completion_time is None
    assert labors[0].completion_event is None
    assert labors[0].for_creator is False
    assert labors[1].completion_time is None
    assert labors[1].completion_event is None
    assert labors[1].for_creator is False

    # Throw event B
    Event.create(sample_data1, host1, "system", fate2.creation_event_type)

    event = (sample_data1.query(Event).order_by(desc(Event.id)).first())

    assert event.host == host1
    assert event.event_type == fate2.creation_event_type

    labors = sample_data1.query(Labor).all()
    assert len(labors) == 2
    assert labors[0].completion_time is not None
    assert labors[0].completion_event is not None
    assert labors[0].for_creator is False
    assert labors[1].completion_time is None
    assert labors[1].completion_event is None
    assert labors[1].for_creator is False

    labors = Labor.get_open_labors(sample_data1).all()
    assert len(labors) == 1

    labors = Labor.get_open_unacknowledged(sample_data1)
    assert len(labors) == 1