1
1
from __future__ import unicode_literals
2
2
3
3
import boto
4
+ import boto3
4
5
from boto .exception import S3ResponseError
5
6
from boto .s3 .lifecycle import Lifecycle , Transition , Expiration , Rule
6
7
7
8
import sure # noqa
9
+ from botocore .exceptions import ClientError
10
+ from datetime import datetime
11
+ from nose .tools import assert_raises
8
12
9
- from moto import mock_s3_deprecated
13
+ from moto import mock_s3_deprecated , mock_s3
10
14
11
15
12
16
@mock_s3_deprecated
@@ -26,6 +30,167 @@ def test_lifecycle_create():
26
30
list (lifecycle .transition ).should .equal ([])
27
31
28
32
33
+ @mock_s3
34
+ def test_lifecycle_with_filters ():
35
+ client = boto3 .client ("s3" )
36
+ client .create_bucket (Bucket = "bucket" )
37
+
38
+ # Create a lifecycle rule with a Filter (no tags):
39
+ lfc = {
40
+ "Rules" : [
41
+ {
42
+ "Expiration" : {
43
+ "Days" : 7
44
+ },
45
+ "ID" : "wholebucket" ,
46
+ "Filter" : {
47
+ "Prefix" : ""
48
+ },
49
+ "Status" : "Enabled"
50
+ }
51
+ ]
52
+ }
53
+ client .put_bucket_lifecycle_configuration (Bucket = "bucket" , LifecycleConfiguration = lfc )
54
+ result = client .get_bucket_lifecycle_configuration (Bucket = "bucket" )
55
+ assert len (result ["Rules" ]) == 1
56
+ assert result ["Rules" ][0 ]["Filter" ]["Prefix" ] == ''
57
+ assert not result ["Rules" ][0 ]["Filter" ].get ("And" )
58
+ assert not result ["Rules" ][0 ]["Filter" ].get ("Tag" )
59
+ with assert_raises (KeyError ):
60
+ assert result ["Rules" ][0 ]["Prefix" ]
61
+
62
+ # With a tag:
63
+ lfc ["Rules" ][0 ]["Filter" ]["Tag" ] = {
64
+ "Key" : "mytag" ,
65
+ "Value" : "mytagvalue"
66
+ }
67
+ client .put_bucket_lifecycle_configuration (Bucket = "bucket" , LifecycleConfiguration = lfc )
68
+ result = client .get_bucket_lifecycle_configuration (Bucket = "bucket" )
69
+ assert len (result ["Rules" ]) == 1
70
+ assert result ["Rules" ][0 ]["Filter" ]["Prefix" ] == ''
71
+ assert not result ["Rules" ][0 ]["Filter" ].get ("And" )
72
+ assert result ["Rules" ][0 ]["Filter" ]["Tag" ]["Key" ] == "mytag"
73
+ assert result ["Rules" ][0 ]["Filter" ]["Tag" ]["Value" ] == "mytagvalue"
74
+ with assert_raises (KeyError ):
75
+ assert result ["Rules" ][0 ]["Prefix" ]
76
+
77
+ # With And (single tag):
78
+ lfc ["Rules" ][0 ]["Filter" ]["And" ] = {
79
+ "Prefix" : "some/prefix" ,
80
+ "Tags" : [
81
+ {
82
+ "Key" : "mytag" ,
83
+ "Value" : "mytagvalue"
84
+ }
85
+ ]
86
+ }
87
+ client .put_bucket_lifecycle_configuration (Bucket = "bucket" , LifecycleConfiguration = lfc )
88
+ result = client .get_bucket_lifecycle_configuration (Bucket = "bucket" )
89
+ assert len (result ["Rules" ]) == 1
90
+ assert result ["Rules" ][0 ]["Filter" ]["Prefix" ] == ""
91
+ assert result ["Rules" ][0 ]["Filter" ]["And" ]["Prefix" ] == "some/prefix"
92
+ assert len (result ["Rules" ][0 ]["Filter" ]["And" ]["Tags" ]) == 1
93
+ assert result ["Rules" ][0 ]["Filter" ]["And" ]["Tags" ][0 ]["Key" ] == "mytag"
94
+ assert result ["Rules" ][0 ]["Filter" ]["And" ]["Tags" ][0 ]["Value" ] == "mytagvalue"
95
+ assert result ["Rules" ][0 ]["Filter" ]["Tag" ]["Key" ] == "mytag"
96
+ assert result ["Rules" ][0 ]["Filter" ]["Tag" ]["Value" ] == "mytagvalue"
97
+ with assert_raises (KeyError ):
98
+ assert result ["Rules" ][0 ]["Prefix" ]
99
+
100
+ # With multiple And tags:
101
+ lfc ["Rules" ][0 ]["Filter" ]["And" ] = {
102
+ "Prefix" : "some/prefix" ,
103
+ "Tags" : [
104
+ {
105
+ "Key" : "mytag" ,
106
+ "Value" : "mytagvalue"
107
+ },
108
+ {
109
+ "Key" : "mytag2" ,
110
+ "Value" : "mytagvalue2"
111
+ }
112
+ ]
113
+ }
114
+ client .put_bucket_lifecycle_configuration (Bucket = "bucket" , LifecycleConfiguration = lfc )
115
+ result = client .get_bucket_lifecycle_configuration (Bucket = "bucket" )
116
+ assert len (result ["Rules" ]) == 1
117
+ assert result ["Rules" ][0 ]["Filter" ]["Prefix" ] == ""
118
+ assert result ["Rules" ][0 ]["Filter" ]["And" ]["Prefix" ] == "some/prefix"
119
+ assert len (result ["Rules" ][0 ]["Filter" ]["And" ]["Tags" ]) == 2
120
+ assert result ["Rules" ][0 ]["Filter" ]["And" ]["Tags" ][0 ]["Key" ] == "mytag"
121
+ assert result ["Rules" ][0 ]["Filter" ]["And" ]["Tags" ][0 ]["Value" ] == "mytagvalue"
122
+ assert result ["Rules" ][0 ]["Filter" ]["Tag" ]["Key" ] == "mytag"
123
+ assert result ["Rules" ][0 ]["Filter" ]["Tag" ]["Value" ] == "mytagvalue"
124
+ assert result ["Rules" ][0 ]["Filter" ]["And" ]["Tags" ][1 ]["Key" ] == "mytag2"
125
+ assert result ["Rules" ][0 ]["Filter" ]["And" ]["Tags" ][1 ]["Value" ] == "mytagvalue2"
126
+ assert result ["Rules" ][0 ]["Filter" ]["Tag" ]["Key" ] == "mytag"
127
+ assert result ["Rules" ][0 ]["Filter" ]["Tag" ]["Value" ] == "mytagvalue"
128
+ with assert_raises (KeyError ):
129
+ assert result ["Rules" ][0 ]["Prefix" ]
130
+
131
+ # Can't have both filter and prefix:
132
+ lfc ["Rules" ][0 ]["Prefix" ] = ''
133
+ with assert_raises (ClientError ) as err :
134
+ client .put_bucket_lifecycle_configuration (Bucket = "bucket" , LifecycleConfiguration = lfc )
135
+ assert err .exception .response ["Error" ]["Code" ] == "MalformedXML"
136
+
137
+ lfc ["Rules" ][0 ]["Prefix" ] = 'some/path'
138
+ with assert_raises (ClientError ) as err :
139
+ client .put_bucket_lifecycle_configuration (Bucket = "bucket" , LifecycleConfiguration = lfc )
140
+ assert err .exception .response ["Error" ]["Code" ] == "MalformedXML"
141
+
142
+ # No filters -- just a prefix:
143
+ del lfc ["Rules" ][0 ]["Filter" ]
144
+ client .put_bucket_lifecycle_configuration (Bucket = "bucket" , LifecycleConfiguration = lfc )
145
+ result = client .get_bucket_lifecycle_configuration (Bucket = "bucket" )
146
+ assert not result ["Rules" ][0 ].get ("Filter" )
147
+ assert result ["Rules" ][0 ]["Prefix" ] == "some/path"
148
+
149
+
150
+ @mock_s3
151
+ def test_lifecycle_with_eodm ():
152
+ client = boto3 .client ("s3" )
153
+ client .create_bucket (Bucket = "bucket" )
154
+
155
+ lfc = {
156
+ "Rules" : [
157
+ {
158
+ "Expiration" : {
159
+ "ExpiredObjectDeleteMarker" : True
160
+ },
161
+ "ID" : "wholebucket" ,
162
+ "Filter" : {
163
+ "Prefix" : ""
164
+ },
165
+ "Status" : "Enabled"
166
+ }
167
+ ]
168
+ }
169
+ client .put_bucket_lifecycle_configuration (Bucket = "bucket" , LifecycleConfiguration = lfc )
170
+ result = client .get_bucket_lifecycle_configuration (Bucket = "bucket" )
171
+ assert len (result ["Rules" ]) == 1
172
+ assert result ["Rules" ][0 ]["Expiration" ]["ExpiredObjectDeleteMarker" ]
173
+
174
+ # Set to False:
175
+ lfc ["Rules" ][0 ]["Expiration" ]["ExpiredObjectDeleteMarker" ] = False
176
+ client .put_bucket_lifecycle_configuration (Bucket = "bucket" , LifecycleConfiguration = lfc )
177
+ result = client .get_bucket_lifecycle_configuration (Bucket = "bucket" )
178
+ assert len (result ["Rules" ]) == 1
179
+ assert not result ["Rules" ][0 ]["Expiration" ]["ExpiredObjectDeleteMarker" ]
180
+
181
+ # With failure:
182
+ lfc ["Rules" ][0 ]["Expiration" ]["Days" ] = 7
183
+ with assert_raises (ClientError ) as err :
184
+ client .put_bucket_lifecycle_configuration (Bucket = "bucket" , LifecycleConfiguration = lfc )
185
+ assert err .exception .response ["Error" ]["Code" ] == "MalformedXML"
186
+ del lfc ["Rules" ][0 ]["Expiration" ]["Days" ]
187
+
188
+ lfc ["Rules" ][0 ]["Expiration" ]["Date" ] = datetime (2015 , 1 , 1 )
189
+ with assert_raises (ClientError ) as err :
190
+ client .put_bucket_lifecycle_configuration (Bucket = "bucket" , LifecycleConfiguration = lfc )
191
+ assert err .exception .response ["Error" ]["Code" ] == "MalformedXML"
192
+
193
+
29
194
@mock_s3_deprecated
30
195
def test_lifecycle_with_glacier_transition ():
31
196
conn = boto .s3 .connect_to_region ("us-west-1" )
0 commit comments