Skip to content

Commit

Permalink
Merge pull request #77 from ORNL/JoshuaSBrown-integrate-message-frame…
Browse files Browse the repository at this point in the history
…work-into-zambeze

Integrating Message Infrastructure into Zambeze
  • Loading branch information
JoshuaSBrown authored Dec 22, 2022
2 parents 4ebf98f + b413d36 commit 741ca9d
Show file tree
Hide file tree
Showing 54 changed files with 1,975 additions and 1,055 deletions.
11 changes: 0 additions & 11 deletions notes.txt

This file was deleted.

8 changes: 5 additions & 3 deletions tests/test_message_activity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Local imports
from zambeze.orchestration.message.message_activity import MessageActivity
from zambeze.orchestration.message.activity_message.message_activity import (
MessageActivity,
)

from zambeze.orchestration.zambeze_types import MessageType

Expand All @@ -15,15 +17,15 @@ def test_message_activity_type():

def test_message_activity_message():
msg_activity = MessageActivity({"body": "testing"})
assert msg_activity.message["body"] == "testing"
assert msg_activity.data["body"] == "testing"


def test_message_activity_immutability():
"""Demonstrate immutability of message once created"""
msg_activity = MessageActivity({"body": "testing"})
failed = False
try:
msg_activity.message["new_key"] == "should fail"
msg_activity.data["new_key"] == "should fail"
except Exception:
failed = True
pass
Expand Down
112 changes: 80 additions & 32 deletions tests/test_message_activity_validator.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,109 @@
import pytest
import time
import uuid

