test_irods_resource_plugin_s3.py 35.8 KB
Newer Older
1
try:
2
   from minio import Minio
3
   from minio.error import ResponseError
4
except ImportError:
5
   print('This test requires minio: perhaps try pip install minio')
6
7
   exit()

8
import commands
Ben Keller's avatar
Ben Keller committed
9
import datetime
10
import os
11
import platform
12
import random
Ben Keller's avatar
Ben Keller committed
13
14
import re
import shutil
15
import string
Ben Keller's avatar
Ben Keller committed
16
import subprocess
17
import urllib3
18

19
20
from resource_suite_s3_nocache import Test_S3_NoCache_Base

21
import sys
22
if sys.version_info >= (2,7):
23
    import unittest
24
25
else:
    import unittest2 as unittest
26

Ben Keller's avatar
Ben Keller committed
27
28
from .. import lib
from . import session
29
from ..configuration import IrodsConfig
Ben Keller's avatar
Ben Keller committed
30
31
32
from .resource_suite import ResourceSuite
from .test_chunkydevtest import ChunkyDevTest

33
34


35
36
37
38
39
40
41
42
class Test_Compound_With_S3_Resource(ResourceSuite, ChunkyDevTest, unittest.TestCase):
    def __init__(self, *args, **kwargs):
        self.keypairfile='/projects/irods/vsphere-testing/externals/amazon_web_services-CI.keypair'
        self.archive_naming_policy='decoupled'
        self.s3stsdate=''
        self.s3region='us-east-1'
        self.s3endPoint='s3.amazonaws.com'
        self.s3signature_version=2
43
        self.s3sse = 0 # server side encryption
44
45
        super(Test_Compound_With_S3_Resource, self).__init__(*args, **kwargs)

46
    def setUp(self):
47
48
49
50
        # skip ssl tests on ub12
        distro_str = ''.join(platform.linux_distribution()[:2]).replace(' ','')
        if self._testMethodName.startswith('test_ssl') and distro_str.lower().startswith('ubuntu12'):
           self.skipTest("skipping ssl tests on ubuntu 12")
51
52

        # set up aws configuration
53
        self.read_aws_keys()
54
55

        # set up s3 bucket
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
        try:
            httpClient = urllib3.poolmanager.ProxyManager(
                os.environ['http_proxy'],
                timeout=urllib3.Timeout.DEFAULT_TIMEOUT,
                cert_reqs='CERT_REQUIRED',
                retries=urllib3.Retry(
                    total=5,
                    backoff_factor=0.2,
                    status_forcelist=[500, 502, 503, 504]
                )
            )
        except KeyError:
            httpClient = None
        s3_client = Minio(self.s3endPoint,
                          access_key=self.aws_access_key_id,
                          secret_key=self.aws_secret_access_key,
                          http_client=httpClient)
73

74
75
        self.s3bucketname = 'irods-ci-' + distro_str + datetime.datetime.utcnow().strftime('-%Y-%m-%d.%H-%M-%S-%f-')
        self.s3bucketname += ''.join(random.choice(string.letters) for i in xrange(10))
76
        self.s3bucketname = self.s3bucketname[:63].lower() # bucket names can be no more than 63 characters long
77
        s3_client.make_bucket(self.s3bucketname, location=self.s3region)
78
79

        # set up resources
80
        hostname = lib.get_hostname()
81
82
83
84
85
86
87
88
89
90
91
92
        s3params = 'S3_RETRY_COUNT=15;S3_WAIT_TIME_SEC=1;S3_PROTO=HTTPS;S3_MPU_CHUNK=10;S3_MPU_THREADS=4;S3_ENABLE_MD5=1'
        s3params += ';S3_STSDATE=' + self.s3stsdate
        s3params += ';S3_DEFAULT_HOSTNAME=' + self.s3endPoint
        s3params += ';S3_AUTH_FILE=' +  self.keypairfile
        s3params += ';S3_REGIONNAME=' + self.s3region
        s3params += ';S3_SIGNATURE_VERSION=' + str(self.s3signature_version)
        s3params += ';ARCHIVE_NAMING_POLICY=' + self.archive_naming_policy
        try:
            s3params += ';S3_SERVER_ENCRYPT=' + str(self.s3sse)
        except AttributeError:
            pass

93
94
        s3params=os.environ.get('S3PARAMS', s3params);

Ben Keller's avatar
Ben Keller committed
95
96
97
        with session.make_session_for_existing_admin() as admin_session:
            irods_config = IrodsConfig()
            admin_session.assert_icommand("iadmin modresc demoResc name origResc", 'STDOUT_SINGLELINE', 'rename', input='yes\n')
