| OLD | NEW |
| 1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
| 2 # | |
| 3 # Copyright 2013 Google Inc. All Rights Reserved. | 2 # Copyright 2013 Google Inc. All Rights Reserved. |
| 4 # | 3 # |
| 5 # Licensed under the Apache License, Version 2.0 (the "License"); | 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 # you may not use this file except in compliance with the License. | 5 # you may not use this file except in compliance with the License. |
| 7 # You may obtain a copy of the License at | 6 # You may obtain a copy of the License at |
| 8 # | 7 # |
| 9 # http://www.apache.org/licenses/LICENSE-2.0 | 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 # | 9 # |
| 11 # Unless required by applicable law or agreed to in writing, software | 10 # Unless required by applicable law or agreed to in writing, software |
| 12 # distributed under the License is distributed on an "AS IS" BASIS, | 11 # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 # See the License for the specific language governing permissions and | 13 # See the License for the specific language governing permissions and |
| 15 # limitations under the License. | 14 # limitations under the License. |
| 15 """Tests for stat command.""" |
| 16 | 16 |
| 17 import posixpath | 17 from __future__ import absolute_import |
| 18 import re | |
| 19 import subprocess | |
| 20 import sys | |
| 21 | 18 |
| 22 import gslib | 19 from gslib.cs_api_map import ApiSelector |
| 23 import gslib.tests.testcase as testcase | 20 import gslib.tests.testcase as testcase |
| 24 from gslib.tests.util import ObjectToURI as suri | 21 from gslib.tests.util import ObjectToURI as suri |
| 25 from gslib.wildcard_iterator import ContainsWildcard | 22 |
| 26 | 23 |
| 27 class TestStat(testcase.GsUtilIntegrationTestCase): | 24 class TestStat(testcase.GsUtilIntegrationTestCase): |
| 28 """Integration tests for stat command.""" | 25 """Integration tests for stat command.""" |
| 29 | 26 |
| 30 def test_stat_output(self): | 27 def test_stat_output(self): |
| 28 """Tests stat output of a single object.""" |
| 31 object_uri = self.CreateObject(contents='z') | 29 object_uri = self.CreateObject(contents='z') |
| 32 stdout = self.RunGsUtil(['stat', suri(object_uri)], return_stdout=True) | 30 stdout = self.RunGsUtil(['stat', suri(object_uri)], return_stdout=True) |
| 33 self.assertIn(object_uri.uri, stdout) | 31 self.assertIn(object_uri.uri, stdout) |
| 34 self.assertIn('Creation time:', stdout) | 32 self.assertIn('Creation time:', stdout) |
| 35 self.assertIn('Cache-Control:', stdout) | 33 |
| 36 self.assertIn('Content-Encoding:', stdout) | 34 # Cache-Control and Content-Encoding can be different depending on |
| 35 # whether the JSON or XML API is used. For JSON, only max-age and |
| 36 # no-cache are respected. Although the object field will be populated |
| 37 # with whatever we set, the actual header returned from the JSON API |
| 38 # may differ from it (and differ from the XML response for the same object). |
| 39 # |
| 40 # Likewise, with contentEncoding, the field value and the header value |
| 41 # are not guaranteed to match or be the same across APIs. |
| 42 # |
| 43 # JSON will not return a Cache-control or content-encoding with the |
| 44 # current test object creation, so check these only for the XML API. |
| 45 if self.default_provider == 'gs': |
| 46 if self.test_api == ApiSelector.XML: |
| 47 self.assertIn('Cache-Control:', stdout) |
| 48 self.assertIn('Content-Encoding:', stdout) |
| 49 self.assertIn('Generation:', stdout) |
| 50 self.assertIn('Metageneration:', stdout) |
| 51 self.assertIn('Hash (crc32c):', stdout) |
| 52 self.assertIn('Hash (md5):', stdout) |
| 37 self.assertIn('Content-Length:', stdout) | 53 self.assertIn('Content-Length:', stdout) |
| 38 self.assertIn('Content-Type:', stdout) | 54 self.assertIn('Content-Type:', stdout) |
| 39 self.assertIn('Hash (crc32c):', stdout) | |
| 40 self.assertIn('Hash (md5):', stdout) | |
| 41 self.assertIn('ETag:', stdout) | 55 self.assertIn('ETag:', stdout) |
| 42 self.assertIn('Generation:', stdout) | |
| 43 self.assertIn('Metageneration:', stdout) | |
| 44 | 56 |
| 45 def test_minus_q_stat(self): | 57 def test_minus_q_stat(self): |
| 46 object_uri = self.CreateObject(contents='z') | 58 object_uri = self.CreateObject(contents='z') |
| 47 stdout = self.RunGsUtil(['-q', 'stat', suri(object_uri)], | 59 stdout = self.RunGsUtil(['-q', 'stat', suri(object_uri)], |
| 48 return_stdout=True) | 60 return_stdout=True) |
| 49 self.assertEquals(0, len(stdout)) | 61 self.assertEquals(0, len(stdout)) |
| 50 stdout = self.RunGsUtil(['-q', 'stat', suri(object_uri, 'junk')], | 62 stdout = self.RunGsUtil(['-q', 'stat', suri(object_uri, 'junk')], |
| 51 return_stdout=True, expected_status=1) | 63 return_stdout=True, expected_status=1) |
| 52 self.assertEquals(0, len(stdout)) | 64 self.assertEquals(0, len(stdout)) |
| 53 | 65 |
| 54 def test_stat_of_non_object_uri(self): | 66 def test_stat_of_non_object_uri(self): |
| 55 self.RunGsUtil(['-q', 'stat', 'gs://'], expected_status=1) | 67 self.RunGsUtil(['-q', 'stat', 'gs://'], expected_status=1) |
| 56 self.RunGsUtil(['-q', 'stat', 'gs://bucket/object'], expected_status=1) | 68 self.RunGsUtil(['-q', 'stat', 'gs://bucket/object'], expected_status=1) |
| 57 self.RunGsUtil(['-q', 'stat', 'file://tmp/abc'], expected_status=1) | 69 self.RunGsUtil(['-q', 'stat', 'file://tmp/abc'], expected_status=1) |
| 70 |
| 71 def test_stat_one_missing(self): |
| 72 bucket_uri = self.CreateBucket() |
| 73 self.CreateObject(bucket_uri=bucket_uri, object_name='notmissing', |
| 74 contents='z') |
| 75 stdout = self.RunGsUtil(['stat', suri(bucket_uri, 'missing'), |
| 76 suri(bucket_uri, 'notmissing')], expected_status=1, |
| 77 return_stdout=True) |
| 78 self.assertIn('No URLs matched %s' % suri(bucket_uri, 'missing'), stdout) |
| 79 self.assertIn('%s:' % suri(bucket_uri, 'notmissing'), stdout) |
| 80 |
| 81 def test_stat_one_missing_wildcard(self): |
| 82 bucket_uri = self.CreateBucket() |
| 83 self.CreateObject(bucket_uri=bucket_uri, object_name='notmissing', |
| 84 contents='z') |
| 85 stdout = self.RunGsUtil(['stat', suri(bucket_uri, 'missin*'), |
| 86 suri(bucket_uri, 'notmissin*')], expected_status=1, |
| 87 return_stdout=True) |
| 88 self.assertIn('No URLs matched %s' % suri(bucket_uri, 'missin*'), stdout) |
| 89 self.assertIn('%s:' % suri(bucket_uri, 'notmissing'), stdout) |
| 90 |
| 91 def test_stat_bucket_wildcard(self): |
| 92 bucket_uri = self.CreateBucket() |
| 93 self.CreateObject(bucket_uri=bucket_uri, object_name='foo', contents='z') |
| 94 stat_string = suri(bucket_uri)[:-1] + '?/foo' |
| 95 self.RunGsUtil(['stat', stat_string]) |
| 96 stat_string2 = suri(bucket_uri)[:-1] + '*/foo' |
| 97 self.RunGsUtil(['stat', stat_string2]) |
| 98 |
| 99 def test_stat_object_wildcard(self): |
| 100 bucket_uri = self.CreateBucket() |
| 101 object1_uri = self.CreateObject(bucket_uri=bucket_uri, object_name='foo1', |
| 102 contents='z') |
| 103 object2_uri = self.CreateObject(bucket_uri=bucket_uri, object_name='foo2', |
| 104 contents='z') |
| 105 stat_string = suri(object1_uri)[:-2] + '*' |
| 106 stdout = self.RunGsUtil(['stat', stat_string], return_stdout=True) |
| 107 self.assertIn(suri(object1_uri), stdout) |
| 108 self.assertIn(suri(object2_uri), stdout) |
| 109 |
| OLD | NEW |