github.com/bishtawi/migrate/v4@v4.8.11/source/aws_s3/s3_test.go (about) 1 package awss3 2 3 import ( 4 "errors" 5 "fmt" 6 "io/ioutil" 7 "strings" 8 "testing" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/service/s3" 12 st "github.com/bishtawi/migrate/v4/source/testing" 13 ) 14 15 func Test(t *testing.T) { 16 s3Client := fakeS3{ 17 bucket: "some-bucket", 18 objects: map[string]string{ 19 "staging/migrations/1_foobar.up.sql": "1 up", 20 "staging/migrations/1_foobar.down.sql": "1 down", 21 "prod/migrations/1_foobar.up.sql": "1 up", 22 "prod/migrations/1_foobar.down.sql": "1 down", 23 "prod/migrations/3_foobar.up.sql": "3 up", 24 "prod/migrations/4_foobar.up.sql": "4 up", 25 "prod/migrations/4_foobar.down.sql": "4 down", 26 "prod/migrations/5_foobar.down.sql": "5 down", 27 "prod/migrations/7_foobar.up.sql": "7 up", 28 "prod/migrations/7_foobar.down.sql": "7 down", 29 "prod/migrations/not-a-migration.txt": "", 30 "prod/migrations/0-random-stuff/whatever.txt": "", 31 }, 32 } 33 driver, err := WithInstance(&s3Client, &Config{ 34 Bucket: "some-bucket", 35 Prefix: "prod/migrations/", 36 }) 37 if err != nil { 38 t.Fatal(err) 39 } 40 st.Test(t, driver) 41 } 42 43 func TestNewS3Driver(t *testing.T) { 44 const expectedBucket = "migration-bucket" 45 const expectedPrefix = "production/" 46 47 driver, err := newS3Driver(fmt.Sprintf("s3://%s/%s", expectedBucket, expectedPrefix)) 48 if err != nil { 49 t.Fatal(err) 50 } 51 52 if driver.config.Bucket != expectedBucket { 53 t.Errorf("Expected: %s; actual: %s", expectedBucket, driver.config.Bucket) 54 } 55 56 if driver.config.Prefix != expectedPrefix { 57 t.Errorf("Expected: %s; actual: %s", expectedPrefix, driver.config.Prefix) 58 } 59 60 if driver.s3client == nil { 61 t.Error("S3 client is not initialized") 62 } 63 64 if driver.migrations == nil { 65 t.Error("Migration source is not initialized") 66 } 67 68 driver, err = newS3Driver(fmt.Sprintf("s3://%s", expectedBucket)) 69 if err != nil { 70 t.Fatal(err) 71 } 72 73 if driver.config.Bucket != expectedBucket { 74 t.Errorf("Expected: %s; actual: %s", expectedBucket, driver.config.Bucket) 75 } 76 77 if driver.config.Prefix != "" { 78 t.Errorf("Prefix should be empty; actual: %s", driver.config.Prefix) 79 } 80 } 81 82 type fakeS3 struct { 83 s3.S3 84 bucket string 85 objects map[string]string 86 } 87 88 func (s *fakeS3) ListObjects(input *s3.ListObjectsInput) (*s3.ListObjectsOutput, error) { 89 bucket := aws.StringValue(input.Bucket) 90 if bucket != s.bucket { 91 return nil, errors.New("bucket not found") 92 } 93 prefix := aws.StringValue(input.Prefix) 94 delimiter := aws.StringValue(input.Delimiter) 95 var output s3.ListObjectsOutput 96 for name := range s.objects { 97 if strings.HasPrefix(name, prefix) { 98 if delimiter == "" || !strings.Contains(strings.Replace(name, prefix, "", 1), delimiter) { 99 output.Contents = append(output.Contents, &s3.Object{ 100 Key: aws.String(name), 101 }) 102 } 103 } 104 } 105 return &output, nil 106 } 107 108 func (s *fakeS3) GetObject(input *s3.GetObjectInput) (*s3.GetObjectOutput, error) { 109 bucket := aws.StringValue(input.Bucket) 110 if bucket != s.bucket { 111 return nil, errors.New("bucket not found") 112 } 113 if data, ok := s.objects[aws.StringValue(input.Key)]; ok { 114 body := ioutil.NopCloser(strings.NewReader(data)) 115 return &s3.GetObjectOutput{Body: body}, nil 116 } 117 return nil, errors.New("object not found") 118 }