Skip to content

Commit ca7dab8

Browse files
author
Sergey Skripnick
committed
Use fake client for availability service
1 parent accc691 commit ca7dab8

7 files changed

Lines changed: 108 additions & 1 deletion

File tree

ceagle/api/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
from ceagle.api import base
1919
from ceagle.api_fake_data import security
20+
from ceagle.api_fake_data import availability
2021
from ceagle import config
2122

2223
FAKE_CLIENT_MAP = {
2324
"security": security.Client,
25+
"availability": availability.Client,
2426
}
2527

2628

ceagle/api/v1/status.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ def get_status_performance(period):
7878

7979

8080
@bp_status.route("/availability/<period>")
81-
@fake_status.get_status_availability
8281
def get_status_availability(period):
8382
ct = client.get_client("availability")
8483
api_endpoint = "/api/v1/availability/{period}".format(period=period)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2016: Mirantis Inc.
2+
# All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
16+
from ceagle.api_fake_data import base
17+
18+
import random
19+
20+
21+
class Client(base.FakeClient):
22+
23+
@base.route("/api/v1/availability/(?P<period>day|week|month)$")
24+
def availability(self, query, period):
25+
data = list(base.generate_data(period,
26+
lambda x: round(random.random(), 2)))
27+
avg = round(sum([i[1] for i in data]) / len(data), 2)
28+
return {
29+
"period": period,
30+
"availability": {
31+
"region1": {
32+
"availability": avg,
33+
"availability_data": data,
34+
}
35+
}
36+
}, 200

ceagle/api_fake_data/base.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,41 @@
1313
# License for the specific language governing permissions and limitations
1414
# under the License.
1515

16+
import datetime
1617
import functools
1718
import random
1819
import re
1920

2021
from ceagle import config
2122

23+
import flask
24+
2225
USE_FAKE_DATA = config.get_config().get("use_fake_api_data", True)
2326

27+
PERIOD_DAYS = {
28+
"day": 1,
29+
"week": 7,
30+
"month": 30,
31+
"year": 365,
32+
}
33+
34+
REGIONS = [
35+
"region1",
36+
"region2",
37+
"far-away.example.net",
38+
]
39+
40+
41+
def generate_data(period, method, chunks=40):
42+
days = PERIOD_DAYS.get(period)
43+
if days is None:
44+
flask.abort(404)
45+
now = datetime.datetime.utcnow()
46+
step = datetime.timedelta(days=days) / chunks
47+
for i in range(chunks):
48+
ts = now - step * (chunks - i)
49+
yield [ts.isoformat()[:16], method(ts)]
50+
2451

2552
def route(reg):
2653
def decorator(method):

ceagle/api_fake_data/health.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2016: Mirantis Inc.
2+
# All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
16+
from ceagle.api_fake_data import base
17+
18+
19+
class Client(base.FakeClient):
20+
21+
@base.route("/api/v1/health/(?P<period>day|week|month)$")
22+
def health(self, query, period):
23+
return {"health": {}}, 200

tests/unit/api/v1/test_regions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
from tests.unit import test
2222

2323

24+
class ApiTestCase(test.TestCase):
25+
26+
def test_api_response_code(self):
27+
code, resp = self.get("/api/v1/regions")
28+
self.assertEqual(code, 200)
29+
code, resp = self.get("/api/v1/regions/detailed")
30+
self.assertEqual(code, 200)
31+
32+
2433
class RegionsApiTestCase(test.TestCase):
2534

2635
def setUp(self):

tests/unit/api_fake_data/test_base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,14 @@ def test_default(self):
4646
c = base.FakeClient("name", "endpoint")
4747
res = c.get("/none")
4848
self.assertEqual(404, res[1])
49+
50+
51+
class GenerateDataTestCase(test.TestCase):
52+
53+
def test_gen(self):
54+
def gen(ts):
55+
return "sample data for %s" % ts
56+
gen = base.generate_data("day", gen)
57+
for i in gen:
58+
print i
59+
self.assertEqual(1, 2)

0 commit comments

Comments
 (0)