Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.3 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 ipaddr import IPNetwork
36
from synnefo.db import models
37
from random import choice
38
from string import letters, digits
39

    
40

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

    
44

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

    
48

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

    
53

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

    
58

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

    
63

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

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

    
73

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

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

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

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

    
93

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

    
97

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

    
101

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

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

    
115

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

    
119

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

    
123

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

    
127

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

    
131

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

    
135

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

    
139

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

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

    
147

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

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

    
164

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

    
168

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

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

    
176

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

    
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

    
203

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

    
212

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

    
218

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

    
223

    
224
class IPv4AddressFactory(factory.DjangoModelFactory):
225
    FACTORY_FOR = models.IPAddress
226

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

    
236

    
237
class IPv6AddressFactory(IPv4AddressFactory):
238
    FACTORY_FOR = models.IPAddress
239

    
240
    subnet = factory.SubFactory(IPv6SubnetFactory)
241
    network = factory.SubFactory(NetworkFactory, state="ACTIVE")
242
    address = "babe::"
243
    nic = factory.SubFactory(NetworkInterfaceFactory,
244
                             network=factory.SelfAttribute('..network'))
245

    
246

    
247
class FloatingIPFactory(IPv4AddressFactory):
248
    network = factory.SubFactory(NetworkFactory, public=True,
249
                                 floating_ip_pool=True, state="ACTIVE")
250
    floating_ip = True
251

    
252

    
253
class SecurityGroupFactory(factory.DjangoModelFactory):
254
    FACTORY_FOR = models.SecurityGroup
255

    
256
    name = factory.LazyAttribute(lambda self: random_string(30))
257

    
258

    
259
class BridgePoolTableFactory(factory.DjangoModelFactory):
260
    FACTORY_FOR = models.BridgePoolTable
261

    
262
    size = 20
263
    base = 'prv'
264

    
265

    
266
class MacPrefixPoolTableFactory(factory.DjangoModelFactory):
267
    FACTORY_FOR = models.MacPrefixPoolTable
268
    size = 100
269
    base = 'aa:00:0'
270

    
271

    
272
class QuotaHolderSerialFactory(factory.DjangoModelFactory):
273
    FACTORY_FOR = models.QuotaHolderSerial
274
    serial = factory.Sequence(lambda x: x, type=int)