Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.8 kB)

1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import factory
35
from synnefo.db import models
36
from random import choice
37
from string import letters, digits
38

    
39

    
40
def prefix_seq(x):
41
    return lambda n: x + '-{0}'.format(n)
42

    
43

    
44
def user_seq():
45
    return lambda n: 'user-{0}.example.com'.format(n)
46

    
47

    
48
def round_seq(x):
49
    size = len(x)
50
    return lambda n: x[int(n) % size]
51

    
52

    
53
def round_seq_first(x):
54
    size = len(x)
55
    return lambda n: x[int(n) % size][0]
56

    
57

    
58
def random_string(x):
59
    """Returns a random string of length x"""
60
    return ''.join([choice(digits + letters) for i in range(x)])
61

    
62

    
63
class FlavorFactory(factory.DjangoModelFactory):
64
    FACTORY_FOR = models.Flavor
65

    
66
    cpu = factory.Sequence(lambda n: n + 2, type=int)
67
    ram = factory.Sequence(lambda n: n * 512, type=int)
68
    disk = factory.Sequence(lambda n: n * 10, type=int)
69
    disk_template = 'drbd'
70
    deleted = False
71

    
72

    
73
class BackendFactory(factory.DjangoModelFactory):
74
    FACTORY_FOR = models.Backend
75

    
76
    clustername = factory.Sequence(prefix_seq('cluster'))
77
    port = 5080
78
    username = factory.Sequence(prefix_seq('username'))
79
    password = factory.Sequence(prefix_seq('password'))
80
    drained = False
81
    offline = False
82

    
83
    mfree = 8192
84
    mtotal = 16384
85
    dfree = 132423
86
    dtotal = 14921932
87
    pinst_cnt = 2
88
    ctotal = 80
89

    
90
    disk_templates = ["file", "plain", "drbd"]
91

    
92

    
93
class DrainedBackend(BackendFactory):
94
    drained = True
95

    
96

    
97
class OfflineBackend(BackendFactory):
98
    offline = True
99

    
100

    
101
class VirtualMachineFactory(factory.DjangoModelFactory):
102
    FACTORY_FOR = models.VirtualMachine
103

    
104
    name = factory.Sequence(prefix_seq('vm'))
105
    userid = factory.Sequence(user_seq())
106
    backend = factory.SubFactory(BackendFactory)
107
    imageid = '78491238479120243171243'
108
    flavor = factory.SubFactory(FlavorFactory)
109
    deleted = False
110
    suspended = False
111
    #operstate = factory.Sequence(round_seq_first(FACTORY_FOR.OPER_STATES))
112
    operstate = "STARTED"
113

    
114

    
115
class DeletedVirtualMachine(VirtualMachineFactory):
116
    deleted = True
117

    
118

    
119
class ErrorVirtualMachine(VirtualMachineFactory):
120
    operstate = "ERROR"
121

    
122

    
123
class BuildVirtualMachine(VirtualMachineFactory):
124
    operstate = "BUILD"
125

    
126

    
127
class DestroyedVirtualMachine(VirtualMachineFactory):
128
    operstate = "DESTROYED"
129

    
130

    
131
class StartedVirtualMachine(VirtualMachineFactory):
132
    operstate = "STARTED"
133

    
134

    
135
class StopedVirtualMachine(VirtualMachineFactory):
136
    operstate = "STOPED"
137

    
138

    
139
class VirtualMachineMetadataFactory(factory.DjangoModelFactory):
140
    FACTORY_FOR = models.VirtualMachineMetadata
141

    
142
    meta_key = factory.Sequence(prefix_seq('key'))
143
    meta_value = factory.Sequence(prefix_seq('pass'))
144
    vm = factory.SubFactory(VirtualMachineFactory)
145

    
146

    
147
class NetworkFactory(factory.DjangoModelFactory):
148
    FACTORY_FOR = models.Network
149

    
150
    name = factory.Sequence(prefix_seq('network'))
151
    userid = factory.Sequence(user_seq())
152
    flavor = factory.Sequence(round_seq(models.Network.FLAVORS.keys()))
153
    mode = factory.LazyAttribute(lambda a:
154
                                 models.Network.FLAVORS[a.flavor]['mode'])
155
    link = factory.Sequence(prefix_seq('snf-link'))
156
    mac_prefix = 'aa:00:0'
157
    tags = factory.LazyAttribute(lambda a:
158
                                 models.Network.FLAVORS[a.flavor]['tags'])
159
    public = False
160
    deleted = False
161
    state = "ACTIVE"
162

    
163

    
164
class DeletedNetwork(NetworkFactory):
165
    deleted = True
166

    
167

    
168
class BackendNetworkFactory(factory.DjangoModelFactory):
169
    FACTORY_FOR = models.BackendNetwork
170

    
171
    network = factory.SubFactory(NetworkFactory, state="ACTIVE")
172
    backend = factory.SubFactory(BackendFactory)
173
    operstate = factory.Sequence(round_seq_first(FACTORY_FOR.OPER_STATES))
