Beispiel #1
0
    def testCopy(self):
        src_path = '/path/to/some/file'
        dest_path = 'gs://bucket/some/gs/path'

        self.mox.StubOutWithMock(utils, 'RunCommand')

        # Set up the test replay script.
        # Run 1, success.
        cmd = [self.gsutil, 'cp', src_path, dest_path]
        utils.RunCommand(cmd,
                         redirect_stdout=True,
                         redirect_stderr=True,
                         return_result=True)
        # Run 2, failure.
        for _ix in xrange(gslib.RETRY_ATTEMPTS + 1):
            cmd = [self.gsutil, 'cp', src_path, dest_path]
            utils.RunCommand(
                cmd,
                redirect_stdout=True,
                redirect_stderr=True,
                return_result=True).AndRaise(
                    utils.CommandFailedException(GS_RETRY_FAILURE))
        self.mox.ReplayAll()

        # Run the test verification.
        gslib.Copy(src_path, dest_path)
        self.assertRaises(gslib.CopyFail, gslib.Copy, src_path, dest_path)
        self.mox.VerifyAll()
Beispiel #2
0
    def testStatFail(self):
        self.mox.StubOutWithMock(utils, 'RunCommand')
        path = 'gs://bucket/some/gs/path'

        # Set up the test replay script.
        cmd = [self.gsutil, 'stat', path]
        utils.RunCommand(cmd,
                         redirect_stdout=True,
                         redirect_stderr=True,
                         return_result=True).AndRaise(
                             utils.CommandFailedException())
        self.mox.ReplayAll()

        # Run the test verification.
        self.assertRaises(gslib.StatFail, gslib.Stat, path)
        self.mox.VerifyAll()
Beispiel #3
0
    def testSetACLFail(self):
        self.mox.StubOutWithMock(utils, 'RunCommand')
        gs_uri = 'gs://bucket/foo/bar/somefile'
        acl_file = '/some/gs/acl/file'

        # Set up the test replay script. (Multiple times because of retry logic)
        cmd = [self.gsutil, 'setacl', acl_file, gs_uri]
        utils.RunCommand(cmd,
                         redirect_stdout=True,
                         redirect_stderr=True,
                         return_result=True).MultipleTimes().AndRaise(
                             utils.CommandFailedException())
        self.mox.ReplayAll()

        # Run the test verification.
        self.assertRaises(gslib.AclFail, gslib.SetACL, gs_uri, acl_file)
        self.mox.VerifyAll()
Beispiel #4
0
    def testDeleteBucketFail(self):
        self.mox.StubOutWithMock(utils, 'RunCommand')

        # Set up the test replay script.
        cmd = [self.gsutil, 'rm', '%s/*' % self.bucket_uri]
        utils.RunCommand(cmd,
                         redirect_stdout=True,
                         redirect_stderr=True,
                         error_ok=True,
                         return_result=True).AndRaise(
                             utils.CommandFailedException(GS_DONE_FAILURE))
        self.mox.ReplayAll()

        # Run the test verification.
        self.assertRaises(gslib.BucketOperationError, gslib.DeleteBucket,
                          self.bucket_name)
        self.mox.VerifyAll()
Beispiel #5
0
    def testRunGsutilCommand(self):
        args = ['TheCommand', 'Arg1', 'Arg2']
        cmd = [self.gsutil] + args

        self.mox.StubOutWithMock(utils, 'RunCommand')

        # Set up the test replay script.
        # Run 1.
        utils.RunCommand(cmd,
                         redirect_stdout=True,
                         redirect_stderr=True,
                         return_result=True).AndReturn(1)
        # Run 2.
        utils.RunCommand(cmd,
                         redirect_stdout=False,
                         redirect_stderr=True,
                         return_result=True).AndReturn(2)
        # Run 3.
        utils.RunCommand(cmd,
                         redirect_stdout=True,
                         redirect_stderr=True,
                         return_result=True,
                         error_ok=True).AndReturn(3)
        # Run 4.
        utils.RunCommand(cmd,
                         redirect_stdout=True,
                         redirect_stderr=True,
                         return_result=True).AndRaise(
                             utils.CommandFailedException())
        # Run 5.
        utils.RunCommand(cmd,
                         redirect_stdout=True,
                         redirect_stderr=True,
                         return_result=True).AndRaise(
                             OSError(errno.ENOENT, 'errmsg'))
        self.mox.ReplayAll()

        # Run the test verification.
        self.assertEqual(1, gslib.RunGsutilCommand(args))
        self.assertEqual(2, gslib.RunGsutilCommand(args,
                                                   redirect_stdout=False))
        self.assertEqual(3, gslib.RunGsutilCommand(args, error_ok=True))
        self.assertRaises(gslib.GSLibError, gslib.RunGsutilCommand, args)
        self.assertRaises(gslib.GsutilMissingError, gslib.RunGsutilCommand,
                          args)
        self.mox.VerifyAll()
