Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Commit

Permalink
Test with kubernetes client library & its dependencies not pre-installed
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Vasilyev committed Jun 14, 2019
1 parent 7b30b0b commit 726bff9
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ env:
- MINIKUBE_VERSION=1.0.1
- E2E=true # to not skip the e2e tests in pytest
matrix:
- KUBERNETES_VERSION=1.14.0 KUBERNETES_CLIENT=yes # only one is enough
- KUBERNETES_VERSION=1.14.0
- KUBERNETES_VERSION=1.13.0
- KUBERNETES_VERSION=1.12.0
Expand All @@ -33,6 +34,7 @@ matrix:

before_script:
- tools/minikube-for-travis.sh
- tools/kubernetes-client.sh

script:
- pytest -v
Expand Down
3 changes: 0 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ urllib3<1.25
# The runtime dependencies of the framework, as if via `pip install kopf`.
-e .

# Optional packages are present for testing (presence is auto-detected).
kubernetes

# Everything needed to develop (test, debug) the framework.
pytest-asyncio
pytest-mock
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import sys

import click.testing
import kubernetes
import pytest

import kopf
Expand Down Expand Up @@ -65,6 +64,7 @@ def clean_modules_cache():

@pytest.fixture(autouse=True)
def clean_kubernetes_client():
kubernetes = pytest.importorskip('kubernetes')
kubernetes.client.configuration.Configuration.set_default(None)


Expand Down
5 changes: 4 additions & 1 deletion tests/cli/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
import logging

import kubernetes
import pytest


Expand Down Expand Up @@ -52,6 +51,8 @@ def test_verbosity(invoke, caplog, options, envvars, expect_debug, expect_info,
(['--verbose']),
], ids=['default', 'q', 'quiet', 'v', 'verbose'])
def test_no_lowlevel_dumps_in_nondebug(invoke, caplog, options, login, preload, real_run):
kubernetes = pytest.importorskip('kubernetes')

result = invoke(['run'] + options)
assert result.exit_code == 0

Expand All @@ -70,6 +71,8 @@ def test_no_lowlevel_dumps_in_nondebug(invoke, caplog, options, login, preload,
(['--debug']),
], ids=['d', 'debug'])
async def test_lowlevel_dumps_in_debug_mode(invoke, caplog, options, login, preload, real_run):
kubernetes = pytest.importorskip('kubernetes')

result = invoke(['run'] + options)
assert result.exit_code == 0

Expand Down
7 changes: 6 additions & 1 deletion tests/cli/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Remember: We do not test the clients, we assume they work when used properly.
We test our own functions here, and check of the clients were called.
"""
import kubernetes.client.rest
import pytest
import requests

Expand Down Expand Up @@ -45,6 +44,7 @@ def test_direct_auth_works_incluster(login_mocks):


def test_direct_auth_works_kubeconfig(login_mocks):
kubernetes = pytest.importorskip('kubernetes')
login_mocks.pykube_in_cluster.side_effect = FileNotFoundError
login_mocks.client_in_cluster.side_effect = kubernetes.config.ConfigException

Expand All @@ -70,6 +70,7 @@ def test_direct_auth_fails_on_errors_in_pykube(login_mocks):


def test_direct_auth_fails_on_errors_in_client(login_mocks):
kubernetes = pytest.importorskip('kubernetes')
login_mocks.client_in_cluster.side_effect = kubernetes.config.ConfigException
login_mocks.client_from_file.side_effect = kubernetes.config.ConfigException

Expand Down Expand Up @@ -100,6 +101,7 @@ def test_direct_check_fails_on_errors_in_pykube(login_mocks):


def test_direct_check_fails_on_errors_in_client(login_mocks):
kubernetes = pytest.importorskip('kubernetes')
login_mocks.client_checker.side_effect = kubernetes.client.rest.ApiException(status=401)

login()
Expand Down Expand Up @@ -142,6 +144,7 @@ def test_clirun_auth_works_incluster(invoke, login_mocks, preload, real_run):


def test_clirun_auth_works_kubeconfig(invoke, login_mocks, preload, real_run):
kubernetes = pytest.importorskip('kubernetes')
login_mocks.pykube_in_cluster.side_effect = FileNotFoundError
login_mocks.client_in_cluster.side_effect = kubernetes.config.ConfigException

Expand Down Expand Up @@ -173,6 +176,7 @@ def test_clirun_auth_fails_on_errors_in_pykube(invoke, login_mocks, preload, rea


def test_clirun_auth_fails_on_errors_in_client(invoke, login_mocks, preload, real_run):
kubernetes = pytest.importorskip('kubernetes')
login_mocks.client_in_cluster.side_effect = kubernetes.config.ConfigException
login_mocks.client_from_file.side_effect = kubernetes.config.ConfigException

Expand Down Expand Up @@ -206,6 +210,7 @@ def test_clirun_check_fails_on_errors_in_pykube(invoke, login_mocks, preload, re


def test_clirun_check_fails_on_errors_in_client(invoke, login_mocks, preload, real_run):
kubernetes = pytest.importorskip('kubernetes')
login_mocks.client_checker.side_effect = kubernetes.client.rest.ApiException(status=401)

result = invoke(['run'])
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from unittest.mock import Mock

import asynctest
import kubernetes.client.rest
import pykube
import pytest
import pytest_mock
Expand Down Expand Up @@ -93,6 +92,8 @@ class LoginMocks:

@pytest.fixture()
def login_mocks(mocker):
kubernetes = pytest.importorskip('kubernetes')

# Pykube config is needed to create a pykube's API instance.
# But we do not want and do not need to actually authenticate, so we mock.
# Some fields are used by pykube's objects: we have to know them ("leaky abstractions").
Expand Down
7 changes: 7 additions & 0 deletions tools/kubernetes-client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -eu
set -x

if [[ "${KUBERNETES_CLIENT:-}" = "yes" ]] ; then
pip install kubernetes
fi

0 comments on commit 726bff9

Please sign in to comment.