def testExistsFalse(self): """Test Exists logic with non-standard output from gsutil.""" expected_output = ('GSResponseError: status=404, code=NoSuchKey,' ' reason="Not Found",' ' message="The specified key does not exist."') err1 = gslib.StatFail(expected_output) err2 = gslib.StatFail('You are using a deprecated alias, "getacl",' 'for the "acl" command.\n' + expected_output) uri = 'gs://any/fake/uri/will/do' cmd = ['stat', uri] self.mox.StubOutWithMock(gslib, 'RunGsutilCommand') # Set up the test replay script. # Run 1, normal. gslib.RunGsutilCommand(cmd, failed_exception=gslib.StatFail, get_headers_from_stdout=True).AndRaise(err1) # Run 2, extra output. gslib.RunGsutilCommand(cmd, failed_exception=gslib.StatFail, get_headers_from_stdout=True).AndRaise(err2) self.mox.ReplayAll() # Run the test verification self.assertFalse(gslib.Exists(uri)) self.assertFalse(gslib.Exists(uri)) self.mox.VerifyAll()
def testExists(self): with gs.TemporaryURL('chromite.gslib.exists') as tempuri: self.populateUri(tempuri) self.assertTrue(gslib.Exists(tempuri)) bogus_gs_path = 'gs://chromeos-releases-test/bogus/non-existent-url' self.assertFalse(gslib.Exists(bogus_gs_path))
def Exists(uri, as_dir=False): """Return True if file exists at given URI. If URI is a directory and as_dir is False then this will return False. Args: uri: URI to consider as_dir: If True then check URI as a directory, otherwise check as a file. Returns: True if file (or directory) exists at URI, False otherwise. """ uri_type = GetUriType(uri) if TYPE_GS == uri_type: if as_dir: # GS does not contain directories. return False return gslib.Exists(uri) if TYPE_LOCAL == uri_type: return filelib.Exists(uri, as_dir=as_dir) if TYPE_HTTP == uri_type or TYPE_HTTPS == uri_type: if as_dir: raise NotSupportedForType(uri_type, extra_msg='with as_dir=True') try: response = urllib2.urlopen(uri) return response.getcode() == 200 except urllib2.HTTPError: return False raise NotSupportedForType(uri_type)
def testExists(self): gs_path = 'gs://bucket/some/path' local_path = '/some/local/path' http_path = 'http://host.domain/some/path' ftp_path = 'ftp://host.domain/some/path' result = 'TheResult' self.mox.StubOutWithMock(gslib, 'Exists') self.mox.StubOutWithMock(filelib, 'Exists') self.mox.StubOutWithMock(urilib.urllib2, 'urlopen') # Set up the test replay script. # Run 1, local, as_dir=False filelib.Exists(local_path, as_dir=False).AndReturn(result) # Run 2, GS, as_dir=False. gslib.Exists(gs_path).AndReturn(result) # Run 3, GS, as_dir=True. # Run 6, HTTP, as_dir=False, code=200. urilib.urllib2.urlopen(http_path).AndReturn(FakeHttpResponse(200)) # Run 7, HTTP, as_dir=False, code=404. urilib.urllib2.urlopen(http_path).AndReturn(FakeHttpResponse(404)) # Run 8, HTTP, as_dir=False, HTTPError. urilib.urllib2.urlopen(http_path).AndRaise( urilib.urllib2.HTTPError('url', 404, 'msg', None, None)) # Run 9, HTTP, as_dir=True. # Run 10, FTP, as_dir=False. self.mox.ReplayAll() # Run the test verification. self.assertEquals(result, urilib.Exists(local_path)) self.assertEquals(result, urilib.Exists(gs_path)) self.assertEquals(False, urilib.Exists(gs_path, as_dir=True)) self.assertTrue(urilib.Exists(http_path)) self.assertFalse(urilib.Exists(http_path)) self.assertFalse(urilib.Exists(http_path)) self.assertRaises(urilib.NotSupportedForType, urilib.Exists, http_path, as_dir=True) self.assertRaises(urilib.NotSupportedForType, urilib.Exists, ftp_path) self.mox.VerifyAll()