Skip to content

Commit

Permalink
Created few unittests to test config creation
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger committed Oct 23, 2018
1 parent ca1b9e9 commit c3e8c0c
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 8 deletions.
17 changes: 10 additions & 7 deletions helpers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def frontend(self):
Checks whether setup is running on a frontend server
:return: bool
"""
return self.__config.get("server_role") == "frontend"
return self.__config.get("multi") is None or \
self.__config.get("server_role") == "frontend"

@property
def frontend_questions(self):
Expand All @@ -88,14 +89,15 @@ def frontend_questions(self):

@property
def dev_mode(self):
return self.__config["dev_mode"] == Config.TRUE
return self.__config.get("dev_mode") == Config.TRUE

@property
def local_install(self):
"""
Checks whether installation is for `Workstation`s
:return: bool
"""
print(self.__config.get("local_installation") == Config.TRUE)
return self.__config.get("local_installation") == Config.TRUE

@property
Expand Down Expand Up @@ -170,9 +172,6 @@ def build(self):
if self.frontend_questions:
self.__questions_public_routes()
else:
# Reset previous choices, in case server role is not the same.
self.__config["multi"] = Config.FALSE
self.__config["use_private_dns"] = Config.FALSE
self.__detect_network()

if self.frontend_questions:
Expand Down Expand Up @@ -406,6 +405,10 @@ def __questions_installation_type(self):
CLI.colored_print("\t2) On a server")
self.__config["local_installation"] = CLI.get_response([Config.TRUE, Config.FALSE],
self.__config.get("local_installation", Config.FALSE))
if self.local_install:
# Reset previous choices, in case server role is not the same.
self.__config["multi"] = Config.FALSE
self.__config["use_private_dns"] = Config.FALSE

def __questions_multi_servers(self):
"""
Expand Down Expand Up @@ -444,7 +447,7 @@ def __questions_postgres(self):
CLI.colored_print("Total Memory in GB?", CLI.COLOR_SUCCESS)
self.__config["postgres_ram"] = CLI.get_response("~\d+", self.__config.get("postgres_ram", "8"))

if self.__config.get("multi") == Config.TRUE:
if self.multi_servers:
self.__config["postgres_profile"] = "OLTP"
CLI.colored_print("Number of connections?", CLI.COLOR_SUCCESS)
self.__config["postgres_max_connections"] = CLI.get_response(
Expand Down Expand Up @@ -691,4 +694,4 @@ def __welcome(self):
CLI.colored_print("║ Just press `enter` to accept the default value. ║", CLI.COLOR_WARNING)
CLI.colored_print("║ Otherwise choose between choices or type your answer. ║", CLI.COLOR_WARNING)
CLI.colored_print("║ ║", CLI.COLOR_WARNING)
CLI.colored_print("╚═══════════════════════════════════════════════════════════════╝", CLI.COLOR_WARNING)
CLI.colored_print("╚═══════════════════════════════════════════════════════════════╝", CLI.COLOR_WARNING)
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,17 @@ User can choose between 2 types of installations:
4. 6379-6380 redis
5. 27017 MongoDB

## Tests

Tests can be run with `pytest`.

- virtualenv for python2 can be created with `requirements_py2_tests.txt`
- virtualenv for python3 can be created with `requirements_py3_tests.txt`

## To-Do

- Handle secondary backend
- Add better input validations
- Tests

## Known issues

Expand Down
14 changes: 14 additions & 0 deletions requirements_py2_tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
argparse==1.2.1
atomicwrites==1.2.1
attrs==18.2.0
funcsigs==1.0.2
mock==2.0.0
more-itertools==4.3.0
pathlib2==2.3.2
pbr==5.0.0
pluggy==0.8.0
py==1.7.0
pytest==3.9.2
scandir==1.9.0
six==1.11.0
wsgiref==0.1.2
8 changes: 8 additions & 0 deletions requirements_py3_tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
atomicwrites==1.2.1
attrs==18.2.0
more-itertools==4.3.0
pathlib2==2.3.2
pluggy==0.8.0
py==1.7.0
pytest==3.9.2
six==1.11.0
Empty file added tests/__init__.py
Empty file.
85 changes: 85 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import pytest
try:
from unittest.mock import patch
except ImportError:
from mock import patch

from helpers.cli import CLI
from helpers.config import Config


def test_read_config():

config_dict = {"kobodocker_path": "/tmp"}
with patch.object(Config, "read_config", return_value=config_dict) as mock_conf:
config_object = Config()
assert config_object.get_config().get("kobodocker_path") == config_dict.get("kobodocker_path")
return config_object


def test_advanced_options():
config_object = test_read_config()
with patch.object(CLI, "colored_input", return_value=Config.TRUE) as mock_ci:
config_object._Config__questions_advanced_options()
assert config_object.advanced_options

with patch.object(CLI, "colored_input", return_value=Config.FALSE) as mock_ci:
config_object._Config__questions_advanced_options()
assert not config_object.advanced_options


def test_installation():
config_object = test_read_config()
with patch.object(CLI, "colored_input", return_value=Config.FALSE) as mock_ci:
config_object._Config__questions_installation_type()
assert not config_object.local_install

with patch.object(CLI, "colored_input", return_value=Config.TRUE) as mock_ci:
config_object._Config__questions_installation_type()
assert config_object.local_install
assert not config_object.multi_servers

return config_object


def test_dev_mode():
config_object = test_read_config()

with patch.object(CLI, "colored_input", return_value=Config.FALSE) as mock_ci:
config_object._Config__questions_dev_mode()
default_nginx_port = config_object.get_config().get("exposed_nginx_docker_port")
assert not config_object.dev_mode

config_object = test_installation()
with patch.object(CLI, "colored_input", return_value=Config.TRUE) as mock_ci:
config_object._Config__questions_dev_mode()
assert config_object.dev_mode
assert default_nginx_port != config_object.get_config().get("exposed_nginx_docker_port")

with patch.object(CLI, "colored_input", return_value=Config.FALSE) as mock_ci:
config_object._Config__questions_dev_mode()
config = config_object.get_config()
assert not config_object.dev_mode
assert config.get("kpi_path") == "" and config.get("kc_path") == ""


def test_server_roles_questions():
config_object = test_read_config()
assert config_object.frontend_questions
assert config_object.backend_questions

with patch.object(CLI, "colored_input", return_value=Config.TRUE) as mock_ci:
config_object._Config__questions_multi_servers()

with patch.object(CLI, "colored_input", return_value="frontend") as mock_ci:
config_object._Config__questions_roles()
assert config_object.frontend_questions
assert not config_object.backend_questions

with patch.object(CLI, "colored_input", return_value="backend") as mock_ci:
config_object._Config__questions_roles()
assert not config_object.frontend_questions
assert config_object.backend_questions

0 comments on commit c3e8c0c

Please sign in to comment.