Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / db / models_factory.py @ 435bb7fb

History | View | Annotate | Download (8.4 kB)

1 672db045 Christos Stavrakakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 672db045 Christos Stavrakakis
#
3 672db045 Christos Stavrakakis
# Redistribution and use in source and binary forms, with or
4 672db045 Christos Stavrakakis
# without modification, are permitted provided that the following
5 672db045 Christos Stavrakakis
# conditions are met:
6 672db045 Christos Stavrakakis
#
7 672db045 Christos Stavrakakis
#   1. Redistributions of source code must retain the above
8 672db045 Christos Stavrakakis
#      copyright notice, this list of conditions and the following
9 672db045 Christos Stavrakakis
#      disclaimer.
10 672db045 Christos Stavrakakis
#
11 672db045 Christos Stavrakakis
#   2. Redistributions in binary form must reproduce the above
12 672db045 Christos Stavrakakis
#      copyright notice, this list of conditions and the following
13 672db045 Christos Stavrakakis
#      disclaimer in the documentation and/or other materials
14 672db045 Christos Stavrakakis
#      provided with the distribution.
15 672db045 Christos Stavrakakis
#
16 672db045 Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 672db045 Christos Stavrakakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 672db045 Christos Stavrakakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 672db045 Christos Stavrakakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 672db045 Christos Stavrakakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 672db045 Christos Stavrakakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 672db045 Christos Stavrakakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 672db045 Christos Stavrakakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 672db045 Christos Stavrakakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 672db045 Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 672db045 Christos Stavrakakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 672db045 Christos Stavrakakis
# POSSIBILITY OF SUCH DAMAGE.
28 672db045 Christos Stavrakakis
#
29 672db045 Christos Stavrakakis
# The views and conclusions contained in the software and
30 672db045 Christos Stavrakakis
# documentation are those of the authors and should not be
31 672db045 Christos Stavrakakis
# interpreted as representing official policies, either expressed
32 672db045 Christos Stavrakakis
# or implied, of GRNET S.A.
33 672db045 Christos Stavrakakis
34 672db045 Christos Stavrakakis
import factory
35 672db045 Christos Stavrakakis
from synnefo.db import models
36 0247388f Christos Stavrakakis
from random import choice
37 0247388f Christos Stavrakakis
from string import letters, digits
38 672db045 Christos Stavrakakis
39 672db045 Christos Stavrakakis
40 672db045 Christos Stavrakakis
def prefix_seq(x):
41 672db045 Christos Stavrakakis
    return lambda n: x + '-{0}'.format(n)
42 672db045 Christos Stavrakakis
43 672db045 Christos Stavrakakis
44 672db045 Christos Stavrakakis
def user_seq():
45 672db045 Christos Stavrakakis
    return lambda n: 'user-{0}.example.com'.format(n)
46 672db045 Christos Stavrakakis
47 672db045 Christos Stavrakakis
48 672db045 Christos Stavrakakis
def round_seq(x):
49 672db045 Christos Stavrakakis
    size = len(x)
50 672db045 Christos Stavrakakis
    return lambda n: x[int(n) % size]
51 672db045 Christos Stavrakakis
52 672db045 Christos Stavrakakis
53 672db045 Christos Stavrakakis
def round_seq_first(x):
54 672db045 Christos Stavrakakis
    size = len(x)
55 672db045 Christos Stavrakakis
    return lambda n: x[int(n) % size][0]
56 672db045 Christos Stavrakakis
57 672db045 Christos Stavrakakis
58 0247388f Christos Stavrakakis
def random_string(x):
59 0247388f Christos Stavrakakis
    """Returns a random string of length x"""
60 0247388f Christos Stavrakakis
    return ''.join([choice(digits + letters) for i in range(x)])
61 0247388f Christos Stavrakakis
62 0247388f Christos Stavrakakis
63 d4325f42 Christos Stavrakakis
class FlavorFactory(factory.DjangoModelFactory):
64 672db045 Christos Stavrakakis
    FACTORY_FOR = models.Flavor
65 672db045 Christos Stavrakakis
66 672db045 Christos Stavrakakis
    cpu = factory.Sequence(lambda n: n + 2, type=int)
