def test_generate_certificates_with_error(self, mock_get_issuer_name): mock_cluster = mock.MagicMock() mock_get_issuer_name.side_effect = exception.MagnumException() self.assertRaises(exception.CertificatesToClusterFailed, cert_manager.generate_certificates_to_cluster, mock_cluster)
def get_removal_nodes(self, hosts_output): if not self._is_scale_down(): return list() cluster = self.new_cluster stack = self.osclient.heat().stacks.get(cluster.stack_id) hosts = hosts_output.get_output_value(stack) if hosts is None: raise exception.MagnumException(_( "Output key '%(output_key)s' is missing from stack " "%(stack_id)s") % {'output_key': hosts_output.heat_output, 'stack_id': stack.id}) hosts_with_container = self._get_hosts_with_container(self.context, cluster) hosts_no_container = list(set(hosts) - hosts_with_container) LOG.debug('List of hosts that has no container: %s', str(hosts_no_container)) num_of_removal = self._get_num_of_removal() if len(hosts_no_container) < num_of_removal: LOG.warning( "About to remove %(num_removal)d nodes, which is larger than " "the number of empty nodes (%(num_empty)d). %(num_non_empty)d " "non-empty nodes will be removed.", { 'num_removal': num_of_removal, 'num_empty': len(hosts_no_container), 'num_non_empty': num_of_removal - len(hosts_no_container)}) hosts_to_remove = hosts_no_container[0:num_of_removal] LOG.info('Require removal of hosts: %s', hosts_to_remove) return hosts_to_remove
def test_create_trustee_and_trust_with_error(self, mock_generate_password): mock_cluster = mock.MagicMock() mock_generate_password.side_effect = exception.MagnumException() self.assertRaises(exception.TrusteeOrTrustToClusterFailed, trust_manager.create_trustee_and_trust, self.osc, mock_cluster)
def test_k8s_monitor_health_unhealthy_api(self, mock_create_client_files): mock_create_client_files.return_value = (tempfile.NamedTemporaryFile(), tempfile.NamedTemporaryFile(), tempfile.NamedTemporaryFile()) self.requests_mock.register_uri( 'GET', f"{self.cluster.api_address}/api/v1/nodes", json={ 'items': [{ 'metadata': { 'name': 'k8s-cluster-node-0' }, 'status': { 'conditions': [{ 'type': 'Ready', 'status': 'True', }] } }] }) self.requests_mock.register_uri( 'GET', f"{self.cluster.api_address}/healthz", exc=exception.MagnumException(message='failed'), ) self.k8s_monitor.poll_health_status() self.assertEqual(self.k8s_monitor.data['health_status'], m_fields.ClusterHealthStatus.UNHEALTHY) self.assertEqual(self.k8s_monitor.data['health_status_reason'], {'api': 'failed'})
def test_k8s_monitor_health_unhealthy_api(self, mock_k8s_api): mock_nodes = mock.MagicMock() mock_node = mock.MagicMock() mock_api_client = mock.MagicMock() mock_node.status = mock.MagicMock() mock_node.metadata.name = 'k8s-cluster-node-0' mock_node.status.conditions = [ NODE_STATUS_CONDITION(type='Ready', status='True') ] mock_nodes.items = [mock_node] mock_k8s_api.return_value.list_node.return_value = (mock_nodes) mock_k8s_api.return_value.api_client = mock_api_client mock_api_client.call_api.side_effect = exception.MagnumException( message='failed') self.k8s_monitor.poll_health_status() self.assertEqual(self.k8s_monitor.data['health_status'], m_fields.ClusterHealthStatus.UNHEALTHY) self.assertEqual(self.k8s_monitor.data['health_status_reason'], {'api': 'failed'})
def get_removal_nodes(self, hosts_output): if not self._is_scale_down(): return list() bay = self.new_bay stack = self.osclient.heat().stacks.get(bay.stack_id) hosts = hosts_output.get_output_value(stack) if hosts is None: raise exception.MagnumException( _("Output key '%(output_key)s' is missing from stack " "%(stack_id)s") % { 'output_key': hosts_output.heat_output, 'stack_id': stack.id }) hosts_no_container = list(hosts) k8s_api = k8s.create_k8s_api(self.context, bay) for pod in k8s_api.list_namespaced_pod(namespace='default').items: host = pod.spec.node_name if host in hosts_no_container: hosts_no_container.remove(host) LOG.debug('List of hosts that has no container: %s', str(hosts_no_container)) num_of_removal = self._get_num_of_removal() if len(hosts_no_container) < num_of_removal: LOG.warning( _LW("About to remove %(num_removal)d nodes, which is larger than " "the number of empty nodes (%(num_empty)d). %(num_non_empty)d " "non-empty nodes will be removed."), { 'num_removal': num_of_removal, 'num_empty': len(hosts_no_container), 'num_non_empty': num_of_removal - len(hosts_no_container) }) hosts_to_remove = hosts_no_container[0:num_of_removal] LOG.info(_LI('Require removal of hosts: %s'), hosts_to_remove) return hosts_to_remove
def execute(self, command, definition=None, namespace=None, print_error=True): if definition: cmd = "cat <<'EOF' | {} {} -f -\n{}\nEOF".format( self.kubectl, command, definition) else: if namespace: cmd = "{} -n {} {}".format(self.kubectl, namespace, command) else: cmd = "{} {}".format(self.kubectl, command) try: return subprocess.check_output(cmd, shell=True) except subprocess.CalledProcessError: if print_error: if "delete" in command: LOG.warning("K8s: Delete failed.") else: exc_msg = "Failed to execute kubectl command, cmd=%s" % cmd LOG.error(exc_msg) raise exception.MagnumException(message=exc_msg)