Statistics
| Branch: | Tag: | Revision:

root / qa / qa_job.py @ 07becab3

History | View | Annotate | Download (3 kB)

1
#
2
#
3

    
4
# Copyright (C) 2012, 2014 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Job-related QA tests.
23

24
"""
25

    
26
from ganeti import constants
27
from ganeti import query
28

    
29
import re
30

    
31
import qa_config
32
import qa_error
33
import qa_utils
34

    
35
from qa_utils import AssertCommand, GetCommandOutput
36

    
37

    
38
def TestJobList():
39
  """gnt-job list"""
40
  qa_utils.GenericQueryTest("gnt-job", query.JOB_FIELDS.keys(),
41
                            namefield="id", test_unknown=False)
42

    
43

    
44
def TestJobListFields():
45
  """gnt-node list-fields"""
46
  qa_utils.GenericQueryFieldsTest("gnt-job", query.JOB_FIELDS.keys())
47

    
48

    
49
def _GetJobStatuses():
50
  """ Invokes gnt-job list and extracts an id to status dictionary.
51

52
  @rtype: dict of string to string
53
  @return: A dictionary mapping job ids to matching statuses
54

55
  """
56
  master = qa_config.GetMasterNode()
57
  list_output = GetCommandOutput(
58
    master.primary, "gnt-job list --no-headers --output=id,status"
59
  )
60
  return dict(map(lambda s: s.split(), list_output.splitlines()))
61

    
62

    
63
def TestJobCancellation():
64
  """gnt-job cancel"""
65
  # The delay used for the first command should be large enough for the next
66
  # command and the cancellation command to complete before the first job is
67
  # done. The second delay should be small enough that not too much time is
68
  # spend waiting in the case of a failed cancel and a running command.
69
  FIRST_COMMAND_DELAY = 10.0
70
  AssertCommand(["gnt-debug", "delay", "--submit", str(FIRST_COMMAND_DELAY)])
71

    
72
  SECOND_COMMAND_DELAY = 1.0
73
  master = qa_config.GetMasterNode()
74

    
75
  # Forcing tty usage does not work on buildbot, so force all output of this
76
  # command to be redirected to stdout
77
  job_id_output = GetCommandOutput(
78
    master.primary, "gnt-debug delay --submit %s 2>&1" % SECOND_COMMAND_DELAY
79
  )
80

    
81
  possible_job_ids = re.findall("JobID: ([0-9]+)", job_id_output)
82
  if len(possible_job_ids) != 1:
83
    raise qa_error.Error("Cannot parse gnt-debug delay output to find job id")
84

    
85
  job_id = possible_job_ids[0]
86
  AssertCommand(["gnt-job", "cancel", job_id])
87

    
88
  # Now wait until the second job finishes, and expect the watch to fail due to
89
  # job cancellation
90
  AssertCommand(["gnt-job", "watch", job_id], fail=True)
91

    
92
  # Then check for job cancellation
93
  status_dict = _GetJobStatuses()
94
  if status_dict.get(job_id, None) != constants.JOB_STATUS_CANCELED:
95
    raise qa_error.Error("Job was not successfully cancelled!")