Skip to content
Merged
Changes from all commits
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
248 changes: 247 additions & 1 deletion tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from oks_cli.main import cli
from unittest.mock import patch, MagicMock

import json
import yaml

# Test the "cluster list" command: verifies listing clusters in a project
@patch("oks_cli.utils.requests.request")
def test_cluster_list_command(mock_request, add_default_profile):
mock_request.side_effect = [
Expand All @@ -15,7 +18,32 @@ def test_cluster_list_command(mock_request, add_default_profile):
assert result.exit_code == 0
assert '"name": "test"' in result.output

# Test the "cluster list" command with all arguments: verifies that advanced filters
@patch("oks_cli.utils.requests.request")
def test_cluster_list_all_args(mock_request, add_default_profile):
mock_request.side_effect = [
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Projects": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Clusters": [{"id": "12345", "name": "test-cluster"}]}),
]

runner = CliRunner()
result = runner.invoke(cli, [
"cluster", "list",
"-p", "test-project",
"--name", "test-cluster",
"-c", "test-cluster",
"--deleted",
"--plain",
"--msword",
"-w",
"-o", "json",
"--profile", "default"
])

assert result.exit_code == 0
assert "test-cluster" in result.output

# Test the "cluster get" command: verifies fetching details of a specific cluster
@patch("oks_cli.utils.requests.request")
def test_cluster_get_command(mock_request, add_default_profile):
mock_request.side_effect = [
Expand All @@ -29,7 +57,58 @@ def test_cluster_get_command(mock_request, add_default_profile):
assert result.exit_code == 0
assert '"name": "test"' in result.output

# Test the "cluster get" command with JSON output: verifies retrieving
@patch("oks_cli.utils.requests.request")
def test_cluster_get_json(mock_request, add_default_profile):
mock_request.side_effect = [
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Projects": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Clusters": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Cluster": {"id": "12345", "name": "test"}}),
]

runner = CliRunner()
result = runner.invoke(cli, [
"cluster", "get",
"-p", "test-project",
"-c", "test",
"-o", "json",
"--profile", "default"
])

assert result.exit_code == 0

data = json.loads(result.output)
assert "id" in data
assert "name" in data
assert data["name"] == "test"

# Test the "cluster get" command with YAML output: verifies retrieving
@patch("oks_cli.utils.requests.request")
def test_cluster_get_yaml(mock_request, add_default_profile):
mock_request.side_effect = [
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Projects": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Clusters": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Cluster": {"id": "12345", "name": "test"}}),
]

runner = CliRunner()
result = runner.invoke(cli, [
"cluster", "get",
"-p", "test-project",
"-c", "test",
"-o", "yaml",
"--profile", "default"
])

assert result.exit_code == 0

data = yaml.safe_load(result.output)
assert "id" in data
assert "name" in data
assert data["name"] == "test"


# Test the "cluster create" command: verifies creating a new cluster in a project
@patch("oks_cli.utils.requests.request")
def test_cluster_create_command(mock_request, add_default_profile):
mock_request.side_effect = [
Expand All @@ -44,7 +123,44 @@ def test_cluster_create_command(mock_request, add_default_profile):
assert result.exit_code == 0
assert '"name": "test"' in result.output

# Test the "cluster create" command with all arguments: verifies creating a cluster
@patch("oks_cli.utils.requests.request")
@patch("oks_cli.cluster.get_template")
def test_cluster_create_all_args(mock_get_template, mock_request, add_default_profile):
mock_request.side_effect = [
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Projects": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Template": {}}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Project": {"id": "12345"}}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Cluster": {"id": "12345", "name": "test"}}),
]

mock_get_template.return_value = {}

runner = CliRunner()
result = runner.invoke(cli, [
"cluster", "create",
"-p", "test-project",
"-c", "test",
"--description", "Test cluster",
"--admin", "10.0.0.1/32",
"--control-plane", "cp.mono.master",
"--quirk", "special-feature",
"--tags", "env=dev",
"--dry-run",
"-o", "json",
"--profile", "default",
])

output = json.loads(result.output)
assert output["name"] == "test"
assert output["description"] == "Test cluster"
assert output["admin_whitelist"] == ["10.0.0.1/32"]
assert output["control_planes"] == "cp.mono.master"
assert output["tags"] == {"env": "dev"}
assert output["quirks"] == ["special-feature"]
assert output["cp_multi_az"] is False

