Skip to content

Commit

Permalink
Merge pull request #126 from kobotoolbox/fix-tests
Browse files Browse the repository at this point in the history
Make the tests pass again.
  • Loading branch information
noliveleger authored Oct 26, 2020
2 parents 4716457 + 2b76002 commit 5553139
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
2 changes: 1 addition & 1 deletion helpers/singleton.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
from __future__ import print_function


# Copy this method from `six` library to avoid import
Expand Down
45 changes: 44 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import unicode_literals, absolute_import

import pytest
import os
Expand Down Expand Up @@ -377,6 +377,10 @@ def test_force_secure_mongo():
new=write_trigger_upsert_db_users)
def test_secure_mongo_advanced_options():
config_object = read_config()
config_object._Config__config["advanced"] = Config.TRUE

# Try when setup is run for the first time.
config_object._Config__first_time = True
with patch("helpers.cli.CLI.colored_input") as mock_ci:
mock_ci.side_effect = iter([
"root",
Expand All @@ -387,6 +391,45 @@ def test_secure_mongo_advanced_options():
config_object._Config__questions_mongo()
assert not os.path.exists("/tmp/upsert_db_users")

# Try when setup has been already run once
# If it's an upgrade, users should not see:
# ╔══════════════════════════════════════════════════════╗
# ║ MongoDB root's and/or user's usernames have changed! ║
# ╚══════════════════════════════════════════════════════╝
config_object._Config__first_time = False
config_object._Config__config["mongo_secured"] = Config.FALSE

with patch("helpers.cli.CLI.colored_input") as mock_ci:
mock_ci.side_effect = iter([
"root",
"root_password",
"mongo_kobo_user",
"mongo_password"
])
config_object._Config__questions_mongo()
assert os.path.exists("/tmp/upsert_db_users")
assert os.path.getsize("/tmp/upsert_db_users") == 0
os.remove("/tmp/upsert_db_users")

# Try when setup has been already run once
# If it's NOT an upgrade, Users should see:
# ╔══════════════════════════════════════════════════════╗
# ║ MongoDB root's and/or user's usernames have changed! ║
# ╚══════════════════════════════════════════════════════╝
config_object._Config__config["mongo_secured"] = Config.TRUE
with patch("helpers.cli.CLI.colored_input") as mock_ci:
mock_ci.side_effect = iter([
"root",
"root_passw0rd",
"kobo_user",
"mongo_password",
Config.TRUE
])
config_object._Config__questions_mongo()
assert os.path.exists("/tmp/upsert_db_users")
assert os.path.getsize("/tmp/upsert_db_users") != 0
os.remove("/tmp/upsert_db_users")


@patch('helpers.config.Config._Config__write_upsert_db_users_trigger_file',
new=write_trigger_upsert_db_users)
Expand Down
14 changes: 7 additions & 7 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from helpers.config import Config
from .utils import (
read_config,
run_command,
MockCommand,
MockDocker,
)

Expand All @@ -24,7 +24,7 @@
@patch('helpers.command.Command.info',
MagicMock(return_value=True))
@patch('helpers.cli.CLI.run_command',
new=run_command)
new=MockCommand.run_command)
def test_toggle_trivial():
config_object = read_config()
Command.start()
Expand All @@ -46,7 +46,7 @@ def test_toggle_trivial():
@patch('helpers.command.Command.info',
MagicMock(return_value=True))
@patch('helpers.cli.CLI.run_command',
new=run_command)
new=MockCommand.run_command)
def test_toggle_no_letsencrypt():
config_object = read_config()
config_object._Config__config['use_letsencrypt'] = Config.FALSE
Expand All @@ -68,7 +68,7 @@ def test_toggle_no_letsencrypt():
@patch('helpers.command.Command.info',
MagicMock(return_value=True))
@patch('helpers.cli.CLI.run_command',
new=run_command)
new=MockCommand.run_command)
def test_toggle_frontend():
config_object = read_config()
Command.start(frontend_only=True)
Expand All @@ -89,7 +89,7 @@ def test_toggle_frontend():
@patch('helpers.command.Command.info',
MagicMock(return_value=True))
@patch('helpers.cli.CLI.run_command',
new=run_command)
new=MockCommand.run_command)
def test_toggle_primary_backend():
config_object = read_config()
config_object._Config__config['backend_server_role'] = 'primary'
Expand All @@ -113,7 +113,7 @@ def test_toggle_primary_backend():
@patch('helpers.command.Command.info',
MagicMock(return_value=True))
@patch('helpers.cli.CLI.run_command',
new=run_command)
new=MockCommand.run_command)
def test_toggle_secondary_backend():
config_object = read_config()
config_object._Config__config['backend_server_role'] = 'secondary'
Expand All @@ -137,7 +137,7 @@ def test_toggle_secondary_backend():
@patch('helpers.command.Command.info',
MagicMock(return_value=True))
@patch('helpers.cli.CLI.run_command',
new=run_command)
new=MockCommand.run_command)
def test_toggle_maintenance():
config_object = read_config()
mock_docker = MockDocker()
Expand Down
29 changes: 17 additions & 12 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
from mock import patch, mock_open
builtin_open = "__builtin__.open"

from six import with_metaclass

from helpers.config import Config
from helpers.singleton import Singleton
from helpers.singleton import Singleton, with_metaclass


def read_config(overrides=None):
Expand All @@ -36,20 +34,27 @@ def reset_config(config_object):
config_object.__config = config_dict


def run_command(command, cwd=None, polling=False):
if 'docker-compose' != command[0]:
raise Exception('Command: `{}` is not implemented!'.format(command[0]))

mock_docker = MockDocker()
return mock_docker.compose(command, cwd)


def write_trigger_upsert_db_users_mock(*args):
def write_trigger_upsert_db_users(*args):
content = args[1]
with open("/tmp/upsert_db_users", "w") as f:
f.write(content)


class MockCommand:
"""
Create a mock class for Python2 retro compatibility.
Python2 does not pass the class as the first argument explicitly when
`run_command` (as a standalone method) is used as a mock.
"""
@classmethod
def run_command(cls, command, cwd=None, polling=False):
if 'docker-compose' != command[0]:
raise Exception('Command: `{}` is not implemented!'.format(command[0]))

mock_docker = MockDocker()
return mock_docker.compose(command, cwd)


class MockDocker(with_metaclass(Singleton)):

PRIMARY_BACKEND_CONTAINERS = ['primary_postgres', 'mongo', 'redis_main', 'redis_cache']
Expand Down

0 comments on commit 5553139

Please sign in to comment.