Skip to content

Commit

Permalink
Testing for umpire endpoint and editing endpoint for better handling WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
emrodas10 committed Oct 28, 2019
1 parent aa90f79 commit 7d86220
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 19 deletions.
3 changes: 0 additions & 3 deletions api/server/db/appapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ def walkoff_version_is_semver(cls, value, values):
semver.parse(value)
return value

# class Config:
# orm_mode = True


# class AppApi(Base):
# __tablename__ = "app_api"
Expand Down
14 changes: 6 additions & 8 deletions api/server/endpoints/umpire.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import uuid
import requests
import asyncio
from typing import List

from fastapi import APIRouter, HTTPException
from common.config import static
Expand All @@ -16,14 +17,13 @@
router = APIRouter()


@router.get("/files/{app_name}/{app_version}")
@router.get("/files/{app_name}/{app_version}",
response_model=List[str], response_description="List of file names")
async def list_all_files(app_name: str, app_version: str):

try:
result = await MinioApi.list_files(app_name, app_version)
return result
except Exception as e:
return e
result = await MinioApi.list_files(app_name, app_version)
return result

# headers = {'content-type': 'application/json'}
# payload = {'app_name': app_name, 'app_version': app_version}
# url = f"http://{static.UMPIRE_SERVICE}:8000/umpire/files"
Expand All @@ -37,7 +37,6 @@ async def list_all_files(app_name: str, app_version: str):

@router.get("/file/{app_name}/{app_version}")
async def get_file_contents(app_name: str, app_version: str, file_path: str):

file_data = await MinioApi.get_file(app_name, app_version, file_path)
try:
return file_data.json()
Expand All @@ -47,7 +46,6 @@ async def get_file_contents(app_name: str, app_version: str, file_path: str):

@router.post("/file_upload/")
async def update_file(body: UploadFile):

file_data_bytes = body.file_data.encode('utf-8')
file_size = len(file_data_bytes)

Expand Down
4 changes: 2 additions & 2 deletions bootloader/base-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ services:
image: bitnami/minio:2019-debian-9
networks:
- walkoff_network
# ports:
# - "9001:9000"
ports:
- "9001:9000"
volumes:
- walkoff_resource_minio_volume:/data
- ./apps:/app/apps
Expand Down
30 changes: 25 additions & 5 deletions common/minio_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import aiodocker
import os
from os import stat
from urllib3.exceptions import ResponseError
from minio.error import NoSuchKey

import asyncio
from pwd import getpwuid
from grp import getgrgid
Expand Down Expand Up @@ -105,8 +108,13 @@ async def get_file(app_name, version, path):
minio_client = Minio(config.MINIO, access_key=config.get_from_file(config.MINIO_ACCESS_KEY_PATH),
secret_key=config.get_from_file(config.MINIO_SECRET_KEY_PATH), secure=False)
abs_path = f"apps/{app_name}/{version}/{path}"
data = minio_client.get_object('apps-bucket', abs_path)
return data.read()
try:
data = minio_client.get_object('apps-bucket', abs_path)
return data.read()
except NoSuchKey as n:
return "No Such Key"
except ResponseError as e:
return "Response Error"

@staticmethod
async def update_file(app_name, version, path, file_data, file_size):
Expand Down Expand Up @@ -135,9 +143,12 @@ async def update_file(app_name, version, path, file_data, file_size):
@staticmethod
async def save_file(app_name, version):
temp = []
editing = False
minio_client = Minio(config.MINIO, access_key=config.get_from_file(config.MINIO_ACCESS_KEY_PATH),
secret_key=config.get_from_file(config.MINIO_SECRET_KEY_PATH), secure=False)
objects = minio_client.list_objects("apps-bucket", recursive=True)
if objects is []:
return False
for obj in objects:
size = obj.size
p_src = Path(obj.object_name)
Expand All @@ -146,12 +157,21 @@ async def save_file(app_name, version):
p_dst = hold[hold.find(app_name):]
p_dst = Path("apps") / p_dst
os.makedirs(p_dst.parent, exist_ok=True)

data = minio_client.get_object('apps-bucket', hold)
editing = True
try:
data = minio_client.get_object('apps-bucket', hold)
except NoSuchKey as n:
return False
except ResponseError as r:
return False
with open(str(p_dst), 'wb+') as file_data:
for d in data.stream(size):
file_data.write(d)
#TODO: Make this more secure, don't just base it off of requirements.txt
owner_id = stat(f"apps/{app_name}/{version}/requirements.txt").st_uid
group_id = stat(f"apps/{app_name}/{version}/requirements.txt").st_gid
os.chown(p_dst, owner_id, group_id)
return True
if editing is False:
return False
else:
return True
42 changes: 41 additions & 1 deletion testing/api/test_umpire_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,47 @@

def test_list_all_files(api: TestClient, auth_header: dict):
list_url = base_umpire_url + f"/files/basics/1.0.0"
print(list_url)
p = api.get(list_url, headers=auth_header)
assert p.status_code == 200


def test_list_file_missing(api: TestClient, auth_header: dict):
list_url = base_umpire_url + f"/files/not_a_real_app/1.0.0"
p = api.get(list_url, headers=auth_header)
assert p.status_code == 200
assert p.content == b'[]'


def test_get_file_contents(api: TestClient, auth_header: dict):
list_url = base_umpire_url + f"/file/basics/1.0.0?file_path=src/app.py"
p = api.get(list_url, headers=auth_header)
assert p.status_code == 200


def test_get_file_contents_fail(api: TestClient, auth_header: dict):
list_url = base_umpire_url + f"/file/not_a_real_app/1.0.0?file_path=src/notpython.py"
p = api.get(list_url, headers=auth_header)
print(p)
assert p.status_code == 200
assert p.content == b'"No Such Key"'
# assert p.content == "Couldn't get file contents from specified application/version number."


def test_build_image(api: TestClient, auth_header: dict):
list_url = base_umpire_url + f"/build/basics/1.0.0"
p = api.post(list_url, headers=auth_header)
assert p.status_code == 200


def test_save_file(api: TestClient, auth_header: dict):
list_url = base_umpire_url + f"/save/basics/1.0.0"
p = api.post(list_url, headers=auth_header)
assert p.status_code == 200
assert p.content == b'"Successful Update to Local Repo"'


def test_save_file_failed(api: TestClient, auth_header: dict):
list_url = base_umpire_url + f"/save/not_a_real_app/1.0.0"
p = api.post(list_url, headers=auth_header)
assert p.status_code == 200
assert p.content == b'"Failed"'

0 comments on commit 7d86220

Please sign in to comment.