Skip to content
Merged
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
15 changes: 15 additions & 0 deletions src/aks-preview/azext_aks_preview/managednamespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from azure.cli.core.util import (
sdk_no_wait,
)
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.mgmt.containerservice import ContainerServiceClient

from azext_aks_preview._consts import (
CONST_NAMESPACE_NETWORK_POLICY_RULE_DENYALL,
Expand All @@ -31,7 +33,14 @@ def aks_managed_namespace_add(cmd, client, raw_parameters, headers, no_wait):
resource_group_name = raw_parameters.get("resource_group_name")
cluster_name = raw_parameters.get("cluster_name")
namespace_name = raw_parameters.get("name")

# Get the cluster location
containerservice_client = get_mgmt_service_client(cmd.cli_ctx, ContainerServiceClient)
cluster = containerservice_client.managed_clusters.get(resource_group_name, cluster_name)
cluster_location = cluster.location

namespace_config = constructNamespace(cmd, raw_parameters, namespace_name)
namespace_config.location = cluster_location

return sdk_no_wait(
no_wait,
Expand Down Expand Up @@ -193,7 +202,13 @@ def aks_managed_namespace_update(cmd, client, raw_parameters, headers, existedNa
resource_group_name = raw_parameters.get("resource_group_name")
cluster_name = raw_parameters.get("cluster_name")
namespace_name = raw_parameters.get("name")

# Get the cluster location
containerservice_client = get_mgmt_service_client(cmd.cli_ctx, ContainerServiceClient)
cluster = containerservice_client.managed_clusters.get(resource_group_name, cluster_name)
cluster_location = cluster.location
namespace_config = updateNamespace(cmd, raw_parameters, existedNamespace)
namespace_config.location = cluster_location

return sdk_no_wait(
no_wait,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# --------------------------------------------------------------------------------------------

import unittest

from unittest.mock import patch, Mock
import azext_aks_preview.managednamespace as ns
from azext_aks_preview.__init__ import register_aks_preview_resource_type
from azure.cli.command_modules.acs.tests.latest.mocks import MockCLI, MockCmd
Expand All @@ -13,11 +13,24 @@
RequiredArgumentMissingError,
)

@patch("azext_aks_preview.managednamespace.get_mgmt_service_client")
class TestAddManagedNamespace(unittest.TestCase):
def test_add_managed_namespace_with_invalid_labels(self):
def test_add_managed_namespace_with_invalid_labels(self, mock_get_client):
register_aks_preview_resource_type()
cli_ctx = MockCLI()
cmd = MockCmd(cli_ctx)

# Set up mock cluster client and location
mock_cluster = Mock()
mock_cluster.location = "eastus"

mock_managed_clusters = Mock()
mock_managed_clusters.get.return_value = mock_cluster

mock_client = Mock()
mock_client.managed_clusters = mock_managed_clusters
mock_get_client.return_value = mock_client

raw_parameters = {
"resource_group_name": "test_rg",
"cluster_name": "test_cluster",
Expand All @@ -29,10 +42,22 @@ def test_add_managed_namespace_with_invalid_labels(self):
ns.aks_managed_namespace_add(cmd, None, raw_parameters, None, False)
self.assertEqual(str(cm.exception), err)

def test_add_managed_namespace_with_invalid_annotations(self):
def test_add_managed_namespace_with_invalid_annotations(self, mock_get_client):
register_aks_preview_resource_type()
cli_ctx = MockCLI()
cmd = MockCmd(cli_ctx)

# Set up mock cluster client and location
mock_cluster = Mock()
mock_cluster.location = "eastus"

mock_managed_clusters = Mock()
mock_managed_clusters.get.return_value = mock_cluster

mock_client = Mock()
mock_client.managed_clusters = mock_managed_clusters
mock_get_client.return_value = mock_client

raw_parameters = {
"resource_group_name": "test_rg",
"cluster_name": "test_cluster",
Expand All @@ -45,10 +70,22 @@ def test_add_managed_namespace_with_invalid_annotations(self):
ns.aks_managed_namespace_add(cmd, None, raw_parameters, None, False)
self.assertEqual(str(cm.exception), err)

def test_add_managed_namespace_with_missing_cpu_request(self):
def test_add_managed_namespace_with_missing_cpu_request(self, mock_get_client):
register_aks_preview_resource_type()
cli_ctx = MockCLI()
cmd = MockCmd(cli_ctx)

# Set up mock cluster client and location
mock_cluster = Mock()
mock_cluster.location = "eastus"

mock_managed_clusters = Mock()
mock_managed_clusters.get.return_value = mock_cluster

mock_client = Mock()
mock_client.managed_clusters = mock_managed_clusters
mock_get_client.return_value = mock_client

raw_parameters = {
"resource_group_name": "test_rg",
"cluster_name": "test_cluster",
Expand All @@ -60,10 +97,22 @@ def test_add_managed_namespace_with_missing_cpu_request(self):
ns.aks_managed_namespace_add(cmd, None, raw_parameters, None, False)
self.assertEqual(str(cm.exception), err)

def test_add_managed_namespace_with_invalid_ingress_policy(self):
def test_add_managed_namespace_with_invalid_ingress_policy(self, mock_get_client):
register_aks_preview_resource_type()
cli_ctx = MockCLI()
cmd = MockCmd(cli_ctx)

# Set up mock cluster client and location
mock_cluster = Mock()
mock_cluster.location = "eastus"

mock_managed_clusters = Mock()
mock_managed_clusters.get.return_value = mock_cluster

mock_client = Mock()
mock_client.managed_clusters = mock_managed_clusters
mock_get_client.return_value = mock_client

raw_parameters = {
"resource_group_name": "test_rg",
"cluster_name": "test_cluster",
Expand All @@ -79,10 +128,22 @@ def test_add_managed_namespace_with_invalid_ingress_policy(self):
ns.aks_managed_namespace_add(cmd, None, raw_parameters, None, False)
self.assertIn(err, str(cm.exception))

def test_add_managed_namespace_with_invalid_egress_policy(self):
def test_add_managed_namespace_with_invalid_egress_policy(self, mock_get_client):
register_aks_preview_resource_type()
cli_ctx = MockCLI()
cmd = MockCmd(cli_ctx)

# Set up mock cluster client and location
mock_cluster = Mock()
mock_cluster.location = "eastus"

mock_managed_clusters = Mock()
mock_managed_clusters.get.return_value = mock_cluster

mock_client = Mock()
mock_client.managed_clusters = mock_managed_clusters
mock_get_client.return_value = mock_client

raw_parameters = {
"resource_group_name": "test_rg",
"cluster_name": "test_cluster",
Expand All @@ -99,10 +160,22 @@ def test_add_managed_namespace_with_invalid_egress_policy(self):
ns.aks_managed_namespace_add(cmd, None, raw_parameters, None, False)
self.assertIn(err, str(cm.exception))

def test_add_managed_namespace_with_invalid_adoption_policy(self):
def test_add_managed_namespace_with_invalid_adoption_policy(self, mock_get_client):
register_aks_preview_resource_type()
cli_ctx = MockCLI()
cmd = MockCmd(cli_ctx)

# Set up mock cluster client and location
mock_cluster = Mock()
mock_cluster.location = "eastus"

mock_managed_clusters = Mock()
mock_managed_clusters.get.return_value = mock_cluster

mock_client = Mock()
mock_client.managed_clusters = mock_managed_clusters
mock_get_client.return_value = mock_client

raw_parameters = {
"resource_group_name": "test_rg",
"cluster_name": "test_cluster",
Expand All @@ -120,10 +193,22 @@ def test_add_managed_namespace_with_invalid_adoption_policy(self):
ns.aks_managed_namespace_add(cmd, None, raw_parameters, None, False)
self.assertIn(err, str(cm.exception))

def test_add_managed_namespace_with_invalid_delete_policy(self):
def test_add_managed_namespace_with_invalid_delete_policy(self, mock_get_client):
register_aks_preview_resource_type()
cli_ctx = MockCLI()
cmd = MockCmd(cli_ctx)

# Set up mock cluster client and location
mock_cluster = Mock()
mock_cluster.location = "eastus"

mock_managed_clusters = Mock()
mock_managed_clusters.get.return_value = mock_cluster

mock_client = Mock()
mock_client.managed_clusters = mock_managed_clusters
mock_get_client.return_value = mock_client

raw_parameters = {
"resource_group_name": "test_rg",
"cluster_name": "test_cluster",
Expand All @@ -145,11 +230,24 @@ def test_add_managed_namespace_with_invalid_delete_policy(self):
# aks_managed_namespace_add(cmd, client, raw_parameters, headers, no_wait):
# aks_managed_namespace_update(cmd, client, raw_parameters, headers, existedNamespace, no_wait)

@patch("azext_aks_preview.managednamespace.get_mgmt_service_client")
class TestUpdateManagedNamespace(unittest.TestCase):
def test_update_managed_namespace_with_invalid_ingress_policy(self):
def test_update_managed_namespace_with_invalid_ingress_policy(self, mock_get_client):
register_aks_preview_resource_type()
cli_ctx = MockCLI()
cmd = MockCmd(cli_ctx)

# Set up mock cluster client and location
mock_cluster = Mock()
mock_cluster.location = "eastus"

mock_managed_clusters = Mock()
mock_managed_clusters.get.return_value = mock_cluster

mock_client = Mock()
mock_client.managed_clusters = mock_managed_clusters
mock_get_client.return_value = mock_client

raw_parameters = {
"resource_group_name": "test_rg",
"cluster_name": "test_cluster",
Expand All @@ -165,10 +263,22 @@ def test_update_managed_namespace_with_invalid_ingress_policy(self):
ns.aks_managed_namespace_update(cmd, None, raw_parameters, None, None, False)
self.assertIn(err, str(cm.exception))

def test_update_managed_namespace_with_invalid_egress_policy(self):
def test_update_managed_namespace_with_invalid_egress_policy(self, mock_get_client):
register_aks_preview_resource_type()
cli_ctx = MockCLI()
cmd = MockCmd(cli_ctx)

# Set up mock cluster client and location
mock_cluster = Mock()
mock_cluster.location = "eastus"

mock_managed_clusters = Mock()
mock_managed_clusters.get.return_value = mock_cluster

mock_client = Mock()
mock_client.managed_clusters = mock_managed_clusters
mock_get_client.return_value = mock_client

raw_parameters = {
"resource_group_name": "test_rg",
"cluster_name": "test_cluster",
Expand All @@ -185,10 +295,22 @@ def test_update_managed_namespace_with_invalid_egress_policy(self):
ns.aks_managed_namespace_update(cmd, None, raw_parameters, None, None, False)
self.assertIn(err, str(cm.exception))

def test_update_managed_namespace_with_invalid_adoption_policy(self):
def test_update_managed_namespace_with_invalid_adoption_policy(self, mock_get_client):
register_aks_preview_resource_type()
cli_ctx = MockCLI()
cmd = MockCmd(cli_ctx)

# Set up mock cluster client and location
mock_cluster = Mock()
mock_cluster.location = "eastus"

mock_managed_clusters = Mock()
mock_managed_clusters.get.return_value = mock_cluster

mock_client = Mock()
mock_client.managed_clusters = mock_managed_clusters
mock_get_client.return_value = mock_client

raw_parameters = {
"resource_group_name": "test_rg",
"cluster_name": "test_cluster",
Expand All @@ -206,10 +328,22 @@ def test_update_managed_namespace_with_invalid_adoption_policy(self):
ns.aks_managed_namespace_update(cmd, None, raw_parameters, None, None, False)
self.assertIn(err, str(cm.exception))

def test_update_managed_namespace_with_invalid_delete_policy(self):
def test_update_managed_namespace_with_invalid_delete_policy(self, mock_get_client):
register_aks_preview_resource_type()
cli_ctx = MockCLI()
cmd = MockCmd(cli_ctx)

# Set up mock cluster client and location
mock_cluster = Mock()
mock_cluster.location = "eastus"

mock_managed_clusters = Mock()
mock_managed_clusters.get.return_value = mock_cluster

mock_client = Mock()
mock_client.managed_clusters = mock_managed_clusters
mock_get_client.return_value = mock_client

raw_parameters = {
"resource_group_name": "test_rg",
"cluster_name": "test_cluster",
Expand Down
Loading