Exemple #1
0
def test_k8s_create_namespace():
    mock = MagicMock()

    with patch('intel.k8s.client_from_config', MagicMock(return_value=mock)):
        k8s.create_namespace(None, "test_namespace")
        called_methods = mock.method_calls
        assert len(called_methods) == 1
        assert called_methods[0][0] == "create_namespace"
        params = called_methods[0][1]
        assert params[0].metadata["name"] == "test_namespace"
def test_k8s_create_namespace():
    mock = MagicMock()

    with patch(CLIENT_CONFIG, MagicMock(return_value=mock)):
        k8s.create_namespace(None, "test_namespace")
        called_methods = mock.method_calls
        assert len(called_methods) == 1
        assert called_methods[0][0] == "create_namespace"
        params = called_methods[0][1]
        assert params[0].metadata["name"] == "test_namespace"
Exemple #3
0
    def __init__(self,
                 num_nodes=1,
                 timeout=60,
                 config_file_location=None,
                 host='https://localhost:443',
                 api_cert_file='/etc/kubernetes/ssl/ca.pem',
                 user_cert_file='/etc/kubernetes/ssl/admin.pem',
                 user_key_file='/etc/kubernetes/ssl/admin-key.pem'):
        """
        Driver constructor is reserving nodes for test(s) and creates namespace. # noqa: E501
        :param num_nodes: Requested number of nodes from cluster(int; default=1) # noqa: E501
        :param timeout: Timeout on waiting for resource arability in seconds(int; default=60) # noqa: E501
        :param config_file_location: Path to kubernetes client configuration(string, default=None) # noqa: E501
        :param host: (if config_file_location is not set) URL to API-server(string, default="https://localhost:443") # noqa: E501
        :param api_cert_file: (if config_file_location is not set) Path to API server cert(string, default="/etc/kubernetes/ssl/ca.pem") # noqa: E501
        :param user_cert_file: (if config_file_location is not set) Path to user cert(string, default="/etc/kubernetes/ssl/admin.pem") # noqa: E501
        :param user_key_file: (if config_file_location is not set) Path to user key(string, default="/etc/kubernetes/ssl/admin-key.pem") # noqa: E501
        """

        self.num_nodes = num_nodes
        self.timeout = timeout

        if config_file_location is None:
            self.configuration = client.Configuration()
            self.configuration.host = host
            self.configuration.ssl_ca_cert = api_cert_file
            self.configuration.cert_file = user_cert_file
            self.configuration.key_file = user_key_file
        else:
            self.configuration = config.load_kube_config(
                config_file=config_file_location)

        # check if cluster is able to reserve requested number of nodes.
        cluster_size = len(k8s.get_compute_nodes(config=self.configuration))
        if cluster_size < self.num_nodes:
            raise Exception("Cluster doesn't have requested number of nodes."
                            "Requested {}. Available {}.".format(
                                self.num_nodes, cluster_size))

        # generate random namespace name.
        self.ns_name = "namespace-%s" % str(uuid.uuid4())[:5]

        # wait for nodes until timeout.
        timeout_destination = int(time.time()) + self.timeout
        while len(self._get_list_unassigned_nodes(
                self.configuration)) < self.num_nodes:
            if int(time.time()) >= timeout_destination:
                raise Exception("Timeout after {} ".format(self.timeout) +
                                "seconds on waiting for resources avability")

            logging.debug("Waiting for resources...")
            time.sleep(1)

        # add the label to the node.
        unassigned_nodes = self._get_list_unassigned_nodes(self.configuration)
        self.nodes = unassigned_nodes[:self.num_nodes]

        for node in self.nodes:
            k8s.set_node_label(self.configuration, node, "namespace",
                               self.ns_name)

        # create namespace
        k8s.create_namespace(self.configuration, self.ns_name)
        self.active = True