Skip to content

Commit

Permalink
Merge pull request #74 from keitaroinc/bugfix-unit-tests
Browse files Browse the repository at this point in the history
Bugfix unit tests
  • Loading branch information
gocemitevski authored Mar 27, 2024
2 parents 6b42b94 + a79e39c commit 74c4c0f
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 235 deletions.
14 changes: 13 additions & 1 deletion enabler/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import os
import logging

import click
import click_log
import subprocess


class CLI:
def __init__(self, runner):
self.runner = runner

def version_command(self):
try:
result = subprocess.run(['enabler', 'version'], capture_output=True, text=True, check=True) # noqa
return result.stdout.strip()
except subprocess.CalledProcessError as e:
raise RuntimeError(f"Failed to get version: {e}")


CONTEXT_SETTINGS = dict(auto_envvar_prefix="ENABLER")
Expand Down
51 changes: 13 additions & 38 deletions enabler/commands/enabler_completion.sh
Original file line number Diff line number Diff line change
@@ -1,55 +1,30 @@
_enabler_complete() {
local cur_word prev_word commands
local cur_word prev_word

# Get the current and previous words
cur_word="${COMP_WORDS[COMP_CWORD]}"
prev_word="${COMP_WORDS[COMP_CWORD-1]}"
local categories="apps kind preflight platform setup version"

case "$prev_word" in
"enabler") # noqa
commands="$categories"
COMPREPLY=( $(compgen -W "apps kind preflight platform setup version" -- "$cur_word") )
;;
esac

# Initialize the variable to store previous words
prev_words=""
local apps="namespace"
local kind="create delete status start stop"
local platform="init info keys release version"
local setup="init metallb istio"

# Loop through previous words and concatenate them
for ((i=1; i<COMP_CWORD; i++));
do
prev_words="${prev_words}${COMP_WORDS[i]} "
done

# Trim any trailing whitespace
prev_words="${prev_words% }"

case "$prev_words" in
"enabler apps")
commands="$commands $apps"
"apps")
COMPREPLY=( $(compgen -W "namespace" -- "$cur_word") )
;;
"platform")
COMPREPLY=( $(compgen -W "init info keys release version" -- "$cur_word") )
;;
"enabler kind")
commands="$commands $kind"
"kind")
COMPREPLY=( $(compgen -W "create delete status start stop" -- "$cur_word") )
;;
"enabler platform")
commands="$commands $platform"
"setup")
COMPREPLY=( $(compgen -W "init metallb istio" -- "$cur_word") )
;;
"enabler setup")
commands="$commands $setup"
*)
COMPREPLY=()
;;
esac

echo ""
echo "$commands"

if [[ "$cur_word" == "$prev_word"* ]]; then
COMPREPLY=( $(compgen -W "$commands" -- "$cur_word") )
fi
}

# Register _enabler_complete to provide completion for the enabler command
complete -F _enabler_complete enabler
16 changes: 16 additions & 0 deletions enabler/unit_tests/apps_unittests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import unittest
from click.testing import CliRunner
from unittest.mock import patch
from enabler.commands.cmd_apps import cli as CLI


class TestAppCommands(unittest.TestCase):
def setUp(self):
self.runner = CliRunner()

@patch('enabler.commands.cmd_apps.s')
def test_create_namespace_command(self, mock_s):
mock_s.run.return_value.returncode = 0
result = self.runner.invoke(CLI, ['namespace', 'test-namespace'])
self.assertEqual(result.exit_code, 0)
# self.assertIn('Namespace created successfully', result.output)
49 changes: 49 additions & 0 deletions enabler/unit_tests/kind_unittests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import unittest
from click.testing import CliRunner
from unittest.mock import MagicMock, patch
from enabler.commands.cmd_kind import cli as CLI


class TestKindCommands(unittest.TestCase):
def setUp(self):
self.runner = CliRunner()

@patch('enabler.commands.cmd_kind.s')
def test_create_command(self, mock_s):
mock_s.run.return_value.returncode = 0
result = self.runner.invoke(CLI, ['create'])
self.assertEqual(result.exit_code, 0)

@patch('enabler.commands.cmd_kind.s')
def test_delete_command(self, mock_s):
mock_s.run.return_value.returncode = 0
result = self.runner.invoke(CLI, ['delete'])
self.assertEqual(result.exit_code, 0)

@patch('enabler.commands.cmd_kind.s')
def test_status_command(self, mock_s):
mock_s.run.return_value.returncode = 0
result = self.runner.invoke(CLI, ['status'])
self.assertEqual(result.exit_code, 0)

@patch('enabler.commands.cmd_kind.docker')
@patch('enabler.commands.cmd_kind.kube')
@patch('enabler.commands.cmd_kind.click_spinner.spinner')
def test_start_command(self, mock_spinner, mock_kube, mock_docker):
mock_kube.kubectl_info.return_value = True
mock_container = MagicMock()
mock_container.name = 'test-control-plane'
mock_container.status = 'running'
mock_docker.from_env.return_value.containers.list.return_value = [mock_container] # noqa
result = self.runner.invoke(CLI, ['start'])
self.assertEqual(result.exit_code, 0)

