Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.1 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

    
113

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

    
117

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

    
121

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

    
125

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

    
129

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

    
133

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

    
137

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

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

    
145

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

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

    
162

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

    
166

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

    
170
    network = factory.SubFactory(NetworkFactory)
171
    backend = factory.SubFactory(BackendFactory)
172
    operstate = factory.Sequence(round_seq_first(FACTORY_FOR.OPER_STATES))
173

    
174

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

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

    
188

    
189
class IPPoolTableFactory(factory.DjangoModelFactory):
190
    FACTORY_FOR = models.IPPoolTable
191
    size = 0
192

    
193

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

    
202

    
203
class IPv4SubnetFactory(SubnetFactory):
204
    ipversion = 4
205
    cidr = factory.Sequence(lambda n: '192.168.{0}.0/24'.format(n))
206
    gateway = factory.LazyAttribute(lambda a: a.cidr[:-4] + '1')
207
    pool = factory.RelatedFactory(IPPoolTableFactory, 'subnet')
208

    
209

    
210
class IPv6SubnetFactory(SubnetFactory):
211
    ipversion = 6
212
    cidr = "2001:648:2ffc:1112::/64"
213
    gateway = None
214

    
215

    
216
class NetworkWithSubnetFactory(NetworkFactory):
217
    subnet = factory.RelatedFactory(IPv4SubnetFactory, 'network')
218
    subnet6 = factory.RelatedFactory(IPv6SubnetFactory, 'network')
219

    
220

    
221
class IPv4AddressFactory(factory.DjangoModelFactory):
222
    FACTORY_FOR = models.IPAddress
223

    
224
    network = factory.SubFactory(NetworkFactory)
225
    subnet = factory.SubFactory(IPv4SubnetFactory,
226
                                network=factory.SelfAttribute('..network'))
227
    address =\
228
        factory.LazyAttributeSequence(lambda self, n: self.subnet.cidr[:-4] +
229
                                      '{0}'.format(int(n) + 2))
230
    nic = factory.SubFactory(NetworkInterfaceFactory,
231
                             network=factory.SelfAttribute('..network'))
232

    
233

    
234
class IPv6AddressFactory(IPv4AddressFactory):
235
    FACTORY_FOR = models.IPAddress
236

    
237
    subnet = factory.SubFactory(IPv6SubnetFactory)
238
    network = factory.SubFactory(NetworkFactory)
239
    address = "babe::"
240
    nic = factory.SubFactory(NetworkInterfaceFactory,
241
                             network=factory.SelfAttribute('..network'))
242

    
243

    
244
class FloatingIPFactory(IPv4AddressFactory):
245
    network = factory.SubFactory(NetworkFactory, public=False,
246
                                 floating_ip_pool=True)
247
    floating_ip = True
248

    
249

    
250
class SecurityGroupFactory(factory.DjangoModelFactory):
251
    FACTORY_FOR = models.SecurityGroup
252

    
253
    name = factory.LazyAttribute(lambda self: random_string(30))
254

    
255

    
256
class BridgePoolTableFactory(factory.DjangoModelFactory):
257
    FACTORY_FOR = models.BridgePoolTable
258

    
259
    size = 20
260
    base = 'prv'
261

    
262

    
263
class MacPrefixPoolTableFactory(factory.DjangoModelFactory):
264
    FACTORY_FOR = models.MacPrefixPoolTable
265
    size = 100
266
    base = 'aa:00:0'
267

    
268

    
269
class QuotaHolderSerialFactory(factory.DjangoModelFactory):
270
    FACTORY_FOR = models.QuotaHolderSerial
271
    serial = factory.Sequence(lambda x: x, type=int)