Skip to content

[fix] fixed xack builder check on str vs int #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "redis-benchmarks-specification"
version = "0.1.5"
version = "0.1.6"
description = "The Redis benchmarks specification describes the cross-language/tools requirements and expectations to foster performance and observability standards around redis related technologies. Members from both industry and academia, including organizations and individuals are encouraged to contribute."
authors = ["filipecosta90 <filipecosta.90@gmail.com>"]
readme = "Readme.md"
Expand Down
113 changes: 8 additions & 105 deletions redis_benchmarks_specification/__api__/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@
#

import logging
from urllib.request import urlopen
from urllib.error import URLError
import logging.handlers
from flask import Flask, jsonify, request
from marshmallow import Schema, fields, ValidationError
from redis_benchmarks_specification import __version__
from json import dumps, loads
import redis
import argparse
from flask_httpauth import HTTPBasicAuth

from redis_benchmarks_specification.__api__.app import create_app
from redis_benchmarks_specification.__common__.env import (
STREAM_KEYNAME_GH_EVENTS_COMMIT,
GH_REDIS_SERVER_HOST,
GH_REDIS_SERVER_PORT,
GH_REDIS_SERVER_AUTH,
Expand All @@ -32,96 +26,6 @@
get_version_string,
)

auth = HTTPBasicAuth()

app = Flask(__name__)
conn = None


@auth.verify_password
def verify_password(username, password):
result = False
try:
auth_server_conn = redis.StrictRedis(
host=REDIS_AUTH_SERVER_HOST,
port=REDIS_AUTH_SERVER_PORT,
decode_responses=True,
username=username,
password=password,
)
auth_server_conn.ping()
result = True
except redis.exceptions.ResponseError:
result = False
except redis.exceptions.AuthenticationError:
result = False
return result


def commit_schema_to_stream(json_str: str, conn: redis.StrictRedis):
""" uses to the provided JSON dict of fields and pushes that info to the corresponding stream """
fields = loads(json_str)
reply_fields = loads(json_str)
result = False
error_msg = None
if "git_hash" not in fields:
error_msg = "Missing required 'git_hash' field"
else:
github_url = "https://github.com/redis/redis/archive/{}.zip".format(
fields["git_hash"]
)
try:
response = urlopen(github_url, timeout=5)
content = response.read()
fields["zip_archive"] = bytes(content)
fields["zip_archive_len"] = len(bytes(content))
reply_fields["archived_zip"] = True
result = True
except URLError as e:
error_msg = "Catched URLError while fetching {} content. Error {}".format(
github_url, e.__str__()
)
logging.error(error_msg)
result = False

if result is True:
id = conn.xadd(STREAM_KEYNAME_GH_EVENTS_COMMIT.encode(), fields)
reply_fields["id"] = id

return result, reply_fields, error_msg


class CommitSchema(Schema):
git_branch = fields.String(required=False)
git_tag = fields.String(required=False)
git_hash = fields.String(required=True)


@app.route("/api/gh/redis/redis/commits", methods=["POST"])
@auth.login_required
def base():
# Get Request body from JSON
request_data = request.json
schema = CommitSchema()
try:
# Validate request body against schema data types
result = schema.load(request_data)
except ValidationError as err:
# Return a nice message if validation fails
return jsonify(err.messages), 400

# Convert request body back to JSON str
data_now_json_str = dumps(result)

result, response_data, err_message = commit_schema_to_stream(
data_now_json_str, conn
)
if result is False:
return jsonify(err_message), 400

# Send data back as JSON
return jsonify(response_data), 200


def main():
_, _, project_version = populate_with_poetry_data()
Expand All @@ -146,7 +50,13 @@ def main():
REDIS_AUTH_SERVER_HOST, REDIS_AUTH_SERVER_PORT
)
)

conn = redis.StrictRedis(
host=GH_REDIS_SERVER_HOST,
port=GH_REDIS_SERVER_PORT,
decode_responses=True,
password=GH_REDIS_SERVER_AUTH,
)
app = create_app(conn)
if args.logname is not None:
print("Writting log to {}".format(args.logname))
handler = logging.handlers.RotatingFileHandler(
Expand All @@ -169,13 +79,6 @@ def main():
GH_REDIS_SERVER_HOST, GH_REDIS_SERVER_PORT
)
)
conn = redis.StrictRedis(
host=GH_REDIS_SERVER_HOST,
port=GH_REDIS_SERVER_PORT,
decode_responses=True,
password=GH_REDIS_SERVER_AUTH,
)

app.run(host="0.0.0.0")


Expand Down
66 changes: 66 additions & 0 deletions redis_benchmarks_specification/__api__/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from flask import Flask, jsonify, request
from marshmallow import ValidationError
from json import dumps
import redis
from flask_httpauth import HTTPBasicAuth

from redis_benchmarks_specification.__api__.schema import (
commit_schema_to_stream,
CommitSchema,
)
from redis_benchmarks_specification.__common__.env import (
REDIS_AUTH_SERVER_HOST,
REDIS_AUTH_SERVER_PORT,
)


def create_app(conn, test_config=None):
app = Flask(__name__)
auth = HTTPBasicAuth()
conn = conn