98
            admin_session.assert_icommand("iadmin mkresc demoResc compound", 'STDOUT_SINGLELINE', 'compound')
Ben Keller's avatar
Ben Keller committed
99
            admin_session.assert_icommand("iadmin mkresc cacheResc 'unixfilesystem' " + hostname + ":" + irods_config.irods_directory + "/cacheRescVault", 'STDOUT_SINGLELINE', 'cacheResc')
100
            admin_session.assert_icommand('iadmin mkresc archiveResc s3 '+hostname+':/'+self.s3bucketname+'/irods/Vault "'+s3params+'"', 'STDOUT_SINGLELINE', 'archiveResc')
101
102
            admin_session.assert_icommand("iadmin addchildtoresc demoResc cacheResc cache")
            admin_session.assert_icommand("iadmin addchildtoresc demoResc archiveResc archive")
103

104
        super(Test_Compound_With_S3_Resource, self).setUp()
105
106

    def tearDown(self):
107
        super(Test_Compound_With_S3_Resource, self).tearDown()
108
109

        # delete s3 bucket
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
        try:
            httpClient = urllib3.poolmanager.ProxyManager(
                os.environ['http_proxy'],
                timeout=urllib3.Timeout.DEFAULT_TIMEOUT,
                cert_reqs='CERT_REQUIRED',
                retries=urllib3.Retry(
                    total=5,
                    backoff_factor=0.2,
                    status_forcelist=[500, 502, 503, 504]
                )
            )
        except KeyError:
            httpClient = None
        s3_client = Minio(self.s3endPoint,
                          access_key=self.aws_access_key_id,
                          secret_key=self.aws_secret_access_key,
                          http_client=httpClient)

128
        objects = s3_client.list_objects_v2(self.s3bucketname, recursive=True)
129
130
131
132
133
        try:
            for del_err in s3_client.remove_objects(self.s3bucketname, [object.object_name for object in objects]):
                print("Deletion Error: {}".format(del_err))
        except ResponseError as err:
            print(err)
134
        s3_client.remove_bucket(self.s3bucketname)
135
136

        # tear down resources
Ben Keller's avatar
Ben Keller committed
137
        with session.make_session_for_existing_admin() as admin_session:
138
139
140
141
142
            admin_session.assert_icommand("iadmin rmchildfromresc demoResc archiveResc")
            admin_session.assert_icommand("iadmin rmchildfromresc demoResc cacheResc")
            admin_session.assert_icommand("iadmin rmresc archiveResc")
            admin_session.assert_icommand("iadmin rmresc cacheResc")
            admin_session.assert_icommand("iadmin rmresc demoResc")
Ben Keller's avatar
Ben Keller committed
143
            admin_session.assert_icommand("iadmin modresc origResc name demoResc", 'STDOUT_SINGLELINE', 'rename', input='yes\n')
144

Ben Keller's avatar
Ben Keller committed
145
        shutil.rmtree(IrodsConfig().irods_directory + "/cacheRescVault", ignore_errors=True)
146

147
    def read_aws_keys(self):
148
149
        # read access keys from keypair file
        with open(self.keypairfile) as f:
150
151
            self.aws_access_key_id = f.readline().rstrip()
            self.aws_secret_access_key = f.readline().rstrip()
152

153
    def test_irm_specific_replica(self):
154
155
156
        self.admin.assert_icommand("ils -L "+self.testfile,'STDOUT_SINGLELINE',self.testfile) # should be listed
        self.admin.assert_icommand("irepl -R "+self.testresc+" "+self.testfile) # creates replica
        self.admin.assert_icommand("ils -L "+self.testfile,'STDOUT_SINGLELINE',self.testfile) # should be listed twice
157
        self.admin.assert_icommand("irm -n 0 "+self.testfile, 'STDOUT_SINGLELINE','deprecated') # remove original from cacheResc only
158
159
160
161
        self.admin.assert_icommand("ils -L "+self.testfile,'STDOUT_SINGLELINE',["2 "+self.testresc,self.testfile]) # replica 2 should still be there
        self.admin.assert_icommand_fail("ils -L "+self.testfile,'STDOUT_SINGLELINE',["0 "+self.admin.default_resource,self.testfile]) # replica 0 should be gone
        trashpath = self.admin.session_collection_trash
        self.admin.assert_icommand_fail("ils -L "+trashpath+"/"+self.testfile,'STDOUT_SINGLELINE',["0 "+self.admin.default_resource,self.testfile]) # replica should not be in trash
