Fix argv patching in unit tests

The argv patching was not actually working; it has something to do
with an interaction between the testscenarios based load_tests we use
and our mocking of argv with

 @mock.patch.object(sys, 'argv', ['./glean', '--hostname'])

What appears to happen is that tests in the same scenario groups get
the *same* reference to the sysv.argument object (i.e. it's only
created once, not once for each call).  Thus each test .append()s to
this same list, and thus we end up appending more and more arguments
to the list.  The sys.argv ends up looking like:

  stdout: {{{['./glean', '--hostname', '--distro=ubuntu', '--distro=ubuntu',
    '--distro=debian', '--distro=redhat', '--distro=redhat', '--distro=centos',
    '--distro=centos', '--distro=gentoo', '--distro=gentoo', '--distro=opensuse', ...

This can't be, Ian, how does it even work?  Well we're lucky in that
for all the existing test-cases, the final arguments appended have
overwritten the prior ones sufficiently that the test-cases work.  If
you start changing the arguments, things go wrong.

To simplify the whole thing, make main() take an argv argument that is
passed to argparse.  For normal use this comes from sys.argv, and in
unit tests we construct our own argv list for each test.

Change-Id: I090164fdd7a46d0c7a29eb6c18f80dc84cfd8469
This commit is contained in:
Ian Wienand 2018-11-20 16:08:28 +11:00
parent 9f629b8531
commit e6b6331a27
2 changed files with 13 additions and 8 deletions

View File

@ -1385,7 +1385,11 @@ def set_hostname_from_config_drive(args):
fh.write(u'%s %s\n' % (host_value, host))
def main():
def main(argv=None):
if argv is None:
args = sys.argv[1:]
parser = argparse.ArgumentParser(description="Static network config")
parser.add_argument(
'-n', '--noop', action='store_true', help='Do not write files')
@ -1414,7 +1418,7 @@ def main():
parser.add_argument(
'--debug', dest='debug', action='store_true',
help="Enable debugging output")
args = parser.parse_args()
args = parser.parse_args(argv)
if args.debug:
logging.basicConfig(level=logging.DEBUG)

View File

@ -17,7 +17,6 @@ import errno
import functools
import json
import os
import sys
import mock
from oslotest import base
@ -150,7 +149,6 @@ class TestGlean(base.BaseTestCase):
@mock.patch('os.listdir', new_callable=mock.Mock)
@mock.patch('os.system', return_value=0, new_callable=mock.Mock)
@mock.patch('glean.cmd.open', new_callable=mock.Mock)
@mock.patch.object(sys, 'argv', ['./glean', '--hostname'])
def _assert_distro_provider(self, distro, provider, interface,
mock_open,
mock_os_system,
@ -182,14 +180,17 @@ class TestGlean(base.BaseTestCase):
mock_open.side_effect = functools.partial(
self.open_side_effect, provider)
sys.argv.append('--distro=%s' % distro.lower())
# default args
argv = ['--hostname']
argv.append('--distro=%s' % distro.lower())
if interface:
sys.argv.append('--interface=%s' % interface)
argv.append('--interface=%s' % interface)
if skip_dns:
sys.argv.append('--skip-dns')
argv.append('--skip-dns')
cmd.main()
cmd.main(argv)
output_filename = '%s.%s.network.out' % (provider, distro.lower())
output_path = os.path.join(sample_data_path, 'test', output_filename)