test_banning.py 19.5 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
36
37
38
39
40
41
42
#   Copyright notice:
#   Copyright CERN, 2014.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import json
import urllib
from datetime import datetime, timedelta

from fts3.model import BannedDN, BannedSE, Job, File
from fts3rest.lib.base import Session
from fts3rest.tests import TestController
from insert_job import insert_job


class TestBanning(TestController):
    """
    Tests for user and storage banning
    """

    def setUp(self):
        self.setup_gridsite_environment()

    def tearDown(self):
        TestController.tearDown(self)
        Session.query(BannedDN).delete()
        Session.query(BannedSE).delete()

    def test_ban_dn(self):
        """
        Just ban a DN and unban it, make sure changes go into the DB
        """
43
44
45
46
47
        canceled = self.app.post(
            url='/ban/dn',
            params={'user_dn': '/DC=cern/CN=someone', 'message': 'TEST BAN'},
            status=200
        ).json
48
49
50
51
52
        self.assertEqual(0, len(canceled))

        banned = Session.query(BannedDN).get('/DC=cern/CN=someone')
        self.assertNotEqual(None, banned)
        self.assertEqual(self.get_user_credentials().user_dn, banned.admin_dn)
53
        self.assertEqual('TEST BAN', banned.message)
54
55
56
57
58

        self.app.delete(url="/ban/dn?user_dn=%s" % urllib.quote('/DC=cern/CN=someone'), status=204)
        banned = Session.query(BannedDN).get('/DC=cern/CN=someone')
        self.assertEqual(None, banned)

59
60
61
62
    def test_list_banned_dns(self):
        """
        Ban a DN and make sure it is in the list
        """
63
64
65
66
67
        canceled = self.app.post(
            url='/ban/dn',
            params={'user_dn': '/DC=cern/CN=someone'},
            status=200
        ).json
68
69
        self.assertEqual(0, len(canceled))

70
        banned = self.app.get(url='/ban/dn', status=200).json
71
72
73
74
        self.assertIn('/DC=cern/CN=someone', [b['dn'] for b in banned])

        self.app.delete(url="/ban/dn?user_dn=%s" % urllib.quote('/DC=cern/CN=someone'), status=204)

75
        banned = self.app.get(url='/ban/dn', status=200).json
76
77
        self.assertNotIn('/DC=cern/CN=someone', [b['dn'] for b in banned])

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
    def test_ban_dn_submission(self):
        """
        If a DN is banned, submissions from this user must not be accepted
        """
        banned = BannedDN()
        banned.dn = self.get_user_credentials().user_dn
        Session.merge(banned)
        Session.commit()

        self.push_delegation()
        self.app.post(url="/jobs", content_type='application/json', params='[]', status=403)

    def test_ban_self(self):
        """
        A user can not ban (him|her)self
        """
        user_dn = self.get_user_credentials().user_dn
        self.app.post(url='/ban/dn', params={'user_dn': user_dn}, status=409)

    def test_ban_dn_cancel(self):
        """
        Ban a DN that has transfers running, make sure they are canceled
        """
        jobs = list()
        jobs.append(
Andrea Manzi's avatar
Andrea Manzi committed
103
            insert_job('testvo', 'gsiftp://source', 'gsiftp://destination', 'SUBMITTED', user_dn='/DC=cern/CN=someone')
104
105
        )
        jobs.append(
Andrea Manzi's avatar
Andrea Manzi committed
106
            insert_job('testvo', 'gsiftp://source', 'gsiftp://destination2', 'ACTIVE', user_dn='/DC=cern/CN=someone')
107
108
        )
        jobs.append(
Andrea Manzi's avatar
Andrea Manzi committed
109
            insert_job('testvo', 'gsiftp://source', 'gsiftp://destination2', 'FAILED', duration=10, queued=20,
110
111
112
                       user_dn='/DC=cern/CN=someone')
        )