162
163
164
165
166
167
168
169
170
171
172
173
174

    @unittest.skip("--wlock has possible race condition due to Compound/Replication PDMO")
    def test_local_iput_collision_with_wlock(self):
        pass

    @unittest.skip("NOTSURE / FIXME ... -K not supported, perhaps")
    def test_local_iput_checksum(self):
        pass

    @unittest.skip("EMPTY_RESC_PATH - no vault path for coordinating resources")
    def test_ireg_as_rodsuser_in_vault(self):
        pass

175
176
177
178
    @unittest.skip("No Vault for S3 archive resource")
    def test_iput_overwrite_others_file__ticket_2086(self):
        pass

179
180
181
    def test_local_iput_with_force_and_destination_resource__ticket_1706(self):
        # local setup
        filename = "iputwithforceanddestination.txt"
182
        filepath = lib.create_local_testfile(filename)
183
184
185
186
        doublefile = "doublefile.txt"
        os.system("cat %s %s > %s" % (filename, filename, doublefile))
        doublesize = str(os.stat(doublefile).st_size)
        # assertions
187
        self.admin.assert_icommand("ils -L "+filename,'STDERR_SINGLELINE',"does not exist")                           # should not be listed
188
189
190
191
192
193
194
195
196
        self.admin.assert_icommand("iput "+filename)                                                      # put file
        self.admin.assert_icommand("irepl -R "+self.testresc+" "+filename)                                # replicate to test resource
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',filename)                                    #
        self.admin.assert_icommand("iput -f -R %s %s %s" % (self.testresc, doublefile, filename) )        # overwrite test repl with different data
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 0 "," "+filename])                        # default resource cache should have dirty copy
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 1 "," "+filename])                        # default resource archive should have dirty copy
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 0 "," "+doublesize+" "," "+filename]) # default resource cache should not have doublesize file
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 1 "," "+doublesize+" "," "+filename]) # default resource archive should not have doublesize file
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 2 "," "+doublesize+" ","& "+filename])    # targeted resource should have new double clean copy
197
198
199
200
201
202
203
204
205
206
207
        # local cleanup
        os.remove(filepath)
        os.remove(doublefile)

    ###################
    # irepl
    ###################

    def test_irepl_update_replicas(self):
        # local setup
        filename = "updatereplicasfile.txt"
208
209
        filepath = lib.create_local_testfile(filename)
        hostname = lib.get_hostname()
210
211
212
213
214
        doublefile = "doublefile.txt"
        os.system("cat %s %s > %s" % (filename, filename, doublefile))
        doublesize = str(os.stat(doublefile).st_size)

        # assertions
215
216
        self.admin.assert_icommand("iadmin mkresc thirdresc unixfilesystem %s:/tmp/thirdrescVault" % hostname, 'STDOUT_SINGLELINE', "Creating")   # create third resource
        self.admin.assert_icommand("iadmin mkresc fourthresc unixfilesystem %s:/tmp/fourthrescVault" % hostname, 'STDOUT_SINGLELINE', "Creating") # create fourth resource
217
        self.admin.assert_icommand("ils -L "+filename,'STDERR_SINGLELINE',"does not exist")              # should not be listed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
        self.admin.assert_icommand("iput "+filename)                                         # put file
        self.admin.assert_icommand("irepl -R "+self.testresc+" "+filename)                   # replicate to test resource
        self.admin.assert_icommand("irepl -R thirdresc "+filename)                           # replicate to third resource
        self.admin.assert_icommand("irepl -R fourthresc "+filename)                          # replicate to fourth resource
        self.admin.assert_icommand("iput -f -R "+self.testresc+" "+doublefile+" "+filename)  # repave overtop test resource
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',filename)                       # for debugging

        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 0 "," & "+filename]) # should have a dirty copy
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 1 "," & "+filename]) # should have a dirty copy
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 2 "," & "+filename])     # should have a clean copy
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 3 "," & "+filename]) # should have a dirty copy
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 4 "," & "+filename]) # should have a dirty copy

        self.admin.assert_icommand("irepl -U "+filename)                                 # update last replica

        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 0 "," & "+filename]) # should have a dirty copy
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 1 "," & "+filename]) # should have a dirty copy
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 2 "," & "+filename])     # should have a clean copy
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 3 "," & "+filename]) # should have a dirty copy
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 4 "," & "+filename])     # should have a clean copy

        self.admin.assert_icommand("irepl -aU "+filename)                                # update all replicas

        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 0 "," & "+filename])     # should have a clean copy
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 1 "," & "+filename])     # should have a clean copy
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 2 "," & "+filename])     # should have a clean copy
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 3 "," & "+filename])     # should have a clean copy
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 4 "," & "+filename])     # should have a clean copy

        self.admin.assert_icommand("irm -f "+filename)                                   # cleanup file
        self.admin.assert_icommand("iadmin rmresc thirdresc")                            # remove third resource
        self.admin.assert_icommand("iadmin rmresc fourthresc")                           # remove third resource