# Test the "cluster update" command: verifies updating a cluster description (dry-run)
@patch("oks_cli.utils.requests.request")
def test_cluster_update_command(mock_request, add_default_profile):
mock_request.side_effect = [
Expand All @@ -57,7 +173,43 @@ def test_cluster_update_command(mock_request, add_default_profile):
assert result.exit_code == 0
assert '"description": "test"' in result.output

# Test the "cluster update" command with all arguments: verifies updating a cluster
@patch("oks_cli.utils.requests.request")
def test_cluster_update_all_args(mock_request, add_default_profile):
mock_request.side_effect = [
MagicMock(
status_code=200, headers={},
json=lambda: {"ResponseContext": {}, "Projects": [{"id": "12345"}]}
),
MagicMock(
status_code=200, headers={},
json=lambda: {"ResponseContext": {}, "Clusters": [{"id": "12345", "name": "test"}]}
),
]

runner = CliRunner()
result = runner.invoke(cli, [
"cluster", "update",
"-p", "test-project",
"-c", "test",
"--description", "Updated cluster",
"--admin", "10.0.0.1/32",
"--control-plane", "cp.mono.master",
"--quirk", "special-feature",
"--tags", "env=dev",
"--dry-run",
"-o", "json",
"--profile", "default",
])

output = json.loads(result.output)
assert output["description"] == "Updated cluster"
assert output["admin_whitelist"] == ["10.0.0.1/32"]
assert output["control_planes"] == "cp.mono.master"
assert output["tags"] == {"env": "dev"}
assert output["quirks"] == ["special-feature"]

# Test the "cluster upgrade" command: verifies upgrading a cluster
@patch("oks_cli.utils.requests.request")
def test_cluster_upgrade_command(mock_request, add_default_profile):
mock_request.side_effect = [
Expand All @@ -71,7 +223,53 @@ def test_cluster_upgrade_command(mock_request, add_default_profile):
assert result.exit_code == 0
assert '"name": "test"' in result.output

# Test the "cluster upgrade" command with JSON output
@patch("oks_cli.utils.requests.request")
def test_cluster_upgrade_command_json(mock_request, add_default_profile):
mock_request.side_effect = [
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Projects": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Clusters": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Cluster": {"name": "test"}}),
]

runner = CliRunner()
result = runner.invoke(cli, [
"cluster", "upgrade",
"-p", "test",
"-c", "test",
"--force",
"-o", "json"
])

assert result.exit_code == 0

output = json.loads(result.output)
assert output["name"] == "test"

# Test the "cluster upgrade" command with YAML output
@patch("oks_cli.utils.requests.request")
def test_cluster_upgrade_command_yaml(mock_request, add_default_profile):
mock_request.side_effect = [
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Projects": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Clusters": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Cluster": {"name": "test"}}),
]

runner = CliRunner()
result = runner.invoke(cli, [
"cluster", "upgrade",
"-p", "test",
"-c", "test",
"--force",
"-o", "yaml"
])

assert result.exit_code == 0

output = yaml.safe_load(result.output)
assert output["name"] == "test"

# Test the "cluster delete" command: verifies dry-run deletion message
@patch("oks_cli.utils.requests.request")
def test_cluster_delete_command(mock_request, add_default_profile):
mock_request.side_effect = [
Expand All @@ -84,7 +282,7 @@ def test_cluster_delete_command(mock_request, add_default_profile):
assert result.exit_code == 0
assert 'Dry run: The cluster would be deleted.' in result.output


# Test the "cluster kubeconfig" command: verifies retrieving the kubeconfig of a cluster
@patch("oks_cli.utils.requests.request")
def test_cluster_kubeconfig_command(mock_request, add_default_profile):
mock_request.side_effect = [
Expand All @@ -98,7 +296,54 @@ def test_cluster_kubeconfig_command(mock_request, add_default_profile):
assert result.exit_code == 0
assert 'kubeconfig' in result.output

# Test the "cluster delete" command with JSON output
@patch("oks_cli.utils.requests.request")
def test_cluster_delete_command_json(mock_request, add_default_profile):
mock_request.side_effect = [
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Projects": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Clusters": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Cluster": {"id": "12345", "name": "test"}}),
]

runner = CliRunner()
result = runner.invoke(cli, [
"cluster", "delete",
"-p", "test",
"-c", "test",
"--force",
"-o", "json"
])

assert result.exit_code == 0

output = json.loads(result.output)
assert output["name"] == "test"

# Test the "cluster delete" command with YAML output
@patch("oks_cli.utils.requests.request")
def test_cluster_delete_command_yaml(mock_request, add_default_profile):
mock_request.side_effect = [
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Projects": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Clusters": [{"id": "12345"}]}),
MagicMock(status_code=200, headers={}, json=lambda: {"ResponseContext": {}, "Cluster": {"id": "12345", "name": "test"}}),
]

runner = CliRunner()
result = runner.invoke(cli, [
"cluster", "delete",
"-p", "test",
"-c", "test",
"--force",
"-o", "yaml"
])

assert result.exit_code == 0

output = yaml.safe_load(result.output)
assert output["name"] == "test"


# Test the "cluster kubectl" command: verifies running kubectl with the cluster's kubeconfig
@patch("oks_cli.utils.subprocess.run")
@patch("oks_cli.utils.requests.request")
def test_cluster_kubectl_command(mock_request, mock_run, add_default_profile):
Expand All @@ -123,6 +368,7 @@ def test_cluster_kubectl_command(mock_request, mock_run, add_default_profile):
assert args[0] == ["kubectl", "get", "pods"]


# Test the "cluster create by one-click" command: verifies creating cluster interactively
@patch("oks_cli.utils.os.fork")
@patch("oks_cli.utils.time.sleep")
@patch("oks_cli.utils.requests.request")
Expand Down