Skip to content

Commit

Permalink
Adds update-rmq-config CLI entrypoint
Browse files Browse the repository at this point in the history
Fixes bug in RMQ config update that allowed for duplicated entries
Adds unit tests to test for duplicated users
  • Loading branch information
Daniel McKnight committed Feb 9, 2024
1 parent de3c089 commit 600bfae
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
8 changes: 8 additions & 0 deletions neon_diana_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from os.path import join

import click

Expand Down Expand Up @@ -139,6 +140,13 @@ def make_rmq_config(username, password, output_file):
generate_rmq_config(username, password, output_file)


@neon_diana_cli.command(help="Update RabbitMQ definitions")
@click.argument("config_path", default=None, required=False)
def update_rmq_config(config_path):
from neon_diana_utils.configuration import update_rmq_config
update_rmq_config(join(config_path, "rabbitmq.json"))


@neon_diana_cli.command(help="Generate a configuration file with access keys")
@click.option("--skip-write", "-s", help="Skip writing config to file",
is_flag=True)
Expand Down
2 changes: 1 addition & 1 deletion neon_diana_utils/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def update_rmq_config(config_file: str = None) -> str:
with open(config_file) as f:
real_config = json.load(f)
new_config = generate_rmq_config("", "")
existing_users = (user['name'] for user in real_config['users'])
existing_users = [user['name'] for user in real_config['users']]
for user in new_config['users']:
if user['name'] in existing_users:
continue
Expand Down
10 changes: 10 additions & 0 deletions tests/test_diana_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ def test_update_rmq_config(self):
for user in old_config['users']:
self.assertIn(user, new_config['users'])

# Ensure no duplicated users
for user in new_config['users']:
self.assertEqual(len([u for u in new_config['users']
if u['name'] == user['name']]), 1, user)
os.remove(test_file)
shutil.move(f"{test_file}.old", test_file)

Expand All @@ -129,6 +133,12 @@ def test_generate_rmq_config(self):
self.assertEqual(set(config.keys()), {'users', 'vhosts', 'permissions'})
for user in config['users']:
self.assertEqual(set(user.keys()), {'name', 'password', 'tags'})

# Ensure no duplicate users
for user in config['users']:
self.assertEqual(len([u for u in config['users']
if u['name'] == user['name']]), 1, user)

for vhost in config['vhosts']:
self.assertEqual(set(vhost.keys()), {'name'})
self.assertTrue(vhost['name'].startswith('/'))
Expand Down

0 comments on commit 600bfae

Please sign in to comment.