Merge pull request #59 from mpeuster/master
[osm/vim-emu.git] / src / emuvim / test / test_resourcemodel_api.py
1 import time
2 from emuvim.test.base import SimpleTestTopology
3 from emuvim.dcemulator.resourcemodel import BaseResourceModel, ResourceFlavor
4 from emuvim.dcemulator.resourcemodel.upb.simple import UpbSimpleCloudDcRM
5 from emuvim.dcemulator.resourcemodel import ResourceModelRegistrar
6
7
8 class testResourceModel(SimpleTestTopology):
9
10 def testBaseResourceModelApi(self):
11 r = BaseResourceModel()
12 # check if default flavors are there
13 assert(len(r._flavors) == 5)
14 # check addFlavor functionality
15 f = ResourceFlavor("test", {"testmetric": 42})
16 r.addFlavour(f)
17 assert("test" in r._flavors)
18 assert(r._flavors.get("test").get("testmetric") == 42)
19 # test if allocate and free runs through
20 assert(len(r.allocate("testc", "tiny")) == 3) # expected: 3tuple
21 assert(r.free("testc"))
22
23 def testAddRmToDc(self):
24 # create network
25 self.createNet(nswitches=0, ndatacenter=1, nhosts=2, ndockers=0)
26 # setup links
27 self.net.addLink(self.dc[0], self.h[0])
28 self.net.addLink(self.h[1], self.dc[0])
29 # add resource model
30 r = BaseResourceModel()
31 self.dc[0].assignResourceModel(r)
32 # start Mininet network
33 self.startNet()
34 # check number of running nodes
35 assert(len(self.getDockernetContainers()) == 0)
36 assert(len(self.net.hosts) == 2)
37 assert(len(self.net.switches) == 1)
38 # check resource model and resource model registrar
39 assert(self.dc[0]._resource_model is not None)
40 assert(len(self.net.rm_registrar.resource_models) == 1)
41
42 # check if alloc was called during startCompute
43 assert(len(r.allocated_compute_instances) == 0)
44 self.dc[0].startCompute("tc1")
45 time.sleep(1)
46 assert(len(r.allocated_compute_instances) == 1)
47 # check if free was called during stopCompute
48 self.dc[0].stopCompute("tc1")
49 assert(len(r.allocated_compute_instances) == 0)
50 # check connectivity by using ping
51 assert(self.net.ping([self.h[0], self.h[1]]) <= 0.0)
52 # stop Mininet network
53 self.stopNet()
54
55
56 class testUpbSimpleCloudDcRM(SimpleTestTopology):
57 """
58 Test the UpbSimpleCloudDc resource model.
59 """
60 def testAllocation(self):
61 # config
62 E_CPU = 1.0
63 MAX_CU = 100
64 # create dummy resource model environment
65 reg = ResourceModelRegistrar(dc_emulation_max_cpu=1.0)
66 rm = UpbSimpleCloudDcRM(max_cu=100, max_mu=100)
67 reg.register("test_dc", rm)
68
69 res = rm.allocate("c1", "tiny") # calculate allocation
70 assert(res[0] == E_CPU / MAX_CU * 1) # validate compute result
71 assert(res[1] < 0) # validate memory result
72 assert(res[2] < 0) # validate disk result
73
74 res = rm.allocate("c2", "small") # calculate allocation
75 assert(res[0] == E_CPU / MAX_CU * 4) # validate compute result
76 assert(res[1] < 0) # validate memory result
77 assert(res[2] < 0) # validate disk result
78
79 res = rm.allocate("c3", "medium") # calculate allocation
80 assert(res[0] == E_CPU / MAX_CU * 8) # validate compute result
81 assert(res[1] < 0) # validate memory result
82 assert(res[2] < 0) # validate disk result
83
84 res = rm.allocate("c4", "large") # calculate allocation
85 assert(res[0] == E_CPU / MAX_CU * 16) # validate compute result
86 assert(res[1] < 0) # validate memory result
87 assert(res[2] < 0) # validate disk result
88
89 res = rm.allocate("c5", "xlarge") # calculate allocation
90 assert(res[0] == E_CPU / MAX_CU * 32) # validate compute result
91 assert(res[1] < 0) # validate memory result
92 assert(res[2] < 0) # validate disk result
93
94 # test over provisioning exeption
95 exception = False
96 try:
97 rm.allocate("c6", "xlarge") # calculate allocation
98 rm.allocate("c7", "xlarge") # calculate allocation
99 rm.allocate("c8", "xlarge") # calculate allocation
100 rm.allocate("c9", "xlarge") # calculate allocation
101 except Exception as e:
102 assert("Not enough compute" in e.message)
103 exception = True
104 assert(exception)
105
106 def testFree(self):
107 # config
108 E_CPU = 1.0
109 MAX_CU = 100
110 # create dummy resource model environment
111 reg = ResourceModelRegistrar(dc_emulation_max_cpu=1.0)
112 rm = UpbSimpleCloudDcRM(max_cu=100, max_mu=100)
113 reg.register("test_dc", rm)
114 rm.allocate("c1", "tiny") # calculate allocation
115 assert(rm.dc_alloc_cu == 1)
116 rm.free("c1")
117 assert(rm.dc_alloc_cu == 0)
118
119
120