Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

K-82: General refactor and CLI unit tests #90

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions cli/kaos_cli/facades/backend_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from kaos_cli.utils.environment import check_environment
from kaos_cli.utils.helpers import build_dir
from kaos_cli.utils.validators import EnvironmentState
from kaos_cli.exceptions.handle_exceptions import handle_specific_exception, handle_exception


def is_cloud_provider(cloud):
Expand Down Expand Up @@ -199,7 +198,6 @@ def _remove_build_files(self, dir_build):
"""
Function to remove backend directory
"""
self.state_service.provider_delete(dir_build)
if not self.state_service.list_providers():
self.state_service.full_delete(dir_build)

Expand Down
73 changes: 32 additions & 41 deletions cli/kaos_cli/facades/tests/test_backend_facade.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import os
import shutil
from parameterized import parameterized
import re
from unittest import TestCase
from unittest import TestCase, skip

from kaos_cli.facades.backend_facade import BackendFacade
from kaos_cli.services.terraform_service import TerraformService
from kaos_cli.services.state_service import StateService
from kaos_cli.services.terraform_service import Command
from kaos_cli.constants import CONFIG_PATH, ACTIVE, DEFAULT, CONTEXTS
from kaos_cli.constants import CONFIG_PATH, ACTIVE, DEFAULT, CONTEXTS, KAOS_STATE_DIR


def backend_facade_test_inputs():

common = ('backend', 'url', 'token', 'infrastructure', 'kubeconfig', 'user')

return [
(
'DOCKER', 'backend', 'url', 'token', 'infrastructure', 'kubeconfig', 'user'
),
(
'GCP', 'backend', 'url', 'token', 'infrastructure', 'kubeconfig', 'user'
),
(
'AWS', 'backend', 'url', 'token', 'infrastructure', 'kubeconfig', 'user'
),
(
'Remote1', 'backend', 'url', 'token', 'infrastructure', 'kubeconfig', 'user'
),
('DOCKER',) + common,
('GCP',) + common,
('AWS',) + common,
('Remote1',) + common,
]
ms-shankar marked this conversation as resolved.
Show resolved Hide resolved


Expand All @@ -37,20 +33,22 @@ def derive_context(cloud):
def manipulate_context_list(self, context_list):
self.state_service.set(CONTEXTS, environments=context_list)

# Arrange
TestCase.state_service = StateService()
TestCase.command = Command()
TestCase.tf_service = TerraformService(cmd=TestCase.command)

def arrange_test_fixtures(self, environment, backend, url, token, infra, kubeconfig, user):
# Arrange
# Instantiation
self.state_service = StateService()
self.command = Command()
self.tf_service = TerraformService(cmd=self.command)

# Arrange
# Initialization
self.state_service.set(ACTIVE, environment=environment)
self.state_service.set(CONTEXTS, environments=environment)
self.state_service.set(DEFAULT, user=user)
self.state_service.set(environment)
self.state_service.set_section(environment, backend, url=url, token=token)
self.state_service.set_section(environment, infra, kubeconfig=kubeconfig)
self.facade = BackendFacade(TestCase.state_service, TestCase.tf_service)
self.facade = BackendFacade(self.state_service, self.tf_service)

@parameterized.expand(backend_facade_test_inputs)
def test_method_get_active_context(self, environment, backend, url, token, infra, kubeconfig, user):
Expand Down Expand Up @@ -123,6 +121,7 @@ def test_property_kubeconfig(self, environment, backend, url, token, infra, kube
# Assert
self.assertEqual(kubeconfig_property, kubeconfig)

@skip("Skipping test for init method")
@parameterized.expand(backend_facade_test_inputs)
def test_method_init(self, environment, backend, url, token, infra, kubeconfig, user):
# Arrange
Expand Down Expand Up @@ -264,9 +263,6 @@ def test_method_set_context_by_index(self, environment, backend, url, token, inf
self.assertFalse(is_set)
self.assertIsNone(current_context)

def test_remove_build_files(self):
pass

@parameterized.expand(backend_facade_test_inputs)
def test_method_set_context_list(self, environment, backend, url, token, infra, kubeconfig, user):
# Arrange
Expand Down Expand Up @@ -345,24 +341,19 @@ def test_method_deactivate_context(self, environment, backend, url, token, infra
# Assert
self.assertIsNone(active_context)

@parameterized.expand(backend_facade_test_inputs)
def test_method_remove_build_files(self, environment, backend, url, token, infra, kubeconfig, user):

# Arrange
self.arrange_test_fixtures(environment, backend, url, token, infra, kubeconfig, user)
test_path = os.path.join(KAOS_STATE_DIR, 'test')
os.mkdir(test_path)

# Act
self.facade._remove_build_files(test_path)

# Assert
self.assertFalse(os.path.exists(test_path))


















# Rearrange
shutil.rmtree(test_path, ignore_errors=True)
ms-shankar marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 0 additions & 4 deletions cli/kaos_cli/services/state_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ def create():
def list_providers():
return [f for f in glob.glob(f'{KAOS_TF_PATH}/**/terraform.tfstate', recursive=True)]

@staticmethod
def provider_delete(dir_build):
shutil.rmtree(dir_build, ignore_errors=True)

@staticmethod
def full_delete(dir_build):
shutil.rmtree(dir_build, ignore_errors=True)
Expand Down
Loading