aboutsummaryrefslogtreecommitdiffstats
path: root/tests/collect_tests/help_output_tests.py
blob: 88f6d212ef1cd248f4fa965394e26f0d6fb53428 (plain) (blame)
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
# This file is part of the sos project: https://github.com/sosreport/sos
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# version 2 of the GNU General Public License.
#
# See the LICENSE file in the source distribution for further information.

import glob
import os
import re

from avocado.utils import software_manager
from sos_tests import StageOneOutputTest, SOS_REPO_ROOT, skipIf

installer = software_manager
sm = installer.manager.SoftwareManager()

PEXPECT_PRESENT = sm.check_installed('python3-pexpect')


class CollectHelpOutputTest(StageOneOutputTest):
    """Ensure that --help is behaving as expected, based on if the placeholder
    component is being used or not

    Note that this is tagged for both stageone and stagetwo due to the config
    of the GCE images used - pexpect is not normally installed, but we will
    install it as part of the CirrusCI setup for stagetwo. This allows us to
    perform checks for both the real and placeholder components in CI, without
    creating a output tests for stagetwo, which would be silly for tests run on
    contributor workstations

    :avocado: tags=stageone,stagetwo
    """

    sos_cmd = 'collect --help'

    @skipIf(
        PEXPECT_PRESENT,
        "python3-pexpect is installed, placeholder will not be used"
    )
    def test_placeholder_warning_shown(self):
        self.assertOutputContains(
            "WARNING: `collect` is not available with this installation!"
        )

    @skipIf(PEXPECT_PRESENT is False, "python3-pexpect not installed locally")
    def test_help_sections_present(self):
        self.assertOutputNotContains(
            "WARNING: `collect` is not available with this installation!"
        )
        self.assertOutputContains("Global Options:")
        self.assertOutputContains("Report Passthru Options:")
        self.assertOutputContains("Collector Options:")


class CollectOptionsHelpTest(StageOneOutputTest):
    """Ensure that available cluster profiles and their options are printed

    Note that this is tagged for both stageone and stagetwo due to the config
    of the GCE images used - pexpect is not normally installed, but we will
    install it as part of the CirrusCI setup for stagetwo. This allows us to
    perform checks for both the real and placeholder components in CI, without
    creating a output tests for stagetwo, which would be silly for tests run on
    contributor workstations

    :avocado: tags=stageone,stagetwo
    """

    _exception_expected = not PEXPECT_PRESENT
    sos_cmd = 'collect --list-options'

    @skipIf(PEXPECT_PRESENT is False, "python3-pexpect not installed locally")
    def test_cluster_profiles_shown(self):
        _out = re.search("Use the short name with --cluster-type or cluster "
                         r"options \(-c\)(.*?)The following cluster options "
                         "are available:",
                         self.cmd_output.stdout, re.S).group(1).splitlines()
        _profs = {}
        for ln in _out:
            if not ln:
                continue
            ln = [c for c in ln.lstrip().split('  ') if c]
            _profs[ln[0]] = ln[1]

        clusters = []
        # get a list of names of profile pyfiles
        for pyfile in glob.glob(os.path.join(SOS_REPO_ROOT,
                                             'sos/collector/clusters/*.py')):
            pyf = os.path.basename(pyfile)
            if pyf.startswith('__'):
                continue
            clusters.append(pyf.split('.py')[0])

        assert len(_profs.keys()) > 0, "No profiles detected in output"

        # make sure each pyfile is reported for supported cluster types
        # this has the secondary effect of enforcing a stylistic requirement
        # where at least one profile must match the pyfile name
        for clus in clusters:
            assert \
                clus in _profs, \
                f"Cluster '{clus}' not displayed in --list-options"

    @skipIf(PEXPECT_PRESENT is False, "python3-pexpect not installed locally")
    def test_cluster_options_shown(self):
        _opts = re.search(" Cluster                   Option Name     "
                          "Type       Default    Description(.*?)Options "
                          "take the form of cluster.name=value",
                          self.cmd_output.stdout, re.S).group(1).splitlines()

        _opts = [o for o in _opts if o]

        assert _opts, "No option output detected"

    @skipIf(
        PEXPECT_PRESENT,
        "python3-pexpect is installed, placeholder will be unused"
    )
    def test_placeholder_component_used(self):
        self.assertOutputContains(
            "(unavailable) Collect an sos report from multiple nodes"
        )