67 672db045 Christos Stavrakakis
    ram = factory.Sequence(lambda n: n * 512, type=int)
68 672db045 Christos Stavrakakis
    disk = factory.Sequence(lambda n: n * 10, type=int)
69 672db045 Christos Stavrakakis
    disk_template = 'drbd'
70 672db045 Christos Stavrakakis
    deleted = False
71 672db045 Christos Stavrakakis
72 672db045 Christos Stavrakakis
73 d4325f42 Christos Stavrakakis
class BackendFactory(factory.DjangoModelFactory):
74 672db045 Christos Stavrakakis
    FACTORY_FOR = models.Backend
75 672db045 Christos Stavrakakis
76 672db045 Christos Stavrakakis
    clustername = factory.Sequence(prefix_seq('cluster'))
77 672db045 Christos Stavrakakis
    port = 5080
78 672db045 Christos Stavrakakis
    username = factory.Sequence(prefix_seq('username'))
79 672db045 Christos Stavrakakis
    password = factory.Sequence(prefix_seq('password'))
80 672db045 Christos Stavrakakis
    drained = False
81 672db045 Christos Stavrakakis
    offline = False
82 672db045 Christos Stavrakakis
83 672db045 Christos Stavrakakis
    mfree = 8192
84 672db045 Christos Stavrakakis
    mtotal = 16384
85 672db045 Christos Stavrakakis
    dfree = 132423
86 672db045 Christos Stavrakakis
    dtotal = 14921932
87 672db045 Christos Stavrakakis
    pinst_cnt = 2
88 672db045 Christos Stavrakakis
    ctotal = 80
89 672db045 Christos Stavrakakis
90 c9976c84 Christos Stavrakakis
    disk_templates = ["file", "plain", "drbd"]
91 c9976c84 Christos Stavrakakis
92 672db045 Christos Stavrakakis
93 672db045 Christos Stavrakakis
class DrainedBackend(BackendFactory):
94 672db045 Christos Stavrakakis
    drained = True
95 672db045 Christos Stavrakakis
96 672db045 Christos Stavrakakis
97 672db045 Christos Stavrakakis
class OfflineBackend(BackendFactory):
98 672db045 Christos Stavrakakis
    offline = True
99 672db045 Christos Stavrakakis
100 672db045 Christos Stavrakakis
101 d4325f42 Christos Stavrakakis
class VirtualMachineFactory(factory.DjangoModelFactory):
102 672db045 Christos Stavrakakis
    FACTORY_FOR = models.VirtualMachine
103 672db045 Christos Stavrakakis
104 672db045 Christos Stavrakakis
    name = factory.Sequence(prefix_seq('vm'))
105 672db045 Christos Stavrakakis
    userid = factory.Sequence(user_seq())
106 672db045 Christos Stavrakakis
    backend = factory.SubFactory(BackendFactory)
107 672db045 Christos Stavrakakis
    imageid = '78491238479120243171243'
108 672db045 Christos Stavrakakis
    flavor = factory.SubFactory(FlavorFactory)
109 672db045 Christos Stavrakakis
    deleted = False
110 672db045 Christos Stavrakakis
    suspended = False
111 f32e8021 Christos Stavrakakis
    #operstate = factory.Sequence(round_seq_first(FACTORY_FOR.OPER_STATES))
112 f32e8021 Christos Stavrakakis
    operstate = "STARTED"
113 672db045 Christos Stavrakakis
114 672db045 Christos Stavrakakis
115 672db045 Christos Stavrakakis
class DeletedVirtualMachine(VirtualMachineFactory):
116 672db045 Christos Stavrakakis
    deleted = True
117 672db045 Christos Stavrakakis
118 672db045 Christos Stavrakakis
119 672db045 Christos Stavrakakis
class ErrorVirtualMachine(VirtualMachineFactory):
120 672db045 Christos Stavrakakis
    operstate = "ERROR"
121 672db045 Christos Stavrakakis
122 672db045 Christos Stavrakakis
123 672db045 Christos Stavrakakis
class BuildVirtualMachine(VirtualMachineFactory):
124 672db045 Christos Stavrakakis
    operstate = "BUILD"
