Example #1
0
    def test_complains_about_too_few_arguments(self):
        configs = self.fake_profile()
        [profile_name] = configs
        resources = configs[profile_name]["description"]["resources"]
        resource_name = random.choice(resources)["name"]
        command = "maas", profile_name, handler_command_name(resource_name)

        with CaptureStandardIO() as stdio:
            error = self.assertRaises(SystemExit, main, command)

        self.assertThat(error.code, Equals(2))
        self.assertThat(
            stdio.getError(),
            DocTestMatches(
                dedent(
                    """\
                usage: maas [-h] COMMAND ...
                <BLANKLINE>
                ...
                <BLANKLINE>
                too few arguments
                """
                )
            ),
        )
Example #2
0
 def test__clearInput_clears_input(self):
     text = factory.make_name("text")
     with CaptureStandardIO() as stdio:
         stdio.addInput(text + "111")
         sys.stdin.read(2)
         stdio.clearInput()
         self.expectThat(sys.stdin.read(2), Equals(""))
Example #3
0
 def test__clearOutput_clears_output(self):
     text = factory.make_name("text")
     with CaptureStandardIO() as stdio:
         sys.stdout.write(text)
         self.expectThat(stdio.getOutput(), Equals(text))
         stdio.clearOutput()
         self.expectThat(stdio.getOutput(), Equals(""))
Example #4
0
 def test__clearError_clears_error(self):
     text = factory.make_name("text")
     with CaptureStandardIO() as stdio:
         sys.stderr.write(text)
         self.expectThat(stdio.getError(), Equals(text))
         stdio.clearError()
         self.expectThat(stdio.getError(), Equals(""))
Example #5
0
 def test__addInput_feeds_stdin(self):
     text = factory.make_name("text")
     with CaptureStandardIO() as stdio:
         stdio.addInput(text + "111")
         self.expectThat(sys.stdin.read(2), Equals(text[:2]))
         stdio.addInput(text + "222")
         self.expectThat(sys.stdin.read(),
                         Equals(text[2:] + "111" + text + "222"))
Example #6
0
 def test_compare_api_hashes_prints_nothing_if_hashes_match(self):
     example_hash = factory.make_name("hash")
     profile = {"description": {"hash": example_hash}}
     response = {"x-maas-api-hash": example_hash}
     with CaptureStandardIO() as stdio:
         api.Action.compare_api_hashes(profile, response)
     self.assertEqual(stdio.getOutput(), "")
     self.assertEqual(stdio.getError(), "")
Example #7
0
 def test_compare_api_hashes_prints_nothing_if_remote_has_no_hash(self):
     example_hash = factory.make_name("hash")
     profile = {"description": {"hash": example_hash}}
     response = {}
     with CaptureStandardIO() as stdio:
         api.Action.compare_api_hashes(profile, response)
     self.assertThat(stdio.getOutput(), Equals(""))
     self.assertThat(stdio.getError(), Equals(""))
Example #8
0
    def test__captures_stderr(self):
        stderr_before = sys.stderr
        with CaptureStandardIO():
            stderr_during = sys.stderr
        stderr_after = sys.stderr

        self.expectThat(stderr_during, Not(Is(stderr_before)))
        self.expectThat(stderr_during, Not(Is(stderr_after)))
        self.expectThat(stderr_after, Is(stderr_before))
Example #9
0
    def test__captures_stdout(self):
        stdout_before = sys.stdout
        with CaptureStandardIO():
            stdout_during = sys.stdout
        stdout_after = sys.stdout

        self.expectThat(stdout_during, Not(Is(stdout_before)))
        self.expectThat(stdout_during, Not(Is(stdout_after)))
        self.expectThat(stdout_after, Is(stdout_before))
Example #10
0
    def test__captures_stdin(self):
        stdin_before = sys.stdin
        with CaptureStandardIO():
            stdin_during = sys.stdin
        stdin_after = sys.stdin

        self.expectThat(stdin_during, Not(Is(stdin_before)))
        self.expectThat(stdin_during, Not(Is(stdin_after)))
        self.expectThat(stdin_after, Is(stdin_before))