Beispiel #6
0
    def testCreateBucketFail(self):
        self.mox.StubOutWithMock(utils, 'RunCommand')

        # Set up the test replay script.
        cmd = [self.gsutil, 'mb', self.bucket_uri]
        for _ix in xrange(gslib.RETRY_ATTEMPTS + 1):
            utils.RunCommand(
                cmd,
                redirect_stdout=True,
                redirect_stderr=True,
                return_result=True).AndRaise(
                    utils.CommandFailedException(GS_RETRY_FAILURE))
        self.mox.ReplayAll()

        # Run the test verification.
        self.assertRaises(gslib.BucketOperationError, gslib.CreateBucket,
                          self.bucket_name)
        self.mox.VerifyAll()
Beispiel #7
0
    def testRemoveFail(self):
        self.mox.StubOutWithMock(utils, 'RunCommand')
        path = 'gs://bucket/some/gs/path'

        # Set up the test replay script.
        cmd = [self.gsutil, 'rm', path]
        for _ix in xrange(gslib.RETRY_ATTEMPTS + 1):
            utils.RunCommand(
                cmd,
                redirect_stdout=True,
                redirect_stderr=True,
                return_result=True,
            ).AndRaise(utils.CommandFailedException(GS_RETRY_FAILURE))
        self.mox.ReplayAll()

        # Run the test verification.
        self.assertRaises(gslib.RemoveFail, gslib.Remove, path)
        self.mox.VerifyAll()
Beispiel #8
0
    def testListFiles(self):
        self.mox.StubOutWithMock(utils, 'RunCommand')
        files = [
            '%s/some/path' % self.bucket_uri,
            '%s/some/file/path' % self.bucket_uri,
        ]
        directories = [
            '%s/some/dir/' % self.bucket_uri,
            '%s/some/dir/path/' % self.bucket_uri,
        ]

        gs_uri = '%s/**' % self.bucket_uri
        cmd = [self.gsutil, 'ls', gs_uri]

        # Prepare cmd_result for a good run.
        # Fake a trailing empty line.
        output = '\n'.join(files + directories + [''])
        cmd_result_ok = cros_test_lib.EasyAttr(output=output, returncode=0)

        # Prepare exception for a run that finds nothing.
        stderr = 'CommandException: One or more URLs matched no objects.\n'
        empty_exception = utils.CommandFailedException(stderr)

        # Prepare exception for a run that triggers a GS failure.
        failure_exception = utils.CommandFailedException(GS_RETRY_FAILURE)

        # Set up the test replay script.
        # Run 1, runs ok.
        utils.RunCommand(cmd,
                         redirect_stdout=True,
                         redirect_stderr=True,
                         return_result=True).AndReturn(cmd_result_ok)
        # Run 2, runs ok, sorts files.
        utils.RunCommand(cmd,
                         redirect_stdout=True,
                         redirect_stderr=True,
                         return_result=True).AndReturn(cmd_result_ok)
        # Run 3, finds nothing.
        utils.RunCommand(cmd,
                         redirect_stdout=True,
                         redirect_stderr=True,
                         return_result=True).AndRaise(empty_exception)
        # Run 4, failure in GS.
        for _ix in xrange(gslib.RETRY_ATTEMPTS + 1):
            utils.RunCommand(cmd,
                             redirect_stdout=True,
                             redirect_stderr=True,
                             return_result=True).AndRaise(failure_exception)
        self.mox.ReplayAll()

        # Run the test verification.
        result = gslib.ListFiles(self.bucket_uri, recurse=True)
        self.assertEqual(files, result)
        result = gslib.ListFiles(self.bucket_uri, recurse=True, sort=True)
        self.assertEqual(sorted(files), result)
        result = gslib.ListFiles(self.bucket_uri, recurse=True)
        self.assertEqual([], result)
        self.assertRaises(gslib.GSLibError,
                          gslib.ListFiles,
                          self.bucket_uri,
                          recurse=True)
        self.mox.VerifyAll()