125 672db045 Christos Stavrakakis
126 672db045 Christos Stavrakakis
127 672db045 Christos Stavrakakis
class DestroyedVirtualMachine(VirtualMachineFactory):
128 672db045 Christos Stavrakakis
    operstate = "DESTROYED"
129 672db045 Christos Stavrakakis
130 672db045 Christos Stavrakakis
131 672db045 Christos Stavrakakis
class StartedVirtualMachine(VirtualMachineFactory):
132 672db045 Christos Stavrakakis
    operstate = "STARTED"
133 672db045 Christos Stavrakakis
134 672db045 Christos Stavrakakis
135 672db045 Christos Stavrakakis
class StopedVirtualMachine(VirtualMachineFactory):
136 672db045 Christos Stavrakakis
    operstate = "STOPED"
137 672db045 Christos Stavrakakis
138 672db045 Christos Stavrakakis
139 d4325f42 Christos Stavrakakis
class VirtualMachineMetadataFactory(factory.DjangoModelFactory):
140 672db045 Christos Stavrakakis
    FACTORY_FOR = models.VirtualMachineMetadata
141 672db045 Christos Stavrakakis
142 672db045 Christos Stavrakakis
    meta_key = factory.Sequence(prefix_seq('key'))
143 672db045 Christos Stavrakakis
    meta_value = factory.Sequence(prefix_seq('pass'))
144 672db045 Christos Stavrakakis
    vm = factory.SubFactory(VirtualMachineFactory)
145 672db045 Christos Stavrakakis
146 672db045 Christos Stavrakakis
147 d4325f42 Christos Stavrakakis
class NetworkFactory(factory.DjangoModelFactory):
148 672db045 Christos Stavrakakis
    FACTORY_FOR = models.Network
149 672db045 Christos Stavrakakis
150 672db045 Christos Stavrakakis
    name = factory.Sequence(prefix_seq('network'))
151 672db045 Christos Stavrakakis
    userid = factory.Sequence(user_seq())
152 672db045 Christos Stavrakakis
    flavor = factory.Sequence(round_seq(models.Network.FLAVORS.keys()))
153 672db045 Christos Stavrakakis
    mode = factory.LazyAttribute(lambda a:
154 672db045 Christos Stavrakakis
                                 models.Network.FLAVORS[a.flavor]['mode'])
155 672db045 Christos Stavrakakis
    link = factory.Sequence(prefix_seq('link'))
156 672db045 Christos Stavrakakis
    mac_prefix = 'aa:00:0'
157 672db045 Christos Stavrakakis
    tags = factory.LazyAttribute(lambda a:
158 672db045 Christos Stavrakakis
                                 models.Network.FLAVORS[a.flavor]['tags'])
159 672db045 Christos Stavrakakis
    public = False
160 672db045 Christos Stavrakakis
    deleted = False
161 8764d304 Christos Stavrakakis
    state = "ACTIVE"
162 672db045 Christos Stavrakakis
163 672db045 Christos Stavrakakis
164 672db045 Christos Stavrakakis
class DeletedNetwork(NetworkFactory):
165 672db045 Christos Stavrakakis
    deleted = True
166 672db045 Christos Stavrakakis
167 672db045 Christos Stavrakakis
168 d4325f42 Christos Stavrakakis
class BackendNetworkFactory(factory.DjangoModelFactory):
169 672db045 Christos Stavrakakis
    FACTORY_FOR = models.BackendNetwork
170 672db045 Christos Stavrakakis
171 f32e8021 Christos Stavrakakis
    network = factory.SubFactory(NetworkFactory, state="ACTIVE")
172 672db045 Christos Stavrakakis
    backend = factory.SubFactory(BackendFactory)
173 672db045 Christos Stavrakakis
    operstate = factory.Sequence(round_seq_first(FACTORY_FOR.OPER_STATES))
174 672db045 Christos Stavrakakis
175 672db045 Christos Stavrakakis
176 d4325f42 Christos Stavrakakis
class NetworkInterfaceFactory(factory.DjangoModelFactory):
177 672db045 Christos Stavrakakis
    FACTORY_FOR = models.NetworkInterface