174

    
175

    
176
class NetworkInterfaceFactory(factory.DjangoModelFactory):
177
    FACTORY_FOR = models.NetworkInterface
178

    
179
    userid = factory.Sequence(user_seq())
180
    name = factory.LazyAttribute(lambda self: random_string(30))
181
    machine = factory.SubFactory(VirtualMachineFactory, operstate="STARTED")
182
    network = factory.SubFactory(NetworkFactory, state="ACTIVE")
183
    index = factory.Sequence(lambda x: x, type=int)
184
    mac = factory.Sequence(lambda n: 'aa:{0}{0}:{0}{0}:aa:{0}{0}:{0}{0}'
185
                           .format(hex(int(n) % 15)[2:3]))
186
    state = "ACTIVE"
187
    firewall_profile =\
188
        factory.Sequence(round_seq_first(FACTORY_FOR.FIREWALL_PROFILES))
189

    
190

    
191
class IPPoolTableFactory(factory.DjangoModelFactory):
192
    FACTORY_FOR = models.IPPoolTable
193

    
194

    
195
class SubnetFactory(factory.DjangoModelFactory):
196
    FACTORY_FOR = models.Subnet
197
    network = factory.SubFactory(NetworkFactory, state="ACTIVE")
198
    name = factory.LazyAttribute(lambda self: random_string(30))
199
    dhcp = True
200
    dns_nameservers = []
201
    host_routes = []
202
    userid = factory.LazyAttribute(lambda self: self.network.userid)
203
    public = factory.LazyAttribute(lambda self: self.network.public)
204

    
205

    
206
class IPv4SubnetFactory(SubnetFactory):
207
    ipversion = 4
208
    cidr = factory.Sequence(lambda n: '192.168.{0}.0/24'.format(n))
209
    gateway = factory.LazyAttribute(lambda a: a.cidr[:-4] + '1')
210
    pool = factory.RelatedFactory(IPPoolTableFactory, 'subnet', base=cidr,
211
                                  offset=2,
212
                                  size=253)
213

    
214

    
215
class IPv6SubnetFactory(SubnetFactory):
216
    ipversion = 6
217
    cidr = "2001:648:2ffc:1112::/64"
218
    gateway = None
219

    
220

    
221
class NetworkWithSubnetFactory(NetworkFactory):
222
    subnet = factory.RelatedFactory(IPv4SubnetFactory, 'network')
223
    subnet6 = factory.RelatedFactory(IPv6SubnetFactory, 'network')
224

    
225

    
226
class IPv4AddressFactory(factory.DjangoModelFactory):
227
    FACTORY_FOR = models.IPAddress
228

    
229
    userid = factory.Sequence(user_seq())
230
    network = factory.SubFactory(NetworkFactory, state="ACTIVE")
231
    subnet = factory.SubFactory(IPv4SubnetFactory,
232
                                network=factory.SelfAttribute('..network'))
233
    ipversion = 4
234
    address =\
235
        factory.LazyAttributeSequence(lambda self, n: self.subnet.cidr[:-4] +
236
                                      '{0}'.format(int(n) + 2))
237
    nic = factory.SubFactory(NetworkInterfaceFactory,
238
                             userid=factory.SelfAttribute('..userid'),
239
                             network=factory.SelfAttribute('..network'))
240

    
241

    
242
class IPv6AddressFactory(IPv4AddressFactory):
243
    FACTORY_FOR = models.IPAddress
244

    
245
    subnet = factory.SubFactory(IPv6SubnetFactory)
246
    network = factory.SubFactory(NetworkFactory, state="ACTIVE")
247
    address = "babe::"
248
    ipversion = 6
249
    nic = factory.SubFactory(NetworkInterfaceFactory,
250
                             network=factory.SelfAttribute('..network'))
251

    
252

    
253
class FloatingIPFactory(IPv4AddressFactory):
254
    network = factory.SubFactory(NetworkFactory, public=True,
255
                                 floating_ip_pool=True, state="ACTIVE")
256
    floating_ip = True
257

    
258

    
259
class SecurityGroupFactory(factory.DjangoModelFactory):
260
    FACTORY_FOR = models.SecurityGroup
261

    
262
    name = factory.LazyAttribute(lambda self: random_string(30))
263

    
264

    
265
class BridgePoolTableFactory(factory.DjangoModelFactory):
266
    FACTORY_FOR = models.BridgePoolTable
267

    
268
    size = 500
269
    base = 'snf-link-'
270

    
271

    
272
class MacPrefixPoolTableFactory(factory.DjangoModelFactory):
273
    FACTORY_FOR = models.MacPrefixPoolTable
274
    size = 500
275
    base = 'aa:00:0'
276

    
277

    
278
class QuotaHolderSerialFactory(factory.DjangoModelFactory):
279
    FACTORY_FOR = models.QuotaHolderSerial
280
    serial = factory.Sequence(lambda x: x, type=int)
281

    
282

    
283
class IPAddressLogFactory(factory.DjangoModelFactory):
284
    FACTORY_FOR = models.IPAddressLog
285
    address = "192.168.2.1"
286
    server_id = 1
287
    network_id = 1
288
    active = True