/
test_deploy.py
72 lines (47 loc) · 2.26 KB
/
test_deploy.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
"""
Tests for the "deploy" plugin filter
"""
import unittest
from ansible.errors import AnsibleFilterError
from filter_plugins.deploy import blue_green_host, blue_green_hosts
class TestBlueGreenHostFilter(unittest.TestCase):
"""Tests for the ``blue_green_host`` filter"""
def test_with_empty_prefix(self):
"""When the prefix is empty, the host should be left unmodified"""
host = "foo.com"
self.assertEqual(blue_green_host(host), host)
self.assertEqual(blue_green_host(host, prefix=""), host)
def test_with_not_allowed_prefix(self):
"""If prefix is not allowed, we should raise an error"""
host = "foo.com"
with self.assertRaises(AnsibleFilterError) as context_manager:
blue_green_host(host, prefix="spam")
self.assertEqual(
context_manager.exception.message,
"prefix 'spam' is not allowed (must be in ['previous', 'current', 'next'])",
)
def test_with_current_prefix(self):
"""When the prefix is "current", the host should be left unmodified"""
host = "foo.com"
self.assertEqual(blue_green_host(host, "current"), host)
def test_with_next_previous_prefix(self):
"""When next or previous prefixes are used, the host should be modified"""
host = "foo.com"
self.assertEqual(blue_green_host(host, "previous"), f"previous.{host}")
self.assertEqual(blue_green_host(host, "next"), f"next.{host}")
class TestBlueGreenHostsFilter(unittest.TestCase):
"""Tests for the ``blue_green_hosts`` filter"""
def test_with_empty_host(self):
"""When the host is empty, the filter should raise an error"""
with self.assertRaises(AnsibleFilterError) as context_manager:
blue_green_hosts(None)
self.assertEqual(context_manager.exception.message, "host cannot be empty")
with self.assertRaises(AnsibleFilterError) as context_manager:
blue_green_hosts("")
self.assertEqual(context_manager.exception.message, "host cannot be empty")
def test_with_valid_base_host(self):
"""Test the filter with a valid host"""
host = "foo.com"
self.assertEqual(
blue_green_hosts(host), "previous.foo.com,foo.com,next.foo.com"
)