@patch('enabler.commands.cmd_kind.docker')
@patch('enabler.commands.cmd_kind.click_spinner.spinner')
def test_stop_command(self, mock_spinner, mock_docker):
mock_container = MagicMock()
mock_container.name = 'test-control-plane'
mock_container.status = 'running'
mock_docker.from_env.return_value.containers.list.return_value = [mock_container] # noqa
result = self.runner.invoke(CLI, ['stop'])
self.assertEqual(result.exit_code, 0)
39 changes: 39 additions & 0 deletions enabler/unit_tests/platform_unittests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
from click.testing import CliRunner
from unittest.mock import MagicMock, patch
from enabler.commands.cmd_platform import cli as CLI


class TestPlatformCommands(unittest.TestCase):
def setUp(self):
self.runner = CliRunner()

@patch('enabler.commands.cmd_platform.get_submodules')
@patch('enabler.commands.cmd_platform.get_repo')
def test_platform_init_command(self, mock_get_repo, mock_get_submodules):
mock_repo = MagicMock()
mock_get_repo.return_value = mock_repo
mock_get_submodules.return_value = ['submodule1', 'submodule2']
with patch('enabler.commands.cmd_platform.click_spinner.spinner'):
result = self.runner.invoke(CLI, ['platform', 'init', 'all'])
self.assertEqual(result.exit_code, 0)
self.assertIn('Platform initialized.', result.output)

@patch('enabler.commands.cmd_platform.s')
def test_platform_info_command(self, mock_s):
mock_s.run.side_effect = [
MagicMock(),
MagicMock(),
]
result = self.runner.invoke(CLI, ['platform', 'info', '--kube-context', 'test-context']) # noqa
self.assertEqual(result.exit_code, 0)
self.assertIn('Platform can be accessed through the URL:', result.output) # noqa

@patch('enabler.commands.cmd_platform.os.path.exists')
@patch('enabler.commands.cmd_platform.rsa.generate_private_key')
def test_platform_keys_command(self, mock_generate_private_key, mock_path_exists): # noqa
mock_generate_private_key.return_value = MagicMock()
mock_path_exists.return_value = False
result = self.runner.invoke(CLI, ['platform', 'keys', '2048'])
self.assertEqual(result.exit_code, 0)
self.assertIn('Keys generated successfully.', result.output)
24 changes: 24 additions & 0 deletions enabler/unit_tests/preflight_unittests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest
from click.testing import CliRunner
from unittest.mock import patch
from enabler.commands.cmd_preflight import cli as CLI


class TestPreflightCommands(unittest.TestCase):
def setUp(self):
self.runner = CliRunner()

@patch('enabler.commands.cmd_preflight.s')
def test_preflight_command(self, mock_s):
mock_s.run.return_value.returncode = 0
with self.runner.isolated_filesystem():
result = self.runner.invoke(CLI)

self.assertEqual(result.exit_code, 0)
self.assertIn('java jdk 11', result.output)
self.assertIn('docker', result.output)
self.assertIn('helm 3', result.output)
self.assertIn('kind', result.output)
self.assertIn('skaffold', result.output)
self.assertIn('kubectl', result.output)
self.assertIn('istioctl', result.output)
48 changes: 48 additions & 0 deletions enabler/unit_tests/setup_unittests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import unittest
from click.testing import CliRunner
from unittest.mock import MagicMock, patch
from enabler.commands.cmd_setup import cli as CLI
import os


class TestSetupCommands(unittest.TestCase):
def setUp(self):
self.runner = CliRunner()

@patch('enabler.commands.cmd_setup.urllib.request')
@patch('enabler.commands.cmd_setup.os.stat')
@patch('enabler.commands.cmd_setup.os.chmod')
def test_init_command(self, mock_chmod, mock_stat, mock_request):

permission = 0o755
os.chmod('enabler/bin', permission)

mock_request.urlretrieve.side_effect = [
('enabler/bin/kubectl', None),
('enabler/bin/helm.tar.gz', None),
('enabler/bin/istioctl.tar.gz', None),
('enabler/bin/kind', None),
('enabler/bin/skaffold', None),
]
mock_stat.return_value.st_mode = 0o755
mock_chmod.return_value = None

result = self.runner.invoke(CLI, ['init'])
self.assertEqual(result.exit_code, 0)
self.assertIn('All dependencies downloaded to bin/', result.output)

@patch('enabler.commands.cmd_setup.docker.from_env')
@patch('enabler.commands.cmd_setup.docker.networks')
@patch('enabler.commands.cmd_setup.logger')
@patch('enabler.commands.cmd_setup.s')
def test_metallb_command(self, mock_s, mock_logger, mock_networks, mock_from_env): # noqa
mock_network = MagicMock()
mock_network['Name'] = 'kind'
mock_network['IPAM']['Config'][0]['Subnet'] = '192.168.0.0/24'
mock_from_env.return_value.networks.return_value = [mock_network]
mock_s.run.return_value.returncode = 0
mock_logger.info.return_value = None

result = self.runner.invoke(CLI, ['metallb'])
self.assertEqual(result.exit_code, 0)
self.assertIn('✓ Metallb installed on cluster.', result.output)
Loading

0 comments on commit 74c4c0f

Please sign in to comment.