File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1717
1818from ceagle .api import base
1919from ceagle .api_fake_data import security
20+ from ceagle .api_fake_data import availability
2021from ceagle import config
2122
2223FAKE_CLIENT_MAP = {
2324 "security" : security .Client ,
25+ "availability" : availability .Client ,
2426}
2527
2628
Original file line number Diff line number Diff 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
8281def get_status_availability (period ):
8382 ct = client .get_client ("availability" )
8483 api_endpoint = "/api/v1/availability/{period}" .format (period = period )
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1313# License for the specific language governing permissions and limitations
1414# under the License.
1515
16+ import datetime
1617import functools
1718import random
1819import re
1920
2021from ceagle import config
2122
23+ import flask
24+
2225USE_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
2552def route (reg ):
2653 def decorator (method ):
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 2121from 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+
2433class RegionsApiTestCase (test .TestCase ):
2534
2635 def setUp (self ):
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments