def _run_command(self, argv, expected_stdout='', expected_stderr='', expected_status=0):
        """
        Runs one command using the ConsoleTool, checking stdout, stderr, and
        the returned status code.

        The ConsoleTool is stateless, so we can make a new one for each
        call, with a fresh stdout and stderr
        """
        expected_stdout = self._trim_leading_spaces(expected_stdout)
        expected_stderr = self._trim_leading_spaces(expected_stderr)
        stdout = six.StringIO()
        stderr = six.StringIO()
        console_tool = ConsoleTool(self.b2_api, stdout, stderr)
        actual_status = console_tool.run_command(['b2'] + argv)

        # The json module in Python 2.6 includes trailing spaces.  Later version of Python don't.
        actual_stdout = self._trim_trailing_spaces(stdout.getvalue())
        actual_stderr = self._trim_trailing_spaces(stderr.getvalue())

        if expected_stdout != actual_stdout:
            print(repr(expected_stdout))
            print(repr(actual_stdout))
        if expected_stderr != actual_stderr:
            print(repr(expected_stderr))
            print(repr(actual_stderr))

        self.assertEqual(expected_stdout, actual_stdout, 'stdout')
        self.assertEqual(expected_stderr, actual_stderr, 'stderr')
        self.assertEqual(expected_status, actual_status, 'exit status code')
Exemplo n.º 2
0
    def _run_command(self,
                     argv,
                     expected_stdout='',
                     expected_stderr='',
                     expected_status=0):
        """
        Runs one command using the ConsoleTool, checking stdout, stderr, and
        the returned status code.

        The ConsoleTool is stateless, so we can make a new one for each
        call, with a fresh stdout and stderr
        """
        expected_stdout = self._trim_leading_spaces(expected_stdout)
        expected_stderr = self._trim_leading_spaces(expected_stderr)
        stdout = six.StringIO()
        stderr = six.StringIO()
        console_tool = ConsoleTool(self.b2_api, stdout, stderr)
        actual_status = console_tool.run_command(['b2'] + argv)

        # The json module in Python 2.6 includes trailing spaces.  Later version of Python don't.
        actual_stdout = self._trim_trailing_spaces(stdout.getvalue())
        actual_stderr = self._trim_trailing_spaces(stderr.getvalue())

        if expected_stdout != actual_stdout:
            print(repr(expected_stdout))
            print(repr(actual_stdout))
        if expected_stderr != actual_stderr:
            print(repr(expected_stderr))
            print(repr(actual_stderr))

        self.assertEqual(expected_stdout, actual_stdout, 'stdout')
        self.assertEqual(expected_stderr, actual_stderr, 'stderr')
        self.assertEqual(expected_status, actual_status, 'exit status code')
Exemplo n.º 3
0
 def test_bad_terminal(self):
     stdout = mock.MagicMock()
     stdout.write = mock.MagicMock(side_effect=[
         UnicodeEncodeError('codec', u'foo', 100, 105,
                            'artificial UnicodeEncodeError')
     ] + list(range(25)))
     stderr = mock.MagicMock()
     console_tool = ConsoleTool(self.b2_api, stdout, stderr)
     console_tool.run_command(
         ['b2', 'authorize_account', 'my-account', 'good-app-key'])
 def test_bad_terminal(self):
     stdout = mock.MagicMock()
     stdout.write = mock.MagicMock(
         side_effect=[
             UnicodeEncodeError('codec', u'foo', 100, 105, 'artificial UnicodeEncodeError')
         ] + list(range(25))
     )
     stderr = mock.MagicMock()
     console_tool = ConsoleTool(self.b2_api, stdout, stderr)
     console_tool.run_command(['b2', 'authorize_account', 'my-account', 'good-app-key'])
    def setUp(self):
        self.master_b2_api = B2Api(
            StubAccountInfo(),
            None,
            api_config=B2HttpApiConfig(_raw_api_class=RawSimulator))
        self.raw_api = self.master_b2_api.session.raw_api
        (self.master_account_id,
         self.master_key) = self.raw_api.create_account()
        self.master_b2_api.authorize_account('production',
                                             self.master_account_id,
                                             self.master_key)
        self.lock_enabled_bucket = self.master_b2_api.create_bucket(
            'lock-enabled-bucket', 'allPrivate', is_file_lock_enabled=True)
        self.lock_disabled_bucket = self.master_b2_api.create_bucket(
            'lock-disabled-bucket', 'allPrivate', is_file_lock_enabled=False)
        new_key = self.master_b2_api.create_key([
            'listKeys',
            'listBuckets',
            'listFiles',
            'readFiles',
        ], 'restricted')
        self.restricted_key_id, self.restricted_key = new_key.id_, new_key.application_key

        self.restricted_b2_api = B2Api(StubAccountInfo(), None)
        self.restricted_b2_api.session.raw_api = self.raw_api
        self.restricted_b2_api.authorize_account('production',
                                                 self.restricted_key_id,
                                                 self.restricted_key)

        self.stdout = StringIO()
        self.stderr = StringIO()
        self.console_tool = ConsoleTool(self.master_b2_api, self.stdout,
                                        self.stderr)
Exemplo n.º 6
0
    def test_get_bucket_with_hidden(self):
        self._authorize_account()
        self._create_my_bucket()

        # Put some files into the test bucket. Unroll the loop for convenience.
        bucket = self.b2_api.get_bucket_by_name('my-bucket')
        bucket.upload(UploadSourceBytes(b'test'), 'upload1')
        bucket.upload(UploadSourceBytes(b'test'), 'upload2')
        bucket.upload(UploadSourceBytes(b'test'), 'upload3')
        bucket.upload(UploadSourceBytes(b'test'), 'upload4')
        bucket.upload(UploadSourceBytes(b'test'), 'upload5')
        bucket.upload(UploadSourceBytes(b'test'), 'upload6')

        # Hide some new files. Don't check the results here; it will be clear enough that
        # something has failed if the output of 'get-bucket' does not match the canon.
        stdout, stderr = self._get_stdouterr()
        console_tool = ConsoleTool(self.b2_api, stdout, stderr)
        console_tool.run_command(['b2', 'hide_file', 'my-bucket', 'hidden1'])
        console_tool.run_command(['b2', 'hide_file', 'my-bucket', 'hidden2'])
        console_tool.run_command(['b2', 'hide_file', 'my-bucket', 'hidden3'])
        console_tool.run_command(['b2', 'hide_file', 'my-bucket', 'hidden4'])

        # Now check the output of get-bucket against the canon.
        expected_stdout = '''
        {
            "accountId": "my-account",
            "bucketId": "bucket_0",
            "bucketInfo": {},
            "bucketName": "my-bucket",
            "bucketType": "allPublic",
            "corsRules": [],
            "fileCount": 10,
            "lifecycleRules": [],
            "revision": 1,
            "totalSize": 24
        }
        '''
        self._run_command(['get-bucket', '--showSize', 'my-bucket'],
                          expected_stdout, '', 0)
Exemplo n.º 7
0
 def _run_command_no_checks(self, argv):
     ConsoleTool(self.b2_api, six.StringIO(),
                 six.StringIO()).run_command(['b2'] + argv)
Exemplo n.º 8
0
 def _run_command_no_checks(self, argv):
     stdout, stderr = self._get_stdouterr()
     ConsoleTool(self.b2_api, stdout, stderr).run_command(['b2'] + argv)