113
114
115
116
117
        canceled_ids = self.app.post(
            url="/ban/dn",
            params={'user_dn': '/DC=cern/CN=someone'},
            status=200
        ).json
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
144

        self.assertEqual(2, len(canceled_ids))
        self.assertIn(jobs[0], canceled_ids)
        self.assertIn(jobs[1], canceled_ids)
        self.assertNotIn(jobs[2], canceled_ids)

        for job_id in jobs[0:2]:
            job = Session.query(Job).get(job_id)
            files = Session.query(File).filter(File.job_id == job_id)
            self.assertEqual('CANCELED', job.job_state)
            self.assertNotEqual(None, job.job_finished)
            self.assertEqual('User banned', job.reason)
            for f in files:
                self.assertEqual('CANCELED', f.file_state)
                self.assertNotEqual(None, f.finish_time)
                self.assertEqual('User banned', f.reason)

        job = Session.query(Job).get(jobs[2])
        self.assertEqual(job.job_state, 'FAILED')
        files = Session.query(File).filter(File.job_id == job.job_id)
        for f in files:
            self.assertEqual('FAILED', f.file_state)

    def test_ban_se(self):
        """
        Just ban a SE and unban it, make sure changes go into the DB
        """
145
146
147
148
149
        canceled = self.app.post(
            url="/ban/se",
            params={'storage': 'gsiftp://nowhere', 'message': 'TEST BAN 42'},
            status=200
        ).json
150
        self.assertEqual(0, len(canceled))
Andrea Manzi's avatar
Andrea Manzi committed
151
        banned = Session.query(BannedSE).filter(BannedSE.se=='gsiftp://nowhere').first()
152
153
154
        self.assertNotEqual(None, banned)
        self.assertEqual(self.get_user_credentials().user_dn, banned.admin_dn)
        self.assertEqual('CANCEL', banned.status)
155
        self.assertEqual('TEST BAN 42', banned.message)
156
        self.app.delete(url="/ban/se?storage=%s" % urllib.quote('gsiftp://nowhere'), status=204)
Andrea Manzi's avatar
Andrea Manzi committed
157
        banned = Session.query(BannedSE).filter(BannedSE.se=='gsiftp://nowhere').first()
158
159
        self.assertEqual(None, banned)

160
161
162
163
    def test_list_banned_ses(self):
        """
        Ban a SE and make sure it is in the list
        """
164
165
166
167
168
        canceled = self.app.post(
            url='/ban/se',
            params={'storage': 'gsiftp://nowhere'},
            status=200
        ).json
169
170
        self.assertEqual(0, len(canceled))

171
        banned = self.app.get(url='/ban/se', status=200).json
172
173
174
175
        self.assertIn('gsiftp://nowhere', [b['se'] for b in banned])

        self.app.delete(url="/ban/se?storage=%s" % urllib.quote('gsiftp://nowhere'), status=204)

176
        banned = self.app.get(url='/ban/se', status=200).json
177
178
        self.assertNotIn('gsiftp://nowhere', [b['se'] for b in banned])

179
180
181
182
    def test_ban_se_vo(self):
        """
        Just ban a SE and unban it, specifying a VO
        """
183
184
        canceled = self.app.post(
            url="/ban/se",
Andrea Manzi's avatar
Andrea Manzi committed
185
            params={'storage': 'gsiftp://nowhere', 'vo_name': 'testvo'},
186
187
            status=200
        ).json
188
189
        self.assertEqual(0, len(canceled))

Andrea Manzi's avatar
Andrea Manzi committed
190
        banned = Session.query(BannedSE).get(('gsiftp://nowhere', 'testvo'))
191
192
193
        self.assertNotEqual(None, banned)
        self.assertEqual(self.get_user_credentials().user_dn, banned.admin_dn)
        self.assertEqual('CANCEL', banned.status)
Andrea Manzi's avatar
Andrea Manzi committed
194
        self.assertEqual('testvo', banned.vo)
195

Andrea Manzi's avatar
Andrea Manzi committed
196
197
        self.app.delete(url="/ban/se?storage=%s&vo_name=testvo" % urllib.quote('gsiftp://nowhere'), status=204)
        banned = Session.query(BannedSE).get(('gsiftp://nowhere', 'someone'))
198
199
200
201
202
203
204
        self.assertEqual(None, banned)

    def test_ban_se_cancel(self):
        """
        Ban a SE that has files queued, make sure they are canceled
        """
        jobs = list()
Andrea Manzi's avatar
Andrea Manzi committed
205
206
207
        jobs.append(insert_job('testvo', 'gsiftp://source', 'gsiftp://destination', 'SUBMITTED'))
        jobs.append(insert_job('testvo', 'gsiftp://source', 'gsiftp://destination2', 'ACTIVE'))
        jobs.append(insert_job('testvo', 'gsiftp://source', 'gsiftp://destination2', 'FAILED', duration=10, queued=20))
