-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python): Python sample application for e2e testing
- Loading branch information
1 parent
c727daf
commit 1dfade3
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
test/deploy/linux/python/install/rhel/roles/configure/tasks/main.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
- debug: | ||
msg: Setting up Sample Python application | ||
|
||
- name: Copy files to templates directory | ||
synchronize: | ||
src: "{{ item }}" | ||
dest: ~/ | ||
mode: push | ||
with_fileglob: | ||
- "../templates/*" | ||
become: true | ||
|
||
- name: Install dependencies from requirements.txt | ||
ansible.builtin.pip: | ||
requirements: /home/ec2-user/requirements.txt | ||
umask: "0022" | ||
become: true | ||
|
100 changes: 100 additions & 0 deletions
100
test/deploy/linux/python/install/rhel/roles/configure/templates/app.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import requests | ||
import threading | ||
import time | ||
import uuid | ||
from flask import Flask, jsonify, request | ||
from flask_cors import CORS | ||
|
||
BOOKS = [ | ||
{ | ||
'id': uuid.uuid4().hex, | ||
'title': 'On the Road', | ||
'author': 'Jack Kerouac', | ||
'read': True | ||
}, | ||
{ | ||
'id': uuid.uuid4().hex, | ||
'title': 'Harry Potter and the Philosopher\'s Stone', | ||
'author': 'J. K. Rowling', | ||
'read': False | ||
}, | ||
{ | ||
'id': uuid.uuid4().hex, | ||
'title': 'Green Eggs and Ham', | ||
'author': 'Dr. Seuss', | ||
'read': True | ||
} | ||
] | ||
|
||
# configuration | ||
DEBUG = True | ||
|
||
# instantiate the app | ||
app = Flask(__name__) | ||
app.config.from_object(__name__) | ||
|
||
# enable CORS | ||
CORS(app, resources={r'/*': {'origins': '*'}}) | ||
|
||
# sanity check route | ||
@app.route('/ping', methods=['GET']) | ||
def ping_pong(): | ||
return jsonify('pong!') | ||
|
||
def remove_book(book_id): | ||
for book in BOOKS: | ||
if book['id'] == book_id: | ||
BOOKS.remove(book) | ||
return True | ||
return False | ||
|
||
@app.route('/books', methods=['GET', 'POST']) | ||
def all_books(): | ||
response_object = {'status': 'success'} | ||
if request.method == 'POST': | ||
post_data = request.get_json() | ||
|
||
# break the app here - change to BOOKSSSS | ||
BOOKS.append({ | ||
'id': uuid.uuid4().hex, | ||
'title': post_data.get('title'), | ||
'author': post_data.get('author'), | ||
'read': post_data.get('read') | ||
}) | ||
response_object['message'] = 'Book added!' | ||
else: | ||
response_object['books'] = BOOKS | ||
return jsonify(response_object) | ||
|
||
@app.route('/books/<book_id>', methods=['PUT', 'DELETE']) | ||
def single_book(book_id): | ||
response_object = {'status': 'success'} | ||
if request.method == 'PUT': | ||
post_data = request.get_json() | ||
remove_book(book_id) | ||
BOOKS.append({ | ||
'id': uuid.uuid4().hex, | ||
'title': post_data.get('title'), | ||
'author': post_data.get('author'), | ||
'read': post_data.get('read') | ||
}) | ||
response_object['message'] = 'Book updated!' | ||
if request.method == 'DELETE': | ||
remove_book(book_id) | ||
response_object['message'] = 'Book removed!' | ||
return jsonify(response_object) | ||
|
||
@app.route('/post_book', methods=['POST']) | ||
def somefunction(): | ||
return jsonify('Posted') | ||
|
||
def generate_traffic(): | ||
while True: | ||
requests.get('http://localhost:5000/books') | ||
requests.get('http://localhost:5000/ping') | ||
time.sleep(5) | ||
|
||
if __name__ == '__main__': | ||
traffic_thread = threading.Thread(target=generate_traffic) | ||
traffic_thread.start() | ||
app.run(threaded=True) |
3 changes: 3 additions & 0 deletions
3
test/deploy/linux/python/install/rhel/roles/configure/templates/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Flask>=2.2.2 | ||
Flask-Cors==3.0.10 | ||
requests==2.28.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"global_tags": { | ||
"owning_team": "virtuoso", | ||
"Environment": "development", | ||
"Department": "product", | ||
"Product": "virtuoso" | ||
}, | ||
"resources": [ | ||
{ | ||
"id": "pythonHost", | ||
"provider": "aws", | ||
"type": "ec2", | ||
"size": "t3.small", | ||
"ami_name": "amzn2-ami-hvm-2.0.????????.?-x86_64-gp2", | ||
"user_name": "ec2-user" | ||
} | ||
], | ||
"services": [ | ||
{ | ||
"id": "pythonApp", | ||
"source_repository": "https://github.com/newrelic/open-install-library.git", | ||
"deploy_script_path": "test/deploy/linux/python/install/rhel/roles/", | ||
"port": 80, | ||
"destinations": ["pythonHost"] | ||
} | ||
] | ||
} |