Skip to content

Commit

Permalink
Merge pull request #83 from keitaroinc/feature-enabler-structure
Browse files Browse the repository at this point in the history
Match the PyPi file structure for packages
  • Loading branch information
gocemitevski authored May 13, 2024
2 parents 61fc511 + 9187ba3 commit d4bb626
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 68 deletions.
4 changes: 2 additions & 2 deletions enabler/dependencies.yaml → dependencies.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kubectl: "https://storage.googleapis.com/kubernetes-release/release/latest/bin/{}/amd64/kubectl" # noqa
kubectl: "https://storage.googleapis.com/kubernetes-release/release/v1.29.0/bin/{}/amd64/kubectl" # noqa
helm: "https://get.helm.sh/helm-v3.1.2-{}-amd64.tar.gz" # noqa
istioctl: "https://github.com/istio/istio/releases/download/1.5.1/istioctl-1.5.1-{}.tar.gz" # noqa
kind: "https://github.com/kubernetes-sigs/kind/releases/download/v0.22.0/kind-{}-amd64" # noqa
skaffold: "https://storage.googleapis.com/skaffold/releases/latest/skaffold-{}-amd64" # noqa
skaffold: "https://storage.googleapis.com/skaffold/releases/latest/skaffold-{}-amd64" # noqa
File renamed without changes.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name="enabler",
version="0.1",
packages=["enabler", "enabler.commands", "enabler.helpers"],
packages=["enabler", "src.enabler_keitaro_inc.commands", "src.enabler_keitaro_inc.helpers"], # noqa
include_package_data=True,
install_requires=["click==7.1.1",
"click-log==0.3.2",
Expand All @@ -16,6 +16,6 @@
"flake8>=7.0.0"],
entry_points="""
[console_scripts]
enabler=enabler.cli:cli
enabler=src.enabler_keitaro_inc.enabler:cli
""",
)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enabler.cli import pass_environment, logger
from src.enabler_keitaro_inc.enabler import pass_environment, logger

import click
import subprocess as s
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enabler.cli import pass_environment, logger
from enabler.helpers import kind, kube
from src.enabler_keitaro_inc.enabler import pass_environment, logger
from src.enabler_keitaro_inc.helpers import kind, kube

import click
import click_spinner
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from enabler.cli import pass_environment, logger
from enabler.helpers.git import get_submodules, get_repo
from enabler.type import semver
from src.enabler_keitaro_inc.enabler import pass_environment, logger
from src.enabler_keitaro_inc.helpers.git import get_submodules, get_repo
from src.enabler_keitaro_inc.type import semver

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
Expand Down Expand Up @@ -218,8 +218,6 @@ def version(ctx, kube_context_cli, submodules, repopath):
# Get the repo from arguments defaults to cwd
try:
repo = get_repo(repopath)
logger.info("REPO")
logger.info(repo)
submodules = get_submodules(repo, submodules)
except Exception as e: # noqa
logger.info("An error occurred while getting submodule")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enabler.cli import pass_environment, logger
from src.enabler_keitaro_inc.enabler import pass_environment, logger

import click
import subprocess as s
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enabler.cli import pass_environment, logger
from src.enabler_keitaro_inc.enabler import pass_environment, logger

import click
import click_spinner
Expand Down Expand Up @@ -41,7 +41,7 @@ def init(ctx, kube_context_cli):

# Load URLs from the JSON file
enabler_path = get_path()
file_path = os.path.join(enabler_path, 'enabler/dependencies.yaml')
file_path = os.path.join(enabler_path, 'dependencies.yaml')
with open(file_path, 'r') as f:
urls = yaml.safe_load(f)

Expand All @@ -59,25 +59,23 @@ def init(ctx, kube_context_cli):
logger.info(f'kubectl already exists at: {kubectl_location}')
else:
logger.info('Downloading kubectl...')
download_and_make_executable(kubectl_url, kubectl_location)
download_and_make_executable(kubectl_url, kubectl_location, 'kubectl') # noqa

# Download helm if not exists or update if necessary
helm_location = 'bin/helm'
if os.path.exists(helm_location):
logger.info(f'helm already exists at: {helm_location}')
update_binary_if_necessary(helm_location, helm_url, ostype)
else:
logger.info('Downloading helm...')
download_and_make_executable(helm_url, helm_location)
download_and_make_executable(helm_url, helm_location, 'helm')

# Download istioctl if not exists or update if necessary
istioctl_location = 'bin/istioctl'
if os.path.exists(istioctl_location):
logger.info(f'istioctl already exists at: {istioctl_location}')
update_binary_if_necessary(istioctl_location, istioctl_url, ostype)
else:
logger.info('Downloading istioctl...')
download_and_make_executable(istioctl_url, istioctl_location)
download_and_make_executable(istioctl_url, istioctl_location, 'istioctl') # noqa