178 672db045 Christos Stavrakakis
179 09b76b7e Christos Stavrakakis
    userid = factory.Sequence(user_seq())
180 0247388f Christos Stavrakakis
    name = factory.LazyAttribute(lambda self: random_string(30))
181 f32e8021 Christos Stavrakakis
    machine = factory.SubFactory(VirtualMachineFactory, operstate="STARTED")
182 f32e8021 Christos Stavrakakis
    network = factory.SubFactory(NetworkFactory, state="ACTIVE")
183 672db045 Christos Stavrakakis
    index = factory.Sequence(lambda x: x, type=int)
184 5aeb4e93 Christos Stavrakakis
    mac = factory.Sequence(lambda n: 'aa:{0}{0}:{0}{0}:aa:{0}{0}:{0}{0}'
185 5aeb4e93 Christos Stavrakakis
                           .format(hex(int(n) % 15)[2:3]))
186 939d71dd Christos Stavrakakis
    state = "ACTIVE"
187 672db045 Christos Stavrakakis
    firewall_profile =\
188 672db045 Christos Stavrakakis
        factory.Sequence(round_seq_first(FACTORY_FOR.FIREWALL_PROFILES))
189 672db045 Christos Stavrakakis
190 672db045 Christos Stavrakakis
191 92d2d1ce Christos Stavrakakis
class IPPoolTableFactory(factory.DjangoModelFactory):
192 92d2d1ce Christos Stavrakakis
    FACTORY_FOR = models.IPPoolTable
193 92d2d1ce Christos Stavrakakis
194 92d2d1ce Christos Stavrakakis
195 8764d304 Christos Stavrakakis
class SubnetFactory(factory.DjangoModelFactory):
196 0247388f Christos Stavrakakis
    FACTORY_FOR = models.Subnet
197 92d2d1ce Christos Stavrakakis
    network = factory.SubFactory(NetworkFactory, state="ACTIVE")
198 0247388f Christos Stavrakakis
    name = factory.LazyAttribute(lambda self: random_string(30))
199 0247388f Christos Stavrakakis
    dhcp = True
200 0247388f Christos Stavrakakis
    dns_nameservers = []
201 0247388f Christos Stavrakakis
    host_routes = []
202 8764d304 Christos Stavrakakis
203 8764d304 Christos Stavrakakis
204 8764d304 Christos Stavrakakis
class IPv4SubnetFactory(SubnetFactory):
205 8764d304 Christos Stavrakakis
    ipversion = 4
206 8764d304 Christos Stavrakakis
    cidr = factory.Sequence(lambda n: '192.168.{0}.0/24'.format(n))
207 8c7c9813 Christos Stavrakakis
    gateway = factory.LazyAttribute(lambda a: a.cidr[:-4] + '1')
208 4445f97a Dionysis Grigoropoulos
    pool = factory.RelatedFactory(IPPoolTableFactory, 'subnet', base=cidr,
209 0711e5aa Christos Stavrakakis
                                  offset=2,
210 0711e5aa Christos Stavrakakis
                                  size=253)
211 0247388f Christos Stavrakakis
212 0247388f Christos Stavrakakis
213 8764d304 Christos Stavrakakis
class IPv6SubnetFactory(SubnetFactory):
214 0247388f Christos Stavrakakis
    ipversion = 6
215 0247388f Christos Stavrakakis
    cidr = "2001:648:2ffc:1112::/64"
216 0247388f Christos Stavrakakis
    gateway = None
217 0247388f Christos Stavrakakis
218 0247388f Christos Stavrakakis
219 8764d304 Christos Stavrakakis
class NetworkWithSubnetFactory(NetworkFactory):
220 8764d304 Christos Stavrakakis
    subnet = factory.RelatedFactory(IPv4SubnetFactory, 'network')
221 8764d304 Christos Stavrakakis
    subnet6 = factory.RelatedFactory(IPv6SubnetFactory, 'network')
222 8764d304 Christos Stavrakakis
223 8764d304 Christos Stavrakakis
224 0247388f Christos Stavrakakis
class IPv4AddressFactory(factory.DjangoModelFactory):
225 0247388f Christos Stavrakakis
    FACTORY_FOR = models.IPAddress