from zambeze.orchestration.message.message_activity_validator import (
from zambeze.orchestration.message.activity_message.message_activity_validator import (
MessageActivityValidator,
)

# fmt: off
from zambeze.orchestration.message.activity_message.\
message_activity_template_generator import createActivityTemplate
# fmt: on
from zambeze.orchestration.zambeze_types import ActivityType

###############################################################################
# Testing Action: Control
###############################################################################


@pytest.mark.unit
def test_message_activity_validator1():
"""This check should fail missing required fields"""
def test_message_activity_validator():
"""This check should fail because a dict is passed to a validator instead
of an immutable message type."""
activity_message = {
"message_id": "",
"submission_time": "",
"type": "",
"activity_id": "",
"message_id": str(uuid.uuid4()),
"submission_time": str(int(time.time())),
"type": "ACTIVITY",
"activity_id": str(uuid.uuid4()),
"campaign_id": str(uuid.uuid4()),
"credential": {},
"body": {},
}
validator = MessageActivityValidator()
result = validator.check(activity_message)
assert result[0] is False


@pytest.mark.unit
def test_message_activity_validator2():
"""This test should be true all required fields are included"""
activity_message = {
"message_id": "",
"type": "",
"activity_id": "",
"agent_id": "",
"campaign_id": "",
"credential": {},
"submission_time": "",
"body": {},
}
def test_message_activity_validator_required_shell():
"""This test should be true all required fields are included
The following attributes should exist
message_id: "",
type: "ACTIVITY",
activity_id: "",
agent_id: "",
campaign_id: "",
credential: {},
submission_time: "",
body: {},
"""
validator = MessageActivityValidator()
activity_message = createActivityTemplate(ActivityType.SHELL)
activity_message.message_id = str(uuid.uuid4())
activity_message.activity_id = str(uuid.uuid4())
activity_message.agent_id = str(uuid.uuid4())
activity_message.campaign_id = str(uuid.uuid4())
activity_message.credential = {}
activity_message.submission_time = str(int(time.time()))
assert activity_message.body.type == "SHELL"

result = validator.check(activity_message)
assert result[0] is True


@pytest.mark.unit
def test_message_activity_validator3():
"""This test should be true all required fields are included as well as all
def test_message_activity_validator_required_plugin():
"""This test should be true all required fields are included
The following attributes should exist
message_id: "",
type: "ACTIVITY",
activity_id: "",
agent_id: "",
campaign_id: "",
credential: {},
submission_time: "",
body: {},
"""
validator = MessageActivityValidator()
activity_message = createActivityTemplate(ActivityType.PLUGIN)
activity_message.message_id = str(uuid.uuid4())
activity_message.activity_id = str(uuid.uuid4())
activity_message.agent_id = str(uuid.uuid4())
activity_message.campaign_id = str(uuid.uuid4())
activity_message.credential = {}
activity_message.submission_time = str(int(time.time()))
assert activity_message.body.type == "PLUGIN"
result = validator.check(activity_message)
assert result[0] is True


@pytest.mark.unit
def test_message_activity_validator_required_and_optional_shell():
"""This test should be true all required fields are defined as well as all
optional fields"""
activity_message = {
"message_id": "",
"type": "",
"activity_id": "",
"agent_id": "",
"campaign_id": "",
"credential": {},
"submission_time": "",
"body": {},
"needs": [],
}
validator = MessageActivityValidator()
activity_message = createActivityTemplate(ActivityType.SHELL)
activity_message.message_id = str(uuid.uuid4())
activity_message.activity_id = str(uuid.uuid4())
activity_message.agent_id = str(uuid.uuid4())
activity_message.campaign_id = str(uuid.uuid4())
activity_message.credential = {}
activity_message.submission_time = str(int(time.time()))
assert activity_message.body.type == "SHELL"
activity_message.needs = []
result = validator.check(activity_message)
assert result[0] is True

Expand Down
147 changes: 140 additions & 7 deletions tests/test_message_factory.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Local imports
from zambeze.orchestration.message.message_factory import MessageFactory

from zambeze.orchestration.zambeze_types import MessageType
from zambeze.orchestration.zambeze_types import MessageType, ActivityType
from zambeze.orchestration.plugins import Plugins

# Standard imports
import pytest
import time
import uuid


@pytest.mark.unit
Expand Down Expand Up @@ -38,11 +39,11 @@ def test_factory_fail():
"""At this point the template can be used to fill in the items required
to exceute the message after it is sent"""
print(status_tuple)
status_tuple[1]["message_id"] = 1
status_tuple[1]["submission_time"] = time.time()
status_tuple[1].message_id = 1
status_tuple[1].submission_time = str(int(time.time()))

"""This field should force failure"""
status_tuple[1]["unsupported_field"] = "should make fail"
status_tuple[1].unsupported_field = "should make fail"

"""Should generate an immutable Status message, it will also verify that
the correct fields have been included."""
Expand All @@ -56,7 +57,100 @@ def test_factory_fail():


@pytest.mark.unit
def test_factory_success():
def test_factory_activity_template_plugin_globus():
"""This test should be true all required fields are defined as well as all
optional fields"""
plugins = Plugins()
msg_factory = MessageFactory(plugins)
activity_template = msg_factory.createTemplate(
MessageType.ACTIVITY,
ActivityType.PLUGIN,
args={"plugin": "globus", "action": "transfer"},
)[1]
print(activity_template)
no_fail = True
try:
activity_template.message_id = str(uuid.uuid4())
activity_template.activity_id = str(uuid.uuid4())
activity_template.agent_id = str(uuid.uuid4())
activity_template.campaign_id = str(uuid.uuid4())
activity_template.credential = {}
activity_template.submission_time = str(int(time.time()))
assert activity_template.body.parameters.transfer.type == "synchronous"
assert activity_template.body.type == "PLUGIN"
assert activity_template.body.plugin == "globus"
activity_template.body.parameters.transfer.items[0].source = ""
activity_template.body.parameters.transfer.items[0].destination = ""
activity_template.needs = []
except Exception as e:
print(e)
no_fail = False
assert no_fail


@pytest.mark.unit
def test_factory_activity_template_plugin_rsync():
"""This test should be true all required fields are defined as well as all
optional fields"""
plugins = Plugins()
msg_factory = MessageFactory(plugins)
activity_template = msg_factory.createTemplate(
MessageType.ACTIVITY,
ActivityType.PLUGIN,
{"plugin": "rsync", "action": "transfer"},
)[1]
print(activity_template)
no_fail = True
try:
activity_template.message_id = str(uuid.uuid4())
activity_template.activity_id = str(uuid.uuid4())
activity_template.agent_id = str(uuid.uuid4())
activity_template.campaign_id = str(uuid.uuid4())
activity_template.credential = {}
activity_template.submission_time = str(int(time.time()))
assert activity_template.body.parameters.transfer.type == "synchronous"
assert activity_template.body.type == "PLUGIN"
assert activity_template.body.plugin == "rsync"
activity_template.body.parameters.transfer.items[0].source.path = ""
activity_template.body.parameters.transfer.items[0].source.user = ""
activity_template.body.parameters.transfer.items[0].destination.path = ""
activity_template.body.parameters.transfer.items[0].destination.user = ""
activity_template.needs = []
except Exception as e:
print(e)
no_fail = False
assert no_fail


@pytest.mark.unit
def test_factory_activity_template_shell():
"""This test should be true all required fields are defined as well as all
optional fields"""
plugins = Plugins()
msg_factory = MessageFactory(plugins)
activity_template = msg_factory.createTemplate(
MessageType.ACTIVITY, ActivityType.SHELL, args={"shell": "bash"}
)[1]
print(activity_template)
no_fail = True
try:
activity_template.message_id = str(uuid.uuid4())
activity_template.activity_id = str(uuid.uuid4())
activity_template.agent_id = str(uuid.uuid4())
activity_template.campaign_id = str(uuid.uuid4())
activity_template.credential = {}
activity_template.submission_time = str(int(time.time()))
assert activity_template.body.type == "SHELL"
assert activity_template.body.shell == "bash"

except Exception as e:
print(e)
no_fail = False
assert no_fail


@pytest.mark.unit
def test_factory_success_status():
plugins = Plugins()
msg_factory = MessageFactory(plugins)

Expand All @@ -83,11 +177,50 @@ def test_factory_success():
status_tuple = msg_factory.createTemplate(MessageType.STATUS)
"""At this point the template can be used to fill in the items required
to exceute the message after it is sent"""
status_tuple[1]["message_id"] = 1
status_tuple[1]["submission_time"] = time.time()
status_tuple[1].message_id = str(uuid.uuid4())
status_tuple[1].submission_time = str(int(time.time()))
status_tuple[1].activity_id = str(uuid.uuid4())
status_tuple[1].target_id = str(uuid.uuid4())
status_tuple[1].campaign_id = str(uuid.uuid4())
status_tuple[1].agent_id = str(uuid.uuid4())
status_tuple[1].body = {}

"""Should generate an immutable Status message, it will also verify that
the correct fields have been included."""
status_msg = msg_factory.create(status_tuple)

assert status_msg.type == MessageType.STATUS


@pytest.mark.unit
def test_factory_create_plugin_rsync():
"""This test should be true all required fields are defined as well as all
optional fields"""
plugins = Plugins()
msg_factory = MessageFactory(plugins)
activity_tuple = msg_factory.createTemplate(
MessageType.ACTIVITY,
ActivityType.PLUGIN,
{"plugin": "rsync", "action": "transfer"},
)
print(activity_tuple)

activity_tuple[1].message_id = str(uuid.uuid4())
activity_tuple[1].activity_id = str(uuid.uuid4())
activity_tuple[1].agent_id = str(uuid.uuid4())
activity_tuple[1].campaign_id = str(uuid.uuid4())
activity_tuple[1].credential = {}
activity_tuple[1].submission_time = str(int(time.time()))
activity_tuple[1].body.parameters.transfer.items[0].source.path = ""
activity_tuple[1].body.parameters.transfer.items[0].source.user = ""
activity_tuple[1].body.parameters.transfer.items[0].source.ip = "127.0.0.1"
activity_tuple[1].body.parameters.transfer.items[0].destination.path = ""
activity_tuple[1].body.parameters.transfer.items[0].destination.user = ""
activity_tuple[1].body.parameters.transfer.items[0].destination.ip = "127.0.0.1"
activity_tuple[1].needs = []

"""Should generate an immutable Activity message, it will also verify that
the correct fields have been included."""
status_msg = msg_factory.create(activity_tuple)

assert status_msg.type == MessageType.ACTIVITY
6 changes: 3 additions & 3 deletions tests/test_message_status.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Local imports
from zambeze.orchestration.message.message_status import MessageStatus
from zambeze.orchestration.message.status_message.message_status import MessageStatus

from zambeze.orchestration.zambeze_types import MessageType

Expand All @@ -15,15 +15,15 @@ def test_message_status_type():

def test_message_status_message():
msg_status = MessageStatus({"body": "testing"})
assert msg_status.message["body"] == "testing"
assert msg_status.data["body"] == "testing"


def test_message_status_immutability():
"""Demonstrate immutability of message once created"""
msg_status = MessageStatus({"body": "testing"})
failed = False
try:
msg_status.message["new_key"] == "should fail"
msg_status.data["new_key"] == "should fail"
except Exception:
failed = True
pass
Expand Down
Loading

0 comments on commit 741ca9d

Please sign in to comment.