This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
testdata.py
143 lines (121 loc) · 5.17 KB
/
testdata.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
138
139
140
141
142
143
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
Put some fake data into a sqlite database. Useful for testing.
"""
# conn is the DB connection
import datetime
import math
import socket
from mozpool.db import model
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-d', '--devices', dest='devices', action='store', type='int',
default=1)
parser.add_option('-r', '--requests', dest='requests', action='store',
type='int', default=0)
parser.add_option('-p', '--port', dest='port', action='store', type='int',
default=80, help='mozpool server port')
parser.add_option('--relayport', dest='relayport', action='store', type='int',
default=2101, help='port for the local fake relay server')
(options, args) = parser.parse_args(args)
# max 8 relays each in max 4 banks per server
num_servers = int(math.ceil(options.devices / 32.0))
if num_servers > 1:
print 'More than the maximum number of devices per server (32) specified.'
print 'Extra devices will be configured with an unreachable server.'
img_svr_ids = []
fqdns = []
relay_server = 'localhost:%d' % options.relayport
for i in range(0, num_servers):
if i == 0:
fqdn = socket.getfqdn()
if options.port != 80:
fqdn += ':%d' % options.port
else:
fqdn = 'fakeserver%d.local' % i
fqdns.append(fqdn)
r = conn.execute(model.imaging_servers.insert(), fqdn=fqdn)
img_svr_ids.append(r.inserted_primary_key[0])
r = conn.execute(model.hardware_types.insert(),
type='panda', model='ES Rev B2')
hardware_type_id = r.inserted_primary_key[0]
r = conn.execute(model.pxe_configs.insert(),
name='fake-b2g',
description='fake b2g config',
contents='mobile-imaging-url=fake/b2g-second-stage.sh',
active=True)
b2g_pxe_config_id = r.inserted_primary_key[0]
r = conn.execute(model.pxe_configs.insert(),
name='fake-android',
description='fake android config',
contents='mobile-imaging-url=fake/android-second-stage.sh',
active=True)
android_pxe_config_id = r.inserted_primary_key[0]
# has_sut_agent is set to False until we get a fake SUT agent into the
# fake pandas.
r = conn.execute(model.images.insert(),
name='b2g',
boot_config_keys='["b2gbase"]',
can_reuse=False,
hidden=False,
has_sut_agent=False)
b2g_image_id = r.inserted_primary_key[0]
r = conn.execute(model.images.insert(),
name='android',
boot_config_keys='[]',
can_reuse=True,
hidden=False,
has_sut_agent=False)
android_image_id = r.inserted_primary_key[0]
r = conn.execute(model.images.insert(),
name='self-test',
boot_config_keys='[]',
can_reuse=False,
hidden=True,
has_sut_agent=False)
android_image_id = r.inserted_primary_key[0]
r = conn.execute(model.images.insert(),
name='maintenance',
boot_config_keys='[]',
can_reuse=False,
hidden=True,
has_sut_agent=False)
android_image_id = r.inserted_primary_key[0]
for image_id, pxe_config_id in ((b2g_image_id, b2g_pxe_config_id),
(android_image_id, android_pxe_config_id)):
conn.execute(model.image_pxe_configs.insert(),
image_id=image_id,
hardware_type_id=hardware_type_id,
pxe_config_id=pxe_config_id)
device_ids = []
for device_id in range(0, options.devices):
server_id, relay = divmod(device_id, 32)
bank, relay = [x + 1 for x in divmod(relay, 8)]
r = conn.execute(model.devices.insert(),
name='device%d' % (device_id + 1),
fqdn='device%d.fqdn' % (device_id + 1),
inventory_id=1111 * (device_id + 1),
state='off',
environment='odd' if (device_id % 2) else 'even',
state_counters='{}',
mac_address='%012x' % device_id,
imaging_server_id=img_svr_ids[server_id],
relay_info='%s:bank%d:relay%d' % (relay_server, bank,
relay),
boot_config='',
hardware_type_id=hardware_type_id)
device_ids.append(r.inserted_primary_key[0])
for request_id in range(1, options.requests+1):
conn.execute(model.requests.insert(),
requested_device='any',
assignee='slave%d' % request_id,
state='new',
state_counters='{}',
imaging_server_id=img_svr_ids[0],
image_id=image_id,
boot_config='{}',
expires=datetime.datetime.now() +
datetime.timedelta(seconds=12*60*60))