test_cray.py 15.1 KB
Newer Older
Paul Rich's avatar
Paul Rich committed
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
# Test Cray-specific utilities/calls.
from nose.tools import raises
from testsuite.TestCobalt.Utilities.assert_functions import assert_match, assert_not_match
from Cobalt.Components.system.CrayNode import CrayNode
import Cobalt.Exceptions
import time
from Cobalt.Components.system.CraySystem import CraySystem
from Cobalt.Components.system.base_pg_manager import ProcessGroupManager
import Cobalt.Components.system.AlpsBridge as AlpsBridge

from mock import MagicMock, Mock, patch


def is_match(a, b):
    return a is b

class TestCrayNode(object):

    def setup(self):
        self.spec = {'name':'test', 'state': 'UP', 'node_id': 1, 'role':'batch',
                'architecture': 'XT', 'SocketArray':['foo', 'bar'],
                }
        self.base_node = CrayNode(self.spec)

    def teardown(self):
        del self.spec
        del self.base_node

    def test_init(self):
30
        '''CrayNode init test'''
Paul Rich's avatar
Paul Rich committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        spec = {'name':'test', 'state': 'UP', 'node_id': 1, 'role':'batch',
                'architecture': 'XT', 'SocketArray':['foo', 'bar'],
                }
        node = CrayNode(spec)
        assert_match(node.status, 'idle', 'bad status')
        assert_match(node.node_id, 1, 'bad nodeid')
        assert_match(node.role, 'BATCH', 'bad role')
        assert_match(node.attributes['architecture'], 'XT',
                'bad architecture',  is_match)
        assert_match(node.segment_details, ['foo', 'bar'],
                'bad segment')
        assert_match(node.ALPS_status, 'UNKNOWN',
                'bad default ALPS status')
        assert 'alps-interactive' in node.RESOURCE_STATUSES, 'alps-interactive not in resource statuses'

    def test_init_alps_states(self):
47
        '''CrayNode: init alps states'''
Paul Rich's avatar
Paul Rich committed
48
49
50
51
52
53
54
55
56
57
58
59
60
        cray_state_list = ['UP', 'DOWN', 'UNAVAILABLE', 'ROUTING', 'SUSPECT',
                           'ADMIN', 'UNKNOWN', 'UNAVAIL', 'SWDOWN', 'REBOOTQ',
                           'ADMINDOWN']
        correct_alps_states = {'UP': 'idle', 'DOWN':'down', 'UNAVAILABLE':'down',
                               'ROUTING':'down', 'SUSPECT':'down', 'ADMIN':'down',
                               'UNKNOWN':'down', 'UNAVAIL': 'down', 'SWDOWN': 'down',
                               'REBOOTQ':'down', 'ADMINDOWN':'down'}
        for state in cray_state_list:
            self.spec['state'] = state
            node = CrayNode(self.spec)
            assert node.status == correct_alps_states[state], "%s should map to %s" % (node.status, correct_alps_states[state])

    def test_non_cray_statuses(self):
61
        '''CrayNode: can set cobalt-tracking statuses.'''
Paul Rich's avatar
Paul Rich committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        test_statuses = ['busy', 'cleanup-pending', 'allocated',
                'alps-interactive']
        for status in test_statuses:
            self.base_node.status = status
            assert_match(self.base_node.status, status, "failed validation")

class TestCraySystem(object):
    @patch.object(AlpsBridge, 'init_bridge')
    @patch.object(CraySystem, '_init_nodes_and_reservations', return_value=None)
    @patch.object(CraySystem, '_run_update_state', return_value=None)
    def setup(self, *args, **kwargs):
        self.system = CraySystem()
        self.base_spec = {'name':'test', 'state': 'UP', 'node_id': '1', 'role':'batch',
                'architecture': 'XT', 'SocketArray':['foo', 'bar'],
                'queues':['default'],
                }
        for i in range(1,6):
            self.base_spec['name'] = "test%s" % i
            self.base_spec['node_id'] = str(i)
            node_dict=dict(self.base_spec)
            self.system.nodes[str(i)] = CrayNode(node_dict)
            self.system.node_name_to_id[node_dict['name']] = node_dict['node_id']
84
85
        for node in self.system.nodes.values():
            node.managed = True
Paul Rich's avatar
Paul Rich committed
86
87
88
        self.system._gen_node_to_queue()

        self.base_job = {'jobid':1, 'user':'crusher', 'attrs':{},
89
                'queue':'default', 'nodes': 1,
Paul Rich's avatar
Paul Rich committed
90
91
92
93
94
95
96
                }

    def teardown(self):
        del self.system
        del self.base_job

    def test_assemble_queue_data(self):
97
        '''CraySystem._assemble_queue_data: base functionality'''
Paul Rich's avatar
Paul Rich committed
98
99
100
101
102
103
        nodecount, nodelist =  self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 5, 'expected 5 got %s' % nodecount
        assert sorted(nodelist) == ['1','2','3','4','5'], 'expected [1, 2, 3, 4, 5] got %s' % nodelist

    def test_assemble_queue_data_bad_queue(self):
