Skip to content
Permalink
Browse files

tests(composer): add unit test for flask

  • Loading branch information...
hanxiao committed Jul 21, 2019
1 parent 2d5654c commit 26a7ad1867fe3c9d22bad94abdc435dc38fac4c3
Showing with 55 additions and 12 deletions.
  1. +4 −0 README.md
  2. +3 −3 gnes/composer/base.py
  3. +10 −5 gnes/composer/flask.py
  4. +20 −2 gnes/resources/compose/gnes-board.html
  5. +18 −2 tests/test_compose.py
@@ -60,6 +60,10 @@ GNES enables large-scale index and semantic search for text-to-text, image-to-im
<img src=".github/gnes-component-overview.svg" alt="component overview">
</p>

<h2 align="center">Install</h2>




<h2 align="center">Getting Started</h2>

@@ -97,9 +97,9 @@ def __repr__(self):
return str(self.components)

def __init__(self, args):
self.args = args
self.logger = set_logger(self.__class__.__name__, self.args.verbose)

self._layers = [] # type: List['YamlComposer.Layer']
self.logger = set_logger(self.__class__.__name__)
with args.yaml_path:
tmp = _yaml.load(args.yaml_path)
stream = StringIO()
@@ -111,7 +111,7 @@ def __init__(self, args):
self._networks = tmp.get('networks', {})
self._volumes = tmp.get('volumes', {})

self.args = args
self._layers = [] # type: List['YamlComposer.Layer']
self._num_layer = 0

if 'services' in tmp:
@@ -17,21 +17,23 @@

from .base import YamlComposer
from ..cli.parser import set_composer_parser
from ..helper import set_logger


class YamlComposerFlask:
def __init__(self, args):
self.args = args
self.logger = set_logger(self.__class__.__name__, self.args.verbose)

def _create_flask_app(self):
try:
from flask import Flask, request, abort, redirect, url_for
from flask import Flask, request
from flask_compress import Compress
from flask_cors import CORS
except ImportError:
raise ImportError('Flask or its dependencies are not fully installed, '
'they are required for serving HTTP requests.'
'Please use "pip install Flask" to install it.')
'Please use "pip install gnes[http]" to install it.')

# support up to 10 concurrent HTTP requests
app = Flask(__name__)
@@ -40,18 +42,21 @@ def _create_flask_app(self):
def _get_homepage():
return YamlComposer(set_composer_parser().parse_args([])).build_all()['html']

@app.route('/refresh', methods=['POST'])
@app.route('/generate', methods=['POST'])
def _regenerate():
data = request.form if request.form else request.json
if not data or 'yaml-config' not in data:
return '<h1>Bad POST request</h1> your POST request does not contain "yaml-config" field!', 406
f = tempfile.NamedTemporaryFile('w', delete=False).name
with open(f, 'w', encoding='utf8') as fp:
fp.write(data['yaml-config'])
try:
return YamlComposer(set_composer_parser().parse_args([
'--yaml_path', f
])).build_all()['html']
except Exception:
return 'Bad YAML input, please kindly check the format, indent and content of your YAML file!'
except Exception as e:
self.logger.error(e)
return '<h1>Bad YAML input</h1> please kindly check the format, indent and content of your YAML file!', 400

CORS(app, origins=self.args.cors)
Compress().init_app(app)
@@ -123,6 +123,21 @@
}
</style>
<title>GNES Board</title>
<meta name="description" content="GNES [jee-nes] is a cloud-native semantic search system based on deep neural network. It enables large-scale index and semantic search for text-to-text, image-to-image, video-to-video and any content form.">
<meta name="author" content="Han Xiao">
<meta name="keywords" content="GNES, neual network, deep learning, search, elastic search, cloud-native, kubernetes, docker swarm, docker container, semantic search, bert, vgg, alexnet, resnet, elmo, faiss, annoy">
<meta property="og:type" content="website">
<meta property="og:title" content="GNES is Generic Neural Elastic Search">
<meta property="og:site_name" content="GNES is Generic Neural Elastic Search">
<meta property="og:description" content="GNES [jee-nes] is a cloud-native semantic search system based on deep neural network. It enables large-scale index and semantic search for text-to-text, image-to-image, video-to-video and any content">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://gnes.ai/img/thumbnail.png">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="GNES is Generic Neural Elastic Search">
<meta name="twitter:description" content="GNES [jee-nes] is a cloud-native semantic search system based on deep neural network. It enables large-scale index and semantic search for text-to-text, image-to-image, video-to-video and any content">
<meta name="twitter:image" content="https://gnes.ai/img/thumbnail.png">
<meta name="twitter:creator" content="@hxiao">
<link rel="icon" type="image/png" href="https://gnes.ai/favicon.png">
</head>
<body>
<nav class="navbar navbar-light bg-light">
@@ -200,7 +215,7 @@
YAML config
</div>
<div class="card-body">
<form action="/refresh" method="post">
<form action="/generate" method="post">
<div class="card-title">
<div class="btn-group" role="group" aria-label="Basic example">
<button type="button" class="btn btn-secondary"
@@ -280,7 +295,7 @@
<hr class="my-4">
<p> 1. Install Docker and Docker Swarm<br>
2. Create a new file say <code>my-compose.yml</code><br>
3. Copy the following content to it
3. Copy the following content to it<br>
4. Run <code>docker stack deploy --compose-file my-compose.yml</code>.</p>
</div>
<div class="card">
@@ -301,6 +316,9 @@
</div>

<div class="tab-pane fade" id="pills-k8s" role="tabpanel">
<div class="jumbotron">
<p class="lead">The generation of Kubenetes config is currently under development.</p>
</div>
<div class="card">
<div class="card-header">
Docker-Swarm/Docker-compose config
@@ -33,15 +33,31 @@ def _test_topology(self, yaml_path: str, num_layer_before: int, num_layer_after:
os.path.exists(self.html_path)
print(a.build_dockerswarm(r))

@unittest.SkipTest
def test_flask(self):
yaml_path = os.path.join(self.dirname, 'yaml', 'topology1.yml')
args = set_composer_flask_parser().parse_args([
'--flask',
'--yaml_path', yaml_path,
'--html_path', self.html_path
])
YamlComposerFlask(args).run()
app = YamlComposerFlask(args)._create_flask_app().test_client()
response = app.get('/', follow_redirects=True)
self.assertEqual(response.status_code, 200)

response = app.post('/generate', follow_redirects=True)
self.assertEqual(response.status_code, 406)

response = app.post('/generate', data={'yaml-config': ''},
follow_redirects=True)
self.assertEqual(response.status_code, 400)

response = app.post('/generate',
data={'yaml-config': 'port: 5566\nservices:\n- name: Preprocessor\n- name: Encoder'},
follow_redirects=True)
self.assertEqual(response.status_code, 200)

response = app.get('/', follow_redirects=True)
self.assertEqual(response.status_code, 200)

def tearDown(self):
if os.path.exists(self.html_path):

0 comments on commit 26a7ad1

Please sign in to comment.
You can’t perform that action at this time.