Skip to content

Commit

Permalink
Use fake client for availability service
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Skripnick committed Dec 27, 2016
1 parent accc691 commit ca7dab8
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ceagle/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

from ceagle.api import base
from ceagle.api_fake_data import security
from ceagle.api_fake_data import availability
from ceagle import config

FAKE_CLIENT_MAP = {
"security": security.Client,
"availability": availability.Client,
}


Expand Down
1 change: 0 additions & 1 deletion ceagle/api/v1/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ def get_status_performance(period):


@bp_status.route("/availability/<period>")
@fake_status.get_status_availability
def get_status_availability(period):
ct = client.get_client("availability")
api_endpoint = "/api/v1/availability/{period}".format(period=period)
Expand Down
36 changes: 36 additions & 0 deletions ceagle/api_fake_data/availability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2016: Mirantis Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from ceagle.api_fake_data import base

import random


class Client(base.FakeClient):

@base.route("/api/v1/availability/(?P<period>day|week|month)$")
def availability(self, query, period):
data = list(base.generate_data(period,
lambda x: round(random.random(), 2)))
avg = round(sum([i[1] for i in data]) / len(data), 2)
return {
"period": period,
"availability": {
"region1": {
"availability": avg,
"availability_data": data,
}
}
}, 200
27 changes: 27 additions & 0 deletions ceagle/api_fake_data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,41 @@
# License for the specific language governing permissions and limitations
# under the License.

import datetime
import functools
import random
import re

from ceagle import config

import flask

USE_FAKE_DATA = config.get_config().get("use_fake_api_data", True)

PERIOD_DAYS = {
"day": 1,
"week": 7,
"month": 30,
"year": 365,
}

REGIONS = [
"region1",
"region2",
"far-away.example.net",
]


def generate_data(period, method, chunks=40):
days = PERIOD_DAYS.get(period)
if days is None:
flask.abort(404)
now = datetime.datetime.utcnow()
step = datetime.timedelta(days=days) / chunks
for i in range(chunks):
ts = now - step * (chunks - i)
yield [ts.isoformat()[:16], method(ts)]


def route(reg):
def decorator(method):
Expand Down
23 changes: 23 additions & 0 deletions ceagle/api_fake_data/health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2016: Mirantis Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from ceagle.api_fake_data import base


class Client(base.FakeClient):

@base.route("/api/v1/health/(?P<period>day|week|month)$")
def health(self, query, period):
return {"health": {}}, 200
9 changes: 9 additions & 0 deletions tests/unit/api/v1/test_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
from tests.unit import test


class ApiTestCase(test.TestCase):

def test_api_response_code(self):
code, resp = self.get("/api/v1/regions")
self.assertEqual(code, 200)
code, resp = self.get("/api/v1/regions/detailed")
self.assertEqual(code, 200)


class RegionsApiTestCase(test.TestCase):

def setUp(self):
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/api_fake_data/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,14 @@ def test_default(self):
c = base.FakeClient("name", "endpoint")
res = c.get("/none")
self.assertEqual(404, res[1])


class GenerateDataTestCase(test.TestCase):

def test_gen(self):
def gen(ts):
return "sample data for %s" % ts
gen = base.generate_data("day", gen)
for i in gen:
print i
self.assertEqual(1, 2)

0 comments on commit ca7dab8

Please sign in to comment.