104
        '''CraySystem._assemble_queue_data: return nothing if queue for job doesn't exist'''
Paul Rich's avatar
Paul Rich committed
105
106
107
108
109
110
111
        self.base_job['queue'] = 'foo'
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 0, 'Nonzero nodecount'
        assert nodelist == [], 'nonempty nodelist'

    def test_assemble_queue_data_multiple_queue(self):
112
        '''CraySystem._assemble_queue_data: return only proper queue nodes'''
Paul Rich's avatar
Paul Rich committed
113
114
115
116
117
118
119
120
121
        self.system.nodes['1'].queues = ['foo']
        self.system.nodes['4'].queues = ['bar']
        self.system._gen_node_to_queue()
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 3, 'Wrong nodecount'
        assert sorted(nodelist) == ['2','3','5'], 'Wrong nodelist'

    def test_assemble_queue_data_multiple_queue_overlap(self):
122
        '''CraySystem._assemble_queue_data: return only proper queue nodes in overlaping queues'''
Paul Rich's avatar
Paul Rich committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
        self.system.nodes['1'].queues = ['foo', 'default', 'bar']
        self.system.nodes['4'].queues = ['default','bar']
        self.system.nodes['5'].queues = ['baz']
        self.system._gen_node_to_queue()
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 4, 'Wrong nodecount'
        assert sorted(nodelist) == ['1','2','3','4'], 'Wrong nodelist'
        self.base_job['queue'] = 'foo'
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 1, 'Wrong nodecount'
        assert nodelist == ['1'], 'Wrong nodelist'
        self.base_job['queue'] = 'bar'
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 2, 'Wrong nodecount'
        assert sorted(nodelist) == ['1','4'], 'Wrong nodelist'
        self.base_job['queue'] = 'baz'
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 1, 'Wrong nodecount'
        assert nodelist == ['5'], 'Wrong nodelist'

    def test_assemble_queue_data_non_idle(self):
148
        '''CraySystem._assemble_queue_data: return only non-idle nodes'''
Paul Rich's avatar
Paul Rich committed
149
150
151
152
153
154
155
156
157
        self.system.nodes['1'].status = 'busy'
        self.system.nodes['4'].status = 'ADMINDOWN'
        self.system._gen_node_to_queue()
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 3, 'Wrong nodecount'
        assert sorted(nodelist) == ['2','3','5'], 'Wrong nodelist'

    def test_assemble_queue_data_attrs_location(self):
158
        '''CraySystem._assemble_queue_data: return only attr locaiton loc'''
Paul Rich's avatar
Paul Rich committed
159
160
161
162
163
164
        self.base_job['attrs'] = {'location':'3'}
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 1, 'Wrong nodecount'
        assert nodelist == ['3'], 'Wrong node in list %s' % nodelist

165
166
167
168
169
170
171
172
    def test_assemble_queue_data_attrs_location_repeats(self):
        '''CraySystem._assemble_queue_data: eliminate repeat location entries'''
        self.base_job['attrs'] = {'location':'1,1,2,3'}
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 3, 'Wrong nodecount got %s expected 3' % nodecount
        assert sorted(nodelist) == ['1', '2', '3'], 'Wrong node in list %s' % nodelist

173
174
    @raises(ValueError)
    def test_assemble_queue_data_attrs_bad_location(self):
175
        '''CraySystem._assemble_queue_data: raise error for location completely outside of
176
177
178
179
180
181
182
        queue'''
        self.base_job['attrs'] = {'location':'6'}
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 1, 'Wrong nodecount'
        assert nodelist == ['3'], 'Wrong node in list %s' % nodelist

Paul Rich's avatar
Paul Rich committed
183
    def test_assemble_queue_data_attrs_location_multi(self):
184
        '''CraySystem._assemble_queue_data: return only attr locaiton complex loc string'''
Paul Rich's avatar
Paul Rich committed
185
186
187
188
189
190
191
        self.base_job['attrs'] = {'location':'1-3,5'}
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 4, 'Wrong nodecount'
        assert sorted(nodelist) == ['1','2','3','5'], 'Wrong nodes in list %s' % nodelist

    def test_assemble_queue_data_forbidden_loc(self):
192
        '''CraySystem._assemble_queue_data: avoid reserved nodes'''
Paul Rich's avatar
Paul Rich committed
193
194
195
196
197
198
199
        self.base_job['forbidden'] = ['1-3','5']
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 1, 'Wrong nodecount %s' % nodecount
        assert sorted(nodelist) == ['4'], 'Wrong nodes in list %s' % nodelist

    def test_assemble_queue_data_forbidden_loc_attrs_loc(self):
200
        '''CraySystem._assemble_queue_data: avoid reserved nodes despite location being set'''
Paul Rich's avatar
Paul Rich committed
201
202
203
204
205
206
207
208
        self.base_job['forbidden'] = ['1-3']
        self.base_job['attrs'] = {'location':'1-4'}
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 1, 'Wrong nodecount %s' % nodecount
        assert sorted(nodelist) == ['4'], 'Wrong nodes in list %s' % nodelist

    def test_assemble_queue_data_forbidden_loc_attrs_loc_complete(self):