250
251
252
253
254
255
256
257

        # local cleanup
        os.remove(filepath)
        os.remove(doublefile)

    def test_irepl_over_existing_second_replica__ticket_1705(self):
        # local setup
        filename = "secondreplicatest.txt"
258
        filepath = lib.create_local_testfile(filename)
259
        # assertions
260
        self.admin.assert_icommand("ils -L "+filename,'STDERR_SINGLELINE',"does not exist")          # should not be listed
261
262
263
264
265
266
267
268
        self.admin.assert_icommand("iput -R "+self.testresc+" "+filename)                # put file
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',filename)                   # for debugging
        self.admin.assert_icommand("irepl "+filename)                                    # replicate to default resource
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',filename)                   # for debugging
        self.admin.assert_icommand("irepl "+filename)                                    # replicate overtop default resource
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 3 "," & "+filename]) # should not have a replica 3
        self.admin.assert_icommand("irepl -R "+self.testresc+" "+filename)               # replicate overtop test resource
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 3 "," & "+filename]) # should not have a replica 3
269
270
271
272
273
274
        # local cleanup
        os.remove(filepath)

    def test_irepl_over_existing_third_replica__ticket_1705(self):
        # local setup
        filename = "thirdreplicatest.txt"
275
276
        filepath = lib.create_local_testfile(filename)
        hostname = lib.get_hostname()
277
        # assertions
278
        self.admin.assert_icommand("iadmin mkresc thirdresc unixfilesystem %s:/tmp/thirdrescVault" % hostname, 'STDOUT_SINGLELINE', "Creating") # create third resource
279
        self.admin.assert_icommand("ils -L "+filename,'STDERR_SINGLELINE',"does not exist") # should not be listed
280
281
282
283
284
285
286
287
288
289
290
291
292
        self.admin.assert_icommand("iput "+filename)                            # put file
        self.admin.assert_icommand("irepl -R "+self.testresc+" "+filename)      # replicate to test resource
        self.admin.assert_icommand("irepl -R thirdresc "+filename)              # replicate to third resource
        self.admin.assert_icommand("irepl "+filename)                           # replicate overtop default resource
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',filename)          # for debugging
        self.admin.assert_icommand("irepl -R "+self.testresc+" "+filename)      # replicate overtop test resource
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',filename)          # for debugging
        self.admin.assert_icommand("irepl -R thirdresc "+filename)              # replicate overtop third resource
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',filename)          # for debugging
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 4 "," & "+filename]) # should not have a replica 4
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 5 "," & "+filename]) # should not have a replica 5
        self.admin.assert_icommand("irm -f "+filename)                          # cleanup file
        self.admin.assert_icommand("iadmin rmresc thirdresc")                   # remove third resource
293
294
295
296
297
298
        # local cleanup
        os.remove(filepath)

    def test_irepl_over_existing_bad_replica__ticket_1705(self):
        # local setup
        filename = "reploverwritebad.txt"
299
        filepath = lib.create_local_testfile(filename)
300
301
302
303
        doublefile = "doublefile.txt"
        os.system("cat %s %s > %s" % (filename, filename, doublefile))
        doublesize = str(os.stat(doublefile).st_size)
        # assertions
304
        self.admin.assert_icommand("ils -L "+filename,'STDERR_SINGLELINE',"does not exist") # should not be listed
