Statistics
| Branch: | Tag: | Revision:

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

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

    
37

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

    
41

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

    
45

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

    
50

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

    
55

    
56
class FlavorFactory(factory.DjangoModelFactory):
57
    FACTORY_FOR = models.Flavor
58

    
59
    cpu = factory.Sequence(lambda n: n + 2, type=int)
60
    ram = factory.Sequence(lambda n: n * 512, type=int)
61
    disk = factory.Sequence(lambda n: n * 10, type=int)
62
    disk_template = 'drbd'
63
    deleted = False
64

    
65

    
66
class BackendFactory(factory.DjangoModelFactory):
67
    FACTORY_FOR = models.Backend
68

    
69
    clustername = factory.Sequence(prefix_seq('cluster'))
70
    port = 5080
71
    username = factory.Sequence(prefix_seq('username'))
72
    password = factory.Sequence(prefix_seq('password'))
73
    drained = False
74
    offline = False
75

    
76
    mfree = 8192
77
    mtotal = 16384
78
    dfree = 132423
79
    dtotal = 14921932
80
    pinst_cnt = 2
81
    ctotal = 80
82

    
83

    
84
class DrainedBackend(BackendFactory):
85
    drained = True
86

    
87

    
88
class OfflineBackend(BackendFactory):
89
    offline = True
90

    
91

    
92
class VirtualMachineFactory(factory.DjangoModelFactory):
93
    FACTORY_FOR = models.VirtualMachine
94

    
95
    name = factory.Sequence(prefix_seq('vm'))
96
    userid = factory.Sequence(user_seq())
97
    backend = factory.SubFactory(BackendFactory)
98
    imageid = '78491238479120243171243'
99
    flavor = factory.SubFactory(FlavorFactory)
100
    deleted = False
101
    suspended = False
102
    operstate = factory.Sequence(round_seq_first(FACTORY_FOR.OPER_STATES))
103

    
104
class DeletedVirtualMachine(VirtualMachineFactory):
105
    deleted = True
106

    
107

    
108
class ErrorVirtualMachine(VirtualMachineFactory):
109
    operstate = "ERROR"
110

    
111

    
112
class BuildVirtualMachine(VirtualMachineFactory):
113
    operstate = "BUILD"
114

    
115

    
116
class DestroyedVirtualMachine(VirtualMachineFactory):
117
    operstate = "DESTROYED"
118

    
119

    
120
class StartedVirtualMachine(VirtualMachineFactory):
121
    operstate = "STARTED"
122

    
123

    
124
class StopedVirtualMachine(VirtualMachineFactory):
125
    operstate = "STOPED"
126

    
127

    
128
class VirtualMachineMetadataFactory(factory.DjangoModelFactory):
129
    FACTORY_FOR = models.VirtualMachineMetadata
130

    
131
    meta_key = factory.Sequence(prefix_seq('key'))
132
    meta_value = factory.Sequence(prefix_seq('pass'))
133
    vm = factory.SubFactory(VirtualMachineFactory)
134

    
135

    
136
class NetworkFactory(factory.DjangoModelFactory):
137
    FACTORY_FOR = models.Network
138

    
139
    name = factory.Sequence(prefix_seq('network'))
140
    userid = factory.Sequence(user_seq())
141
    subnet = factory.Sequence(lambda n: '192.168.{0}.0/24'.format(n))
142
    gateway = factory.LazyAttribute(lambda a: a.subnet[:-4] + '1')
143
    subnet6 = "2001:648:2ffc:1112::/64"
144
    dhcp = False
145
    flavor = factory.Sequence(round_seq(models.Network.FLAVORS.keys()))
146
    mode = factory.LazyAttribute(lambda a:
147
                                 models.Network.FLAVORS[a.flavor]['mode'])
148
    link = factory.Sequence(prefix_seq('link'))
149
    mac_prefix = 'aa:00:0'
150
    tags = factory.LazyAttribute(lambda a:
151
                                 models.Network.FLAVORS[a.flavor]['tags'])
152
    public = False
153
    deleted = False
154
    state = factory.Sequence(round_seq_first(models.Network.OPER_STATES))
155

    
156

    
157
class IPv6NetworkFactory(NetworkFactory):
158
    subnet = None
159
    gateway = None
160

    
161

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

    
165

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

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

    
173

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

    
177
    machine = factory.SubFactory(VirtualMachineFactory)
178
    network = factory.SubFactory(NetworkFactory)
179
    index = factory.Sequence(lambda x: x, type=int)
180
    mac = factory.Sequence(lambda n: 'aa:{0}{0}:{0}{0}:aa:{0}{0}:{0}{0}'
181
                           .format(hex(int(n) % 15)[2:3]))
182
    ipv4 = factory.LazyAttributeSequence(lambda a, n: a.network.subnet[:-4] +
183
                                         '{0}'.format(int(n) + 2))
184
    state = "ACTIVE"
185
    firewall_profile =\
186
        factory.Sequence(round_seq_first(FACTORY_FOR.FIREWALL_PROFILES))
187

    
188

    
189
class FloatingIPFactory(factory.DjangoModelFactory):
190
    FACTORY_FOR = models.FloatingIP
191

    
192
    machine = factory.SubFactory(VirtualMachineFactory)
193
    network = factory.SubFactory(NetworkFactory, public=False, deleted=False,
194
                                 floating_ip_pool=True)
195
    ipv4 = factory.LazyAttributeSequence(lambda a, n: a.network.subnet[:-4] +
196
                                         '{0}'.format(int(n) + 2))
197

    
198

    
199
class BridgePoolTableFactory(factory.DjangoModelFactory):
200
    FACTORY_FOR = models.BridgePoolTable
201

    
202
    size = 20
203
    base = 'prv'
204

    
205

    
206
class MacPrefixPoolTableFactory(factory.DjangoModelFactory):
207
    FACTORY_FOR = models.MacPrefixPoolTable
208
    size = 100
209
    base = 'aa:00:0'
210

    
211

    
212
class QuotaHolderSerialFactory(factory.DjangoModelFactory):
213
    FACTORY_FOR = models.QuotaHolderSerial
214
    serial = factory.Sequence(lambda x: x, type=int)