209
        '''CraySystem._assemble_queue_data: avoid reserved nodes block location if superset'''
Paul Rich's avatar
Paul Rich committed
210
211
212
213
214
215
216
217
        self.base_job['forbidden'] = ['1-3']
        self.base_job['attrs'] = {'location':'1-3'}
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 0, 'Wrong nodecount %s' % nodecount
        assert sorted(nodelist) == [], 'Wrong nodes in list %s' % nodelist

    def test_assemble_queue_data_forbidden_loc_attrs_loc_permit(self):
218
        '''CraySystem._assemble_queue_data: forbidden doesn't block everything'''
Paul Rich's avatar
Paul Rich committed
219
220
221
222
223
224
225
226
        self.base_job['forbidden'] = ['1-3']
        self.base_job['attrs'] = {'location':'4-5'}
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 2, 'Wrong nodecount %s' % nodecount
        assert sorted(nodelist) == ['4','5'], 'Wrong nodes in list %s' % nodelist

    def test_assemble_queue_data_reserved_loc(self):
227
        '''CraySystem._assemble_queue_data: return reservation nodes'''
Paul Rich's avatar
Paul Rich committed
228
229
230
231
232
233
234
235
        self.base_job['required'] = ['2-4']
        self.base_job['queue'] = 'reservation'
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 3, 'Wrong nodecount %s' % nodecount
        assert sorted(nodelist) == ['2','3','4'], 'Wrong nodes in list %s' % nodelist

    def test_assemble_queue_data_reserved_loc_idle_only(self):
236
        '''CraySystem._assemble_queue_data: return reservation nodes that are idle'''
Paul Rich's avatar
Paul Rich committed
237
238
239
240
241
242
243
244
245
246
247
248
        self.system.nodes['1'].status = 'busy'
        self.system.nodes['2'].status = 'cleanup-pending'
        self.system.nodes['3'].status = 'allocated'
        self.system.nodes['4'].status = 'ADMINDOWN'
        self.base_job['required'] = ['1-5']
        self.base_job['queue'] = 'reservation'
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 1, 'Wrong nodecount %s' % nodecount
        assert sorted(nodelist) == ['5'], 'Wrong nodes in list %s' % nodelist

    def test_assemble_queue_data_reserved_loc_location_set(self):
249
        '''CraySystem._assemble_queue_data: return reservation nodes for job with location set'''
Paul Rich's avatar
Paul Rich committed
250
251
252
253
254
255
256
        self.base_job['required'] = ['1-4']
        self.base_job['attrs'] = {'location':'1,2,4'}
        self.base_job['queue'] = 'reservation'
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 3, 'Wrong nodecount %s' % nodecount
        assert sorted(nodelist) == ['1','2','4'], 'Wrong nodes in list %s' % nodelist
257

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
    #need testcase with loc targeting down nodes.
    def test_assemble_queue_data_attrs_location_blocked_nodes(self):
        '''CraySystem._assemble_queue_data: return only idle locations'''
        self.system.nodes['1'].status = 'busy'
        self.system.nodes['2'].status = 'cleanup-pending'
        self.system.nodes['3'].status = 'allocated'
        self.system.nodes['4'].status = 'ADMINDOWN'
        self.base_job['attrs'] = {'location':'1-5'}
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 1, 'Wrong nodecount'
        assert nodelist == ['5'], 'Wrong node in list %s' % nodelist

    def test_assemble_queue_data_attrs_location_all_blocked_nodes(self):
        '''CraySystem._assemble_queue_data: return no locations if attrs location nodes are
        all non idle'''
        self.system.nodes['1'].status = 'busy'
        self.system.nodes['2'].status = 'cleanup-pending'
        self.system.nodes['3'].status = 'allocated'
        self.system.nodes['4'].status = 'ADMINDOWN'
        self.base_job['attrs'] = {'location':'1-4'}
        nodecount, nodelist = self.system._assemble_queue_data(self.base_job,
                self.system._idle_nodes_by_queue())
        assert nodecount == 0, 'Wrong nodecount'
        assert nodelist == [], 'Wrong node in list %s' % nodelist
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300

    def fake_reserve(self,job, new_time, node_id_list):
        if job['nodes'] < len(node_id_list):
            return node_id_list[:int(job['nodes'])]
        else:
            return []

    @patch.object(CraySystem, '_ALPS_reserve_resources', fake_reserve)
    @patch.object(time, 'time', return_value=500.000)
    def test_find_job_location_basic(self, *args, **kwargs):
        '''CraySystem.find_job_locaton: Assign basic job to nodes'''
        retval = self.system.find_job_location([self.base_job], [], [])
        assert retval == {1: ['1']}, 'bad loc: expected %s, got %s' % ({1: ['1']}, retval)
        assert self.system.pending_starts[1] == 1700.0, 'bad pending start: expected %s, got %s' % (1700.0, self.system.pending_starts[1])
        assert self.system.nodes['1'].reserved_jobid == 1, 'Node not reserved'
        assert self.system.nodes['1'].reserved_until == 800.0, 'reserved until expected 800.0, got %s' % self.system_nodes['1'].reserved_until