This repository has been archived by the owner on Jan 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_arcaflow_plugin_aws_ec2_control.py
137 lines (111 loc) · 4.63 KB
/
test_arcaflow_plugin_aws_ec2_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
import unittest
import ec2_plugin
from arcaflow_plugin_sdk import plugin
import boto3
from moto import mock_ec2
mock_region = "us-east-2"
class AwsEC2Test(unittest.TestCase):
@staticmethod
def test_serialization():
plugin.test_object_serialization(
ec2_plugin.NodeActionParams(
action=ec2_plugin.NodeAction.START,
instance_id="none",
aws_access_key_id="none",
aws_access_private_key="none",
aws_region="none",
wait=True,
wait_timeout=30,
)
)
plugin.test_object_serialization(
ec2_plugin.SuccessOutput(ms_duration=50, final_power_state_on=True)
)
plugin.test_object_serialization(
ec2_plugin.ErrorOutput(error="This is an error")
)
# @patch("boto3.session.session.resource.Instance.wait_until_running")
# @patch("boto3.session.client.start_instances")
# @patch("boto3.session.session.resource.Instance")
# @patch("ec2_plugin.boto3.client")
# @patch("ec2_plugin.boto3")
@mock_ec2
def test_functional(self):
# Setup the instance in mock
# client = boto3.client("ec2", region_name=mock_region)
ec2 = boto3.resource("ec2", region_name=mock_region)
instance = ec2.create_instances(ImageId="ami-12c6146b", MinCount=1, MaxCount=1)[
0
]
input = ec2_plugin.NodeActionParams(
action=ec2_plugin.NodeAction.START,
instance_id=instance.id,
aws_access_key_id="test",
aws_access_private_key="test",
aws_region=mock_region,
wait=True,
wait_timeout=1,
)
# Test start
output_id, output_data = ec2_plugin.aws_action(input)
# Verify that it completed successfully
self.assertEqual("success", output_id)
self.assertEqual(True, output_data.final_power_state_on)
# Verify that it did what it says it did
instance.reload()
self.assertEqual("running", instance.state["Name"])
# Test stop
input.action = ec2_plugin.NodeAction.STOP
output_id, output_data = ec2_plugin.aws_action(input)
self.assertEqual("success", output_id)
self.assertEqual(False, output_data.final_power_state_on)
# Verify that it did what it says it did
instance.reload()
self.assertNotEqual("running", instance.state["Name"])
# Start again
input.action = ec2_plugin.NodeAction.START
output_id, output_data = ec2_plugin.aws_action(input)
self.assertEqual("success", output_id)
self.assertEqual(True, output_data.final_power_state_on)
# Verify that it did what it says it did
instance.reload()
self.assertEqual("running", instance.state["Name"])
# Test force stop
input.action = ec2_plugin.NodeAction.FORCE_STOP
output_id, output_data = ec2_plugin.aws_action(input)
self.assertEqual("success", output_id)
self.assertEqual(False, output_data.final_power_state_on)
# Verify that it did what it says it did
instance.reload()
self.assertNotEqual("running", instance.state["Name"])
# Test reboot when node is off
input.action = ec2_plugin.NodeAction.REBOOT
output_id, output_data = ec2_plugin.aws_action(input)
self.assertEqual("error", output_id)
# Start again
input.action = ec2_plugin.NodeAction.START
output_id, output_data = ec2_plugin.aws_action(input)
self.assertEqual("success", output_id)
self.assertEqual(True, output_data.final_power_state_on)
# Verify that it did what it says it did
instance.reload()
self.assertEqual("running", instance.state["Name"])
# Test reboot when node is on
input.action = ec2_plugin.NodeAction.REBOOT
output_id, output_data = ec2_plugin.aws_action(input)
self.assertEqual("success", output_id)
self.assertEqual(True, output_data.final_power_state_on)
# Verify that it did what it says it did
instance.reload()
self.assertEqual("running", instance.state["Name"])
# Test terminate
input.action = ec2_plugin.NodeAction.TERMINATE
output_id, output_data = ec2_plugin.aws_action(input)
self.assertEqual("success", output_id)
self.assertEqual(False, output_data.final_power_state_on)
# Verify that it did what it says it did
instance.reload()
self.assertEqual("terminated", instance.state["Name"])
if __name__ == "__main__":
unittest.main()