Skip to content

Commit

Permalink
Merge pull request #23 from olitheolix/oli-sane-ci
Browse files Browse the repository at this point in the history
Improved style check coverage and made it easer to run the locally
  • Loading branch information
tomplus authored Jun 24, 2018
2 parents f1a092f + 48d9b40 commit 3444267
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 119 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ services:

matrix:
include:
- python: 3.5
env: TOXENV=style-checks
- python: 3.6
env: TOXENV=style-checks
- python: 3.5
env: TOXENV=py35
- python: 3.5
Expand All @@ -15,8 +19,6 @@ matrix:
env: TOXENV=py36
- python: 3.6
env: TOXENV=py36-functional
- python: 3.6
env: TOXENV=update-pep8
- python: 3.6
env: TOXENV=docs
- python: 3.6
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ From [PyPi](https://pypi.python.org/pypi/kubernetes_asyncio/) directly:
pip install kubernetes_asyncio
```

## Development
Install the development packages:

```bash
pip install -r requirements.txt
pip install -r test-requirements.txt
```

You can run the style checks and tests with

```bash
flake8 && isort -c
nosetests
```

## Example

To list all pods:
Expand Down
4 changes: 2 additions & 2 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = [
'sphinx.ext.autodoc',
#'sphinx.ext.intersphinx',
# 'sphinx.ext.intersphinx',
]

# autodoc generation is a bit aggressive and a nuisance when doing heavy
Expand Down Expand Up @@ -79,4 +79,4 @@
]

# Example configuration for intersphinx: refer to the Python standard library.
#intersphinx_mapping = {'http://docs.python.org/': None}
# intersphinx_mapping = {'http://docs.python.org/': None}
6 changes: 4 additions & 2 deletions kubernetes_asyncio/config/incluster_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import unittest

from .config_exception import ConfigException
from .incluster_config import (SERVICE_HOST_ENV_NAME, SERVICE_PORT_ENV_NAME,
InClusterConfigLoader, _join_host_port)
from .incluster_config import (
SERVICE_HOST_ENV_NAME, SERVICE_PORT_ENV_NAME, InClusterConfigLoader,
_join_host_port,
)

_TEST_TOKEN = "temp_token"
_TEST_CERT = "temp_cert"
Expand Down
18 changes: 10 additions & 8 deletions kubernetes_asyncio/config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
import shutil
import tempfile
import unittest
from types import SimpleNamespace

import yaml
from six import PY3

from .config_exception import ConfigException
from .dateutil import parse_rfc3339
from .kube_config import (ConfigNode, FileOrData, KubeConfigLoader,
_cleanup_temp_files, _create_temp_file_with_content,
list_kube_config_contexts, load_kube_config,
new_client_from_config)
from .kube_config import (
ConfigNode, FileOrData, KubeConfigLoader, _cleanup_temp_files,
_create_temp_file_with_content, list_kube_config_contexts,
load_kube_config, new_client_from_config,
)

BEARER_TOKEN_FORMAT = "Bearer %s"

Expand Down Expand Up @@ -525,9 +526,10 @@ def test_load_gcp_token_no_refresh(self):

def test_load_gcp_token_with_refresh(self):

def cred(): return None
cred.token = TEST_ANOTHER_DATA_BASE64
cred.expiry = datetime.datetime.now()
cred = SimpleNamespace(
token=TEST_ANOTHER_DATA_BASE64,
expiry=datetime.datetime.now(),
)

loader = KubeConfigLoader(
config_dict=self.TEST_KUBE_CONFIG,
Expand Down
2 changes: 1 addition & 1 deletion kubernetes_asyncio/e2e_test/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# under the License.

import uuid

import asynctest

from kubernetes_asyncio.client import api_client
Expand All @@ -26,7 +27,6 @@ class TestClientBatch(asynctest.TestCase):
def setUpClass(cls):
cls.config = base.get_e2e_configuration()


async def test_job_apis(self):
client = api_client.ApiClient(configuration=self.config)
api = batch_v1_api.BatchV1Api(client)
Expand Down
88 changes: 49 additions & 39 deletions kubernetes_asyncio/e2e_test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.

import json
import time
import uuid

import asynctest

from kubernetes_asyncio.client import api_client
Expand Down Expand Up @@ -61,14 +61,12 @@ async def test_pod_apis(self):
}
}

resp = await api.create_namespaced_pod(body=pod_manifest,
namespace='default')
resp = await api.create_namespaced_pod(body=pod_manifest, namespace='default')
self.assertEqual(name, resp.metadata.name)
self.assertTrue(resp.status.phase)

while True:
resp = await api.read_namespaced_pod(name=name,
namespace='default')
resp = await api.read_namespaced_pod(name=name, namespace='default')
self.assertEqual(name, resp.metadata.name)
self.assertTrue(resp.status.phase)
if resp.status.phase != 'Pending':
Expand All @@ -78,27 +76,26 @@ async def test_pod_apis(self):
exec_command = ['/bin/sh',
'-c',
'for i in $(seq 1 3); do date; done']
resp = await api_ws.connect_get_namespaced_pod_exec(name, 'default',
command=exec_command,
stderr=False, stdin=False,
stdout=True, tty=False)
resp = await api_ws.connect_get_namespaced_pod_exec(
name, 'default', command=exec_command,
stderr=False, stdin=False, stdout=True, tty=False
)
print('EXEC response : %s' % resp)
self.assertEqual(3, len(resp.splitlines()))

exec_command = 'uptime'
resp = await api_ws.connect_post_namespaced_pod_exec(name, 'default',
command=exec_command,
stderr=False, stdin=False,
stdout=True, tty=False)
resp = await api_ws.connect_post_namespaced_pod_exec(
name, 'default', command=exec_command,
stderr=False, stdin=False, stdout=True, tty=False
)
print('EXEC response : %s' % resp)
self.assertEqual(1, len(resp.splitlines()))

resp = await api.list_pod_for_all_namespaces()
number_of_pods = len(resp.items)
self.assertTrue(number_of_pods > 0)

resp = await api.delete_namespaced_pod(name=name, body={},
namespace='default')
resp = await api.delete_namespaced_pod(name=name, body={}, namespace='default')

async def test_service_apis(self):
client = api_client.ApiClient(configuration=self.config)
Expand All @@ -116,28 +113,29 @@ async def test_service_apis(self):
'targetPort': 80}],
'selector': {'name': name}}}

resp = await api.create_namespaced_service(body=service_manifest,
namespace='default')
resp = await api.create_namespaced_service(body=service_manifest, namespace='default')
self.assertEqual(name, resp.metadata.name)
self.assertTrue(resp.status)

resp = await api.read_namespaced_service(name=name,
namespace='default')
resp = await api.read_namespaced_service(name=name, namespace='default')
self.assertEqual(name, resp.metadata.name)
self.assertTrue(resp.status)

service_manifest['spec']['ports'] = [{'name': 'new',
'port': 8080,
'protocol': 'TCP',
'targetPort': 8080}]
resp = await api.patch_namespaced_service(body=service_manifest,
name=name,
namespace='default')
service_manifest['spec']['ports'] = [
{'name': 'new',
'port': 8080,
'protocol': 'TCP',
'targetPort': 8080}
]
resp = await api.patch_namespaced_service(
body=service_manifest,
name=name,
namespace='default'
)
self.assertEqual(2, len(resp.spec.ports))
self.assertTrue(resp.status)

resp = await api.delete_namespaced_service(name=name, body={},
namespace='default')
resp = await api.delete_namespaced_service(name=name, body={}, namespace='default')

async def test_replication_controller_apis(self):
client = api_client.ApiClient(configuration=self.config)
Expand All @@ -147,17 +145,29 @@ async def test_replication_controller_apis(self):
rc_manifest = {
'apiVersion': 'v1',
'kind': 'ReplicationController',
'metadata': {'labels': {'name': name},
'name': name},
'spec': {'replicas': 2,
'selector': {'name': name},
'template': {'metadata': {
'labels': {'name': name}},
'spec': {'containers': [{
'image': 'nginx',
'name': 'nginx',
'ports': [{'containerPort': 80,
'protocol': 'TCP'}]}]}}}}
'metadata': {
'labels': {'name': name},
'name': name
},
'spec': {
'replicas': 2,
'selector': {
'name': name
},
'template': {
'metadata': {'labels': {'name': name}},
'spec': {
'containers': [
{
'image': 'nginx',
'name': 'nginx',
'ports': [{'containerPort': 80, 'protocol': 'TCP'}]
}
]
}
}
}
}

resp = await api.create_namespaced_replication_controller(
body=rc_manifest, namespace='default')
Expand Down
3 changes: 2 additions & 1 deletion kubernetes_asyncio/e2e_test/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.

import asynctest
import uuid

import asynctest
import yaml

from kubernetes_asyncio.client import api_client
Expand Down
4 changes: 2 additions & 2 deletions kubernetes_asyncio/stream/ws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
# License for the specific language governing permissions and limitations
# under the License.

from six.moves.urllib.parse import urlencode, urlparse, urlunparse

from kubernetes_asyncio.client import ApiClient
from six.moves.urllib.parse import urlencode, quote_plus, urlparse, urlunparse
from kubernetes_asyncio.client.rest import RESTResponse


STDIN_CHANNEL = 0
STDOUT_CHANNEL = 1
STDERR_CHANNEL = 2
Expand Down
23 changes: 15 additions & 8 deletions kubernetes_asyncio/stream/ws_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from asynctest import CoroutineMock, Mock, TestCase, patch
from asynctest import CoroutineMock, TestCase, patch

from kubernetes_asyncio.stream.ws_client import get_websocket_url, WsResponse
from kubernetes_asyncio import client, config
from kubernetes_asyncio import client
from kubernetes_asyncio.stream import WsApiClient
from kubernetes_asyncio.stream.ws_client import WsResponse, get_websocket_url


class WSClientTest(TestCase):
Expand Down Expand Up @@ -53,7 +53,7 @@ async def __anext__(self):
self.iter += 1
if self.iter > 5:
raise StopAsyncIteration
return WsResponse((chr(1)+'mock').encode('utf-8'))
return WsResponse((chr(1) + 'mock').encode('utf-8'))

mock = CoroutineMock()
mock.RESTClientObject.return_value.pool_manager = mock
Expand All @@ -67,10 +67,17 @@ async def __anext__(self):
stdout=True, tty=False)

ret = await resp
self.assertEqual(ret, 'mock'*5)
mock.ws_connect.assert_called_once_with('wss://localhost/api/v1/namespaces/namespace/pods/pod/exec?command=mock-command&stderr=True&stdin=False&stdout=True&tty=False',
headers={'sec-websocket-protocol': 'v4.channel.k8s.io', 'Accept': '*/*', 'User-Agent':
'Swagger-Codegen/6.0.0-snapshot/python', 'Content-Type': 'application/json'})
self.assertEqual(ret, 'mock' * 5)
mock.ws_connect.assert_called_once_with(
'wss://localhost/api/v1/namespaces/namespace/pods/pod/exec?'
'command=mock-command&stderr=True&stdin=False&stdout=True&tty=False',
headers={
'sec-websocket-protocol': 'v4.channel.k8s.io',
'Accept': '*/*',
'User-Agent': 'Swagger-Codegen/6.0.0-snapshot/python',
'Content-Type': 'application/json'
}
)


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions kubernetes_asyncio/watch/watch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async def test_watch_k8s_empty_response(self):
# Iteration must cease after all valid responses were received.
watch = kubernetes_asyncio.watch.Watch()
cnt = 0
async for _ in watch.stream(fake_api.get_namespaces):
async for _ in watch.stream(fake_api.get_namespaces): # noqa
cnt += 1
self.assertEqual(cnt, len(side_effects))

Expand Down Expand Up @@ -137,7 +137,7 @@ async def test_watch_with_exception(self):

with self.assertRaises(KeyError):
watch = kubernetes_asyncio.watch.Watch()
async for e in watch.stream(fake_api.get_namespaces, timeout_seconds=10):
async for e in watch.stream(fake_api.get_namespaces, timeout_seconds=10): # noqa
pass


Expand Down
Loading

0 comments on commit 3444267

Please sign in to comment.