You've already forked devops-exercises
Update flask_container_ci challenge
This commit is contained in:
@@ -12,7 +12,7 @@ Please read carefully all the instructions.
|
||||
If any of the following steps is not working, it is expected from you to fix them
|
||||
|
||||
1. Move to `challenges/flask_container_ci` directory, if you are not already there
|
||||
1. Run `export FLASK_APP=app/app.py`
|
||||
1. Run `export FLASK_APP=app/main.py`
|
||||
1. To run the app execute `flask run`. If it doesn't works, fix it
|
||||
3. Access `http://127.0.0.1:5000`. You should see the following
|
||||
|
||||
@@ -50,7 +50,7 @@ docker run -d -p 5000:5000 app
|
||||
Great, now that we have a working app and also can run it in a container, let's set up a CI for it so it won't break again in the future
|
||||
In current directory you have a file called tests.py which includes the tests for the app. What is expected from you is:
|
||||
|
||||
1. The CI should run the app tests. You are free to choose whatever CI system or service you prefer.
|
||||
1. The CI should run the app tests. You are free to choose whatever CI system or service you prefer. Use `python tests.py` for running the tests.
|
||||
2. There should be some kind of test for the Dockerfile you wrote
|
||||
3. Add additional unit test (or another level of tests) for testing the app
|
||||
|
||||
|
||||
58
challenges/flask_container_ci/app/main.py
Normal file
58
challenges/flask_container_ci/app/main.py
Normal file
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python
|
||||
# coding=utf-8
|
||||
|
||||
from flask import Flask
|
||||
from flask import make_response
|
||||
|
||||
import json
|
||||
from werkzeug.exceptions import NotFound
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
with open("./users.json", "r") as f:
|
||||
users = json.load(f)
|
||||
|
||||
|
||||
@app.route("/", methods=['GET'])
|
||||
def index():
|
||||
return pretty_json({
|
||||
"resources": {
|
||||
"users": "/users",
|
||||
"user": "/users/<username>",
|
||||
},
|
||||
"current_uri": "/"
|
||||
})
|
||||
|
||||
|
||||
@app.route("/users", methods=['GET'])
|
||||
def all_users():
|
||||
return pretty_json(users)
|
||||
|
||||
|
||||
@app.route("/users/<username>", methods=['GET'])
|
||||
def user_data(username):
|
||||
if username not in users:
|
||||
raise NotFound
|
||||
|
||||
return pretty_json(users[username])
|
||||
|
||||
|
||||
@app.route("/users/<username>/something", methods=['GET'])
|
||||
def user_something(username):
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
def pretty_json(arg):
|
||||
response = make_response(json.dumps(arg, sort_keys=True, indent=4))
|
||||
response.headers['Content-type'] = "application/json"
|
||||
return response
|
||||
|
||||
|
||||
def create_test_app():
|
||||
app = Flask(__name__)
|
||||
return app
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(port=5000)
|
||||
@@ -1,27 +1,23 @@
|
||||
#!/usr/bin/env python
|
||||
# coding=utf-8
|
||||
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from config import basedir
|
||||
from app import app
|
||||
from app import db
|
||||
from app import main
|
||||
|
||||
|
||||
class TestCase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
app.config['TESTING'] = True
|
||||
app.config['WTF_CSRF_ENABLED'] = False
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(
|
||||
basedir, 'test.db')
|
||||
self.app = app.test_client()
|
||||
db.create_all()
|
||||
self.app = main.app.test_client()
|
||||
|
||||
def tearDown(self):
|
||||
db.session.remove()
|
||||
db.drop_all()
|
||||
def test_main_page(self):
|
||||
response = self.app.get('/', follow_redirects=True)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_users_page(self):
|
||||
response = self.app.get('/users', follow_redirects=True)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user