Skip to content

Commit

Permalink
Container service test code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhuvaneswari Santharam committed Jun 2, 2021
1 parent 55d8260 commit c551f3b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from azure.mgmt.containerservice.models import (ContainerServiceOrchestratorTypes,
ContainerService,
ContainerServiceOrchestratorProfile)
from azure.mgmt.containerservice.v2020_03_01.models import ManagedClusterAddonProfile
from azure.cli.core.util import CLIError
from azure.cli.command_modules.acs._consts import (CONST_HTTP_APPLICATION_ROUTING_ADDON_NAME,
CONST_MONITORING_ADDON_NAME,
Expand Down Expand Up @@ -660,9 +661,6 @@ def test_update_addons(self, rg_def, cf_resource_groups, cf_resources):
# kube-dashboard disabled, there's existing dashboard addon profile
instance.addon_profiles.pop(CONST_KUBE_DASHBOARD_ADDON_NAME, None)
# test lower cased key name
ManagedClusterAddonProfile = cmd.get_models('ManagedClusterAddonProfile',
resource_type=ResourceType.MGMT_CONTAINERSERVICE,
operation_group='managed_clusters')
instance.addon_profiles['kubedashboard'] = ManagedClusterAddonProfile(enabled=True)
instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000',
'clitest000001', 'clitest000001', 'kube-dashboard', enable=False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,52 @@
# --------------------------------------------------------------------------------------------

import unittest
import mock
from azure.cli.command_modules.acs import _helpers as helpers

from azure.cli.core._config import GLOBAL_CONFIG_DIR, ENV_VAR_PREFIX
from azure.cli.core.cloud import get_active_cloud
from azure.cli.core.profiles import get_sdk, ResourceType, supported_api_version

class MockCLI(CLI):
def __init__(self):
super(MockCLI, self).__init__(cli_name='mock_cli', config_dir=GLOBAL_CONFIG_DIR,
config_env_var_prefix=ENV_VAR_PREFIX, commands_loader_cls=MockLoader)
self.cloud = get_active_cloud(self)

class MockLoader(object):
def __init__(self, ctx):
self.ctx = ctx

def get_models(self, *attr_args, **_):
from azure.cli.core.profiles import get_sdk
return get_sdk(self.ctx, ResourceType.MGMT_CONTAINERSERVICE, *attr_args, mod='models')

class MockCmd(object):
def __init__(self, ctx, arguments={}):
self.cli_ctx = ctx
self.loader = MockLoader(self.cli_ctx)
self.arguments = arguments

def get_models(self, *attr_args, **kwargs):
return get_sdk(self.cli_ctx, ResourceType.MGMT_CONTAINERSERVICE, *attr_args, **kwargs)

class TestPopulateApiServerAccessProfile(unittest.TestCase):
def setUp(self):
self.cli = MockCLI()

def test_single_cidr_with_spaces(self):
api_server_authorized_ip_ranges = "0.0.0.0/32 "
profile = helpers._populate_api_server_access_profile(api_server_authorized_ip_ranges, enable_private_cluster=False)
profile = helpers._populate_api_server_access_profile(MockCmd(self.cli), api_server_authorized_ip_ranges, enable_private_cluster=False)
self.assertListEqual(profile.authorized_ip_ranges, ["0.0.0.0/32"])

def test_multi_cidr_with_spaces(self):
def test_multi_cidr_with_spaces(cmd, self):
api_server_authorized_ip_ranges = " 0.0.0.0/32 , 129.1.1.1/32"
profile = helpers._populate_api_server_access_profile(api_server_authorized_ip_ranges, enable_private_cluster=False)
profile = helpers._populate_api_server_access_profile(MockCmd(self.cli), api_server_authorized_ip_ranges, enable_private_cluster=False)
self.assertListEqual(profile.authorized_ip_ranges, ["0.0.0.0/32", "129.1.1.1/32"])

def test_private_cluster(self):
profile = helpers._populate_api_server_access_profile(None, enable_private_cluster=True)
def test_private_cluster(cmd, self):
profile = helpers._populate_api_server_access_profile(MockCmd(self.cli), None, enable_private_cluster=True)
self.assertListEqual(profile.authorized_ip_ranges, [])
self.assertEqual(profile.enable_private_cluster, True)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@
# --------------------------------------------------------------------------------------------
import unittest

from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterLoadBalancerProfile
from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterLoadBalancerProfileManagedOutboundIPs
from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterLoadBalancerProfileOutboundIPPrefixes
from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterLoadBalancerProfileOutboundIPs
from azure.cli.core.util import CLIError
from azure.cli.command_modules.acs import _loadbalancer as loadbalancer


class TestLoadBalancer(unittest.TestCase):
def test_configure_load_balancer_profile(cmd, self):
def test_configure_load_balancer_profile(self):
managed_outbound_ip_count = 5
outbound_ips = None
outbound_ip_prefixes = None
outbound_ports = 80
idle_timeout = 3600
ManagedClusterLoadBalancerProfile = cmd.get_models('ManagedClusterLoadBalancerProfile',
resource_type=ResourceType.MGMT_CONTAINERSERVICE)
ManagedClusterLoadBalancerProfileManagedOutboundIPs = cmd.get_models(
'ManagedClusterLoadBalancerProfileManagedOutboundIPs', resource_type=ResourceType.MGMT_CONTAINERSERVICE)
ManagedClusterLoadBalancerProfileOutboundIPs = cmd.get_models(
'ManagedClusterLoadBalancerProfileOutboundIPs', resource_type=ResourceType.MGMT_CONTAINERSERVICE)
ManagedClusterLoadBalancerProfileOutboundIPPrefixes = cmd.get_models(
'ManagedClusterLoadBalancerProfileOutboundIPPrefixes', resource_type=ResourceType.MGMT_CONTAINERSERVICE)

profile = ManagedClusterLoadBalancerProfile()
profile.managed_outbound_ips = ManagedClusterLoadBalancerProfileManagedOutboundIPs(
count=2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,39 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
import unittest
import mock
from azure.cli.core.util import CLIError
from azure.cli.command_modules.acs import _validators as validators

from azure.cli.core._config import GLOBAL_CONFIG_DIR, ENV_VAR_PREFIX
from azure.cli.core.cloud import get_active_cloud
from azure.cli.core.profiles import get_sdk, ResourceType, supported_api_version

class MockCLI(CLI):
def __init__(self):
super(MockCLI, self).__init__(cli_name='mock_cli', config_dir=GLOBAL_CONFIG_DIR,
config_env_var_prefix=ENV_VAR_PREFIX, commands_loader_cls=MockLoader)
self.cloud = get_active_cloud(self)

class MockLoader(object):
def __init__(self, ctx):
self.ctx = ctx

def get_models(self, *attr_args, **_):
from azure.cli.core.profiles import get_sdk
return get_sdk(self.ctx, ResourceType.MGMT_CONTAINERSERVICE, *attr_args, mod='models')

class MockCmd(object):
def __init__(self, ctx, arguments={}):
self.cli_ctx = ctx
self.loader = MockLoader(self.cli_ctx)
self.arguments = arguments

def get_models(self, *attr_args, **kwargs):
return get_sdk(self.cli_ctx, ResourceType.MGMT_CONTAINERSERVICE, *attr_args, **kwargs)

class TestValidateIPRanges(unittest.TestCase):

def test_simultaneous_allow_and_disallow_with_spaces(self):
api_server_authorized_ip_ranges = " 0.0.0.0/32 , 129.1.1.1.1 "
namespace = Namespace(api_server_authorized_ip_ranges)
Expand Down Expand Up @@ -62,53 +90,56 @@ def test_IPv6(self):


class TestClusterAutoscalerParamsValidators(unittest.TestCase):
def test_empty_key_empty_value(cmd, self):
def setUp(self):
self.cli = MockCLI()

def test_empty_key_empty_value(self):
cluster_autoscaler_profile = ["="]
namespace = Namespace(cluster_autoscaler_profile=cluster_autoscaler_profile)
err = "Empty key specified for cluster-autoscaler-profile"

with self.assertRaises(CLIError) as cm:
validators.validate_cluster_autoscaler_profile(cmd, namespace)
validators.validate_cluster_autoscaler_profile(MockCmd(self.cli), namespace)
self.assertEqual(str(cm.exception), err)

def test_non_empty_key_empty_value(cmd, self):
def test_non_empty_key_empty_value(self):
cluster_autoscaler_profile = ["scan-interval="]
namespace = Namespace(cluster_autoscaler_profile=cluster_autoscaler_profile)

validators.validate_cluster_autoscaler_profile(cmd, namespace)
validators.validate_cluster_autoscaler_profile(MockCmd(self.cli), namespace)

def test_two_empty_keys_empty_value(cmd, self):
def test_two_empty_keys_empty_value(self):
cluster_autoscaler_profile = ["=", "="]
namespace = Namespace(cluster_autoscaler_profile=cluster_autoscaler_profile)
err = "Empty key specified for cluster-autoscaler-profile"

with self.assertRaises(CLIError) as cm:
validators.validate_cluster_autoscaler_profile(cmd, namespace)
validators.validate_cluster_autoscaler_profile(MockCmd(self.cli), namespace)
self.assertEqual(str(cm.exception), err)

def test_one_empty_key_in_pair_one_non_empty(cmd, self):
def test_one_empty_key_in_pair_one_non_empty(self):
cluster_autoscaler_profile = ["scan-interval=20s", "="]
namespace = Namespace(cluster_autoscaler_profile=cluster_autoscaler_profile)
err = "Empty key specified for cluster-autoscaler-profile"

with self.assertRaises(CLIError) as cm:
validators.validate_cluster_autoscaler_profile(cmd, namespace)
validators.validate_cluster_autoscaler_profile(MockCmd(self.cli), namespace)
self.assertEqual(str(cm.exception), err)

def test_invalid_key(cmd, self):
def test_invalid_key(self):
cluster_autoscaler_profile = ["bad-key=val"]
namespace = Namespace(cluster_autoscaler_profile=cluster_autoscaler_profile)
err = "'bad-key' is an invalid key for cluster-autoscaler-profile"

with self.assertRaises(CLIError) as cm:
validators.validate_cluster_autoscaler_profile(cmd, namespace)
validators.validate_cluster_autoscaler_profile(MockCmd(self.cli), namespace)
self.assertIn(err, str(cm.exception),)

def test_valid_parameters(cmd, self):
def test_valid_parameters(self):
cluster_autoscaler_profile = ["scan-interval=20s", "scale-down-delay-after-add=15m"]
namespace = Namespace(cluster_autoscaler_profile=cluster_autoscaler_profile)

validators.validate_cluster_autoscaler_profile(cmd, namespace)
validators.validate_cluster_autoscaler_profile(MockCmd(self.cli), namespace)


class Namespace:
Expand Down

0 comments on commit c551f3b

Please sign in to comment.