def test_version(self, capsys): # pylint: disable=no-self-use """Calling --version should return the version and exit.""" with pytest.raises(SystemExit) as e: cli.main(argv=['--version']) out, err = capsys.readouterr() sys.stdout.write(out) sys.stderr.write(err) assert e.type == SystemExit assert e.value.code == 0 assert err == '' assert len(out.splitlines()) == 1 assert __version__ in out
def test_dry_run(self, capsys): """With --dry-run only the matching hosts are printed.""" params = ['--dry-run', self.all_nodes, 'date'] rc = cli.main(argv=self.default_params + params) out, err = capsys.readouterr() sys.stdout.write(out) sys.stderr.write(err) assert _EXPECTED_LINES['all_targeted'] in err, _EXPECTED_LINES[ 'all_targeted'] assert _EXPECTED_LINES['dry_run'] in err, _EXPECTED_LINES['dry_run'] assert _EXPECTED_LINES['successfully'] not in err, _EXPECTED_LINES[ 'successfully'] assert _EXPECTED_LINES['failed'] not in err, _EXPECTED_LINES['failed'] assert _EXPECTED_LINES['global_timeout'] not in err, _EXPECTED_LINES[ 'global_timeout'] assert rc == 0
def test_single_command_supfanout(self, capsys): """Executing one command on a subset of nodes greater than the ClusterShell fanout.""" params = [self.all_nodes, 'date'] rc = cli.main(argv=self.default_params + params) out, err = capsys.readouterr() sys.stdout.write(out) sys.stderr.write(err) assert _EXPECTED_LINES['all_targeted'] in err, _EXPECTED_LINES[ 'all_targeted'] assert _EXPECTED_LINES['date_success'] in err, _EXPECTED_LINES[ 'date_success'] assert _EXPECTED_LINES['all_success'] in err, _EXPECTED_LINES[ 'all_success'] assert _EXPECTED_LINES['failed'] not in err, _EXPECTED_LINES['failed'] assert _EXPECTED_LINES['global_timeout'] not in err, _EXPECTED_LINES[ 'global_timeout'] assert rc == 0
def test_variant(self, capsys): """Test variant generated function.""" argv = self.default_params + params['params'] + [self.all_nodes ] + params['commands'] rc = cli.main(argv=argv) out, err = capsys.readouterr() sys.stdout.write(out) sys.stderr.write(err) if params['rc'] is None: params['rc'] = get_rc(params) assert rc == params['rc'] assert _EXPECTED_LINES['all_targeted'] in err, _EXPECTED_LINES[ 'all_targeted'] labels = params.get('assert_true', []) labels += get_global_timeout_expected_lines(params) if 'async' in params['params']: mode = 'async' else: mode = 'sync' labels += (get_ls_expected_lines(params) + get_date_expected_lines(params) + get_timeout_expected_lines(params)) for label in labels: if label in ('all_success', 'all_failure') and '-p' in params['params']: label = '{label}_threshold'.format(label=label) if label in _EXPECTED_LINES[mode]: string = _EXPECTED_LINES[mode][label] else: string = _EXPECTED_LINES[label] if label.endswith('_re'): assert re.search(string, err) is not None, string else: assert string in err, string for label in params.get('assert_false', []): assert _EXPECTED_LINES[label] not in err, _EXPECTED_LINES[label]
def test_timeout(self, capsys): """With a timeout shorter than a command it should fail.""" params = ['--global-timeout', '1', self.all_nodes, 'sleep 2'] rc = cli.main(argv=self.default_params + params) out, err = capsys.readouterr() sys.stdout.write(out) sys.stderr.write(err) assert _EXPECTED_LINES['all_targeted'] in err, _EXPECTED_LINES[ 'all_targeted'] assert re.search(_EXPECTED_LINES['global_timeout_executing_re'], err) is not None, \ _EXPECTED_LINES['global_timeout_executing_re'] assert re.search(_EXPECTED_LINES['global_timeout_pending_re'], err) is not None, \ _EXPECTED_LINES['global_timeout_pending_re'] assert _EXPECTED_LINES['sleep_total_failure'] in err, _EXPECTED_LINES[ 'sleep_total_failure'] assert _EXPECTED_LINES['all_failure'] in err, _EXPECTED_LINES[ 'all_failure'] assert _EXPECTED_LINES['failed'] not in err, _EXPECTED_LINES['failed'] assert rc == 2
def test_out_txt(self, capsys): """The -o/--out txt option should print the output expanded for each host, prefixed by the hostname.""" params = ['-o', 'txt', self.all_nodes, 'cat /tmp/out'] rc = cli.main(argv=self.default_params + params) out, err = capsys.readouterr() sys.stdout.write(out) sys.stderr.write(err) assert _EXPECTED_LINES['all_targeted'] in err, _EXPECTED_LINES[ 'all_targeted'] assert _EXPECTED_LINES['successfully'] in err, _EXPECTED_LINES[ 'successfully'] assert _EXPECTED_LINES['failed'] not in err, _EXPECTED_LINES['failed'] assert rc == 0 expected_out = '\n'.join( _TXT_EXPECTED_SINGLE_OUTPUT.format(prefix=self.nodes_prefix, node_id=i) for i in range(1, 6)) assert out.split(cli.OUTPUT_SEPARATOR + '\n')[1] == expected_out + '\n'
def test_out_json(self, capsys): """The -o/--out json option should print a JSON with hostnames as keys and output as values.""" params = ['-o', 'json', self.all_nodes, 'cat /tmp/out'] rc = cli.main(argv=self.default_params + params) out, err = capsys.readouterr() sys.stdout.write(out) sys.stderr.write(err) assert _EXPECTED_LINES['all_targeted'] in err, _EXPECTED_LINES[ 'all_targeted'] assert _EXPECTED_LINES['successfully'] in err, _EXPECTED_LINES[ 'successfully'] assert _EXPECTED_LINES['failed'] not in err, _EXPECTED_LINES['failed'] assert rc == 0 expected_out = { self.nodes_prefix + str(i): _JSON_EXPECTED_SINGLE_OUTPUT for i in range(1, 6) } assert json.loads(out.split(cli.OUTPUT_SEPARATOR + '\n')[1]) == expected_out