Skip to content

Commit

Permalink
Make required changes in README file and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
taleksovska committed Apr 9, 2024
1 parent db96a6c commit 1582364
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 54 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ The commands in this group can be accessed using the prefix enabler platform + n
```

- **keys**: Generate encryption keys used by the application services
- **release**: Release platform by tagging platform repo and tagging all individual components (git submodules) using their respective SHA that the submodules point at
- **version**: Check versions of microservices in git submodules you can provide a comma separated list of submodules or you can use 'all' for all submodules
- **release**: Release platform by tagging platform repo and tagging all individual components (git submodules) using their respective SHA that the submodules point at


```bash
enabler platform release <version> <submodule_name>
```


### preflight

Expand Down Expand Up @@ -115,7 +121,7 @@ enabler kind name_of_command --kube-context keitaro

These commands are used to setup the infrastructure to run kubernetes. With this group we can download the necessary packages and install them. To run commands from this group we use enabler setup + name_of_command.

- **init**: download binaries for all dependencies such as kubectl, helm, istioctl, kind and skaffold
- **init**: download binaries for all dependencies such as kubectl, helm, istioctl, kind and skaffold in /enabler/bin folder
- **metallb**: install and setup metallb on k8s
This command has a --kube-context option, which should be defined as the name of the kind cluster and can be executed with this command in Terminal:

Expand Down
42 changes: 20 additions & 22 deletions enabler/commands/cmd_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,48 +144,46 @@ def keys(ctx, kube_context_cli, bits):


@cli.command('release', short_help='Make a platform release')
@click.argument('version',
type=semver.BasedVersionParamType(),
required=True)
@click.argument('submodule',
required=True)
@click.argument('repopath',
required=True,
type=click.Path(exists=True),
default=os.getcwd())
@click.argument('version', type=semver.BasedVersionParamType(), required=True)
@click.argument('submodule_path', required=True, type=click.Path(exists=True))
@click.pass_context
@pass_environment
def release(ctx, kube_context_cli, version, submodule, repopath):
def release(ctx, kube_context_cli, version, submodule_path):
"""Release platform by tagging platform repo and
tagging all individual components (git submodules)
using their respective SHA that the submodules point at"""

# Get the repo from arguments defaults to cwd
repo = get_repo(repopath)
tagging the individual component (git submodule)
using its respective SHA that the submodule points at"""
submodule_name = os.path.basename(submodule_path)

# Get the repository
repo = get_repo(os.getcwd())
if not repo:
click.echo("Repository not found.")
return

# Check if submodule exists in the repository
if submodule not in repo.submodules:
click.echo(f"Submodule '{submodule}' not found in the repository.")
submodule = next((s for s in repo.submodules if s.name.endswith(submodule_name)), None) # noqa
if not submodule:
click.echo(f"Submodule '{submodule_name}' not found in the repository.") # noqa
return

# Tag platform at provided version
platform_tag_name = f"v{version}"
tag_result = tag_repo(repo, platform_tag_name)

if tag_result:
click.echo(f"Platform tagged with version {platform_tag_name}")
click.echo(f"Platform version: {platform_tag_name}")
else:
click.echo("Failed to tag platform")

submodule_path = os.path.join(repo.working_dir, submodule)
submodule_path = os.path.join(repo.working_dir, submodule_path)
submodule_repo = git.Repo(submodule_path)
submodule_sha = submodule_repo.head.commit.hexsha
submodule_tag_name = f"{submodule}-{platform_tag_name}"
submodule_tag_name = f"{submodule_name}-{platform_tag_name}"
tag_result = tag_repo(submodule_repo, submodule_tag_name, submodule_sha)
if tag_result:
click.echo(f"{submodule} tagged with version {platform_tag_name}")
click.echo(f"{submodule_name} version: {platform_tag_name}")
else:
click.echo(f"Failed to tag {submodule} at {submodule_sha}")
click.echo(f"Failed to tag {submodule_name} at {submodule_sha}")


def tag_repo(repo, tag_name, commit_sha=None):
Expand Down
4 changes: 2 additions & 2 deletions enabler/unit_tests/kind_unittests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def test_create_command(self, mock_s):

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

@patch('enabler.commands.cmd_kind.s')
def test_status_command(self, mock_s):
Expand Down
74 changes: 47 additions & 27 deletions enabler/unit_tests/platform_unittests.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,59 @@
import unittest
import tempfile
import shutil
from click.testing import CliRunner
from unittest.mock import MagicMock, patch
from enabler.commands.cmd_platform import cli as CLI
from unittest.mock import patch
from git import Repo
from enabler.cli import cli as CLI
import os


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

@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)
# Create a temporary Git repository
self.repo = Repo.init(self.temp_dir)
self.repo.git.config('--local', 'user.email', 'test@example.com')
self.repo.git.config('--local', 'user.name', 'Test')

def tearDown(self):
# Clean up the temporary directory after the test
shutil.rmtree(self.temp_dir)

@patch('enabler.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')
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
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)
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'])

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

@patch('enabler.commands.cmd_platform.s')
def test_platform_release(self, mock_s):
mock_s.run.return_value.returncode = 0
simulated_path = 'platform/microservice'
# Create the simulated path if it doesn't exist
if not os.path.exists(simulated_path):
os.makedirs(simulated_path)
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')
def test_platform_version(self, mock_s):
mock_s.run.return_value.returncode = 0
result = self.runner.invoke(CLI, ['platform', 'version'])
self.assertEqual(result.exit_code, 0)
self.assertIn('Keys generated successfully.', result.output)
2 changes: 1 addition & 1 deletion enabler/unit_tests/setup_unittests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def test_init_command(self, mock_chmod, mock_stat, mock_request):
mock_chmod.return_value = None

result = self.runner.invoke(CLI, ['init'])
print(result.output)
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')
Expand Down

0 comments on commit 1582364

Please sign in to comment.