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

import factory
from synnefo.db import models
36
37
from random import choice
from string import letters, digits
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57


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


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


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


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


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


63
class FlavorFactory(factory.DjangoModelFactory):
64
65
66
67
68
69
70
71
72
    FACTORY_FOR = models.Flavor

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


73
class BackendFactory(factory.DjangoModelFactory):
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    FACTORY_FOR = models.Backend

    clustername = factory.Sequence(prefix_seq('cluster'))
    port = 5080
    username = factory.Sequence(prefix_seq('username'))
    password = factory.Sequence(prefix_seq('password'))
    drained = False
    offline = False

    mfree = 8192
    mtotal = 16384
    dfree = 132423
    dtotal = 14921932
    pinst_cnt = 2
    ctotal = 80

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

92
93
94
95
96
97
98
99
100

class DrainedBackend(BackendFactory):
    drained = True


class OfflineBackend(BackendFactory):
    offline = True


101
class VirtualMachineFactory(factory.DjangoModelFactory):
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    FACTORY_FOR = models.VirtualMachine

    name = factory.Sequence(prefix_seq('vm'))
    userid = factory.Sequence(user_seq())
    backend = factory.SubFactory(BackendFactory)
    imageid = '78491238479120243171243'
    flavor = factory.SubFactory(FlavorFactory)
    deleted = False
    suspended = False
    operstate = factory.Sequence(round_seq_first(FACTORY_FOR.OPER_STATES))


class DeletedVirtualMachine(VirtualMachineFactory):
    deleted = True


class ErrorVirtualMachine(VirtualMachineFactory):
    operstate = "ERROR"


class BuildVirtualMachine(VirtualMachineFactory):
    operstate = "BUILD"


class DestroyedVirtualMachine(VirtualMachineFactory):
    operstate = "DESTROYED"


class StartedVirtualMachine(VirtualMachineFactory):
    operstate = "STARTED"


class StopedVirtualMachine(VirtualMachineFactory):
    operstate = "STOPED"


138
class VirtualMachineMetadataFactory(factory.DjangoModelFactory):
139
140
141
142
143
144
145
    FACTORY_FOR = models.VirtualMachineMetadata

    meta_key = factory.Sequence(prefix_seq('key'))
    meta_value = factory.Sequence(prefix_seq('pass'))
    vm = factory.SubFactory(VirtualMachineFactory)


146
class NetworkFactory(factory.DjangoModelFactory):
147
148
149
150
151
152
153
154
155
156
157
158
159
    FACTORY_FOR = models.Network

    name = factory.Sequence(prefix_seq('network'))
    userid = factory.Sequence(user_seq())
    flavor = factory.Sequence(round_seq(models.Network.FLAVORS.keys()))
    mode = factory.LazyAttribute(lambda a:
                                 models.Network.FLAVORS[a.flavor]['mode'])
    link = factory.Sequence(prefix_seq('link'))
    mac_prefix = 'aa:00:0'
    tags = factory.LazyAttribute(lambda a:
                                 models.Network.FLAVORS[a.flavor]['tags'])
    public = False
    deleted = False
160
    state = "ACTIVE"
161
162
163
164
165
166


class DeletedNetwork(NetworkFactory):
    deleted = True


167
class BackendNetworkFactory(factory.DjangoModelFactory):
168
169
170
171
172
173
174
    FACTORY_FOR = models.BackendNetwork

    network = factory.SubFactory(NetworkFactory)
    backend = factory.SubFactory(BackendFactory)
    operstate = factory.Sequence(round_seq_first(FACTORY_FOR.OPER_STATES))


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

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


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


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


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


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


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


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

    network = factory.SubFactory(NetworkFactory)
226
227
    subnet = factory.SubFactory(IPv4SubnetFactory,
                                network=factory.SelfAttribute('..network'))
228
229
230
    address =\
        factory.LazyAttributeSequence(lambda self, n: self.subnet.cidr[:-4] +
                                      '{0}'.format(int(n) + 2))
231
232
233
234
235
236
237
238
239
240
241
242
    nic = factory.SubFactory(NetworkInterfaceFactory,
                             network=factory.SelfAttribute('..network'))


class IPv6AddressFactory(IPv4AddressFactory):
    FACTORY_FOR = models.IPAddress

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


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


Marios Kogias's avatar
Marios Kogias committed
251
252
253
254
255
256
class SecurityGroupFactory(factory.DjangoModelFactory):
    FACTORY_FOR = models.SecurityGroup

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


257
class BridgePoolTableFactory(factory.DjangoModelFactory):
258
259
260
261
262
263
    FACTORY_FOR = models.BridgePoolTable

    size = 20
    base = 'prv'


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


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