# Download kind if not exists or update if necessary
kind_location = 'bin/kind'
Expand All @@ -86,15 +84,15 @@ def init(ctx, kube_context_cli):
update_binary_if_necessary(kind_location, kind_url, ostype)
else:
logger.info('Downloading kind...')
download_and_make_executable(kind_url, kind_location)
download_and_make_executable(kind_url, kind_location, 'kind')

# Download skaffold if not exists
skaffold_location = 'bin/skaffold'
if os.path.exists(skaffold_location):
logger.info(f'skaffold already exists at: {skaffold_location}')
else:
logger.info('Downloading skaffold...')
download_and_make_executable(skaffold_url, skaffold_location)
download_and_make_executable(skaffold_url, skaffold_location, 'skaffold') # noqa

logger.info('All dependencies downloaded to bin/')
logger.info('IMPORTANT: Please add the path to your user profile to ' +
Expand All @@ -114,8 +112,8 @@ def get_latest_version_from_github(repo_url, ostype):
else:
logger.error("Failed to find latest release tag")
return None
except requests.exceptions.RequestException as e:
logger.error(f"Error fetching latest version from GitHub: {e}")
except requests.exceptions.RequestException as e: # noqa
logger.info("Latest release not found")
return None


Expand Down Expand Up @@ -143,11 +141,27 @@ def extract_version_from_filename(filename, binary_name):
return None


def download_and_make_executable(url, destination):
urllib.request.urlretrieve(url, destination)
st = os.stat(destination)
os.chmod(destination, st.st_mode | stat.S_IEXEC)
logger.info(f'{os.path.basename(destination)} downloaded and made executable!') # noqa
def download_and_make_executable(url, destination, binary_name):
if binary_name in ['skaffold', 'kind', 'kubectl']:
urllib.request.urlretrieve(url, destination)
st = os.stat(destination)
os.chmod(destination, st.st_mode | stat.S_IEXEC)
logger.info(f'{os.path.basename(destination)} downloaded and made executable!') # noqa
elif binary_name in ['helm', 'istioctl']:
download_and_extract_tar(url, destination, binary_name)


def download_and_extract_tar(url, destination, binary_name):
tar_filename = f'bin/{binary_name}.tar.gz'
urllib.request.urlretrieve(url, tar_filename)
tar = tarfile.open(tar_filename, 'r:gz')
for member in tar.getmembers():
if member.isreg():
member.name = os.path.basename(member.name)
tar.extract(member, 'bin')
tar.close()
os.remove(tar_filename)
logger.info(f'{binary_name} downloaded and made executable!')


def update_binary_if_necessary(binary_location, binary_url, ostype):
Expand Down Expand Up @@ -415,7 +429,7 @@ def istio(ctx, kube_context_cli, kube_context, monitoring_tools):
raise click.Abort()
if monitoring_tools == 'monitoring-tools':
try:
grafana_virtual_service = s.run(['kubectl', 'apply', '-f', 'enabler/grafana-vs.yaml'], capture_output=True, check=True) # noqa
grafana_virtual_service = s.run(['kubectl', 'apply', '-f', 'grafana-vs.yaml'], capture_output=True, check=True) # noqa
except Exception as e:
logger.error('Error setting grafana URL')
logger.error(str(e))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enabler.cli import pass_environment, logger
from src.enabler_keitaro_inc.enabler import pass_environment, logger
import pkg_resources
import click

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion enabler/cli.py → src/enabler_keitaro_inc/enabler.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def list_commands(self, ctx):
def get_command(self, ctx, name):
try:
mod = __import__(
"enabler.commands.cmd_{}".format(name), None, None, ["cli"]
"src.enabler_keitaro_inc.commands.cmd_{}".format(name), None, None, ["cli"] # noqa
)
except ImportError:
return
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enabler.cli import logger
from src.enabler_keitaro_inc.enabler import logger
import click
import git
import os
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enabler.cli import logger
from src.enabler_keitaro_inc.enabler import logger

import click
import subprocess as s
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enabler.cli import logger
from src.enabler_keitaro_inc.enabler import logger
import subprocess as s


Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import unittest
from click.testing import CliRunner
from unittest.mock import patch
from enabler.commands.cmd_apps import cli as CLI
from src.enabler_keitaro_inc.commands.cmd_apps import cli as CLI


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

@patch('enabler.commands.cmd_apps.s')
@patch('src.enabler_keitaro_inc.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)
18 changes: 9 additions & 9 deletions enabler/unit_tests/kind_unittests.py → tests/kind_unittests.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import unittest
from click.testing import CliRunner
from unittest.mock import MagicMock, patch
from enabler.commands.cmd_kind import cli as CLI
from src.enabler_keitaro_inc.commands.cmd_kind import cli as CLI


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

@patch('enabler.commands.cmd_kind.s')
@patch('src.enabler_keitaro_inc.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')
@patch('src.enabler_keitaro_inc.commands.cmd_kind.s')
def test_delete_command(self, mock_s):
mock_s.run.return_value.returncode = 1
result = self.runner.invoke(CLI, ['delete'])
self.assertEqual(result.exit_code, 1)

@patch('enabler.commands.cmd_kind.s')
@patch('src.enabler_keitaro_inc.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')
@patch('src.enabler_keitaro_inc.commands.cmd_kind.docker')
@patch('src.enabler_keitaro_inc.commands.cmd_kind.kube')
@patch('src.enabler_keitaro_inc.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()
Expand All @@ -38,8 +38,8 @@ def test_start_command(self, mock_spinner, mock_kube, mock_docker):
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')
@patch('src.enabler_keitaro_inc.commands.cmd_kind.docker')
@patch('src.enabler_keitaro_inc.commands.cmd_kind.click_spinner.spinner')
def test_stop_command(self, mock_spinner, mock_docker):
mock_container = MagicMock()
mock_container.name = 'test-control-plane'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from click.testing import CliRunner
from unittest.mock import patch
from git import Repo
from enabler.cli import cli as CLI
from src.enabler_keitaro_inc.enabler import cli as CLI
import os


Expand All @@ -22,27 +22,27 @@ def tearDown(self):
# Clean up the temporary directory after the test
shutil.rmtree(self.temp_dir)

@patch('enabler.commands.cmd_platform.s')
@patch('src.enabler_keitaro_inc.commands.cmd_platform.s')
def test_platform_init(self, mock_s):
mock_s.run.return_value.returncode = 0
result = self.runner.invoke(CLI, ['platform', 'init', 'all', self.temp_dir]) # noqa
self.assertEqual(result.exit_code, 0)

@patch('enabler.commands.cmd_platform.s')
@patch('src.enabler_keitaro_inc.commands.cmd_platform.s')
def test_platform_info(self, mock_s):
mock_s.run.return_value.returncode = 0
result = self.runner.invoke(CLI, ['platform', 'info', '--kube-context', '']) # noqa
self.assertEqual(result.exit_code, 0)

@patch('enabler.commands.cmd_platform.click.confirm')
@patch('enabler.commands.cmd_platform.s')
@patch('src.enabler_keitaro_inc.commands.cmd_platform.click.confirm')
@patch('src.enabler_keitaro_inc.commands.cmd_platform.s')
def test_platform_keys(self, mock_s, mock_confirm):
mock_s.run.return_value.returncode = 1
mock_s.run.return_value.returncode = 0
mock_confirm.return_value = False
result = self.runner.invoke(CLI, ['platform', 'keys'])
self.assertEqual(result.exit_code, 1)
self.assertEqual(result.exit_code, 0)

@patch('enabler.commands.cmd_platform.s')
@patch('src.enabler_keitaro_inc.commands.cmd_platform.s')
def test_platform_release(self, mock_s):
mock_s.run.return_value.returncode = 0
simulated_path = 'platform/microservice'
Expand All @@ -52,7 +52,7 @@ def test_platform_release(self, mock_s):
result = self.runner.invoke(CLI, ['platform', 'release', '2.1.7', simulated_path]) # noqa
self.assertEqual(result.exit_code, 0)

@patch('enabler.commands.cmd_platform.s')
@patch('src.enabler_keitaro_inc.commands.cmd_platform.s')
def test_platform_version(self, mock_s):
mock_s.run.return_value.returncode = 0
result = self.runner.invoke(CLI, ['platform', 'version'])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import unittest
from click.testing import CliRunner
from unittest.mock import patch
from enabler.commands.cmd_preflight import cli as CLI
from src.enabler_keitaro_inc.commands.cmd_preflight import cli as CLI


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

@patch('enabler.commands.cmd_preflight.s')
@patch('src.enabler_keitaro_inc.commands.cmd_preflight.s')
def test_preflight_command(self, mock_s):
mock_s.run.return_value.returncode = 0
with self.runner.isolated_filesystem():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import unittest
from click.testing import CliRunner
from unittest.mock import MagicMock, patch
from enabler.commands.cmd_setup import cli as CLI
from src.enabler_keitaro_inc.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')
@patch('src.enabler_keitaro_inc.commands.cmd_setup.urllib.request')
@patch('src.enabler_keitaro_inc.commands.cmd_setup.os.stat')
@patch('src.enabler_keitaro_inc.commands.cmd_setup.os.chmod')
def test_init_command(self, mock_chmod, mock_stat, mock_request):

permission = 0o755
Expand All @@ -31,10 +31,10 @@ def test_init_command(self, mock_chmod, mock_stat, mock_request):
print(result.output)
self.assertEqual(result.exit_code, 0)

@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')
@patch('src.enabler_keitaro_inc.commands.cmd_setup.docker.from_env')
@patch('src.enabler_keitaro_inc.commands.cmd_setup.docker.networks')
@patch('src.enabler_keitaro_inc.commands.cmd_setup.logger')
@patch('src.enabler_keitaro_inc.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'
Expand Down
Loading

0 comments on commit d4bb626

Please sign in to comment.