Skip to content

Commit

Permalink
Add support for kubecontext fixture.
Browse files Browse the repository at this point in the history
  • Loading branch information
blakewatters authored and edaniszewski committed Dec 16, 2020
1 parent 4ff48e3 commit fa29fff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
29 changes: 20 additions & 9 deletions docs/writing_tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,41 @@ you can access it from wherever the tests are being run.
Cluster Configuration
---------------------

By default, kubetest will look for a config file at ``~/.kube/config`` -- this
is the same place that ``kubectl`` looks for the cluster config. Generally, if
you can reach your cluster via. ``kubectl``, you should be able to use it with
kubetest.
By default, kubetest will look for a config file at ``~/.kube/config`` and the
current context -- this is the same behavior that ``kubectl`` utilizes for the
resolving cluster config. Generally, if you can reach your cluster via.
``kubectl``, you should be able to use it with kubetest.

If you wish to specify a different config file, you can pass it in via the
``--kube-config`` flag. See :ref:`command_line_usage` for more details.
If you wish to specify a different config file and/or context, you can pass it
in via the ``--kube-config`` and ``--kube-context`` flags.
See :ref:`command_line_usage` for more details.

You can also write a ``kubeconfig`` fixture which provides the path to the
config path. This may be useful in case your cluster is generated as part
of the tests.
config file and/or a ``kubecontext`` fixture which provides the name of the
context to be used. This may be useful in case your cluster is generated as
part of the tests or you wish to use specific contexts in different parts of
the suite.

.. code-block:: python
import pytest
import subprocess
from typing import Optional
@pytest.fixture
def kubeconfig():
def kubeconfig() -> str:
# Here, Terraform creates a cluster and outputs a kubeconfig
# at somepath
subprocess.check_call(['terraform', 'apply'])
return 'somepath/kubeconfig'
@pytest.fixture
def kubecontext() -> Optional[str]:
# Return None to use the current context as set in the kubeconfig
# Or return the name of a specific context in the kubeconfig
return 'kubetest-cluster'
def test_my_terraformed_cluster(kube):
Expand Down
18 changes: 15 additions & 3 deletions kubetest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import pytest
import urllib3

from typing import Optional

from kubetest import errors, markers
from kubetest.client import TestClient
from kubetest.manager import KubetestManager
Expand Down Expand Up @@ -423,15 +425,25 @@ def clusterinfo(kubeconfig) -> ClusterInfo:


@pytest.fixture
def kubeconfig(request) -> str:
def kubeconfig(request) -> Optional[str]:
"""Return the name of the configured kube config file loaded for the tests."""

config_file = request.session.config.getoption('kube_config')
return config_file


@pytest.fixture
def kubecontext(request) -> Optional[str]:
"""Return the context in the kubeconfig to use for the tests.
When None, use the current context as set in the kubeconfig.
"""

context = request.session.config.getoption('kube_context')
return context

@pytest.fixture()
def kube(kubeconfig, request) -> TestClient:
def kube(kubeconfig, kubecontext, request) -> TestClient:
"""Return a client for managing a Kubernetes cluster for testing."""

if request.session.config.getoption('in_cluster'):
Expand All @@ -441,7 +453,7 @@ def kube(kubeconfig, request) -> TestClient:
if kubeconfig:
kubernetes.config.load_kube_config(
config_file=os.path.expandvars(os.path.expanduser(kubeconfig)),
context=request.session.config.getoption('kube_context'),
context=kubecontext,
)
else:
log.error(
Expand Down

0 comments on commit fa29fff

Please sign in to comment.