208

209
210
211
212
213
        canceled_ids = self.app.post(
            url="/ban/se",
            params={'storage': 'gsiftp://source'},
            status=200
        ).json
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

        self.assertEqual(2, len(canceled_ids))
        self.assertIn(jobs[0], canceled_ids)
        self.assertIn(jobs[1], canceled_ids)
        self.assertNotIn(jobs[2], canceled_ids)

        for job_id in jobs[0:2]:
            job = Session.query(Job).get(job_id)
            files = Session.query(File).filter(File.job_id == job_id)
            self.assertEqual('CANCELED', job.job_state)
            self.assertNotEqual(None, job.job_finished)
            for f in files:
                self.assertEqual('CANCELED', f.file_state)
                self.assertNotEqual(None, f.finish_time)
                self.assertEqual('Storage banned', f.reason)

        job = Session.query(Job).get(jobs[2])
        self.assertEqual(job.job_state, 'FAILED')
        files = Session.query(File).filter(File.job_id == job.job_id)
        for f in files:
            self.assertEqual('FAILED', f.file_state)

    def test_ban_se_partial_job(self):
        """
        Ban a SE that has files queued. If a job has other pairs, the job must remain!
        """
        job_id = insert_job(
Andrea Manzi's avatar
Andrea Manzi committed
241
            'testvo',
242
243
            multiple=[('gsiftp://source', 'gsiftp://destination'), ('gsiftp://other', 'gsiftp://destination')]
        )
244
245
246
247
248
        canceled_ids = self.app.post(
            url="/ban/se",
            params={'storage': 'gsiftp://source'},
            status=200
        ).json
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269

        self.assertEqual(1, len(canceled_ids))
        self.assertEqual(job_id, canceled_ids[0])

        job = Session.query(Job).get(job_id)
        self.assertEqual('SUBMITTED', job.job_state)
        self.assertEqual(None, job.job_finished)

        files = Session.query(File).filter(File.job_id == job_id)
        for f in files:
            if f.source_se == 'gsiftp://source':
                self.assertEqual('CANCELED', f.file_state)
                self.assertNotEqual(None, f.finish_time)
            else:
                self.assertEqual('SUBMITTED', f.file_state)

    def test_ban_se_cancel_vo(self):
        """
        Cancel a SE that has files queued, make sure they are canceled (with VO)
        """
        jobs = list()
Andrea Manzi's avatar
Andrea Manzi committed
270
        jobs.append(insert_job('testvo', 'gsiftp://source', 'gsiftp://destination', 'SUBMITTED'))
271
272
273
        jobs.append(insert_job('atlas', 'gsiftp://source', 'gsiftp://destination', 'SUBMITTED'))
        jobs.append(insert_job('atlas', 'gsiftp://source', 'gsiftp://destination2', 'SUBMITTED'))

274
        canceled_ids = self.app.post(
275
            url="/ban/se",
Andrea Manzi's avatar
Andrea Manzi committed
276
            params={'storage': 'gsiftp://source', 'status': 'cancel', 'vo_name': 'testvo'},
277
            status=200
278
        ).json
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301

        self.assertEqual(1, len(canceled_ids))
        self.assertIn(jobs[0], canceled_ids)

        for job_id in jobs:
            job = Session.query(Job).get(job_id)
            files = Session.query(File).filter(File.job_id == job_id)

            if job_id in canceled_ids:
                self.assertEqual('CANCELED', job.job_state)
            else:
                self.assertEqual('SUBMITTED', job.job_state)
            for f in files:
                if job_id in canceled_ids:
                    self.assertEqual('CANCELED', f.file_state)
                else:
                    self.assertEqual('SUBMITTED', f.file_state)

    def test_ban_se_wait(self):
        """
        Ban a SE, but instead of canceling, give jobs some time to finish
        """
        jobs = list()
Andrea Manzi's avatar
Andrea Manzi committed
302
303
304
        jobs.append(insert_job('testvo', 'gsiftp://source', 'gsiftp://destination', 'SUBMITTED'))
        jobs.append(insert_job('testvo', 'gsiftp://source', 'gsiftp://destination2', 'ACTIVE'))
        jobs.append(insert_job('testvo', 'gsiftp://source', 'gsiftp://destination2', 'FAILED', duration=10, queued=20))
305

