Statistics
| Branch: | Tag: | Revision:

root / qa / qa_job.py @ 66cb789f

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
import time
31

    
32
import qa_config
33
import qa_error
34
import qa_utils
35

    
36
from qa_utils import AssertCommand, GetCommandOutput
37

    
38

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

    
44

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

    
49

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

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

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

    
63

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

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

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

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

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

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

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