Skip to content

Commit

Permalink
test(docker): mocked unittests external calls (#1958)
Browse files Browse the repository at this point in the history
test: mocked external calls
  • Loading branch information
slettner committed May 4, 2021
1 parent 4c4571b commit 0f70d99
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 86 deletions.
102 changes: 101 additions & 1 deletion tests/integration/hub_usage/test_hub_usage.py
@@ -1,7 +1,11 @@
import os
from pathlib import Path

import docker
import pytest
from jina.logging import JinaLogger

from jina.enums import BuildTestLevel

from jina import __version__ as jina_version
from jina.docker import hubapi
Expand All @@ -12,7 +16,12 @@
from jina.helper import expand_dict
from jina.jaml import JAML
from jina.parsers import set_pod_parser
from jina.parsers.hub import set_hub_build_parser, set_hub_list_parser
from jina.parsers.hub import (
set_hub_build_parser,
set_hub_list_parser,
set_hub_pushpull_parser,
set_hub_new_parser,
)
from jina.peapods import Pod

cur_dir = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -92,6 +101,26 @@ def test_build_timeout_ready():
pass


@pytest.mark.timeout(360)
def test_hub_pull():
args = set_hub_pushpull_parser().parse_args(['jinahub/pod.dummy_mwu_encoder:0.0.6'])
HubIO(args).pull()


@pytest.mark.timeout(360)
def test_hub_list():
args = set_hub_list_parser().parse_args(['--keywords', 'numeric'])
response = HubIO(args).list()
assert len(response) > 0


@pytest.mark.timeout(360)
def test_hub_list_non_existing_kind():
args = set_hub_list_parser().parse_args(['--kind', 'does-not-exist'])
response = HubIO(args).list()
assert not response


@pytest.mark.skip('https://github.com/jina-ai/jina/issues/1641')
@pytest.mark.skipif(
condition='GITHUB_TOKEN' not in os.environ, reason='Token not found'
Expand Down Expand Up @@ -204,3 +233,74 @@ def test_hub_build_multistage(dockerfile_path):
)
result = HubIO(args).build()
assert result['is_build_success']


@pytest.mark.parametrize('new_type', ['pod', 'app', 'template'])
def test_create_new(tmpdir, new_type):
args = set_hub_new_parser().parse_args(
['--output-dir', str(tmpdir), '--type', new_type]
)
HubIO(args).new(no_input=True)
list_dir = os.listdir(str(tmpdir))
assert len(list_dir) == 1


@pytest.fixture(scope='function')
def test_workspace(tmpdir):
os.environ['JINA_TEST_JOINT'] = str(tmpdir)
workspace_path = os.environ['JINA_TEST_JOINT']
yield workspace_path
del os.environ['JINA_TEST_JOINT']


@pytest.fixture(scope='function')
def docker_image():
def _filter_repo_tag(image, image_name='jinahub/pod.dummy_mwu_encoder'):
tags = image.attrs['RepoTags'] if 'RepoTags' in image.attrs else None
if tags:
return tags[0].startswith(image_name)
else:
return False

img_name = 'jinahub/pod.dummy_mwu_encoder:0.0.6'
client = docker.from_env()
client.images.pull(img_name)
images = client.images.list()
image_name = list(filter(lambda image: _filter_repo_tag(image), images))[0]
return image_name


def test_hub_build_level_pass(monkeypatch, test_workspace, docker_image):
args = set_hub_build_parser().parse_args(
['path/hub-mwu', '--push', '--host-info', '--test-level', 'EXECUTOR']
)
expected_failed_levels = []

_, failed_levels = HubIO(args)._test_build(
docker_image,
BuildTestLevel.EXECUTOR,
os.path.join(cur_dir, 'yaml/test-joint.yml'),
60000,
True,
JinaLogger('unittest'),
)

assert expected_failed_levels == failed_levels


def test_hub_build_level_fail(monkeypatch, test_workspace, docker_image):
args = set_hub_build_parser().parse_args(
['path/hub-mwu', '--push', '--host-info', '--test-level', 'FLOW']
)
expected_failed_levels = [BuildTestLevel.POD_DOCKER, BuildTestLevel.FLOW]

_, failed_levels = HubIO(args)._test_build(
docker_image,
BuildTestLevel.FLOW,
os.path.join(cur_dir, 'yaml/test-joint.yml'),
60000,
True,
JinaLogger('unittest'),
)

assert expected_failed_levels == failed_levels
File renamed without changes.
17 changes: 17 additions & 0 deletions tests/unit/docker/test_hub_build.py
Expand Up @@ -91,6 +91,23 @@ def test_hub_build_uses():
HubIO(args).build()


@pytest.mark.timeout(360)
def test_hub_build_uses():
args = set_hub_build_parser().parse_args(
[os.path.join(cur_dir, 'hub-mwu'), '--test-uses', '--raise-error']
)
assert HubIO(args).build()['is_build_success']
# build again it shall not fail
assert HubIO(args).build()['is_build_success']

args = set_hub_build_parser().parse_args(
[os.path.join(cur_dir, 'hub-mwu'), '--test-uses', '--daemon', '--raise-error']
)
assert HubIO(args).build()['is_build_success']
# build again it shall not fail
assert HubIO(args).build()['is_build_success']


def test_hub_build_failures():
for j in [
'bad-dockerfile',
Expand Down
71 changes: 0 additions & 71 deletions tests/unit/docker/test_hub_build_level.py

This file was deleted.

18 changes: 15 additions & 3 deletions tests/unit/docker/test_hub_list.py
@@ -1,4 +1,5 @@
import pytest
import json
import mock

from jina.docker.hubio import HubIO
from jina.parsers.hub import set_hub_list_parser
Expand All @@ -10,25 +11,36 @@ def test_hub_list_local_with_submodule():
assert len(response) > 1


def test_hub_list_keywords():
@mock.patch('jina.docker.hubapi.remote.urlopen')
def test_hub_list_keywords(mocker):
mocker.return_value.__enter__.return_value.read.return_value = json.dumps(
[{'name': 'foo'}, {'name': 'bar'}]
)

args = set_hub_list_parser().parse_args(['--keywords', 'numeric'])
response = HubIO(args).list()
numeric_count = len(response)

mocker.assert_called_once()
assert numeric_count > 1

args = set_hub_list_parser().parse_args(['--keywords', 'numeric', 'randjojd'])
response = HubIO(args).list()
combined_count = len(response)

assert mocker.call_count == 2
assert combined_count > 1

# Making sure both arguments are --keywords are considered as `either or`
assert combined_count >= numeric_count


def test_hub_list_nonexisting_kind():
@mock.patch('jina.docker.hubapi.remote.urlopen')
def test_hub_list_nonexisting_kind(mocker):
mocker.return_value.__enter__.return_value.read.return_value = json.dumps([])

args = set_hub_list_parser().parse_args(['--kind', 'blah'])
response = HubIO(args).list()

mocker.assert_called_once()
assert not response
10 changes: 0 additions & 10 deletions tests/unit/docker/test_hubio.py
Expand Up @@ -12,16 +12,6 @@
cur_dir = os.path.dirname(os.path.abspath(__file__))


@pytest.mark.parametrize('new_type', ['pod', 'app', 'template'])
def test_create_new(tmpdir, new_type):
args = set_hub_new_parser().parse_args(
['--output-dir', str(tmpdir), '--type', new_type]
)
HubIO(args).new(no_input=True)
list_dir = os.listdir(str(tmpdir))
assert len(list_dir) == 1


def test_login(tmpdir, monkeypatch, mocker):
import requests
import webbrowser
Expand Down
1 change: 0 additions & 1 deletion tests/unit/drivers/test_load_groundtruth_driver.py
@@ -1,6 +1,5 @@
from typing import Optional, Iterable

import numpy as np
import pytest

from jina import Request
Expand Down

0 comments on commit 0f70d99

Please sign in to comment.