226 0247388f Christos Stavrakakis
227 99988465 Christos Stavrakakis
    userid = factory.Sequence(user_seq())
228 f32e8021 Christos Stavrakakis
    network = factory.SubFactory(NetworkFactory, state="ACTIVE")
229 8764d304 Christos Stavrakakis
    subnet = factory.SubFactory(IPv4SubnetFactory,
230 8764d304 Christos Stavrakakis
                                network=factory.SelfAttribute('..network'))
231 0247388f Christos Stavrakakis
    address =\
232 0247388f Christos Stavrakakis
        factory.LazyAttributeSequence(lambda self, n: self.subnet.cidr[:-4] +
233 0247388f Christos Stavrakakis
                                      '{0}'.format(int(n) + 2))
234 92d2d1ce Christos Stavrakakis
    nic = factory.SubFactory(NetworkInterfaceFactory,
235 09b76b7e Christos Stavrakakis
                             userid=factory.SelfAttribute('..userid'),
236 92d2d1ce Christos Stavrakakis
                             network=factory.SelfAttribute('..network'))
237 92d2d1ce Christos Stavrakakis
238 92d2d1ce Christos Stavrakakis
239 92d2d1ce Christos Stavrakakis
class IPv6AddressFactory(IPv4AddressFactory):
240 92d2d1ce Christos Stavrakakis
    FACTORY_FOR = models.IPAddress
241 92d2d1ce Christos Stavrakakis
242 92d2d1ce Christos Stavrakakis
    subnet = factory.SubFactory(IPv6SubnetFactory)
243 f32e8021 Christos Stavrakakis
    network = factory.SubFactory(NetworkFactory, state="ACTIVE")
244 92d2d1ce Christos Stavrakakis
    address = "babe::"
245 92d2d1ce Christos Stavrakakis
    nic = factory.SubFactory(NetworkInterfaceFactory,
246 92d2d1ce Christos Stavrakakis
                             network=factory.SelfAttribute('..network'))
247 0247388f Christos Stavrakakis
248 0247388f Christos Stavrakakis
249 0247388f Christos Stavrakakis
class FloatingIPFactory(IPv4AddressFactory):
250 08e6fe1b Christos Stavrakakis
    network = factory.SubFactory(NetworkFactory, public=True,
251 f32e8021 Christos Stavrakakis
                                 floating_ip_pool=True, state="ACTIVE")
252 0247388f Christos Stavrakakis
    floating_ip = True
253 c2876228 Christos Stavrakakis
254 c2876228 Christos Stavrakakis
255 593851e0 Buildbot
class SecurityGroupFactory(factory.DjangoModelFactory):
256 593851e0 Buildbot
    FACTORY_FOR = models.SecurityGroup
257 593851e0 Buildbot
258 593851e0 Buildbot
    name = factory.LazyAttribute(lambda self: random_string(30))
259 593851e0 Buildbot
260 593851e0 Buildbot
261 d4325f42 Christos Stavrakakis
class BridgePoolTableFactory(factory.DjangoModelFactory):
262 672db045 Christos Stavrakakis
    FACTORY_FOR = models.BridgePoolTable
263 672db045 Christos Stavrakakis
264 672db045 Christos Stavrakakis
    size = 20
265 672db045 Christos Stavrakakis
    base = 'prv'
266 672db045 Christos Stavrakakis
267 672db045 Christos Stavrakakis
268 d4325f42 Christos Stavrakakis
class MacPrefixPoolTableFactory(factory.DjangoModelFactory):
269 672db045 Christos Stavrakakis
    FACTORY_FOR = models.MacPrefixPoolTable
270 672db045 Christos Stavrakakis
    size = 100
271 672db045 Christos Stavrakakis
    base = 'aa:00:0'
272 d552dddd Christos Stavrakakis
273 d552dddd Christos Stavrakakis
274 d4325f42 Christos Stavrakakis
class QuotaHolderSerialFactory(factory.DjangoModelFactory):
275 d552dddd Christos Stavrakakis
    FACTORY_FOR = models.QuotaHolderSerial
276 41a7fae7 Christos Stavrakakis
    serial = factory.Sequence(lambda x: x, type=int)