@auth.verify_password
def verify_password(username, password):
result = False
try:
auth_server_conn = redis.StrictRedis(
host=REDIS_AUTH_SERVER_HOST,
port=REDIS_AUTH_SERVER_PORT,
decode_responses=True,
username=username,
password=password,
)
auth_server_conn.ping()
result = True
except redis.exceptions.ResponseError:
result = False
except redis.exceptions.AuthenticationError:
result = False
return result

@app.route("/api/gh/redis/redis/commits", methods=["POST"])
@auth.login_required
def base():
# Get Request body from JSON
request_data = request.json
schema = CommitSchema()
try:
# Validate request body against schema data types
result = schema.load(request_data)
except ValidationError as err:
# Return a nice message if validation fails
return jsonify(err.messages), 400

# Convert request body back to JSON str
data_now_json_str = dumps(result)

result, response_data, err_message = commit_schema_to_stream(
data_now_json_str, conn
)
if result is False:
return jsonify(err_message), 400

# Send data back as JSON
return jsonify(response_data), 200

return app
50 changes: 50 additions & 0 deletions redis_benchmarks_specification/__api__/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import logging
from json import loads
from urllib.error import URLError
from urllib.request import urlopen

import redis
from marshmallow import Schema, fields

from redis_benchmarks_specification.__common__.env import (
STREAM_KEYNAME_GH_EVENTS_COMMIT,
)


def commit_schema_to_stream(json_str: str, conn: redis.StrictRedis):
""" uses to the provided JSON dict of fields and pushes that info to the corresponding stream """
fields = loads(json_str)
reply_fields = loads(json_str)
result = False
error_msg = None
if "git_hash" not in fields:
error_msg = "Missing required 'git_hash' field"
else:
github_url = "https://github.com/redis/redis/archive/{}.zip".format(
fields["git_hash"]
)
try:
response = urlopen(github_url, timeout=5)
content = response.read()
fields["zip_archive"] = bytes(content)
fields["zip_archive_len"] = len(bytes(content))
reply_fields["archived_zip"] = True
result = True
except URLError as e:
error_msg = "Catched URLError while fetching {} content. Error {}".format(
github_url, e.__str__()
)
logging.error(error_msg)
result = False

if result is True:
id = conn.xadd(STREAM_KEYNAME_GH_EVENTS_COMMIT.encode(), fields)
reply_fields["id"] = id

return result, reply_fields, error_msg


class CommitSchema(Schema):
git_branch = fields.String(required=False)
git_tag = fields.String(required=False)
git_hash = fields.String(required=True)
2 changes: 1 addition & 1 deletion redis_benchmarks_specification/__builder__/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def builder_process_stream(builders_folder, conn, different_build_specs, previou
)
if type(ack_reply) == bytes:
ack_reply = ack_reply.decode()
if ack_reply == "1":
if ack_reply == "1" or ack_reply == 1:
logging.info(
"Sucessfully acknowledge build variation stream with id {}.".format(
streamId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def self_contained_coordinator_blocking_read(
)
if type(ack_reply) == bytes:
ack_reply = ack_reply.decode()
if ack_reply == "1":
if ack_reply == "1" or ack_reply == 1:
logging.info(
"Sucessfully acknowledge build variation stream with id {}.".format(
stream_id
Expand Down
13 changes: 11 additions & 2 deletions utils/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
# Copyright (c) 2021., Redis Labs
# All rights reserved.
#
from redis_benchmarks_specification.__api__.api import commit_schema_to_stream
import redis_benchmarks_specification
from redis_benchmarks_specification.__api__.app import create_app

from redis_benchmarks_specification.__api__.schema import commit_schema_to_stream
import redis

from redis_benchmarks_specification.__common__.env import (
STREAM_KEYNAME_GH_EVENTS_COMMIT,
)

import pytest

import pytest

# First party modules


def test_commit_schema_to_stream():
result, reply_fields, error_msg = commit_schema_to_stream(
Expand All @@ -18,7 +27,7 @@ def test_commit_schema_to_stream():
assert result == False
assert error_msg is not None
try:
conn = redis.StrictRedis()
conn = redis.StrictRedis(port=16379)
conn.ping()
conn.flushall()
result, reply_fields, error_msg = commit_schema_to_stream(
Expand Down
2 changes: 1 addition & 1 deletion utils/tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
import os

from redis_benchmarks_specification.__api__.api import commit_schema_to_stream
from redis_benchmarks_specification.__api__.schema import commit_schema_to_stream
import redis

from redis_benchmarks_specification.__builder__.builder import (
Expand Down
2 changes: 1 addition & 1 deletion utils/tests/test_data/api_builder_common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from redis_benchmarks_specification.__api__.api import commit_schema_to_stream
from redis_benchmarks_specification.__api__.schema import commit_schema_to_stream
from redis_benchmarks_specification.__builder__.builder import (
builder_consumer_group_create,
builder_process_stream,
Expand Down
2 changes: 1 addition & 1 deletion utils/tests/test_self_contained_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from redisbench_admin.utils.utils import get_ts_metric_name

from redis_benchmarks_specification.__api__.api import commit_schema_to_stream
from redis_benchmarks_specification.__api__.schema import commit_schema_to_stream
from redis_benchmarks_specification.__builder__.builder import (
builder_consumer_group_create,
builder_process_stream,
Expand Down