| 1 |
# Copyright (c) 2021, Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
from unittest.mock import patch |
| 8 |
|
| 9 |
import nomad |
| 10 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 11 |
AnsibleExitJson, |
| 12 |
AnsibleFailJson, |
| 13 |
ModuleTestCase, |
| 14 |
set_module_args, |
| 15 |
) |
| 16 |
|
| 17 |
from ansible_collections.community.general.plugins.modules import nomad_token |
| 18 |
|
| 19 |
|
| 20 |
def mock_acl_get_tokens(empty_list=False): |
| 21 |
response_object = [] |
| 22 |
|
| 23 |
if not empty_list: |
| 24 |
response_object = [ |
| 25 |
{ |
| 26 |
"AccessorID": "bac2b162-2a63-efa2-4e68-55d79dcb7721", |
| 27 |
"Name": "Bootstrap Token", |
| 28 |
"Type": "management", |
| 29 |
"Policies": None, |
| 30 |
"Roles": None, |
| 31 |
"Global": True, |
| 32 |
"Hash": "BUJ3BerTfrqFVm1P+vZr1gz9ubOkd+JAvYjNAJyaU9Y=", |
| 33 |
"CreateTime": "2023-11-12T18:44:39.740562185Z", |
| 34 |
"ExpirationTime": None, |
| 35 |
"CreateIndex": 9, |
| 36 |
"ModifyIndex": 9, |
| 37 |
}, |
| 38 |
{ |
| 39 |
"AccessorID": "0d01c55f-8d63-f832-04ff-1866d4eb594e", |
| 40 |
"Name": "devs", |
| 41 |
"Type": "client", |
| 42 |
"Policies": ["readonly"], |
| 43 |
"Roles": None, |
| 44 |
"Global": True, |
| 45 |
"Hash": "eSn8H8RVqh8As8WQNnC2vlBRqXy6DECogc5umzX0P30=", |
| 46 |
"CreateTime": "2023-11-12T18:48:34.248857001Z", |
| 47 |
"ExpirationTime": None, |
| 48 |
"CreateIndex": 14, |
| 49 |
"ModifyIndex": 836, |
| 50 |
}, |
| 51 |
] |
| 52 |
|
| 53 |
return response_object |
| 54 |
|
| 55 |
|
| 56 |
def mock_acl_generate_bootstrap(): |
| 57 |
response_object = { |
| 58 |
"AccessorID": "0d01c55f-8d63-f832-04ff-1866d4eb594e", |
| 59 |
"Name": "Bootstrap Token", |
| 60 |
"Type": "management", |
| 61 |
"Policies": None, |
| 62 |
"Roles": None, |
| 63 |
"Global": True, |
| 64 |
"Hash": "BUJ3BerTfrqFVm1P+vZr1gz9ubOkd+JAvYjNAJyaU9Y=", |
| 65 |
"CreateTime": "2023-11-12T18:48:34.248857001Z", |
| 66 |
"ExpirationTime": None, |
| 67 |
"ExpirationTTL": "", |
| 68 |
"CreateIndex": 14, |
| 69 |
"ModifyIndex": 836, |
| 70 |
"SecretID": "d539a03d-337a-8504-6d12-000f861337bc", |
| 71 |
} |
| 72 |
return response_object |
| 73 |
|
| 74 |
|
| 75 |
def mock_acl_create_update_token(): |
| 76 |
response_object = { |
| 77 |
"AccessorID": "0d01c55f-8d63-f832-04ff-1866d4eb594e", |
| 78 |
"Name": "dev", |
| 79 |
"Type": "client", |
| 80 |
"Policies": ["readonly"], |
| 81 |
"Roles": None, |
| 82 |
"Global": True, |
| 83 |
"Hash": "eSn8H8RVqh8As8WQNnC2vlBRqXy6DECogc5umzX0P30=", |
| 84 |
"CreateTime": "2023-11-12T18:48:34.248857001Z", |
| 85 |
"ExpirationTime": None, |
| 86 |
"ExpirationTTL": "", |
| 87 |
"CreateIndex": 14, |
| 88 |
"ModifyIndex": 836, |
| 89 |
"SecretID": "d539a03d-337a-8504-6d12-000f861337bc", |
| 90 |
} |
| 91 |
|
| 92 |
return response_object |
| 93 |
|
| 94 |
|
| 95 |
def mock_acl_delete_token(): |
| 96 |
return {} |
| 97 |
|
| 98 |
|
| 99 |
class TestNomadTokenModule(ModuleTestCase): |
| 100 |
def setUp(self): |
| 101 |
super().setUp() |
| 102 |
self.module = nomad_token |
| 103 |
|
| 104 |
def tearDown(self): |
| 105 |
super().tearDown() |
| 106 |
|
| 107 |
def test_should_fail_without_parameters(self): |
| 108 |
with self.assertRaises(AnsibleFailJson): |
| 109 |
with set_module_args({}): |
| 110 |
self.module.main() |
| 111 |
|
| 112 |
def test_should_create_token_type_client(self): |
| 113 |
module_args = {"host": "localhost", "name": "Dev token", "token_type": "client", "state": "present"} |
| 114 |
|
| 115 |
with set_module_args(module_args): |
| 116 |
with patch.object(nomad.api.acl.Acl, "get_tokens", return_value=mock_acl_get_tokens()) as mock_get_tokens: |
| 117 |
with patch.object( |
| 118 |
nomad.api.acl.Acl, "create_token", return_value=mock_acl_create_update_token() |
| 119 |
) as mock_create_update_token: |
| 120 |
with self.assertRaises(AnsibleExitJson): |
| 121 |
self.module.main() |
| 122 |
|
| 123 |
self.assertIs(mock_get_tokens.call_count, 1) |
| 124 |
self.assertIs(mock_create_update_token.call_count, 1) |
| 125 |
|
| 126 |
def test_should_create_token_type_bootstrap(self): |
| 127 |
module_args = {"host": "localhost", "token_type": "bootstrap", "state": "present"} |
| 128 |
|
| 129 |
with set_module_args(module_args): |
| 130 |
with patch.object(nomad.api.acl.Acl, "get_tokens") as mock_get_tokens: |
| 131 |
with patch.object(nomad.api.Acl, "generate_bootstrap") as mock_generate_bootstrap: |
| 132 |
mock_get_tokens.return_value = mock_acl_get_tokens(empty_list=True) |
| 133 |
mock_generate_bootstrap.return_value = mock_acl_generate_bootstrap() |
| 134 |
|
| 135 |
with self.assertRaises(AnsibleExitJson): |
| 136 |
self.module.main() |
| 137 |
|
| 138 |
self.assertIs(mock_get_tokens.call_count, 1) |
| 139 |
self.assertIs(mock_generate_bootstrap.call_count, 1) |
| 140 |
|
| 141 |
def test_should_fail_delete_without_name_parameter(self): |
| 142 |
module_args = {"host": "localhost", "state": "absent"} |
| 143 |
|
| 144 |
with set_module_args(module_args): |
| 145 |
with patch.object(nomad.api.acl.Acl, "get_tokens") as mock_get_tokens: |
| 146 |
with patch.object(nomad.api.acl.Acl, "delete_token") as mock_delete_token: |
| 147 |
mock_get_tokens.return_value = mock_acl_get_tokens() |
| 148 |
mock_delete_token.return_value = mock_acl_delete_token() |
| 149 |
|
| 150 |
with self.assertRaises(AnsibleFailJson): |
| 151 |
self.module.main() |
| 152 |
|
| 153 |
def test_should_fail_delete_bootstrap_token(self): |
| 154 |
module_args = {"host": "localhost", "token_type": "boostrap", "state": "absent"} |
| 155 |
|
| 156 |
with set_module_args(module_args): |
| 157 |
with self.assertRaises(AnsibleFailJson): |
| 158 |
self.module.main() |
| 159 |
|
| 160 |
def test_should_fail_delete_boostrap_token_by_name(self): |
| 161 |
module_args = {"host": "localhost", "name": "Bootstrap Token", "state": "absent"} |
| 162 |
|
| 163 |
with set_module_args(module_args): |
| 164 |
with self.assertRaises(AnsibleFailJson): |
| 165 |
self.module.main() |
| 166 |
|
| 167 |
def test_should_delete_client_token(self): |
| 168 |
module_args = {"host": "localhost", "name": "devs", "state": "absent"} |
| 169 |
|
| 170 |
with set_module_args(module_args): |
| 171 |
with patch.object(nomad.api.acl.Acl, "get_tokens") as mock_get_tokens: |
| 172 |
with patch.object(nomad.api.acl.Acl, "delete_token") as mock_delete_token: |
| 173 |
mock_get_tokens.return_value = mock_acl_get_tokens() |
| 174 |
mock_delete_token.return_value = mock_acl_delete_token() |
| 175 |
|
| 176 |
with self.assertRaises(AnsibleExitJson): |
| 177 |
self.module.main() |
| 178 |
|
| 179 |
self.assertIs(mock_delete_token.call_count, 1) |
| 180 |
|
| 181 |
def test_should_update_client_token(self): |
| 182 |
module_args = {"host": "localhost", "name": "devs", "token_type": "client", "state": "present"} |
| 183 |
|
| 184 |
with set_module_args(module_args): |
| 185 |
with patch.object(nomad.api.acl.Acl, "get_tokens") as mock_get_tokens: |
| 186 |
with patch.object(nomad.api.acl.Acl, "update_token") as mock_create_update_token: |
| 187 |
mock_get_tokens.return_value = mock_acl_get_tokens() |
| 188 |
mock_create_update_token.return_value = mock_acl_create_update_token() |
| 189 |
|
| 190 |
with self.assertRaises(AnsibleExitJson): |
| 191 |
self.module.main() |
| 192 |
self.assertIs(mock_get_tokens.call_count, 1) |
| 193 |
self.assertIs(mock_create_update_token.call_count, 1) |