| 1 |
# (c) 2017 Red Hat Inc. |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
# Make coding more python3-ish |
| 7 |
from __future__ import absolute_import |
| 8 |
from __future__ import division |
| 9 |
from __future__ import print_function |
| 10 |
|
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
import collections |
| 14 |
import json |
| 15 |
import os |
| 16 |
|
| 17 |
import pytest |
| 18 |
|
| 19 |
from ansible.module_utils._text import to_text |
| 20 |
|
| 21 |
try: |
| 22 |
import boto3 |
| 23 |
except ImportError: |
| 24 |
pass |
| 25 |
|
| 26 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import HAS_BOTO3 |
| 27 |
|
| 28 |
from ansible_collections.community.aws.plugins.modules import data_pipeline |
| 29 |
|
| 30 |
if not HAS_BOTO3: |
| 31 |
pytestmark = pytest.mark.skip("test_data_pipeline.py requires the python modules 'boto3' and 'botocore'") |
| 32 |
|
| 33 |
|
| 34 |
class FailException(Exception): |
| 35 |
pass |
| 36 |
|
| 37 |
|
| 38 |
@pytest.fixture(scope="module", name="dp_setup") |
| 39 |
def fixture_dp_setup(): |
| 40 |
""" |
| 41 |
Yield a FakeModule object, data pipeline id of a vanilla data pipeline, and data pipeline objects |
| 42 |
|
| 43 |
This fixture is module-scoped, since this can be reused for multiple tests. |
| 44 |
""" |
| 45 |
Dependencies = collections.namedtuple("Dependencies", ["module", "data_pipeline_id", "objects"]) |
| 46 |
|
| 47 |
# get objects to use to test populating and activating the data pipeline |
| 48 |
if not os.getenv("PLACEBO_RECORD"): |
| 49 |
objects = [ |
| 50 |
{"name": "Every 1 day", "id": "DefaultSchedule", "fields": []}, |
| 51 |
{"name": "Default", "id": "Default", "fields": []}, |
| 52 |
] |
| 53 |
else: |
| 54 |
s3 = boto3.client("s3") |
| 55 |
data = s3.get_object(Bucket="ansible-test-datapipeline", Key="pipeline-object/new.json") |
| 56 |
objects = json.loads(to_text(data["Body"].read())) |
| 57 |
|
| 58 |
# create a module with vanilla data pipeline parameters |
| 59 |
params = { |
| 60 |
"name": "ansible-test-create-pipeline", |
| 61 |
"description": "ansible-datapipeline-unit-test", |
| 62 |
"state": "present", |
| 63 |
"timeout": 300, |
| 64 |
"objects": [], |
| 65 |
"tags": {}, |
| 66 |
"parameters": [], |
| 67 |
"values": [], |
| 68 |
} |
| 69 |
module = FakeModule(**params) |
| 70 |
|
| 71 |
# yield a module, the data pipeline id, and the data pipeline objects (that are not yet defining the vanilla data pipeline) |
| 72 |
if not os.getenv("PLACEBO_RECORD"): |
| 73 |
yield Dependencies(module=module, data_pipeline_id="df-0590406117G8DPQZY2HA", objects=objects) |
| 74 |
else: |
| 75 |
connection = boto3.client("datapipeline") |
| 76 |
_changed, result = data_pipeline.create_pipeline(connection, module) |
| 77 |
data_pipeline_id = result["data_pipeline"]["pipeline_id"] |
| 78 |
yield Dependencies(module=module, data_pipeline_id=data_pipeline_id, objects=objects) |
| 79 |
|
| 80 |
# remove data pipeline |
| 81 |
if os.getenv("PLACEBO_RECORD"): |
| 82 |
module.params.update(state="absent") |
| 83 |
data_pipeline.delete_pipeline(connection, module) |
| 84 |
|
| 85 |
|
| 86 |
class FakeModule(object): |
| 87 |
def __init__(self, **kwargs): |
| 88 |
self.params = kwargs |
| 89 |
|
| 90 |
def fail_json(self, *args, **kwargs): |
| 91 |
self.exit_args = args |
| 92 |
self.exit_kwargs = kwargs |
| 93 |
raise FailException("FAIL") |
| 94 |
|
| 95 |
def exit_json(self, *args, **kwargs): |
| 96 |
self.exit_args = args |
| 97 |
self.exit_kwargs = kwargs |
| 98 |
|
| 99 |
|
| 100 |
def test_create_pipeline_already_exists(placeboify, maybe_sleep, dp_setup): |
| 101 |
connection = placeboify.client("datapipeline") |
| 102 |
changed, result = data_pipeline.create_pipeline(connection, dp_setup.module) |
| 103 |
assert changed is False |
| 104 |
assert "Data Pipeline ansible-test-create-pipeline is present" in result["msg"] |
| 105 |
|
| 106 |
|
| 107 |
def test_pipeline_field(placeboify, maybe_sleep, dp_setup): |
| 108 |
connection = placeboify.client("datapipeline") |
| 109 |
pipeline_field_info = data_pipeline.pipeline_field(connection, dp_setup.data_pipeline_id, "@pipelineState") |
| 110 |
assert pipeline_field_info == "PENDING" |
| 111 |
|
| 112 |
|
| 113 |
def test_define_pipeline(placeboify, maybe_sleep, dp_setup): |
| 114 |
connection = placeboify.client("datapipeline") |
| 115 |
changed, result = data_pipeline.define_pipeline( |
| 116 |
connection, dp_setup.module, dp_setup.objects, dp_setup.data_pipeline_id |
| 117 |
) |
| 118 |
assert changed is True |
| 119 |
assert "has been updated" in result |
| 120 |
|
| 121 |
|
| 122 |
def test_deactivate_pipeline(placeboify, maybe_sleep, dp_setup): |
| 123 |
connection = placeboify.client("datapipeline") |
| 124 |
_changed, result = data_pipeline.deactivate_pipeline(connection, dp_setup.module) |
| 125 |
# XXX possible bug |
| 126 |
# assert changed is True |
| 127 |
assert "Data Pipeline ansible-test-create-pipeline deactivated" in result["msg"] |
| 128 |
|
| 129 |
|
| 130 |
def test_activate_without_population(placeboify, maybe_sleep, dp_setup): |
| 131 |
connection = placeboify.client("datapipeline") |
| 132 |
with pytest.raises(FailException): |
| 133 |
_changed, _result = data_pipeline.activate_pipeline(connection, dp_setup.module) |
| 134 |
assert dp_setup.module.exit_kwargs.get("msg") == "You need to populate your pipeline before activation." |
| 135 |
|
| 136 |
|
| 137 |
def test_create_pipeline(placeboify, maybe_sleep): |
| 138 |
connection = placeboify.client("datapipeline") |
| 139 |
params = { |
| 140 |
"name": "ansible-unittest-create-pipeline", |
| 141 |
"description": "ansible-datapipeline-unit-test", |
| 142 |
"state": "present", |
| 143 |
"timeout": 300, |
| 144 |
"tags": {}, |
| 145 |
} |
| 146 |
m = FakeModule(**params) |
| 147 |
changed, result = data_pipeline.create_pipeline(connection, m) |
| 148 |
assert changed is True |
| 149 |
assert result["msg"] == "Data Pipeline ansible-unittest-create-pipeline created." |
| 150 |
|
| 151 |
data_pipeline.delete_pipeline(connection, m) |
| 152 |
|
| 153 |
|
| 154 |
def test_create_pipeline_with_tags(placeboify, maybe_sleep): |
| 155 |
connection = placeboify.client("datapipeline") |
| 156 |
params = { |
| 157 |
"name": "ansible-unittest-create-pipeline_tags", |
| 158 |
"description": "ansible-datapipeline-unit-test", |
| 159 |
"state": "present", |
| 160 |
"tags": {"ansible": "test"}, |
| 161 |
"timeout": 300, |
| 162 |
} |
| 163 |
m = FakeModule(**params) |
| 164 |
changed, result = data_pipeline.create_pipeline(connection, m) |
| 165 |
assert changed is True |
| 166 |
assert result["msg"] == "Data Pipeline ansible-unittest-create-pipeline_tags created." |
| 167 |
|
| 168 |
data_pipeline.delete_pipeline(connection, m) |
| 169 |
|
| 170 |
|
| 171 |
def test_delete_nonexistent_pipeline(placeboify, maybe_sleep): |
| 172 |
connection = placeboify.client("datapipeline") |
| 173 |
params = { |
| 174 |
"name": "ansible-test-nonexistent", |
| 175 |
"description": "ansible-test-nonexistent", |
| 176 |
"state": "absent", |
| 177 |
"objects": [], |
| 178 |
"tags": {"ansible": "test"}, |
| 179 |
"timeout": 300, |
| 180 |
} |
| 181 |
m = FakeModule(**params) |
| 182 |
changed, _result = data_pipeline.delete_pipeline(connection, m) |
| 183 |
assert changed is False |
| 184 |
|
| 185 |
|
| 186 |
def test_delete_pipeline(placeboify, maybe_sleep): |
| 187 |
connection = placeboify.client("datapipeline") |
| 188 |
params = { |
| 189 |
"name": "ansible-test-nonexistent", |
| 190 |
"description": "ansible-test-nonexistent", |
| 191 |
"state": "absent", |
| 192 |
"objects": [], |
| 193 |
"tags": {"ansible": "test"}, |
| 194 |
"timeout": 300, |
| 195 |
} |
| 196 |
m = FakeModule(**params) |
| 197 |
data_pipeline.create_pipeline(connection, m) |
| 198 |
changed, _result = data_pipeline.delete_pipeline(connection, m) |
| 199 |
assert changed is True |
| 200 |
|
| 201 |
|
| 202 |
def test_build_unique_id_different(): |
| 203 |
m = FakeModule(**{"name": "ansible-unittest-1", "description": "test-unique-id"}) |
| 204 |
m2 = FakeModule(**{"name": "ansible-unittest-1", "description": "test-unique-id-different"}) |
| 205 |
assert data_pipeline.build_unique_id(m) != data_pipeline.build_unique_id(m2) |
| 206 |
|
| 207 |
|
| 208 |
def test_build_unique_id_same(): |
| 209 |
m = FakeModule(**{"name": "ansible-unittest-1", "description": "test-unique-id", "tags": {"ansible": "test"}}) |
| 210 |
m2 = FakeModule(**{"name": "ansible-unittest-1", "description": "test-unique-id", "tags": {"ansible": "test"}}) |
| 211 |
assert data_pipeline.build_unique_id(m) == data_pipeline.build_unique_id(m2) |
| 212 |
|
| 213 |
|
| 214 |
def test_build_unique_id_obj(): |
| 215 |
# check that the object can be different and the unique id should be the same; should be able to modify objects |
| 216 |
m = FakeModule(**{"name": "ansible-unittest-1", "objects": [{"first": "object"}]}) |
| 217 |
m2 = FakeModule(**{"name": "ansible-unittest-1", "objects": [{"second": "object"}]}) |
| 218 |
assert data_pipeline.build_unique_id(m) == data_pipeline.build_unique_id(m2) |
| 219 |
|
| 220 |
|
| 221 |
def test_format_tags(): |
| 222 |
unformatted_tags = {"key1": "val1", "key2": "val2", "key3": "val3"} |
| 223 |
formatted_tags = data_pipeline.format_tags(unformatted_tags) |
| 224 |
for tag_set in formatted_tags: |
| 225 |
assert unformatted_tags[tag_set["key"]] == tag_set["value"] |
| 226 |
|
| 227 |
|
| 228 |
def test_format_empty_tags(): |
| 229 |
unformatted_tags = {} |
| 230 |
formatted_tags = data_pipeline.format_tags(unformatted_tags) |
| 231 |
assert formatted_tags == [] |
| 232 |
|
| 233 |
|
| 234 |
def test_pipeline_description(placeboify, maybe_sleep, dp_setup): |
| 235 |
connection = placeboify.client("datapipeline") |
| 236 |
dp_id = dp_setup.data_pipeline_id |
| 237 |
pipelines = data_pipeline.pipeline_description(connection, dp_id) |
| 238 |
assert dp_id == pipelines["pipelineDescriptionList"][0]["pipelineId"] |
| 239 |
|
| 240 |
|
| 241 |
def test_pipeline_description_nonexistent(placeboify, maybe_sleep): |
| 242 |
hypothetical_pipeline_id = "df-015440025PF7YGLDK47C" |
| 243 |
connection = placeboify.client("datapipeline") |
| 244 |
with pytest.raises(data_pipeline.DataPipelineNotFound): |
| 245 |
data_pipeline.pipeline_description(connection, hypothetical_pipeline_id) |
| 246 |
|
| 247 |
|
| 248 |
def test_check_dp_exists_true(placeboify, maybe_sleep, dp_setup): |
| 249 |
connection = placeboify.client("datapipeline") |
| 250 |
exists = data_pipeline.check_dp_exists(connection, dp_setup.data_pipeline_id) |
| 251 |
assert exists is True |
| 252 |
|
| 253 |
|
| 254 |
def test_check_dp_exists_false(placeboify, maybe_sleep): |
| 255 |
hypothetical_pipeline_id = "df-015440025PF7YGLDK47C" |
| 256 |
connection = placeboify.client("datapipeline") |
| 257 |
exists = data_pipeline.check_dp_exists(connection, hypothetical_pipeline_id) |
| 258 |
assert exists is False |
| 259 |
|
| 260 |
|
| 261 |
def test_check_dp_status(placeboify, maybe_sleep, dp_setup): |
| 262 |
inactive_states = ["INACTIVE", "PENDING", "FINISHED", "DELETING"] |
| 263 |
connection = placeboify.client("datapipeline") |
| 264 |
state = data_pipeline.check_dp_status(connection, dp_setup.data_pipeline_id, inactive_states) |
| 265 |
assert state is True |
| 266 |
|
| 267 |
|
| 268 |
def test_activate_pipeline(placeboify, maybe_sleep, dp_setup): |
| 269 |
# use objects to define pipeline before activating |
| 270 |
connection = placeboify.client("datapipeline") |
| 271 |
data_pipeline.define_pipeline( |
| 272 |
connection, module=dp_setup.module, objects=dp_setup.objects, dp_id=dp_setup.data_pipeline_id |
| 273 |
) |
| 274 |
changed, _result = data_pipeline.activate_pipeline(connection, dp_setup.module) |
| 275 |
assert changed is True |