Skip to content

Commit

Permalink
Fix df check and add unit test (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Sep 13, 2021
1 parent 03a376e commit e51eb49
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/k8s-extension/azext_k8s_extension/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
EXTENSION_PACKAGE_NAME = "azext_k8s_extension"
PROVIDER_NAMESPACE = 'Microsoft.KubernetesConfiguration'
REGISTERED = "Registered"
DF_RM_ENDPOINT = 'https://api-dogfood.resources.windows-int.net/'
DF_RM_HOSTNAME = 'api-dogfood.resources.windows-int.net'
7 changes: 4 additions & 3 deletions src/k8s-extension/azext_k8s_extension/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import json
from knack.log import get_logger
from urllib.parse import urlparse

from azure.cli.core.azclierror import ResourceNotFoundError, MutuallyExclusiveArgumentError, \
InvalidArgumentValueError, CommandNotFoundError, RequiredArgumentMissingError
Expand Down Expand Up @@ -138,7 +139,7 @@ def create_k8s_extension(cmd, client, resource_group_name, cluster_name, name, c

# Create identity, if required
# We don't create the identity if we are in DF
if create_identity and not __is_dogfood_cluster(cmd):
if create_identity and not is_dogfood_cluster(cmd):
extension_instance.identity, extension_instance.location = \
__create_identity(cmd, resource_group_name, cluster_name, cluster_type, cluster_rp)

Expand Down Expand Up @@ -291,5 +292,5 @@ def __read_config_settings_file(file_path):
raise Exception("File {} is not a valid JSON file".format(file_path)) from ex


def __is_dogfood_cluster(cmd):
return cmd.cli_ctx.cloud.endpoints.resource_manager == consts.DF_RM_ENDPOINT
def is_dogfood_cluster(cmd):
return urlparse(cmd.cli_ctx.cloud.endpoints.resource_manager).hostname == consts.DF_RM_HOSTNAME
24 changes: 24 additions & 0 deletions src/k8s-extension/azext_k8s_extension/tests/latest/MockClasses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------


from azure.cli.core import AzCommandsLoader


class MockCommand:
def __init__(self):
self.cli_ctx = MockCLIContext()

class MockCLIContext:
def __init__(self):
self.cloud = MockCloud()

class MockCloud:
def __init__(self):
self.endpoints = Endpoints()

class Endpoints:
def __init__(self):
self.resource_manager = ""
38 changes: 38 additions & 0 deletions src/k8s-extension/azext_k8s_extension/tests/latest/test_custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import os
from typing import no_type_check
import unittest

from .MockClasses import MockCLIContext, MockCommand
from azext_k8s_extension.custom import is_dogfood_cluster


class TestIsDogfoodCluster(unittest.TestCase):
def test_dogfood_cluster(self):
cmd = MockCommand()
cmd.cli_ctx.cloud.endpoints.resource_manager = "https://api-dogfood.resources.windows-int.net"
assert is_dogfood_cluster(cmd)

def test_dogfood_cluster_with_slash(self):
cmd = MockCommand()
cmd.cli_ctx.cloud.endpoints.resource_manager = "https://api-dogfood.resources.windows-int.net/"
assert is_dogfood_cluster(cmd)

def test_longer_hostname(self):
cmd = MockCommand()
cmd.cli_ctx.cloud.endpoints.resource_manager = "https://api-dogfood.resources.windows-int.otherwebsite.net/"
assert not is_dogfood_cluster(cmd)

def malformed_url(self):
cmd = MockCommand()
cmd.cli_ctx.cloud.endpoints.resource_manager = "htmalformed~2987493"
assert not is_dogfood_cluster(cmd)

def test_prod_cluster(self):
cmd = MockCommand()
cmd.cli_ctx.cloud.endpoints.resource_manager = "https://management.azure.com"
assert not is_dogfood_cluster(cmd)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from azure.cli.core.azclierror import InvalidArgumentValueError
from azext_k8s_extension.partner_extensions.OpenServiceMesh import _get_tested_distros

TEST_DIR = os.path.abspath(os.path.join(os.path.abspath(__file__), '..'))

class TestOpenServiceMesh(unittest.TestCase):
def test_bad_osm_arc_version(self):
Expand Down

0 comments on commit e51eb49

Please sign in to comment.