305
306
307
308
309
310
311
312
313
314
315
316
317
        self.admin.assert_icommand("iput "+filename)                            # put file
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',filename)          # for debugging
        self.admin.assert_icommand("irepl -R "+self.testresc+" "+filename)      # replicate to test resource
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',filename)          # for debugging
        self.admin.assert_icommand("iput -f %s %s" % (doublefile, filename) )   # overwrite default repl with different data
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 0 "," & "+filename]) # default resource cache should have clean copy
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 0 "," "+doublesize+" "," & "+filename]) # default resource cache should have new double clean copy
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 1 "," & "+filename]) # default resource archive should have clean copy
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 1 "," "+doublesize+" "," & "+filename]) # default resource archive should have new double clean copy
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 2 "+self.testresc," "+doublesize+" ","  "+filename]) # test resource should not have doublesize file
        self.admin.assert_icommand("irepl -R "+self.testresc+" "+filename)      # replicate back onto test resource
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 2 "+self.testresc," "+doublesize+" "," & "+filename]) # test resource should have new clean doublesize file
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 3 "," & "+filename]) # should not have a replica 3
318
319
320
321
322
323
324
325
326
327
328
329
330
        # local cleanup
        os.remove(filepath)
        os.remove(doublefile)

    def test_iput_with_purgec(self):
        # local setup
        filename = "purgecfile.txt"
        filepath = os.path.abspath(filename)
        f = open(filepath,'wb')
        f.write("TESTFILE -- ["+filepath+"]")
        f.close()

        # assertions
331
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',filename) # should not be listed
332
        self.admin.assert_icommand("iput -f --purgec "+filename, 'STDOUT', 'Specifying a minimum number of replicas to keep is deprecated.') # get file and purge 'cached' replica
333
334
335
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 0 ",filename]) # should not be listed (trimmed)
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 1 ",filename]) # should be listed once - replica 1
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 2 ",filename]) # should be listed only once
336
337
338
339
340
341
342
343
344
345
346
347
348

        # local cleanup
        output = commands.getstatusoutput( 'rm '+filepath )

    def test_iget_with_purgec(self):
        # local setup
        filename = "purgecgetfile.txt"
        filepath = os.path.abspath(filename)
        f = open(filepath,'wb')
        f.write("TESTFILE -- ["+filepath+"]")
        f.close()

        # assertions
349
350
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',filename) # should not be listed
        self.admin.assert_icommand("iput "+filename) # put file
351
        self.admin.assert_icommand("iget -f --purgec "+filename, 'STDOUT', 'Specifying a minimum number of replicas to keep is deprecated.') # get file and purge 'cached' replica
352
353
354
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 0 ",filename]) # should not be listed (trimmed)
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 1 ",filename]) # should be listed once
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 2 ",filename]) # should not be listed
355
356
357
358
359
360
361
362
363
364
365
366
367

        # local cleanup
        output = commands.getstatusoutput( 'rm '+filepath )

    def test_irepl_with_purgec(self):
        # local setup
        filename = "purgecreplfile.txt"
        filepath = os.path.abspath(filename)
        f = open(filepath,'wb')
        f.write("TESTFILE -- ["+filepath+"]")
        f.close()

        # assertions
368
369
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',filename) # should not be listed
        self.admin.assert_icommand("iput "+filename) # put file
370
        self.admin.assert_icommand("irepl -R " + self.testresc + " --purgec " + filename, 'STDOUT', 'Specifying a minimum number of replicas to keep is deprecated.')  # replicate to test resource
371
372
373
        self.admin.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',[" 0 ",filename]) # should not be listed (trimmed)
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 1 ",filename]) # should be listed twice - 2 of 3
        self.admin.assert_icommand("ils -L "+filename,'STDOUT_SINGLELINE',[" 2 ",filename]) # should be listed twice - 1 of 3
374
375
376

        # local cleanup
        output = commands.getstatusoutput( 'rm '+filepath )
377
378
379
380
381
382

    def test_decoupled_naming_policy(self):
        if self.archive_naming_policy != 'decoupled':
            self.skipTest("Archive naming policy is not set to 'decoupled'")

        # local setup
Ben Keller's avatar
Ben Keller committed
383
        filename = self.testfile
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403

        # run as regular user
        session = self.user0
        collection = session.session_collection

        # iquest to get the object id of the replica on the S3 archive
        id_query = ( "select DATA_ID where COLL_NAME =" + "'" + collection + "'" +
                       " and DATA_NAME =" + "'" + filename + "'" +
                       " and DATA_REPL_NUM ='1'" )

        # iquest to get the pysical path of the replica on the S3 archive
        path_query = ( "select DATA_PATH where COLL_NAME =" + "'" + collection + "'" +
                       " and DATA_NAME =" + "'" + filename + "'" +
                       " and DATA_REPL_NUM ='1'" )

        # assertions
        session.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',filename) # should not be listed
        session.assert_icommand("iput "+filename) # put file

        # get object id
Ben Keller's avatar
Ben Keller committed
404
        object_id = session.run_icommand('iquest "%s" ' + '"' + id_query + '"')[0].strip()