306
307
308
309
310
        waiting_ids = self.app.post(
            url="/ban/se",
            params={'storage': 'gsiftp://source', 'status': 'wait', 'timeout': 1234},
            status=200
        ).json
311

312
        self.assertEqual(1, len(waiting_ids))
313
        self.assertIn(jobs[0], waiting_ids)
314
        self.assertNotIn(jobs[1], waiting_ids)
315
316
317
318
319
320
321
322
        self.assertNotIn(jobs[2], waiting_ids)

        for job_id in jobs[0:2]:
            job = Session.query(Job).get(job_id)
            files = Session.query(File).filter(File.job_id == job_id)
            self.assertIn(job.job_state, ['ACTIVE', 'SUBMITTED'])
            self.assertEqual(None, job.job_finished)
            for f in files:
323
                self.assertIn(f.file_state, ['ACTIVE', 'ON_HOLD'])
324
325
326
327
328
329
330
331
                self.assertEqual(None, f.finish_time)

        job = Session.query(Job).get(jobs[2])
        self.assertEqual(job.job_state, 'FAILED')
        files = Session.query(File).filter(File.job_id == job.job_id)
        for f in files:
            self.assertEqual('FAILED', f.file_state)

Andrea Manzi's avatar
Andrea Manzi committed
332
        banned = Session.query(BannedSE).get(('gsiftp://source', 'testvo'))
333
334
335
336
337
338
339
        self.assertEqual('WAIT', banned.status)

    def test_ban_se_wait_vo(self):
        """
        Ban a SE, but instead of canceling, give jobs some time to finish (with VO)
        """
        jobs = list()
Andrea Manzi's avatar
Andrea Manzi committed
340
        jobs.append(insert_job('testvo', 'gsiftp://source', 'gsiftp://destination', 'SUBMITTED'))
341
342
343
        jobs.append(insert_job('atlas', 'gsiftp://source', 'gsiftp://destination', 'SUBMITTED'))
        jobs.append(insert_job('atlas', 'gsiftp://source', 'gsiftp://destination2', 'SUBMITTED'))

344
        waiting_ids = self.app.post(
345
            url="/ban/se",
Andrea Manzi's avatar
Andrea Manzi committed
346
            params={'storage': 'gsiftp://source', 'status': 'wait', 'vo_name': 'testvo', 'timeout': 33},
347
            status=200
348
        ).json
349
350
351
352
353
354
355
356
357
358
359

        self.assertEqual(1, len(waiting_ids))
        self.assertIn(jobs[0], waiting_ids)

        for job_id in jobs:
            job = Session.query(Job).get(job_id)
            files = Session.query(File).filter(File.job_id == job_id)

            self.assertEqual('SUBMITTED', job.job_state)
            for f in files:
                if job_id in waiting_ids:
360
                    self.assertEqual('ON_HOLD', f.file_state)
361
                else:
362
                    self.assertEqual('SUBMITTED', f.file_state)
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405

    def test_ban_se_no_submit(self):
        """
        Ban a SE. Submissions to/from se must not be accepted
        """
        self.push_delegation()

        self.app.post(url="/ban/se", params={'storage': 'gsiftp://source'}, status=200)

        job = {
            'files': [{
                'sources': ['gsiftp://source/path/'],
                'destinations': ['gsiftp://destination/file'],
            }]
        }
        self.app.post(url="/jobs", content_type='application/json', params=json.dumps(job), status=403)

        # The other way around
        job = {
            'files': [{
                'sources': ['gsiftp://destination/file'],
                'destinations': ['gsiftp://source/path/']
            }]
        }
        self.app.post(url="/jobs", content_type='application/json', params=json.dumps(job), status=403)

    def test_ban_se_with_submission(self):
        """
        Ban a SE but allowing submissions
        """
        self.push_delegation()

        self.app.post(
            url="/ban/se", params={'storage': 'gsiftp://source', 'status': 'wait', 'allow_submit': True},
            status=200
        )

        job = {
            'files': [{
                'sources': ['gsiftp://source/path/'],
                'destinations': ['gsiftp://destination/file'],
            }]
        }
406
407
408
409
410
411
        job_id = self.app.post(
            url="/jobs",
            content_type='application/json',
            params=json.dumps(job),
            status=200
        ).json['job_id']
412
413
414

        files = Session.query(File).filter(File.job_id == job_id)
        for f in files:
415
            self.assertEqual('ON_HOLD', f.file_state)
