From e08c1cbf473dc6394765bdaf718bc7ba66623055 Mon Sep 17 00:00:00 2001 From: abregman Date: Mon, 23 Dec 2019 13:08:32 +0200 Subject: [PATCH] Update flask_container_ci challenge --- README.md | 12 +++-- challenges/flask_container_ci/README.md | 4 +- challenges/flask_container_ci/app/main.py | 58 +++++++++++++++++++++++ challenges/flask_container_ci/tests.py | 22 ++++----- 4 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 challenges/flask_container_ci/app/main.py diff --git a/README.md b/README.md index 8403328..2539230 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ :information_source:  This repository contains questions on various DevOps and SRE related topics -:bar_chart:  There are currently **700** questions +:bar_chart:  There are currently **701** questions :books:  To learn more about DevOps check the resources in [DevOpsBit.com](https://devopsbit.com) @@ -23,7 +23,7 @@ - + @@ -1664,7 +1664,7 @@ Re-install the OS IS NOT the right answer :) What is sudo? How do you set it up?
-##### Random and Strange :) +#### Random and Strange :)
Give 5 commands which are two letters long
@@ -1672,6 +1672,12 @@ Re-install the OS IS NOT the right answer :) ls, wc, dd, df, du, ps, ip, cp, cd ...
+#### Commands + +
+What the lsof command does?
+
+ #### :star: Advanced diff --git a/challenges/flask_container_ci/README.md b/challenges/flask_container_ci/README.md index b9d97bc..222e3a2 100644 --- a/challenges/flask_container_ci/README.md +++ b/challenges/flask_container_ci/README.md @@ -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 diff --git a/challenges/flask_container_ci/app/main.py b/challenges/flask_container_ci/app/main.py new file mode 100644 index 0000000..71d0643 --- /dev/null +++ b/challenges/flask_container_ci/app/main.py @@ -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/", + }, + "current_uri": "/" + }) + + +@app.route("/users", methods=['GET']) +def all_users(): + return pretty_json(users) + + +@app.route("/users/", methods=['GET']) +def user_data(username): + if username not in users: + raise NotFound + + return pretty_json(users[username]) + + +@app.route("/users//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) diff --git a/challenges/flask_container_ci/tests.py b/challenges/flask_container_ci/tests.py index 6fa91db..a696fb5 100644 --- a/challenges/flask_container_ci/tests.py +++ b/challenges/flask_container_ci/tests.py @@ -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__':
DevOps
DevOps

Beginner :baby:
Advanced :star:
Jenkins
Jenkins

Beginner :baby:
Advanced :star:
Jenkins
Jenkins

Beginner :baby:
Advanced :star:
Git
Git

Beginner :baby:
Advanced :star:
Ansible
Ansible

Beginner :baby:
Advanced :star:
Network
Network

Beginner :baby:
Advanced :star: