Skip to content

Commit 869eb60

Browse files
committed
Add test predictUserLoad with 2 cases
1 parent d9eae04 commit 869eb60

7 files changed

+114
-39
lines changed

__pycache__/prediction.cpython-35.pyc

1.52 KB
Binary file not shown.

__pycache__/utils.cpython-35.pyc

18.2 KB
Binary file not shown.

prediction.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import MySQLdb
44

55

6-
def changeAlgorithm(new_avg, prev_avg, difference):
7-
return new_avg
6+
def changeAlgorithm(new_avg, prev_avg):
7+
return (new_avg + prev_avg) / 2
88

99

1010
def predictUserLoad(user, jsample_repo, pred_repo):
@@ -17,17 +17,8 @@ def predictUserLoad(user, jsample_repo, pred_repo):
1717
# As previous system statistic
1818
pred = pred_repo.get(user.uid)
1919

20-
# We want to adjust the value of average CPU for the user
21-
# in the prediction table by using an algorithm that can
22-
# adjust the value based on the difference
23-
difference = stat.avg_cpu - pred.avg_cpu
24-
new_avg_cpu = changeAlgorithm(stat.avg_cpu, pred.avg_cpu,
25-
difference)
26-
27-
# Similiar with RAM
28-
difference = stat.avg_ram - pred.avg_ram
29-
new_avg_ram = changeAlgorithm(stat.avg_ram, pred.avg_ram,
30-
difference)
20+
new_avg_cpu = changeAlgorithm(stat.avg_cpu, pred.avg_cpu)
21+
new_avg_ram = changeAlgorithm(stat.avg_ram, pred.avg_ram)
3122

3223
# We check if the user have heavier CPU jobs
3324
# running in the system

prediction.pyc

1.73 KB
Binary file not shown.

test_prediction.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from prediction import predictUserLoad
2+
from utils import *
3+
import unittest
4+
from mock import MagicMock
5+
6+
7+
class TestPredictionMethod(unittest.TestCase):
8+
def setUp(self):
9+
# Mock jSampleRepository
10+
self.jsample_repo = jSampleRepository(None)
11+
12+
self.stat = jSampleStatistic(
13+
avg_cpu=10.0, max_cpu=17.2,
14+
avg_ram=20.0, max_ram=25.4,
15+
run_time="2017-4-8 11:25:00"
16+
)
17+
self.jsample_repo.statistic = MagicMock(
18+
return_value=self.stat
19+
)
20+
21+
# Mock PredictionRepository
22+
self.pred_repo = PredictionRepository(None)
23+
24+
self.user = User(1000, "user1", "server1")
25+
26+
def test_predictUserLoad_UserExist(self):
27+
self.pred_repo.exist = MagicMock(
28+
return_value=True
29+
)
30+
31+
newuser = User(1000, "user1", "server2")
32+
33+
newstat = jSampleStatistic(
34+
avg_cpu=15.0, max_cpu=20.3,
35+
avg_ram=25.333, max_ram=30.1,
36+
run_time="2017-4-8 10:00:00"
37+
)
38+
pred = Prediction(newuser, newstat)
39+
40+
self.pred_repo.get = MagicMock(
41+
return_value=pred
42+
)
43+
self.pred_repo.update = MagicMock()
44+
self.jsample_repo.deleteUidsEarlierThan = MagicMock()
45+
46+
predictUserLoad(self.user, self.jsample_repo,
47+
self.pred_repo)
48+
49+
self.pred_repo.get.assert_called_once_with(1000)
50+
51+
self.assertEqual(pred.avg_cpu, 12.5)
52+
self.assertEqual(pred.max_cpu, 20.3)
53+
self.assertEqual(pred.avg_ram, 22.666)
54+
self.assertEqual(pred.max_ram, 30.1)
55+
self.assertEqual(pred.last_used_server, "server1")
56+
self.assertEqual(pred.last_login, "2017-4-8 11:25:00")
57+
58+
def test_predictUserLoad_UserNotExist(self):
59+
self.pred_repo.exist = MagicMock(
60+
return_value=False
61+
)
62+
self.pred_repo.add = MagicMock()
63+
64+
predictUserLoad(self.user, self.jsample_repo,
65+
self.pred_repo)
66+
67+
self.pred_repo.add.assert_called_once()
68+
69+
# pred_repo.add call with right args
70+
pred_called = self.pred_repo.add.call_args[0][0]
71+
pred = Prediction(self.user, self.stat)
72+
self.assertEqual(pred_called.uid, pred.uid)
73+
self.assertEqual(pred_called.username, pred.username)
74+
self.assertEqual(pred_called.last_used_server,
75+
pred.last_used_server)
76+
self.assertEqual(pred_called.last_login, pred.last_login)
77+
self.assertEqual(pred_called.avg_cpu, pred.avg_cpu)
78+
self.assertEqual(pred_called.max_cpu, pred.max_cpu)
79+
self.assertEqual(pred_called.avg_ram, pred.avg_ram)
80+
self.assertEqual(pred_called.max_ram, pred.max_ram)
81+
82+
83+
if __name__ == '__main__':
84+
unittest.main()

test_psutil.py

+26-26
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ def BytesToMB(bytes):
1010
# Black box testing
1111
class TestPsutil(unittest.TestCase):
1212
def test_cpu_count(self):
13-
assert(psutil.cpu_count() == 4)
13+
self.assertEqual(psutil.cpu_count(), 4)
1414

1515
def test_cpu_percent(self):
1616
percent = psutil.cpu_percent()
17-
assert(percent >= 0 and percent <= 400)
17+
self.assertTrue(percent >= 0 and percent <= 400)
1818

1919
def test_virtual_memory(self):
2020
total, available, percent, used, free, active, inactive, \
2121
buff, cache, shared = psutil.virtual_memory()
2222

23-
assert(percent >= 0 and percent <= 100)
23+
self.assertTrue(percent >= 0 and percent <= 100)
2424
available = BytesToMB(available)
2525
cache = BytesToMB(cache)
26-
assert(cache >= 100 and cache < 2048)
27-
assert(available >= 128)
28-
assert(available <= 4096)
26+
self.assertTrue(cache >= 100 and cache < 2048)
27+
self.assertTrue(available >= 128)
28+
self.assertTrue(available <= 4096)
2929

3030
def test_disk_io_counters(self):
3131
read_count, write_count, read_bytes, write_bytes, \
@@ -34,35 +34,35 @@ def test_disk_io_counters(self):
3434
= psutil.disk_io_counters()
3535
disk_in = BytesToMB(read_bytes)
3636
disk_out = BytesToMB(write_bytes)
37-
assert(disk_in < 10 * 1024)
38-
assert(disk_in > 128)
39-
assert(disk_out < 10 * 1024)
40-
assert(disk_out > 128)
37+
self.assertTrue(disk_in < 10 * 1024)
38+
self.assertTrue(disk_in > 128)
39+
self.assertTrue(disk_out < 10 * 1024)
40+
self.assertTrue(disk_out > 128)
4141

4242
def test_process_iter_and_uids(self):
4343
count = 0
4444
for proc in psutil.process_iter():
4545
count += 1
4646
real, effective, saved = proc.uids()
47-
assert(real >= 0 and real <= 0xffff)
48-
assert(effective >= 0 and effective <= 0xffff)
49-
assert(saved >= 0 and saved <= 0xffff)
50-
assert(count > 0)
47+
self.assertTrue(real >= 0 and real <= 0xffff)
48+
self.assertTrue(effective >= 0 and effective <= 0xffff)
49+
self.assertTrue(saved >= 0 and saved <= 0xffff)
50+
self.assertTrue(count > 0)
5151

5252
def test_psutil_process_functions(self):
5353
for proc in psutil.process_iter():
5454
real, effective, saved = proc.uids()
5555
if real < 1000:
5656
continue
57-
assert(proc.pid <= 0xffff)
58-
assert(proc.ppid() >= 0 and proc.ppid() <= 0xffff)
59-
assert(isinstance(proc.name(), str))
60-
assert(isinstance(proc.username(), str))
57+
self.assertTrue(proc.pid <= 0xffff)
58+
self.assertTrue(proc.ppid() >= 0 and proc.ppid() <= 0xffff)
59+
self.assertIsInstance(proc.name(), str)
60+
self.assertIsInstance(proc.username(), str)
6161
timestamp = datetime.datetime \
6262
.fromtimestamp(proc.create_time()) \
6363
.strftime("%Y-%m-%d %H:%M:%S")
64-
assert(isinstance(timestamp, str))
65-
assert(isinstance(proc.cmdline(), list))
64+
self.assertIsInstance(timestamp, str)
65+
self.assertIsInstance(proc.cmdline(), list)
6666

6767
rIO, wIO, rB, wB, read_chars, write_chars \
6868
= proc.io_counters()
@@ -76,12 +76,12 @@ def test_psutil_process_functions(self):
7676
rIO = BytesToMB(rIO)
7777
wIO = BytesToMB(wIO)
7878

79-
assert(rss <= 1024)
80-
assert(vms <= 1024)
81-
assert(cpu >= 0 and cpu <= 100)
82-
assert(ram >= 0 and ram <= 100)
83-
assert(rIO < 1024)
84-
assert(wIO < 1024)
79+
self.assertTrue(rss <= 1024)
80+
self.assertTrue(vms <= 1024)
81+
self.assertTrue(cpu >= 0 and cpu <= 100)
82+
self.assertTrue(ram >= 0 and ram <= 100)
83+
self.assertTrue(rIO < 1024)
84+
self.assertTrue(wIO < 1024)
8585
break
8686

8787

utils.pyc

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)