def test_service_and_deploy_not_cached( self, mock_list_memcached_for_all_namespaces, mock_read_namespaced_service, mock_create_service, mock_cache_version, mock_is_version_cached, mock_update_service, mock_read_namespaced_deployment, mock_create_deployment, mock_update_deployment): # Mock list memcached call with 0 items mock_list_memcached_for_all_namespaces.return_value = self.base_list_result check_existing() mock_list_memcached_for_all_namespaces.assert_called_once_with() mock_read_namespaced_service.assert_called_once_with( self.name, self.namespace) assert mock_create_service.called is False cache_version_calls = [ call(client.V1Service()), call(client.V1beta1Deployment()) ] mock_cache_version.assert_has_calls(cache_version_calls) is_version_cached_calls = [ call(client.V1Service()), call(client.V1beta1Deployment()) ] mock_is_version_cached.assert_has_calls(is_version_cached_calls) mock_update_service.assert_called_once_with(self.cluster_object) mock_read_namespaced_deployment.assert_called_once_with( self.name, self.namespace) assert mock_create_deployment.called is False mock_update_deployment.assert_called_once_with(self.cluster_object)
def setUp(self): self.name = 'testname123' self.namespace = 'testnamespace456' svc_list = client.V1ServiceList() svc = client.V1Service() svc.metadata = client.V1ObjectMeta(name=self.name, namespace=self.namespace) svc_list.items = [svc] self.correct_svc_list = svc_list deploy_list = client.V1beta1DeploymentList() deploy = client.V1beta1Deployment() deploy.metadata = client.V1ObjectMeta(name=self.name, namespace=self.namespace) deploy_list.items = [deploy] self.correct_deploy_list = deploy_list
class TestCheckExisting(): def setUp(self): self.name = 'testname123' self.namespace = 'testnamespace456' self.cluster_object = { 'metadata': { 'name': self.name, 'namespace': self.namespace } } self.base_list_result = {'items': [self.cluster_object]} @patch('memcached_operator.memcached_operator.periodical.logging') @patch( 'memcached_operator.memcached_operator.memcached_tpr_v1alpha1_api.MemcachedThirdPartyResourceV1Alpha1Api.list_memcached_for_all_namespaces', side_effect=client.rest.ApiException()) def test_list_memcached_exception(self, mock_list_memcached_for_all_namespaces, mock_logging): result = check_existing() assert mock_logging.exception.called is True assert result is False @patch('memcached_operator.memcached_operator.periodical.update_deployment' ) @patch('memcached_operator.memcached_operator.periodical.create_deployment' ) @patch('kubernetes.client.ExtensionsV1beta1Api.read_namespaced_deployment') @patch('memcached_operator.memcached_operator.periodical.update_service') @patch('memcached_operator.memcached_operator.periodical.is_version_cached' ) @patch('memcached_operator.memcached_operator.periodical.cache_version') @patch('memcached_operator.memcached_operator.periodical.create_service') @patch('kubernetes.client.CoreV1Api.read_namespaced_service') @patch( 'memcached_operator.memcached_operator.memcached_tpr_v1alpha1_api.MemcachedThirdPartyResourceV1Alpha1Api.list_memcached_for_all_namespaces' ) def test_no_memcached_tprs(self, mock_list_memcached_for_all_namespaces, mock_read_namespaced_service, mock_create_service, mock_cache_version, mock_is_version_cached, mock_update_service, mock_read_namespaced_deployment, mock_create_deployment, mock_update_deployment): # Mock list memcached call with 0 items no_item_result = deepcopy(self.base_list_result) no_item_result['items'] = [] mock_list_memcached_for_all_namespaces.return_value = no_item_result check_existing() mock_list_memcached_for_all_namespaces.assert_called_once_with() assert mock_read_namespaced_service.called is False assert mock_create_service.called is False assert mock_cache_version.called is False assert mock_is_version_cached.called is False assert mock_update_service.called is False assert mock_read_namespaced_deployment.called is False assert mock_create_deployment.called is False assert mock_update_deployment.called is False @patch('memcached_operator.memcached_operator.periodical.update_deployment' ) @patch( 'memcached_operator.memcached_operator.periodical.create_deployment', return_value=client.V1beta1Deployment()) @patch('kubernetes.client.ExtensionsV1beta1Api.read_namespaced_deployment', side_effect=client.rest.ApiException(status=404)) @patch('memcached_operator.memcached_operator.periodical.update_service') @patch( 'memcached_operator.memcached_operator.periodical.is_version_cached', return_value=True) @patch('memcached_operator.memcached_operator.periodical.cache_version') @patch('memcached_operator.memcached_operator.periodical.create_service', return_value=client.V1Service()) @patch('kubernetes.client.CoreV1Api.read_namespaced_service', side_effect=client.rest.ApiException(status=404)) @patch( 'memcached_operator.memcached_operator.memcached_tpr_v1alpha1_api.MemcachedThirdPartyResourceV1Alpha1Api.list_memcached_for_all_namespaces' ) def test_service_and_deploy_404( self, mock_list_memcached_for_all_namespaces, mock_read_namespaced_service, mock_create_service, mock_cache_version, mock_is_version_cached, mock_update_service, mock_read_namespaced_deployment, mock_create_deployment, mock_update_deployment): # Mock list memcached call with 0 items mock_list_memcached_for_all_namespaces.return_value = self.base_list_result check_existing() mock_list_memcached_for_all_namespaces.assert_called_once_with() mock_read_namespaced_service.assert_called_once_with( self.name, self.namespace) mock_create_service.assert_called_once_with(self.cluster_object) cache_version_calls = [ call(client.V1Service()), call(client.V1beta1Deployment()) ] mock_cache_version.assert_has_calls(cache_version_calls) assert mock_is_version_cached.called is False assert mock_update_service.called is False mock_read_namespaced_deployment.assert_called_once_with( self.name, self.namespace) mock_create_deployment.assert_called_once_with(self.cluster_object) assert mock_update_deployment.called is False @patch('memcached_operator.memcached_operator.periodical.update_deployment' ) @patch( 'memcached_operator.memcached_operator.periodical.create_deployment', return_value=False) @patch('kubernetes.client.ExtensionsV1beta1Api.read_namespaced_deployment', side_effect=client.rest.ApiException(status=404)) @patch('memcached_operator.memcached_operator.periodical.update_service') @patch( 'memcached_operator.memcached_operator.periodical.is_version_cached', return_value=True) @patch('memcached_operator.memcached_operator.periodical.cache_version') @patch('memcached_operator.memcached_operator.periodical.create_service', return_value=False) @patch('kubernetes.client.CoreV1Api.read_namespaced_service', side_effect=client.rest.ApiException(status=404)) @patch( 'memcached_operator.memcached_operator.memcached_tpr_v1alpha1_api.MemcachedThirdPartyResourceV1Alpha1Api.list_memcached_for_all_namespaces' ) def test_service_and_deploy_404_yet_create_false( self, mock_list_memcached_for_all_namespaces, mock_read_namespaced_service, mock_create_service, mock_cache_version, mock_is_version_cached, mock_update_service, mock_read_namespaced_deployment, mock_create_deployment, mock_update_deployment): # Mock list memcached call with 0 items mock_list_memcached_for_all_namespaces.return_value = self.base_list_result check_existing() mock_list_memcached_for_all_namespaces.assert_called_once_with() mock_read_namespaced_service.assert_called_once_with( self.name, self.namespace) mock_create_service.assert_called_once_with(self.cluster_object) assert mock_cache_version.called is False assert mock_is_version_cached.called is False assert mock_update_service.called is False mock_read_namespaced_deployment.assert_called_once_with( self.name, self.namespace) mock_create_deployment.assert_called_once_with(self.cluster_object) assert mock_update_deployment.called is False @patch('memcached_operator.memcached_operator.periodical.logging') @patch('memcached_operator.memcached_operator.periodical.update_deployment' ) @patch('memcached_operator.memcached_operator.periodical.create_deployment' ) @patch('kubernetes.client.ExtensionsV1beta1Api.read_namespaced_deployment', side_effect=client.rest.ApiException(status=500)) @patch('memcached_operator.memcached_operator.periodical.update_service') @patch('memcached_operator.memcached_operator.periodical.is_version_cached' ) @patch('memcached_operator.memcached_operator.periodical.cache_version') @patch('memcached_operator.memcached_operator.periodical.create_service') @patch('kubernetes.client.CoreV1Api.read_namespaced_service', side_effect=client.rest.ApiException(status=500)) @patch( 'memcached_operator.memcached_operator.memcached_tpr_v1alpha1_api.MemcachedThirdPartyResourceV1Alpha1Api.list_memcached_for_all_namespaces' ) def test_service_and_deploy_500( self, mock_list_memcached_for_all_namespaces, mock_read_namespaced_service, mock_create_service, mock_cache_version, mock_is_version_cached, mock_update_service, mock_read_namespaced_deployment, mock_create_deployment, mock_update_deployment, mock_logging): # Mock list memcached call mock_list_memcached_for_all_namespaces.return_value = self.base_list_result check_existing() mock_list_memcached_for_all_namespaces.assert_called_once_with() mock_read_namespaced_service.assert_called_once_with( self.name, self.namespace) assert mock_create_service.called is False assert mock_cache_version.called is False assert mock_is_version_cached.called is False assert mock_update_service.called is False mock_read_namespaced_deployment.assert_called_once_with( self.name, self.namespace) assert mock_create_deployment.called is False assert mock_update_deployment.called is False mock_logging.exception.call_count == 2 @patch('memcached_operator.memcached_operator.periodical.update_deployment' ) @patch('memcached_operator.memcached_operator.periodical.create_deployment' ) @patch('kubernetes.client.ExtensionsV1beta1Api.read_namespaced_deployment', return_value=client.V1beta1Deployment()) @patch('memcached_operator.memcached_operator.periodical.update_service') @patch( 'memcached_operator.memcached_operator.periodical.is_version_cached', return_value=True) @patch('memcached_operator.memcached_operator.periodical.cache_version') @patch('memcached_operator.memcached_operator.periodical.create_service') @patch('kubernetes.client.CoreV1Api.read_namespaced_service', return_value=client.V1Service()) @patch( 'memcached_operator.memcached_operator.memcached_tpr_v1alpha1_api.MemcachedThirdPartyResourceV1Alpha1Api.list_memcached_for_all_namespaces' ) def test_service_and_deploy_cached( self, mock_list_memcached_for_all_namespaces, mock_read_namespaced_service, mock_create_service, mock_cache_version, mock_is_version_cached, mock_update_service, mock_read_namespaced_deployment, mock_create_deployment, mock_update_deployment): # Mock list memcached call with 0 items mock_list_memcached_for_all_namespaces.return_value = self.base_list_result check_existing() mock_list_memcached_for_all_namespaces.assert_called_once_with() mock_read_namespaced_service.assert_called_once_with( self.name, self.namespace) assert mock_create_service.called is False assert mock_cache_version.called is False is_version_cached_calls = [ call(client.V1Service()), call(client.V1beta1Deployment()) ] mock_is_version_cached.assert_has_calls(is_version_cached_calls) assert mock_update_service.called is False mock_read_namespaced_deployment.assert_called_once_with( self.name, self.namespace) assert mock_create_deployment.called is False assert mock_update_deployment.called is False @patch( 'memcached_operator.memcached_operator.periodical.update_deployment', return_value=client.V1beta1Deployment()) @patch('memcached_operator.memcached_operator.periodical.create_deployment' ) @patch('kubernetes.client.ExtensionsV1beta1Api.read_namespaced_deployment', return_value=client.V1beta1Deployment()) @patch('memcached_operator.memcached_operator.periodical.update_service', return_value=client.V1Service()) @patch( 'memcached_operator.memcached_operator.periodical.is_version_cached', return_value=False) @patch('memcached_operator.memcached_operator.periodical.cache_version') @patch('memcached_operator.memcached_operator.periodical.create_service') @patch('kubernetes.client.CoreV1Api.read_namespaced_service', return_value=client.V1Service()) @patch( 'memcached_operator.memcached_operator.memcached_tpr_v1alpha1_api.MemcachedThirdPartyResourceV1Alpha1Api.list_memcached_for_all_namespaces' ) def test_service_and_deploy_not_cached( self, mock_list_memcached_for_all_namespaces, mock_read_namespaced_service, mock_create_service, mock_cache_version, mock_is_version_cached, mock_update_service, mock_read_namespaced_deployment, mock_create_deployment, mock_update_deployment): # Mock list memcached call with 0 items mock_list_memcached_for_all_namespaces.return_value = self.base_list_result check_existing() mock_list_memcached_for_all_namespaces.assert_called_once_with() mock_read_namespaced_service.assert_called_once_with( self.name, self.namespace) assert mock_create_service.called is False cache_version_calls = [ call(client.V1Service()), call(client.V1beta1Deployment()) ] mock_cache_version.assert_has_calls(cache_version_calls) is_version_cached_calls = [ call(client.V1Service()), call(client.V1beta1Deployment()) ] mock_is_version_cached.assert_has_calls(is_version_cached_calls) mock_update_service.assert_called_once_with(self.cluster_object) mock_read_namespaced_deployment.assert_called_once_with( self.name, self.namespace) assert mock_create_deployment.called is False mock_update_deployment.assert_called_once_with(self.cluster_object) @patch( 'memcached_operator.memcached_operator.periodical.update_deployment', return_value=False) @patch('memcached_operator.memcached_operator.periodical.create_deployment' ) @patch('kubernetes.client.ExtensionsV1beta1Api.read_namespaced_deployment', return_value=client.V1beta1Deployment()) @patch('memcached_operator.memcached_operator.periodical.update_service', return_value=False) @patch( 'memcached_operator.memcached_operator.periodical.is_version_cached', return_value=False) @patch('memcached_operator.memcached_operator.periodical.cache_version') @patch('memcached_operator.memcached_operator.periodical.create_service') @patch('kubernetes.client.CoreV1Api.read_namespaced_service', return_value=client.V1Service()) @patch( 'memcached_operator.memcached_operator.memcached_tpr_v1alpha1_api.MemcachedThirdPartyResourceV1Alpha1Api.list_memcached_for_all_namespaces' ) def test_service_and_deploy_not_cached_yet_update_exception( self, mock_list_memcached_for_all_namespaces, mock_read_namespaced_service, mock_create_service, mock_cache_version, mock_is_version_cached, mock_update_service, mock_read_namespaced_deployment, mock_create_deployment, mock_update_deployment): # Mock list memcached call with 0 items mock_list_memcached_for_all_namespaces.return_value = self.base_list_result check_existing() mock_list_memcached_for_all_namespaces.assert_called_once_with() mock_read_namespaced_service.assert_called_once_with( self.name, self.namespace) assert mock_create_service.called is False print(mock_cache_version.call_args) assert mock_cache_version.called is False is_version_cached_calls = [ call(client.V1Service()), call(client.V1beta1Deployment()) ] mock_is_version_cached.assert_has_calls(is_version_cached_calls) mock_update_service.assert_called_once_with(self.cluster_object) mock_read_namespaced_deployment.assert_called_once_with( self.name, self.namespace) assert mock_create_deployment.called is False mock_update_deployment.assert_called_once_with(self.cluster_object)
class TestUpdateDeployment(): def setUp(self): self.cluster_object = DEPLOYMENT_CLUSTER_OBJECT self.name = self.cluster_object['metadata']['name'] self.namespace = self.cluster_object['metadata']['namespace'] self.replicas = self.cluster_object['replicas'] @patch('memcached_operator.memcached_operator.kubernetes_helpers.logging') @patch('kubernetes.client.ExtensionsV1beta1Api.patch_namespaced_deployment', return_value=client.V1beta1Deployment()) def test_success(self, mock_patch_namespaced_deployment, mock_logging): deployment = update_deployment(self.cluster_object) body = get_deployment_object(self.cluster_object) mock_patch_namespaced_deployment.assert_called_once_with( self.name, self.namespace, body) mock_logging.info.assert_called_once_with( 'updated deploy/{} in ns/{}'.format(self.name, self.namespace)) assert isinstance(deployment, client.V1beta1Deployment) @patch('memcached_operator.memcached_operator.kubernetes_helpers.logging') @patch('kubernetes.client.ExtensionsV1beta1Api.patch_namespaced_deployment', side_effect=client.rest.ApiException(status=500)) def test_rest_exception(self, mock_patch_namespaced_deployment, mock_logging): deployment = update_deployment(self.cluster_object) assert mock_logging.exception.called is True assert deployment is False