Example #11
0
 def test__clearAll_clears_input_output_and_error(self):
     text = factory.make_name("text")
     with CaptureStandardIO() as stdio:
         stdio.addInput(text)
         sys.stdout.write(text)
         sys.stderr.write(text)
         stdio.clearAll()
         self.expectThat(stdio.getInput(), Equals(""))
         self.expectThat(stdio.getOutput(), Equals(""))
         self.expectThat(stdio.getError(), Equals(""))
Example #12
0
def call_nnn(command, **options):
    """Call `command`, return captures standard IO.

    See `call_command`.

    :return: :class:`CaptureStandardIO` instance.
    """
    with CaptureStandardIO() as stdio:
        call_command(command, **options)
    return stdio
Example #13
0
 def make_boot_resources_create_action(self):
     self.stdio = self.useFixture(CaptureStandardIO())
     action_bases = (BootResourcesCreateAction,)
     action_ns = {
         "action": {"method": "POST"},
         "handler": {"uri": b"/MAAS/api/2.0/boot-resources/", "params": []},
         "profile": {"credentials": make_api_credentials()},
     }
     action_class = type("create", action_bases, action_ns)
     action = action_class(Mock())
     return action
Example #14
0
 def make_sshkeys_import_action(self):
     self.stdio = self.useFixture(CaptureStandardIO())
     action_bases = (SSHKeysImportAction,)
     action_ns = {
         "action": {"method": "POST"},
         "handler": {"uri": b"/MAAS/api/2.0/sshkeys/", "params": []},
         "profile": {"credentials": make_api_credentials()},
     }
     action_class = type("import", action_bases, action_ns)
     action = action_class(Mock())
     return action
Example #15
0
 def test__not_available_in_production(self):
     self.useFixture(ImportErrorFixture("maasserver.testing", "sampledata"))
     self.patch(sampledata, "populate")
     with CaptureStandardIO() as stdio:
         self.assertRaises(SystemExit, call_command, "generate_sample_data")
     self.assertThat(sampledata.populate, MockNotCalled())
     self.assertThat(stdio.getOutput(), Equals(""))
     self.assertThat(
         stdio.getError(),
         MatchesRegex(
             "Sample data generation is available only in development "
             "and test environments.\n\\s*"))
Example #16
0
 def test__rejects_file_name_not_on_white_list(self):
     filename = factory.make_name("/some/where", sep="/")
     args = self.script.arg_parser.parse_args([filename])
     with CaptureStandardIO() as stdio:
         error = self.assertRaises(SystemExit, self.script.main, args)
     self.assertThat(error.code, GreaterThan(0))
     self.assertThat(self.script.atomic_delete, MockNotCalled())
     self.assertThat(stdio.getOutput(), Equals(""))
     self.assertThat(
         stdio.getError(),
         DocTestMatches("usage: ... Given filename ... is not in the "
                        "white list. Choose from: ..."))
Example #17
0
 def test__rejects_file_name_not_on_white_list(self):
     filename = factory.make_name("/some/where", sep="/")
     mode = random.randint(0o000, 0o777)  # Inclusive of endpoints.
     args = self.script.arg_parser.parse_args([filename, oct(mode)])
     with CaptureStandardIO() as stdio:
         error = self.assertRaises(SystemExit, self.script.main, args,
                                   io.BytesIO())
     self.assertThat(error.code, GreaterThan(0))
     self.assertThat(self.script.atomic_write, MockNotCalled())
     self.assertThat(stdio.getOutput(), Equals(""))
     self.assertThat(
         stdio.getError(),
         DocTestMatches("usage: ... Given filename ... is not in the "
                        "white list. Choose from: ..."))
Example #18
0
    def test_complains_about_too_few_arguments(self):
        configs = self.fake_profile()
        [profile_name] = configs
        resources = configs[profile_name]["description"]["resources"]
        resource_name = random.choice(resources)["name"]
        command = "maas", profile_name, handler_command_name(resource_name)

        with CaptureStandardIO() as stdio:
            error = self.assertRaises(SystemExit, main, command)

        self.assertEqual(error.code, 2)
        error = stdio.getError()
        self.assertIn("usage: maas [-h] COMMAND ...", error)
        self.assertIn("too few arguments", error)