416
417
418
419
420
421
422
423

        # The other way around
        job = {
            'files': [{
                'sources': ['gsiftp://destination/file'],
                'destinations': ['gsiftp://source/path/']
            }]
        }
424
425
426
427
428
429
        job_id = self.app.post(
            url="/jobs",
            content_type='application/json',
            params=json.dumps(job),
            status=200
        ).json['job_id']
430
431
432

        files = Session.query(File).filter(File.job_id == job_id)
        for f in files:
433
            self.assertEqual('ON_HOLD', f.file_state)
434

435
436
437
438
439
    def test_unban_wait(self):
        """
        Regression for FTS-297
        When unbanning a storage, if any file was left on wait, they must re-enter the queue
        """
Andrea Manzi's avatar
Andrea Manzi committed
440
        job_id = insert_job('testvo', 'gsiftp://source', 'gsiftp://destination', 'SUBMITTED', user_dn='/DC=cern/CN=someone')
441
442
443
444
445
446
447
        self.app.post(
            url="/ban/se", params={'storage': 'gsiftp://source', 'status': 'wait', 'allow_submit': True},
            status=200
        )

        files = Session.query(File).filter(File.job_id == job_id)
        for f in files:
448
            self.assertEqual('ON_HOLD', f.file_state)
449
450
451
452
453

        self.app.delete(url="/ban/se?storage=%s" % urllib.quote('gsiftp://source'), status=204)

        files = Session.query(File).filter(File.job_id == job_id)
        for f in files:
454
            self.assertEqual('SUBMITTED', f.file_state)
455

456
457
458
459
460
    # Some requests that must be rejected
    def test_ban_dn_empty(self):
        """
        Banning with a missing dn must fail
        """
461
        self.app.post_json(url="/ban/dn", params={}, status=400)
462
463
464
465
466
467
468
469
470
471
472

    def test_unban_dn_empty(self):
        """
        Unbanning with a missing dn must fail
        """
        self.app.delete(url="/ban/dn", status=400)

    def test_ban_se_empty(self):
        """
        Ask for banning with a missing storage must fail
        """
473
        self.app.post_json(url="/ban/se", params={}, status=400)
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497

    def test_unban_se_empty(self):
        """
        Unbanning with a missing se must fail
        """
        self.app.delete(url="/ban/se", status=400)

    def test_ban_se_cancel_and_submit(self):
        """
        Setting status = cancel and ask for allow_submit must fail
        """
        self.app.post(
            url="/ban/se", params={'storage': 'gsiftp://source', 'status': 'cancel', 'allow_submit': True},
            status=400
        )

    def test_ban_se_bad_status(self):
        """
        Unbanning with something else than cancel or wait must fail
        """
        self.app.post(
            url="/ban/se", params={'storage': 'gsiftp://source', 'status': 'blahblah'},
            status=400
        )
498
499
500
501
502
503
504
505

    def test_ban_se_staging(self):
        """
        Ban a storage with transfers queued as STAGING, submit a new STAGING, unban.
        Final state must be STAGING
        """
        self.push_delegation()

Andrea Manzi's avatar
Andrea Manzi committed
506
        pre_job_id = insert_job('testvo', 'srm://source', 'srm://destination', 'STAGING', user_dn='/DC=cern/CN=someone')
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
        self.app.post(
            url="/ban/se", params={'storage': 'srm://source', 'status': 'wait', 'allow_submit': True},
            status=200
        )

        files = Session.query(File).filter(File.job_id == pre_job_id)
        for f in files:
            self.assertEqual('ON_HOLD_STAGING', f.file_state)

        job = {
            'files': [{
                'sources': ['srm://source/file'],
                'destinations': ['gsiftp://destination2/path/']
            }],
            'params': {
                'copy_pin_lifetime': 1234
            }
        }
        post_job_id = self.app.post(
            url="/jobs",
            content_type='application/json',
            params=json.dumps(job),
            status=200
        ).json['job_id']

        files = Session.query(File).filter(File.job_id == post_job_id)
        for f in files:
            self.assertEqual('ON_HOLD_STAGING', f.file_state)

        self.app.delete(url="/ban/se?storage=%s" % urllib.quote('srm://source'), status=204)

        files = Session.query(File).filter(File.job_id.in_((pre_job_id, post_job_id)))
        for f in files:
            self.assertEqual('STAGING', f.file_state)