github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/thirdparty/s3_test.go (about) 1 package thirdparty 2 3 import ( 4 "io/ioutil" 5 "strings" 6 "testing" 7 8 "github.com/evergreen-ci/evergreen/testutil" 9 "github.com/evergreen-ci/evergreen/util" 10 "github.com/goamz/goamz/aws" 11 . "github.com/smartystreets/goconvey/convey" 12 ) 13 14 var ( 15 sourceURL = "s3://build-push-testing/test/source/testfile" 16 destUrl = "s3://build-push-testing/test/dest/testfile" 17 testURLBucket = "build-push-testing" 18 testURLPath = "/test/push/path/mongodb-pushname-pusharch-latest.tgz" 19 testURL = "s3://s3_key:s3_secret@build-push-testing/test/" + 20 "push/path/mongodb-pushname-pusharch-latest.tgz" 21 ) 22 23 func TestS3ParseUrl(t *testing.T) { 24 testutil.ConfigureIntegrationTest(t, testConfig, "TestS3ParseUrl") 25 Convey("When given an S3 location to parse...", t, func() { 26 Convey("the bucket and path should be parsed correctly", func() { 27 bucket, path, err := GetS3Location(testURL) 28 So(err, ShouldBeNil) 29 So(bucket, ShouldEqual, testURLBucket) 30 So(path, ShouldEqual, testURLPath) 31 }) 32 }) 33 } 34 35 func TestPutS3File(t *testing.T) { 36 testutil.ConfigureIntegrationTest(t, testConfig, "TestPutS3File") 37 Convey("When given a file to copy to S3...", t, func() { 38 Convey("a valid source file with a long key should return an error ", func() { 39 //Make a test file with some random content. 40 tempfile, err := ioutil.TempFile("", "randomString") 41 So(err, ShouldBeNil) 42 randStr := util.RandomString() 43 _, err = tempfile.Write([]byte(randStr)) 44 So(err, ShouldBeNil) 45 So(tempfile.Close(), ShouldBeNil) 46 47 // put the test file on S3 48 auth := &aws.Auth{ 49 AccessKey: testConfig.Providers.AWS.Id, 50 SecretKey: testConfig.Providers.AWS.Secret, 51 } 52 longURLKey := sourceURL + strings.Repeat("suffix", 300) 53 err = PutS3File(auth, tempfile.Name(), longURLKey, "application/x-tar", "public-read") 54 So(err, ShouldNotEqual, nil) 55 }) 56 Convey("a valid source file with a valid key should return no errors", func() { 57 //Make a test file with some random content. 58 tempfile, err := ioutil.TempFile("", "randomString") 59 So(err, ShouldBeNil) 60 randStr := util.RandomString() 61 _, err = tempfile.Write([]byte(randStr)) 62 So(err, ShouldBeNil) 63 So(tempfile.Close(), ShouldBeNil) 64 65 // put the test file on S3 66 auth := &aws.Auth{ 67 AccessKey: testConfig.Providers.AWS.Id, 68 SecretKey: testConfig.Providers.AWS.Secret, 69 } 70 err = PutS3File(auth, tempfile.Name(), sourceURL, "application/x-tar", "public-read") 71 So(err, ShouldBeNil) 72 }) 73 }) 74 } 75 76 func TestS3Copy(t *testing.T) { 77 testutil.ConfigureIntegrationTest(t, testConfig, "TestS3Copy") 78 Convey("When given a source and destination URL to copy from/to...", t, func() { 79 Convey("a valid source file should be copied to the valid destination", func() { 80 //Make a test file with some random content. 81 tempfile, err := ioutil.TempFile("", "randomString") 82 So(err, ShouldBeNil) 83 randStr := util.RandomString() 84 85 _, err = tempfile.Write([]byte(randStr)) 86 So(err, ShouldBeNil) 87 So(tempfile.Close(), ShouldBeNil) 88 89 // Put the test file on S3 90 auth := &aws.Auth{ 91 AccessKey: testConfig.Providers.AWS.Id, 92 SecretKey: testConfig.Providers.AWS.Secret, 93 } 94 err = PutS3File(auth, tempfile.Name(), sourceURL, "application/x-tar", "public-read") 95 So(err, ShouldBeNil) 96 97 // Copy the test file over to another location 98 err = CopyS3File(auth, sourceURL, destUrl, "public-read") 99 So(err, ShouldBeNil) 100 101 // Ensure that the file was actually copied 102 rdr, err := GetS3File(auth, destUrl) 103 defer func() { 104 So(rdr.Close(), ShouldBeNil) 105 }() 106 So(err, ShouldBeNil) 107 108 fileContents, err := ioutil.ReadAll(rdr) 109 So(err, ShouldBeNil) 110 So(string(fileContents), ShouldEqual, randStr) 111 }) 112 Convey("a valid source file with a long key should return an error "+ 113 "even if sent to a valid destination", func() { 114 //Make a test file with some random content. 115 tempfile, err := ioutil.TempFile("", "randomString") 116 So(err, ShouldBeNil) 117 randStr := util.RandomString() 118 _, err = tempfile.Write([]byte(randStr)) 119 So(err, ShouldBeNil) 120 So(tempfile.Close(), ShouldBeNil) 121 122 // put the test file on S3 123 auth := &aws.Auth{ 124 AccessKey: testConfig.Providers.AWS.Id, 125 SecretKey: testConfig.Providers.AWS.Secret, 126 } 127 longURLKey := sourceURL + strings.Repeat("suffix", 300) 128 err = PutS3File(auth, tempfile.Name(), longURLKey, "application/x-tar", "public-read") 129 So(err, ShouldNotEqual, nil) 130 }) 131 Convey("a valid source file copied to a destination with too long a "+ 132 "key name should return an error", func() { 133 //Make a test file with some random content. 134 tempfile, err := ioutil.TempFile("", "randomString") 135 So(err, ShouldBeNil) 136 randStr := util.RandomString() 137 _, err = tempfile.Write([]byte(randStr)) 138 So(err, ShouldBeNil) 139 So(tempfile.Close(), ShouldBeNil) 140 141 // put the test file on S3 142 auth := &aws.Auth{ 143 AccessKey: testConfig.Providers.AWS.Id, 144 SecretKey: testConfig.Providers.AWS.Secret, 145 } 146 err = PutS3File(auth, tempfile.Name(), sourceURL, "application/x-tar", "public-read") 147 So(err, ShouldBeNil) 148 149 longURLKey := sourceURL + strings.Repeat("suffix", 300) 150 // Attempt to copy the test file over to another location 151 err = CopyS3File(auth, sourceURL, longURLKey, "public-read") 152 So(err, ShouldNotEqual, nil) 153 154 // Ensure that the file was not copied 155 rdr, err := GetS3File(auth, longURLKey) 156 So(rdr, ShouldEqual, nil) 157 So(err, ShouldNotEqual, nil) 158 }) 159 }) 160 }