405
406
407
408
409

        # physical path we expect to see: /{bucket_name}/{reversed_id}/{obj_name}
        target_path = '/' + self.s3bucketname + '/' + object_id[::-1] + '/' + filename

        # get object path
Ben Keller's avatar
Ben Keller committed
410
        physical_path = session.run_icommand('iquest "%s" ' + '"' + path_query + '"')[0].strip()
411
412
413
414

        # verify object path
        self.assertEqual(target_path, physical_path)

415
416
417
418
419
        # move the file
        new_filename = "%s.new" % filename
        session.assert_icommand("imv %s %s" % (filename, new_filename))

        # get and purge cache replica
420
        session.assert_icommand("iget -f --purgec %s" % new_filename, 'STDOUT', 'Specifying a minimum number of replicas to keep is deprecated.') # get file and purge 'cached' replica
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472

        # get again now that it is not in cache
        session.assert_icommand("iget -f %s" % new_filename) # get file

        # cleanup
        session.run_icommand('irm -f ' + new_filename)

    def test_decoupled_naming_policy_issue1855(self):
        if self.archive_naming_policy != 'decoupled':
            self.skipTest("Archive naming policy is not set to 'decoupled'")

        # local setup
        filename = self.testfile

        # run as regular user
        session = self.user0
        collection = session.session_collection

        # modify the s3 archive resource so that it only has the bucket name in the context
        self.admin.assert_icommand('iadmin modresc archiveResc path /%s' % self.s3bucketname, 'STDOUT_SINGLELINE', 'Previous resource path:')

        # iquest to get the object id of the replica on the S3 archive
        id_query = ( "select DATA_ID where COLL_NAME =" + "'" + collection + "'" +
                       " and DATA_NAME =" + "'" + filename + "'" +
                       " and DATA_REPL_NUM ='1'" )

        # iquest to get the pysical path of the replica on the S3 archive
        path_query = ( "select DATA_PATH where COLL_NAME =" + "'" + collection + "'" +
                       " and DATA_NAME =" + "'" + filename + "'" +
                       " and DATA_REPL_NUM ='1'" )

        # assertions
        session.assert_icommand_fail("ils -L "+filename,'STDOUT_SINGLELINE',filename) # should not be listed
        session.assert_icommand("iput "+filename) # put file

        # get object id
        object_id = session.run_icommand('iquest "%s" ' + '"' + id_query + '"')[0].strip()

        # physical path we expect to see: /{bucket_name}/{reversed_id}/{obj_name}
        target_path = '/' + self.s3bucketname + '/' + object_id[::-1] + '/' + filename

        # get object path
        physical_path = session.run_icommand('iquest "%s" ' + '"' + path_query + '"')[0].strip()

        # verify object path
        self.assertEqual(target_path, physical_path)

        # move the file
        new_filename = "%s.new" % filename
        session.assert_icommand("imv %s %s" % (filename, new_filename))

        # get and purge cache replica
473
        session.assert_icommand("iget -f --purgec %s" % new_filename, 'STDOUT', 'Specifying a minimum number of replicas to keep is deprecated.') # get file and purge 'cached' replica
474
475
476
477

        # get again now that it is not in cache
        session.assert_icommand("iget -f %s" % new_filename) # get file

478
479
        # cleanup
        session.run_icommand('irm -f ' + filename)
480

