Permalink
Browse files

Adds tests for setversioning

  • Loading branch information...
1 parent 44d85a4 commit 3bc36651272b9704576a225392ff66dd4efcb94e @fungusakafungus committed Apr 18, 2011
Showing with 96 additions and 4 deletions.
  1. +1 −0 .gitignore
  2. +19 −1 S3/S3.py
  3. +16 −0 S3/Versioning.py
  4. +42 −2 run-tests.py
  5. +14 −0 s3cmd
  6. +4 −1 s3cmd.1
View
@@ -0,0 +1 @@
+*.iml
View
@@ -26,6 +26,7 @@
from ACL import ACL, GranteeLogDelivery
from AccessLog import AccessLog
from S3Uri import S3Uri
+from Versioning import VersioningConfiguration
__all__ = []
class S3Request(object):
@@ -239,9 +240,18 @@ def bucket_delete(self, bucket):
return response
def bucket_info(self, uri):
+ request = self.create_request("BUCKET_LIST", bucket = uri.bucket(), extra = "?versioning")
+ response = self.send_request(request)
+ tree = getTreeFromXml(response['data'])
+ status_node = tree.find('Status')
+ versioning_state = status_node.text if status_node is not None else "Disabled"
+
request = self.create_request("BUCKET_LIST", bucket = uri.bucket(), extra = "?location")
response = self.send_request(request)
- response['bucket-location'] = getTextFromXml(response['data'], "LocationConstraint") or "any"
+ bucket_location = getTextFromXml(response['data'], "LocationConstraint") or "any"
+
+ response['bucket-location'] = bucket_location
+ response['versioning-state'] = versioning_state
return response
def object_put(self, filename, uri, extra_headers = None, extra_label = ""):
@@ -345,6 +355,14 @@ def set_acl(self, uri, acl):
response = self.send_request(request, body)
return response
+ def set_versioning(self, uri, versioning):
+ request = self.create_request("BUCKET_CREATE", bucket = uri.bucket(), extra = "?versioning")
+ body = str(VersioningConfiguration(state=versioning))
+ response = self.send_request(request, body)
+
+ return response
+
+
def get_accesslog(self, uri):
request = self.create_request("BUCKET_LIST", bucket = uri.bucket(), extra = "?logging")
response = self.send_request(request)
View
@@ -0,0 +1,16 @@
+__author__ = 'ilya'
+
+class VersioningConfiguration:
+ template = \
+"""<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
+ <Status>%(state)s</Status>
+</VersioningConfiguration>"""
+ def __init__(self, *args, **kwargs):
+ self.state = kwargs['state']
+
+
+ def __str__(self):
+ return VersioningConfiguration.template % {'state':self.state}
+
+if __name__ == '__main__':
+ print VersioningConfiguration(state = 'Suspended')
View
44 run-tests.py 100755 → 100644
@@ -124,8 +124,8 @@ def compile_list(_list, regexps = False):
if not cmd_args:
return skip()
-
- p = Popen(cmd_args, stdout = PIPE, stderr = STDOUT, universal_newlines = True)
+ os.environ['LANG'] = 'en_US.utf8'
+ p = Popen(cmd_args, stdout = PIPE, stderr = STDOUT, universal_newlines = True, env = os.environ)
stdout, stderr = p.communicate()
if retcode != p.returncode:
return failure("retcode: %d, expected: %d" % (p.returncode, retcode))
@@ -362,6 +362,46 @@ def pbucket(tail):
test("Retrieve from URL", ['wget', '-O', 'testsuite-out/logo.png', 'http://%s.s3.amazonaws.com/xyz/etc/logo.png' % bucket(1)],
must_find_re = [ 'logo.png.*saved \[22059/22059\]' ])
+## ====== Verify Default Versioning
+test_s3cmd("Verify default versioning", ['info', '%s' % pbucket(1) ],
+ must_find_re = [ "Versioning:.*Disabled"])
+
+
+## ====== Set Versioning to Disabled
+test_s3cmd("Change versioning to Disabled", ['setversioning', '%s' % pbucket(1), '0'],
+ must_find = [ "%s/: Versioning set to Suspended" % pbucket(1)])
+
+
+## ====== Verify disabling versioning when disabled
+test_s3cmd("Verify disabling versioning when disabled", ['info', '%s' % pbucket(1) ],
+ must_find_re = [ "Versioning:.*Suspended"])
+
+
+## ====== Change Versioning to Enabled
+test_s3cmd("Change versioning to Enabled", ['setversioning', '%s' % pbucket(1), '1'],
+ must_find = [ "%s/: Versioning set to Enabled" % pbucket(1)])
+
+
+## ====== Verify enabling versioning when disabled
+test_s3cmd("Verify enabling versioning when disabled", ['info', '%s' % pbucket(1) ],
+ must_find_re = [ "Versioning:.*Enabled"])
+
+## ====== Disable versioning when enabled
+test_s3cmd("Change versioning to Disabled", ['setversioning', '%s' % pbucket(1), '0'],
+ must_find = [ "%s/: Versioning set to Suspended" % pbucket(1) ])
+
+
+## ====== Verify disabling versioning
+test_s3cmd("Verify disabling versioning when enabled", ['info', '%s' % pbucket(1) ],
+ must_find_re = [ "Versioning:.*Suspended"])
+
+
+## ====== Verify Private ACL
+if have_wget:
+ test("Verify Private ACL", ['wget', '-O', 'testsuite-out/logo.png', 'http://%s.s3.amazonaws.com/xyz/etc/logo.png' % bucket(1)],
+ retcode = 1,
+ must_find_re = [ 'ERROR 403: Forbidden' ])
+
## ====== Change ACL to Private
test_s3cmd("Change ACL to Private", ['setacl', '--acl-private', '%s/xyz/etc/l*.png' % pbucket(1)],
View
14 s3cmd
@@ -509,6 +509,7 @@ def cmd_info(args):
info = s3.bucket_info(uri)
output(u"%s (bucket):" % uri.uri())
output(u" Location: %s" % info['bucket-location'])
+ output(u" Versioning: %s" % info['versioning-state'])
acl = s3.get_acl(uri)
acl_grant_list = acl.getGrantList()
for grant in acl_grant_list:
@@ -910,6 +911,18 @@ def cmd_sync(args):
return cmd_sync_remote2remote(args)
raise ParameterError("Invalid source/destination: '%s'" % "' '".join(args))
+def cmd_setversioning(args):
+ s3 = S3(cfg)
+
+ uri = S3Uri(args[0])
+
+ versioning = ('Suspended', 'Enabled')[int(args[1])]
+ response = s3.set_versioning(uri, versioning)
+
+ if response['status'] == 200:
+ output(u"%s: Versioning set to %s" % (uri, versioning))
+
+
def cmd_setacl(args):
def _update_acl(uri, seq_label = ""):
something_changed = False
@@ -1319,6 +1332,7 @@ def get_commands_list():
{"cmd":"cp", "label":"Copy object", "param":"s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]", "func":cmd_cp, "argc":2},
{"cmd":"mv", "label":"Move object", "param":"s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]", "func":cmd_mv, "argc":2},
{"cmd":"setacl", "label":"Modify Access control list for Bucket or Files", "param":"s3://BUCKET[/OBJECT]", "func":cmd_setacl, "argc":1},
+ {"cmd":"setversioning", "label":"Set versioning status for Bucket", "param":"s3://BUCKET {0|1}", "func":cmd_setversioning, "argc":2},
{"cmd":"accesslog", "label":"Enable/disable bucket access logging", "param":"s3://BUCKET", "func":cmd_accesslog, "argc":1},
{"cmd":"sign", "label":"Sign arbitrary string using the secret key", "param":"STRING-TO-SIGN", "func":cmd_sign, "argc":1},
{"cmd":"fixbucket", "label":"Fix invalid file names in a bucket", "param":"s3://BUCKET[/PREFIX]", "func":cmd_fixbucket, "argc":1},
View
@@ -57,6 +57,9 @@ Move object
s3cmd \fBsetacl\fR \fIs3://BUCKET[/OBJECT]\fR
Modify Access control list for Bucket or Files
.TP
+s3cmd \fBsetversioning\fR \fIs3://BUCKET {0|1}\fR
+Set versioning status for Bucket
+.TP
s3cmd \fBaccesslog\fR \fIs3://BUCKET\fR
Enable/disable bucket access logging
.TP
@@ -105,7 +108,7 @@ show this help message and exit
Invoke interactive (re)configuration tool.
.TP
\fB\-c\fR FILE, \fB\-\-config\fR=FILE
-Config file name. Defaults to /home/mludvig/.s3cfg
+Config file name. Defaults to /home/margolin/.s3cfg
.TP
\fB\-\-dump\-config\fR
Dump current configuration after parsing config files

0 comments on commit 3bc3665

Please sign in to comment.