1 # -*- coding: utf-8 -*-
2 # Copyright 2013 Google Inc. All Rights Reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Tests for stat command."""
17 from __future__
import absolute_import
19 from gslib
.cs_api_map
import ApiSelector
20 import gslib
.tests
.testcase
as testcase
21 from gslib
.tests
.util
import ObjectToURI
as suri
24 class TestStat(testcase
.GsUtilIntegrationTestCase
):
25 """Integration tests for stat command."""
27 def test_stat_output(self
):
28 """Tests stat output of a single object."""
29 object_uri
= self
.CreateObject(contents
='z')
30 stdout
= self
.RunGsUtil(['stat', suri(object_uri
)], return_stdout
=True)
31 self
.assertIn(object_uri
.uri
, stdout
)
32 self
.assertIn('Creation time:', 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).
40 # Likewise, with contentEncoding, the field value and the header value
41 # are not guaranteed to match or be the same across APIs.
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
)
53 self
.assertIn('Content-Length:', stdout
)
54 self
.assertIn('Content-Type:', stdout
)
55 self
.assertIn('ETag:', stdout
)
57 def test_minus_q_stat(self
):
58 object_uri
= self
.CreateObject(contents
='z')
59 stdout
= self
.RunGsUtil(['-q', 'stat', suri(object_uri
)],
61 self
.assertEquals(0, len(stdout
))
62 stdout
= self
.RunGsUtil(['-q', 'stat', suri(object_uri
, 'junk')],
63 return_stdout
=True, expected_status
=1)
64 self
.assertEquals(0, len(stdout
))
66 def test_stat_of_non_object_uri(self
):
67 self
.RunGsUtil(['-q', 'stat', 'gs://'], expected_status
=1)
68 self
.RunGsUtil(['-q', 'stat', 'gs://bucket/object'], expected_status
=1)
69 self
.RunGsUtil(['-q', 'stat', 'file://tmp/abc'], expected_status
=1)
71 def test_stat_one_missing(self
):
72 bucket_uri
= self
.CreateBucket()
73 self
.CreateObject(bucket_uri
=bucket_uri
, object_name
='notmissing',
75 stdout
, stderr
= self
.RunGsUtil(
76 ['stat', suri(bucket_uri
, 'missing'), suri(bucket_uri
, 'notmissing')],
77 expected_status
=1, return_stdout
=True, return_stderr
=True)
78 self
.assertIn('No URLs matched %s' % suri(bucket_uri
, 'missing'), stderr
)
79 self
.assertIn('%s:' % suri(bucket_uri
, 'notmissing'), stdout
)
81 def test_stat_one_missing_wildcard(self
):
82 bucket_uri
= self
.CreateBucket()
83 self
.CreateObject(bucket_uri
=bucket_uri
, object_name
='notmissing',
85 stdout
, stderr
= self
.RunGsUtil(
86 ['stat', suri(bucket_uri
, 'missin*'), suri(bucket_uri
, 'notmissin*')],
87 expected_status
=1, return_stdout
=True, return_stderr
=True)
88 self
.assertIn('No URLs matched %s' % suri(bucket_uri
, 'missin*'), stderr
)
89 self
.assertIn('%s:' % suri(bucket_uri
, 'notmissing'), stdout
)
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
])
99 def test_stat_object_wildcard(self
):
100 bucket_uri
= self
.CreateBucket()
101 object1_uri
= self
.CreateObject(bucket_uri
=bucket_uri
, object_name
='foo1',
103 object2_uri
= self
.CreateObject(bucket_uri
=bucket_uri
, object_name
='foo2',
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
)