481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
    @unittest.skip("skip until minio added to CI")
    def test_multiple_s3_endpoints_replication_issue1858(self):

        # local setup
        filename = self.testfile

        # run as regular user
        session = self.user0
        collection = session.session_collection

        # set up resources

        # TODO change these as necessary
        minio_auth_file = '/var/lib/irods/s3.keypair'
        minio_bucket_name = 'irods-bucket'

        hostname = lib.get_hostname()
        s3params_aws = 'S3_RETRY_COUNT=1;S3_WAIT_TIME_SEC=1;S3_PROTO=HTTPS;S3_MPU_CHUNK=10;S3_MPU_THREADS=4;S3_ENABLE_MD5=1'
        s3params_aws += ';S3_DEFAULT_HOSTNAME=%s' % self.s3endPoint
        s3params_aws += ';S3_AUTH_FILE=%s' % self.keypairfile
        s3params_aws += ';S3_REGIONNAME=%s' % self.s3region
        s3params_aws += ';ARCHIVE_NAMING_POLICY=%s' % self.archive_naming_policy

        s3params_minio = 'S3_RETRY_COUNT=1;S3_WAIT_TIME_SEC=1;S3_PROTO=HTTP;S3_MPU_CHUNK=10;S3_MPU_THREADS=4;S3_ENABLE_MD5=1'
        s3params_minio += ';S3_DEFAULT_HOSTNAME=%s:9000' % hostname
        s3params_minio += ';S3_AUTH_FILE=%s' % minio_auth_file
        s3params_minio += ';S3_REGIONNAME=%s' % self.s3region
        s3params_minio += ';ARCHIVE_NAMING_POLICY=%s' % self.archive_naming_policy

        try:

            # make resource tree with repl and two compound resources underneath
            self.admin.assert_icommand('iadmin mkresc s3repl_1858 replication', 'STDOUT_SINGLELINE', 'Creating')
            self.admin.assert_icommand('iadmin mkresc s3compound1_1858 compound', 'STDOUT_SINGLELINE', 'Creating')
            self.admin.assert_icommand('iadmin mkresc s3compound2_1858 compound', 'STDOUT_SINGLELINE', 'Creating')
            self.admin.assert_icommand('iadmin mkresc s3cache1_1858 unixfilesystem %s:/tmp/s3cache1_1858 unixfilesystem' % hostname, 'STDOUT_SINGLELINE', 'Creating')
            self.admin.assert_icommand('iadmin mkresc s3archive1_1858 s3 %s:/%s/irods/Vault %s' % (hostname, self.s3bucketname, s3params_aws), 'STDOUT_SINGLELINE', 's3archive1_1858')
            self.admin.assert_icommand('iadmin mkresc s3cache2_1858 unixfilesystem %s:/tmp/s3cache2_1858 unixfilesystem' % hostname, 'STDOUT_SINGLELINE', 'Creating')
            self.admin.assert_icommand('iadmin mkresc s3archive2_1858 s3 %s:/%s/irods/s3archive2_1858_vault %s' % (hostname, minio_bucket_name, s3params_minio), 'STDOUT_SINGLELINE', 's3archive2_1858')
            self.admin.assert_icommand('iadmin addchildtoresc s3repl_1858 s3compound1_1858')
            self.admin.assert_icommand('iadmin addchildtoresc s3repl_1858 s3compound2_1858')
            self.admin.assert_icommand('iadmin addchildtoresc s3compound1_1858 s3cache1_1858 cache')
            self.admin.assert_icommand('iadmin addchildtoresc s3compound1_1858 s3archive1_1858 archive')
            self.admin.assert_icommand('iadmin addchildtoresc s3compound2_1858 s3cache2_1858 cache')
            self.admin.assert_icommand('iadmin addchildtoresc s3compound2_1858 s3archive2_1858 archive')

            # put a file to this tree
            session.assert_icommand('iput -R s3repl_1858 %s' % filename) # put file

            # make sure we have four replicas
            session.assert_icommand('ils -L %s' % filename, 'STDOUT_MULTILINE', ['s3repl_1858;s3compound1_1858;s3cache1_1858',
                                                                                 's3repl_1858;s3compound1_1858;s3archive1_1858',
                                                                                 's3repl_1858;s3compound2_1858;s3cache2_1858',
                                                                                 's3repl_1858;s3compound2_1858;s3archive2_1858'])
        finally:

            # remove the file
            session.assert_icommand('irm -f %s' % filename) # remove file

            # cleanup
            self.admin.assert_icommand('iadmin rmchildfromresc s3repl_1858 s3compound1_1858')
            self.admin.assert_icommand('iadmin rmchildfromresc s3repl_1858 s3compound2_1858')
            self.admin.assert_icommand('iadmin rmchildfromresc s3compound1_1858 s3cache1_1858 cache')
            self.admin.assert_icommand('iadmin rmchildfromresc s3compound1_1858 s3archive1_1858 archive')
            self.admin.assert_icommand('iadmin rmchildfromresc s3compound2_1858 s3cache2_1858 cache')
            self.admin.assert_icommand('iadmin rmchildfromresc s3compound2_1858 s3archive2_1858 archive')
            self.admin.assert_icommand('iadmin rmresc s3repl_1858')
            self.admin.assert_icommand('iadmin rmresc s3compound1_1858')
            self.admin.assert_icommand('iadmin rmresc s3compound2_1858')
            self.admin.assert_icommand('iadmin rmresc s3cache1_1858')
            self.admin.assert_icommand('iadmin rmresc s3archive1_1858')
            self.admin.assert_icommand('iadmin rmresc s3cache2_1858')
            self.admin.assert_icommand('iadmin rmresc s3archive2_1858')
554