Example #19
0
 def test_compare_api_hashes_prints_warning_if_hashes_dont_match(self):
     example_hash = factory.make_name("hash")
     profile = {"description": {"hash": example_hash + "foo"}}
     response = {"x-maas-api-hash": example_hash + "bar"}
     with CaptureStandardIO() as stdio:
         api.Action.compare_api_hashes(profile, response)
     self.assertThat(stdio.getOutput(), Equals(""))
     self.assertThat(stdio.getError(), Equals(dedent("""\
     **********************************************************************
     *** WARNING! The API on the server differs from the description that
     *** is cached locally. This may result in failed API calls. Refresh
     *** the local API description with `maas refresh`.
     **********************************************************************
     """)))
Example #20
0
 def test__rejects_file_mode_with_high_bits_set(self):
     filename = random.choice(list(self.script.whitelist))
     mode = random.randint(0o1000, 0o7777)  # Inclusive of endpoints.
     args = self.script.arg_parser.parse_args([filename, oct(mode)])
     with CaptureStandardIO() as stdio:
         error = self.assertRaises(SystemExit, self.script.main, args,
                                   io.BytesIO())
     self.assertThat(error.code, GreaterThan(0))
     self.assertThat(self.script.atomic_write, MockNotCalled())
     self.assertThat(stdio.getOutput(), Equals(""))
     self.assertThat(
         stdio.getError(),
         DocTestMatches(
             "usage: ... Given file mode 0o... is not permitted; "
             "only permission bits may be set."))
Example #21
0
    def assert_getter_returns_data_written_to_stream(self, getter, name):
        stream = self.patch(sys, name)

        before = factory.make_name("before")
        during = factory.make_name("during")
        after = factory.make_name("after")
        end = factory.make_name("end")

        print(before, file=getattr(sys, name), end=end)
        with CaptureStandardIO() as stdio:
            print(during, file=getattr(sys, name), end=end)
        print(after, file=getattr(sys, name), end=end)

        self.expectThat(getter(stdio), Equals(during + end))
        self.expectThat(stream.write, MockCallsMatch(
            call(before), call(end), call(after), call(end)))
 def make_boot_resources_create_action(self):
     self.stdio = self.useFixture(CaptureStandardIO())
     action_bases = (BootResourcesCreateAction, )
     action_ns = {
         "action": {
             'method': 'POST'
         },
         "handler": {
             'uri': b'/MAAS/api/2.0/boot-resources/',
             'params': []
         },
         "profile": {
             'credentials': make_api_credentials()
         }
     }
     action_class = type("create", action_bases, action_ns)
     action = action_class(Mock())
     return action
Example #23
0
 def test__getInput_returns_data_waiting_to_be_read(self):
     stdio = CaptureStandardIO()
     stdio.addInput("one\ntwo\n")
     with stdio:
         self.expectThat(sys.stdin.readline(), Equals("one\n"))
         self.expectThat(stdio.getInput(), Equals("two\n"))
Example #24
0
 def setUp(self):
     super(TestSelectorArguments, self).setUp()
     self.stdio = self.useFixture(CaptureStandardIO())
     self.patch_autospec(parallel, "test")
     parallel.test.return_value = True
Example #25
0
 def setUp(self):
     super(TestActionScript, self).setUp()
     # ActionScript.setup() is not safe to run in the test suite.
     self.patch(ActionScript, "setup", lambda self: None)
     # ArgumentParser sometimes likes to print to stdout/err.
     self.stdio = self.useFixture(CaptureStandardIO())
Example #26
0
 def test__non_text_strings_are_rejected_on_stderr(self):
     with CaptureStandardIO():
         error = self.assertRaises(TypeError, sys.stderr.write,
                                   sample_binary_data)
     self.assertDocTestMatches("write() argument must be str, not bytes",
                               str(error))