def test_kube_monitor_beta_url_construction(self): api_beta1_base_url = "{0}/apis/extensions/v1beta1".format( self.base_url) check_beta1_url = lambda x: x.get_component_url() == \ "/".join([api_beta1_base_url, resource_name]) api_beta2_base_url = "{0}/apis/extensions/v1beta2".format( self.base_url) check_beta2_url = lambda x: x.get_component_url() == \ "/".join([api_beta2_base_url, resource_name]) # Validate that, if beta api is requested, we will construct beta # url, with default version of v1beta1. resource_name = "ingresses" ingress_monitor = kube_monitor.KubeMonitor(self.args, logger=mock.Mock(), beta=True, resource_name=resource_name) self.assertTrue(check_beta1_url(ingress_monitor)) # Validate that, if beta api of a specific version is requested, we will # construct beta url, with requested version. resource_name = "endpoints" ep_monitor = kube_monitor.KubeMonitor(self.args, logger=mock.Mock(), beta=True, api_version="v1beta2", resource_name=resource_name) self.assertTrue(check_beta2_url(ep_monitor))
def test_kube_monitor_beta_url_construction(self): api_beta1_base_url = "{0}/apis/extensions/v1beta1".format( self.base_url) check_beta1_url = lambda x: x.get_component_url() == \ "/".join([api_beta1_base_url, k8s_url_resource]) api_beta2_base_url = "{0}/apis/extensions/v1beta2".format( self.base_url) check_beta2_url = lambda x: x.get_component_url() == \ "/".join([api_beta2_base_url, k8s_url_resource]) # Validate that, if beta resource is requested, we will construct beta # url, with the right api_group and api_version for known resource_type resource_type = "ingress" ingress_monitor = kube_monitor.KubeMonitor(self.args, logger=mock.Mock(), resource_type=resource_type) k8s_url_resource =\ self.get_k8s_url_resource(ingress_monitor.k8s_api_resources, resource_type) self.assertTrue(check_beta1_url(ingress_monitor)) # Validate that, if beta api of a specific version is requested for unknown # resource_type, we will construt beta url with passed api_group and api_version resource_type = "testresources" test_monitor = kube_monitor.KubeMonitor(self.args, logger=mock.Mock(), api_group="apis/extensions", api_version="v1beta2", resource_type=resource_type) k8s_url_resource =\ self.get_k8s_url_resource(test_monitor.k8s_api_resources, resource_type) self.assertTrue(check_beta2_url(test_monitor))
def test_kube_monitor_api_v1_url_construction(self): api_v1_base_url = "{0}/api/v1".format(self.base_url) check_v1_url = lambda x: x.get_component_url() == \ "/".join([api_v1_base_url, k8s_url_resource]) api_v2_base_url = "{0}/api/v2".format(self.base_url) check_v2_url = lambda x: x.get_component_url() == \ "/".join([api_v2_base_url, k8s_url_resource]) get_custom_group_url =\ lambda group, version: "/".join([self.base_url, group, version]) check_custom_url = lambda x, custom_url: x.get_component_url() == \ "/".join([custom_url, k8s_url_resource]) # Validate that, if no explicit api_group and api_version is given, we will # use the default api_group and api_version for the known resource_type. resource_type = "namespace" namespace_monitor = kube_monitor.KubeMonitor( self.args, resource_type=resource_type, logger=mock.Mock()) k8s_url_resource =\ self.get_k8s_url_resource(namespace_monitor.k8s_api_resources, resource_type) self.assertTrue(check_v1_url(namespace_monitor)) # Validate that, even an explicit api_version is given, we will construct # appropriate api_version for the known resource_type. resource_type = "pod" pod_monitor = kube_monitor.KubeMonitor(self.args, resource_type=resource_type, logger=mock.Mock(), api_version="v2") k8s_url_resource =\ self.get_k8s_url_resource(pod_monitor.k8s_api_resources, resource_type) self.assertFalse(check_v2_url(pod_monitor)) # Validate that, even an explicit api_group is given, we will construct # url with appropriate api_group for the known resource_type custom_url = get_custom_group_url("apis/extensions", "v1beta1") resource_type = "networkpolicy" netpol_monitor = kube_monitor.KubeMonitor( self.args, resource_type=resource_type, logger=mock.Mock(), api_group="networking.k8s.io") k8s_url_resource =\ self.get_k8s_url_resource(netpol_monitor.k8s_api_resources, resource_type) self.assertTrue(check_custom_url(netpol_monitor, custom_url)) # Validate that, if an explicit api_group and api_version given, we will construct # url with appropriate api_group and api_version for the unknown resource_type. custom_url = get_custom_group_url("networking.k8s.io", "v3") resource_type = "testresources" test_monitor = kube_monitor.KubeMonitor(self.args, resource_type=resource_type, logger=mock.Mock(), api_group="networking.k8s.io", api_version="v3") k8s_url_resource =\ self.get_k8s_url_resource(test_monitor.k8s_api_resources, resource_type) self.assertTrue(check_custom_url(test_monitor, custom_url))
def test_kube_monitor_api_v1_url_construction(self): api_v1_base_url = "{0}/api/v1".format(self.base_url) check_v1_url = lambda x: x.get_component_url() == \ "/".join([api_v1_base_url, resource_name]) api_v2_base_url = "{0}/api/v2".format(self.base_url) check_v2_url = lambda x: x.get_component_url() == \ "/".join([api_v2_base_url, resource_name]) get_custom_group_url =\ lambda group, version: "/".join([self.base_url, group, version]) check_custom_url = lambda x, custom_url: x.get_component_url() == \ "/".join([custom_url, resource_name]) # Validate that, if no explicit version is given, we will default # to api/v1. resource_name = "namespace" namespace_monitor = kube_monitor.KubeMonitor( self.args, resource_name=resource_name, logger=mock.Mock()) self.assertTrue(check_v1_url(namespace_monitor)) # Validate that, if an explicit version is given, we will construct # appropriate api version. resource_name = "pods" pod_monitor = kube_monitor.KubeMonitor(self.args, resource_name=resource_name, logger=mock.Mock(), api_version="v2") self.assertTrue(check_v2_url(pod_monitor)) # Validate that, if an explicit group is given, we will construct # url with appropriate group but with version v1. custom_netpol_url = get_custom_group_url("networking.k8s.io", "v1") resource_name = "networkpolicies" netpol_monitor = kube_monitor.KubeMonitor( self.args, resource_name=resource_name, logger=mock.Mock(), api_group="networking.k8s.io") self.assertTrue(check_custom_url(netpol_monitor, custom_netpol_url)) # Validate that, if an explicit group and version given, we will construct # url with appropriate group and version. custom_netpol_url = get_custom_group_url("networking.k8s.io", "v3") resource_name = "networkpolicies" netpol_monitor = kube_monitor.KubeMonitor( self.args, resource_name=resource_name, logger=mock.Mock(), api_group="networking.k8s.io", api_version="v3") self.assertTrue(check_custom_url(netpol_monitor, custom_netpol_url))