Skip to content

Commit

Permalink
feat(python): Python sample application for e2e testing
Browse files Browse the repository at this point in the history
  • Loading branch information
NSSPKrishna authored and mbruzina committed Jan 31, 2024
1 parent c727daf commit 1dfade3
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
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 test/deploy/linux/python/install/rhel/roles/configure/templates/app.py
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)
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
27 changes: 27 additions & 0 deletions test/manual/definitions/apm/python/python-al2.json
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"]
}
]
}

0 comments on commit 1dfade3

Please sign in to comment.