| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import boto | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | from boto.exception import S3ResponseError | 
					
						
							|  |  |  | from boto.s3.lifecycle import Lifecycle, Transition, Expiration, Rule | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import sure  # noqa | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  | from botocore.exceptions import ClientError | 
					
						
							|  |  |  | from datetime import datetime | 
					
						
							|  |  |  | from nose.tools import assert_raises | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  | from moto import mock_s3_deprecated, mock_s3 | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | def test_lifecycle_create(): | 
					
						
							|  |  |  |     conn = boto.s3.connect_to_region("us-west-1") | 
					
						
							| 
									
										
										
										
											2020-02-02 10:36:51 +00:00
										 |  |  |     bucket = conn.create_bucket("foobar", location="us-west-1") | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lifecycle = Lifecycle() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     lifecycle.add_rule("myid", "", "Enabled", 30) | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  |     bucket.configure_lifecycle(lifecycle) | 
					
						
							|  |  |  |     response = bucket.get_lifecycle_config() | 
					
						
							|  |  |  |     len(response).should.equal(1) | 
					
						
							|  |  |  |     lifecycle = response[0] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     lifecycle.id.should.equal("myid") | 
					
						
							|  |  |  |     lifecycle.prefix.should.equal("") | 
					
						
							|  |  |  |     lifecycle.status.should.equal("Enabled") | 
					
						
							| 
									
										
										
										
											2016-11-06 10:29:50 -05:00
										 |  |  |     list(lifecycle.transition).should.equal([]) | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  | @mock_s3 | 
					
						
							|  |  |  | def test_lifecycle_with_filters(): | 
					
						
							|  |  |  |     client = boto3.client("s3") | 
					
						
							| 
									
										
										
										
											2020-02-02 10:36:51 +00:00
										 |  |  |     client.create_bucket( | 
					
						
							|  |  |  |         Bucket="bucket", CreateBucketConfiguration={"LocationConstraint": "us-west-1"} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create a lifecycle rule with a Filter (no tags): | 
					
						
							|  |  |  |     lfc = { | 
					
						
							|  |  |  |         "Rules": [ | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Expiration": {"Days": 7}, | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |                 "ID": "wholebucket", | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Filter": {"Prefix": ""}, | 
					
						
							|  |  |  |                 "Status": "Enabled", | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |             } | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     assert result["Rules"][0]["Filter"]["Prefix"] == "" | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     assert not result["Rules"][0]["Filter"].get("And") | 
					
						
							|  |  |  |     assert not result["Rules"][0]["Filter"].get("Tag") | 
					
						
							|  |  |  |     with assert_raises(KeyError): | 
					
						
							|  |  |  |         assert result["Rules"][0]["Prefix"] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     # Without any prefixes and an empty filter (this is by default a prefix for the whole bucket): | 
					
						
							|  |  |  |     lfc = { | 
					
						
							|  |  |  |         "Rules": [ | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Expiration": {"Days": 7}, | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |                 "ID": "wholebucket", | 
					
						
							|  |  |  |                 "Filter": {}, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Status": "Enabled", | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |             } | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							|  |  |  |     with assert_raises(KeyError): | 
					
						
							|  |  |  |         assert result["Rules"][0]["Prefix"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # If we remove the filter -- and don't specify a Prefix, then this is bad: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     lfc["Rules"][0].pop("Filter") | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     with assert_raises(ClientError) as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |             Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     assert err.exception.response["Error"]["Code"] == "MalformedXML" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     # With a tag: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     lfc["Rules"][0]["Filter"] = {"Tag": {"Key": "mytag", "Value": "mytagvalue"}} | 
					
						
							|  |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     with assert_raises(KeyError): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         assert result["Rules"][0]["Filter"]["Prefix"] | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     assert not result["Rules"][0]["Filter"].get("And") | 
					
						
							|  |  |  |     assert result["Rules"][0]["Filter"]["Tag"]["Key"] == "mytag" | 
					
						
							|  |  |  |     assert result["Rules"][0]["Filter"]["Tag"]["Value"] == "mytagvalue" | 
					
						
							|  |  |  |     with assert_raises(KeyError): | 
					
						
							|  |  |  |         assert result["Rules"][0]["Prefix"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # With And (single tag): | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     lfc["Rules"][0]["Filter"] = { | 
					
						
							|  |  |  |         "And": { | 
					
						
							|  |  |  |             "Prefix": "some/prefix", | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "Tags": [{"Key": "mytag", "Value": "mytagvalue"}], | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							|  |  |  |     assert not result["Rules"][0]["Filter"].get("Prefix") | 
					
						
							|  |  |  |     assert result["Rules"][0]["Filter"]["And"]["Prefix"] == "some/prefix" | 
					
						
							|  |  |  |     assert len(result["Rules"][0]["Filter"]["And"]["Tags"]) == 1 | 
					
						
							|  |  |  |     assert result["Rules"][0]["Filter"]["And"]["Tags"][0]["Key"] == "mytag" | 
					
						
							|  |  |  |     assert result["Rules"][0]["Filter"]["And"]["Tags"][0]["Value"] == "mytagvalue" | 
					
						
							|  |  |  |     with assert_raises(KeyError): | 
					
						
							|  |  |  |         assert result["Rules"][0]["Prefix"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # With multiple And tags: | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     lfc["Rules"][0]["Filter"]["And"] = { | 
					
						
							|  |  |  |         "Prefix": "some/prefix", | 
					
						
							|  |  |  |         "Tags": [ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"Key": "mytag", "Value": "mytagvalue"}, | 
					
						
							|  |  |  |             {"Key": "mytag2", "Value": "mytagvalue2"}, | 
					
						
							|  |  |  |         ], | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     assert not result["Rules"][0]["Filter"].get("Prefix") | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     assert result["Rules"][0]["Filter"]["And"]["Prefix"] == "some/prefix" | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     assert len(result["Rules"][0]["Filter"]["And"]["Tags"]) == 2 | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     assert result["Rules"][0]["Filter"]["And"]["Tags"][0]["Key"] == "mytag" | 
					
						
							|  |  |  |     assert result["Rules"][0]["Filter"]["And"]["Tags"][0]["Value"] == "mytagvalue" | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     assert result["Rules"][0]["Filter"]["And"]["Tags"][1]["Key"] == "mytag2" | 
					
						
							|  |  |  |     assert result["Rules"][0]["Filter"]["And"]["Tags"][1]["Value"] == "mytagvalue2" | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     with assert_raises(KeyError): | 
					
						
							|  |  |  |         assert result["Rules"][0]["Prefix"] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     # And filter without Prefix but multiple Tags: | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     lfc["Rules"][0]["Filter"]["And"] = { | 
					
						
							|  |  |  |         "Tags": [ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             {"Key": "mytag", "Value": "mytagvalue"}, | 
					
						
							|  |  |  |             {"Key": "mytag2", "Value": "mytagvalue2"}, | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |         ] | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     with assert_raises(KeyError): | 
					
						
							|  |  |  |         assert result["Rules"][0]["Filter"]["And"]["Prefix"] | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     assert len(result["Rules"][0]["Filter"]["And"]["Tags"]) == 2 | 
					
						
							|  |  |  |     assert result["Rules"][0]["Filter"]["And"]["Tags"][0]["Key"] == "mytag" | 
					
						
							|  |  |  |     assert result["Rules"][0]["Filter"]["And"]["Tags"][0]["Value"] == "mytagvalue" | 
					
						
							|  |  |  |     assert result["Rules"][0]["Filter"]["And"]["Tags"][1]["Key"] == "mytag2" | 
					
						
							|  |  |  |     assert result["Rules"][0]["Filter"]["And"]["Tags"][1]["Value"] == "mytagvalue2" | 
					
						
							|  |  |  |     with assert_raises(KeyError): | 
					
						
							|  |  |  |         assert result["Rules"][0]["Prefix"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Can't have both filter and prefix: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     lfc["Rules"][0]["Prefix"] = "" | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     with assert_raises(ClientError) as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |             Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     assert err.exception.response["Error"]["Code"] == "MalformedXML" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     lfc["Rules"][0]["Prefix"] = "some/path" | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     with assert_raises(ClientError) as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |             Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     assert err.exception.response["Error"]["Code"] == "MalformedXML" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # No filters -- just a prefix: | 
					
						
							|  |  |  |     del lfc["Rules"][0]["Filter"] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert not result["Rules"][0].get("Filter") | 
					
						
							|  |  |  |     assert result["Rules"][0]["Prefix"] == "some/path" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     # Can't have Tag, Prefix, and And in a filter: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     del lfc["Rules"][0]["Prefix"] | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     lfc["Rules"][0]["Filter"] = { | 
					
						
							|  |  |  |         "Prefix": "some/prefix", | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "Tag": {"Key": "mytag", "Value": "mytagvalue"}, | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |     with assert_raises(ClientError) as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |             Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     assert err.exception.response["Error"]["Code"] == "MalformedXML" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     lfc["Rules"][0]["Filter"] = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "Tag": {"Key": "mytag", "Value": "mytagvalue"}, | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |         "And": { | 
					
						
							|  |  |  |             "Prefix": "some/prefix", | 
					
						
							|  |  |  |             "Tags": [ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 {"Key": "mytag", "Value": "mytagvalue"}, | 
					
						
							|  |  |  |                 {"Key": "mytag2", "Value": "mytagvalue2"}, | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |     with assert_raises(ClientError) as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |             Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     assert err.exception.response["Error"]["Code"] == "MalformedXML" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Make sure multiple rules work: | 
					
						
							|  |  |  |     lfc = { | 
					
						
							|  |  |  |         "Rules": [ | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Expiration": {"Days": 7}, | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |                 "ID": "wholebucket", | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Filter": {"Prefix": ""}, | 
					
						
							|  |  |  |                 "Status": "Enabled", | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |             }, | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Expiration": {"Days": 10}, | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |                 "ID": "Tags", | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Filter": {"Tag": {"Key": "somekey", "Value": "somevalue"}}, | 
					
						
							|  |  |  |                 "Status": "Enabled", | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |         ] | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket")["Rules"] | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  |     assert len(result) == 2 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     assert result[0]["ID"] == "wholebucket" | 
					
						
							|  |  |  |     assert result[1]["ID"] == "Tags" | 
					
						
							| 
									
										
										
										
											2019-10-05 11:46:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | @mock_s3 | 
					
						
							|  |  |  | def test_lifecycle_with_eodm(): | 
					
						
							|  |  |  |     client = boto3.client("s3") | 
					
						
							| 
									
										
										
										
											2020-02-02 10:36:51 +00:00
										 |  |  |     client.create_bucket( | 
					
						
							|  |  |  |         Bucket="bucket", CreateBucketConfiguration={"LocationConstraint": "us-west-1"} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lfc = { | 
					
						
							|  |  |  |         "Rules": [ | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Expiration": {"ExpiredObjectDeleteMarker": True}, | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |                 "ID": "wholebucket", | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Filter": {"Prefix": ""}, | 
					
						
							|  |  |  |                 "Status": "Enabled", | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |             } | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							|  |  |  |     assert result["Rules"][0]["Expiration"]["ExpiredObjectDeleteMarker"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Set to False: | 
					
						
							|  |  |  |     lfc["Rules"][0]["Expiration"]["ExpiredObjectDeleteMarker"] = False | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							|  |  |  |     assert not result["Rules"][0]["Expiration"]["ExpiredObjectDeleteMarker"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # With failure: | 
					
						
							|  |  |  |     lfc["Rules"][0]["Expiration"]["Days"] = 7 | 
					
						
							|  |  |  |     with assert_raises(ClientError) as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |             Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     assert err.exception.response["Error"]["Code"] == "MalformedXML" | 
					
						
							|  |  |  |     del lfc["Rules"][0]["Expiration"]["Days"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     lfc["Rules"][0]["Expiration"]["Date"] = datetime(2015, 1, 1) | 
					
						
							|  |  |  |     with assert_raises(ClientError) as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |             Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-04-02 14:19:14 -07:00
										 |  |  |     assert err.exception.response["Error"]["Code"] == "MalformedXML" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  | @mock_s3 | 
					
						
							|  |  |  | def test_lifecycle_with_nve(): | 
					
						
							|  |  |  |     client = boto3.client("s3") | 
					
						
							| 
									
										
										
										
											2020-02-02 10:36:51 +00:00
										 |  |  |     client.create_bucket( | 
					
						
							|  |  |  |         Bucket="bucket", CreateBucketConfiguration={"LocationConstraint": "us-west-1"} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lfc = { | 
					
						
							|  |  |  |         "Rules": [ | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "NoncurrentVersionExpiration": {"NoncurrentDays": 30}, | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |                 "ID": "wholebucket", | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Filter": {"Prefix": ""}, | 
					
						
							|  |  |  |                 "Status": "Enabled", | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |             } | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							|  |  |  |     assert result["Rules"][0]["NoncurrentVersionExpiration"]["NoncurrentDays"] == 30 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Change NoncurrentDays: | 
					
						
							|  |  |  |     lfc["Rules"][0]["NoncurrentVersionExpiration"]["NoncurrentDays"] = 10 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							|  |  |  |     assert result["Rules"][0]["NoncurrentVersionExpiration"]["NoncurrentDays"] == 10 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-05 11:04:55 -05:00
										 |  |  |     # TODO: Add test for failures due to missing children | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_s3 | 
					
						
							|  |  |  | def test_lifecycle_with_nvt(): | 
					
						
							|  |  |  |     client = boto3.client("s3") | 
					
						
							| 
									
										
										
										
											2020-02-02 10:36:51 +00:00
										 |  |  |     client.create_bucket( | 
					
						
							|  |  |  |         Bucket="bucket", CreateBucketConfiguration={"LocationConstraint": "us-west-1"} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lfc = { | 
					
						
							|  |  |  |         "Rules": [ | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "NoncurrentVersionTransitions": [ | 
					
						
							|  |  |  |                     {"NoncurrentDays": 30, "StorageClass": "ONEZONE_IA"} | 
					
						
							|  |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |                 "ID": "wholebucket", | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Filter": {"Prefix": ""}, | 
					
						
							|  |  |  |                 "Status": "Enabled", | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |             } | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							| 
									
										
										
										
											2018-10-03 01:11:11 -05:00
										 |  |  |     assert result["Rules"][0]["NoncurrentVersionTransitions"][0]["NoncurrentDays"] == 30 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     assert ( | 
					
						
							|  |  |  |         result["Rules"][0]["NoncurrentVersionTransitions"][0]["StorageClass"] | 
					
						
							|  |  |  |         == "ONEZONE_IA" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Change NoncurrentDays: | 
					
						
							| 
									
										
										
										
											2018-10-03 01:11:11 -05:00
										 |  |  |     lfc["Rules"][0]["NoncurrentVersionTransitions"][0]["NoncurrentDays"] = 10 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							| 
									
										
										
										
											2018-10-03 01:11:11 -05:00
										 |  |  |     assert result["Rules"][0]["NoncurrentVersionTransitions"][0]["NoncurrentDays"] == 10 | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Change StorageClass: | 
					
						
							| 
									
										
										
										
											2018-10-03 01:11:11 -05:00
										 |  |  |     lfc["Rules"][0]["NoncurrentVersionTransitions"][0]["StorageClass"] = "GLACIER" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     assert ( | 
					
						
							|  |  |  |         result["Rules"][0]["NoncurrentVersionTransitions"][0]["StorageClass"] | 
					
						
							|  |  |  |         == "GLACIER" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # With failures for missing children: | 
					
						
							| 
									
										
										
										
											2018-10-03 01:11:11 -05:00
										 |  |  |     del lfc["Rules"][0]["NoncurrentVersionTransitions"][0]["NoncurrentDays"] | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |     with assert_raises(ClientError) as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |             Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |     assert err.exception.response["Error"]["Code"] == "MalformedXML" | 
					
						
							| 
									
										
										
										
											2018-10-03 01:11:11 -05:00
										 |  |  |     lfc["Rules"][0]["NoncurrentVersionTransitions"][0]["NoncurrentDays"] = 30 | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-03 01:11:11 -05:00
										 |  |  |     del lfc["Rules"][0]["NoncurrentVersionTransitions"][0]["StorageClass"] | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |     with assert_raises(ClientError) as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |             Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |     assert err.exception.response["Error"]["Code"] == "MalformedXML" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_s3 | 
					
						
							|  |  |  | def test_lifecycle_with_aimu(): | 
					
						
							|  |  |  |     client = boto3.client("s3") | 
					
						
							| 
									
										
										
										
											2020-02-02 10:36:51 +00:00
										 |  |  |     client.create_bucket( | 
					
						
							|  |  |  |         Bucket="bucket", CreateBucketConfiguration={"LocationConstraint": "us-west-1"} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lfc = { | 
					
						
							|  |  |  |         "Rules": [ | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "AbortIncompleteMultipartUpload": {"DaysAfterInitiation": 7}, | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |                 "ID": "wholebucket", | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "Filter": {"Prefix": ""}, | 
					
						
							|  |  |  |                 "Status": "Enabled", | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |             } | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     assert ( | 
					
						
							|  |  |  |         result["Rules"][0]["AbortIncompleteMultipartUpload"]["DaysAfterInitiation"] == 7 | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Change DaysAfterInitiation: | 
					
						
							|  |  |  |     lfc["Rules"][0]["AbortIncompleteMultipartUpload"]["DaysAfterInitiation"] = 30 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client.put_bucket_lifecycle_configuration( | 
					
						
							|  |  |  |         Bucket="bucket", LifecycleConfiguration=lfc | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  |     result = client.get_bucket_lifecycle_configuration(Bucket="bucket") | 
					
						
							|  |  |  |     assert len(result["Rules"]) == 1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     assert ( | 
					
						
							|  |  |  |         result["Rules"][0]["AbortIncompleteMultipartUpload"]["DaysAfterInitiation"] | 
					
						
							|  |  |  |         == 30 | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-05 11:04:55 -05:00
										 |  |  |     # TODO: Add test for failures due to missing children | 
					
						
							| 
									
										
										
										
											2018-10-03 00:40:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | def test_lifecycle_with_glacier_transition(): | 
					
						
							|  |  |  |     conn = boto.s3.connect_to_region("us-west-1") | 
					
						
							| 
									
										
										
										
											2020-02-02 10:36:51 +00:00
										 |  |  |     bucket = conn.create_bucket("foobar", location="us-west-1") | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lifecycle = Lifecycle() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     transition = Transition(days=30, storage_class="GLACIER") | 
					
						
							|  |  |  |     rule = Rule( | 
					
						
							|  |  |  |         "myid", prefix="", status="Enabled", expiration=None, transition=transition | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  |     lifecycle.append(rule) | 
					
						
							|  |  |  |     bucket.configure_lifecycle(lifecycle) | 
					
						
							|  |  |  |     response = bucket.get_lifecycle_config() | 
					
						
							|  |  |  |     transition = response[0].transition | 
					
						
							|  |  |  |     transition.days.should.equal(30) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     transition.storage_class.should.equal("GLACIER") | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  |     transition.date.should.equal(None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | def test_lifecycle_multi(): | 
					
						
							|  |  |  |     conn = boto.s3.connect_to_region("us-west-1") | 
					
						
							| 
									
										
										
										
											2020-02-02 10:36:51 +00:00
										 |  |  |     bucket = conn.create_bucket("foobar", location="us-west-1") | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     date = "2022-10-12T00:00:00.000Z" | 
					
						
							|  |  |  |     sc = "GLACIER" | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  |     lifecycle = Lifecycle() | 
					
						
							|  |  |  |     lifecycle.add_rule("1", "1/", "Enabled", 1) | 
					
						
							|  |  |  |     lifecycle.add_rule("2", "2/", "Enabled", Expiration(days=2)) | 
					
						
							|  |  |  |     lifecycle.add_rule("3", "3/", "Enabled", Expiration(date=date)) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     lifecycle.add_rule("4", "4/", "Enabled", None, Transition(days=4, storage_class=sc)) | 
					
						
							|  |  |  |     lifecycle.add_rule( | 
					
						
							|  |  |  |         "5", "5/", "Enabled", None, Transition(date=date, storage_class=sc) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bucket.configure_lifecycle(lifecycle) | 
					
						
							|  |  |  |     # read the lifecycle back | 
					
						
							|  |  |  |     rules = bucket.get_lifecycle_config() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for rule in rules: | 
					
						
							|  |  |  |         if rule.id == "1": | 
					
						
							|  |  |  |             rule.prefix.should.equal("1/") | 
					
						
							|  |  |  |             rule.expiration.days.should.equal(1) | 
					
						
							|  |  |  |         elif rule.id == "2": | 
					
						
							|  |  |  |             rule.prefix.should.equal("2/") | 
					
						
							|  |  |  |             rule.expiration.days.should.equal(2) | 
					
						
							|  |  |  |         elif rule.id == "3": | 
					
						
							|  |  |  |             rule.prefix.should.equal("3/") | 
					
						
							|  |  |  |             rule.expiration.date.should.equal(date) | 
					
						
							|  |  |  |         elif rule.id == "4": | 
					
						
							|  |  |  |             rule.prefix.should.equal("4/") | 
					
						
							|  |  |  |             rule.transition.days.should.equal(4) | 
					
						
							|  |  |  |             rule.transition.storage_class.should.equal(sc) | 
					
						
							|  |  |  |         elif rule.id == "5": | 
					
						
							|  |  |  |             rule.prefix.should.equal("5/") | 
					
						
							|  |  |  |             rule.transition.date.should.equal(date) | 
					
						
							|  |  |  |             rule.transition.storage_class.should.equal(sc) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             assert False, "Invalid rule id" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_s3_deprecated | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | def test_lifecycle_delete(): | 
					
						
							|  |  |  |     conn = boto.s3.connect_to_region("us-west-1") | 
					
						
							| 
									
										
										
										
											2020-02-02 10:36:51 +00:00
										 |  |  |     bucket = conn.create_bucket("foobar", location="us-west-1") | 
					
						
							| 
									
										
										
										
											2015-06-02 23:13:51 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lifecycle = Lifecycle() | 
					
						
							|  |  |  |     lifecycle.add_rule(expiration=30) | 
					
						
							|  |  |  |     bucket.configure_lifecycle(lifecycle) | 
					
						
							|  |  |  |     response = bucket.get_lifecycle_config() | 
					
						
							|  |  |  |     response.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bucket.delete_lifecycle_configuration() | 
					
						
							|  |  |  |     bucket.get_lifecycle_config.when.called_with().should.throw(S3ResponseError) |