555
class Test_Compound_With_S3_Resource_EU_Central_1(Test_Compound_With_S3_Resource):
556
557
558
    '''
    This also tests signature V4 with the x-amz-date header.
    '''
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
    def __init__(self, *args, **kwargs):
        self.keypairfile='/projects/irods/vsphere-testing/externals/amazon_web_services-CI.keypair'
        self.archive_naming_policy='decoupled'
        self.s3stsdate=''
        self.s3region='eu-central-1'
        self.s3endPoint='s3.eu-central-1.amazonaws.com'
        self.s3signature_version=4
        super(Test_Compound_With_S3_Resource, self).__init__(*args, **kwargs)

class Test_Compound_With_S3_Resource_STSDate_Header(Test_Compound_With_S3_Resource):
    def __init__(self, *args, **kwargs):
        self.keypairfile='/projects/irods/vsphere-testing/externals/amazon_web_services-CI.keypair'
        self.archive_naming_policy='decoupled'
        self.s3stsdate='date'
        self.s3region='us-east-1'
        self.s3endPoint='s3.amazonaws.com'
        self.s3signature_version=2
        super(Test_Compound_With_S3_Resource, self).__init__(*args, **kwargs)
577
578
579
580
581
582
583
584
585
586

class Test_Compound_With_S3_Resource_STSDate_Header_V4(Test_Compound_With_S3_Resource):
    def __init__(self, *args, **kwargs):
        self.keypairfile='/projects/irods/vsphere-testing/externals/amazon_web_services-CI.keypair'
        self.archive_naming_policy='decoupled'
        self.s3stsdate='date'
        self.s3region='us-east-1'
        self.s3endPoint='s3.amazonaws.com'
        self.s3signature_version=4
        super(Test_Compound_With_S3_Resource, self).__init__(*args, **kwargs)
587
588
589
590
591
592
593
594
595
596
597

class Test_Compound_With_S3_Resource_V4_SSE(Test_Compound_With_S3_Resource):
    def __init__(self, *args, **kwargs):
        self.keypairfile='/projects/irods/vsphere-testing/externals/amazon_web_services-CI.keypair'
        self.archive_naming_policy='decoupled'
        self.s3stsdate=''
        self.s3sse=1
        self.s3region='us-east-1'
        self.s3endPoint='s3.amazonaws.com'
        self.s3signature_version=4
        super(Test_Compound_With_S3_Resource, self).__init__(*args, **kwargs)
598

599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
class Test_S3_NoCache_V4(Test_S3_NoCache_Base, unittest.TestCase):

    def __init__(self, *args, **kwargs):
        self.keypairfile='/projects/irods/vsphere-testing/externals/amazon_web_services-CI.keypair'
        self.s3region='us-east-1'
        self.s3endPoint='s3.amazonaws.com'
        self.s3signature_version=4
        self.s3EnableMPU=1
        super(Test_S3_NoCache_Base, self).__init__(*args, **kwargs)

class Test_S3_NoCache_EU_Central_1(Test_S3_NoCache_Base, unittest.TestCase):
    '''
    This also tests signature V4 with the x-amz-date header.
    '''
    def __init__(self, *args, **kwargs):
        self.keypairfile='/projects/irods/vsphere-testing/externals/amazon_web_services-CI.keypair'
        self.s3region='eu-central-1'
        self.s3endPoint='s3.eu-central-1.amazonaws.com'
        self.s3signature_version=4
        self.s3EnableMPU=1
        super(Test_S3_NoCache_Base, self).__init__(*args, **kwargs)

class Test_S3_NoCache_V2(Test_S3_NoCache_Base, unittest.TestCase):
    def __init__(self, *args, **kwargs):
        self.keypairfile='/projects/irods/vsphere-testing/externals/amazon_web_services-CI.keypair'
        self.s3region='us-east-1'
        self.s3endPoint='s3.amazonaws.com'
        self.s3signature_version=2
        self.s3EnableMPU=1
        super(Test_S3_NoCache_Base, self).__init__(*args, **kwargs)


class Test_S3_NoCache_MPU_Disabled(Test_S3_NoCache_Base, unittest.TestCase):
    def __init__(self, *args, **kwargs):
        self.keypairfile='/projects/irods/vsphere-testing/externals/amazon_web_services-CI.keypair'
        self.s3region='us-east-1'
        self.s3endPoint='s3.amazonaws.com'
        self.s3signature_version=2
        self.s3EnableMPU=0
        super(Test_S3_NoCache_Base, self).__init__(*args, **kwargs)