github.com/etecs-ru/gnomock@v0.13.2/preset/localstack/README.md (about) 1 # Gnomock Localstack 2 3 Gnomock Localstack is a [Gnomock](https://github.com/orlangure/gnomock) preset for running tests against AWS services 4 locally, powered by 5 [Localstack](https://github.com/localstack/localstack) project. It allows to setup a number of supported AWS services 6 locally, run tests against them, and tear them down easily. 7 8 See [Localstack](https://github.com/localstack/localstack) documentation for more details. 9 10 ### Testing against local S3 11 12 ```go 13 package localstack_test 14 15 import ( 16 "bytes" 17 "encoding/json" 18 "fmt" 19 20 "github.com/aws/aws-sdk-go/aws" 21 "github.com/aws/aws-sdk-go/aws/session" 22 "github.com/aws/aws-sdk-go/service/s3" 23 "github.com/orlangure/gnomock" 24 "github.com/orlangure/gnomock/preset/localstack" 25 ) 26 27 func ExamplePreset_s3() { 28 p := localstack.Preset(localstack.WithServices(localstack.S3)) 29 c, _ := gnomock.Start(p) 30 31 defer func() { _ = gnomock.Stop(c) }() 32 33 s3Endpoint := fmt.Sprintf("http://%s/", c.Address(localstack.APIPort)) 34 config := &aws.Config{ 35 Region: aws.String("us-east-1"), 36 Endpoint: aws.String(s3Endpoint), 37 S3ForcePathStyle: aws.Bool(true), 38 Credentials: credentials.NewStaticCredentials("a", "b", "c"), 39 } 40 sess, _ := session.NewSession(config) 41 svc := s3.New(sess) 42 43 _, _ = svc.CreateBucket(&s3.CreateBucketInput{ 44 Bucket: aws.String("foo"), 45 }) 46 47 out, _ := svc.ListObjectsV2(&s3.ListObjectsV2Input{ 48 Bucket: aws.String("foo"), 49 }) 50 fmt.Println("keys before:", *out.KeyCount) 51 52 _, _ = svc.PutObject(&s3.PutObjectInput{ 53 Body: bytes.NewReader([]byte("this is a file")), 54 Key: aws.String("file"), 55 Bucket: aws.String("foo"), 56 }) 57 58 out, _ = svc.ListObjectsV2(&s3.ListObjectsV2Input{ 59 Bucket: aws.String("foo"), 60 }) 61 fmt.Println("keys after:", *out.KeyCount) 62 63 // Output: 64 // keys before: 0 65 // keys after: 1 66 } 67 ``` 68 69 ### Testing against local SQS+SNS 70 71 ```go 72 package localstack_test 73 74 import ( 75 "bytes" 76 "encoding/json" 77 "fmt" 78 79 "github.com/aws/aws-sdk-go/aws" 80 "github.com/aws/aws-sdk-go/aws/session" 81 "github.com/aws/aws-sdk-go/service/sns" 82 "github.com/aws/aws-sdk-go/service/sqs" 83 "github.com/orlangure/gnomock" 84 "github.com/orlangure/gnomock/preset/localstack" 85 ) 86 87 func ExamplePreset_sqs_sns() { 88 p := localstack.Preset( 89 localstack.WithServices(localstack.SNS, localstack.SQS), 90 ) 91 c, _ := gnomock.Start(p) 92 93 defer func() { _ = gnomock.Stop(c) }() 94 95 endpoint := fmt.Sprintf("http://%s", c.Address(localstack.APIPort)) 96 97 sess, _ := session.NewSession(&aws.Config{ 98 Region: aws.String("us-east-1"), 99 Endpoint: aws.String(endpoint), 100 Credentials: credentials.NewStaticCredentials("a", "b", "c"), 101 }) 102 103 sqsService := sqs.New(sess) 104 snsService := sns.New(sess) 105 106 _, _ = sqsService.CreateQueue(&sqs.CreateQueueInput{ 107 QueueName: aws.String("my_queue"), 108 }) 109 110 _, _ = snsService.CreateTopic(&sns.CreateTopicInput{ 111 Name: aws.String("my_topic"), 112 }) 113 114 queues, _ := sqsService.ListQueues(&sqs.ListQueuesInput{}) 115 fmt.Println("queues:", len(queues.QueueUrls)) 116 117 queueURL := queues.QueueUrls[0] 118 119 topics, _ := snsService.ListTopics(&sns.ListTopicsInput{}) 120 fmt.Println("topics:", len(topics.Topics)) 121 122 topic := topics.Topics[0] 123 124 _, _ = snsService.Subscribe(&sns.SubscribeInput{ 125 Protocol: aws.String("sqs"), 126 Endpoint: queueURL, 127 TopicArn: topic.TopicArn, 128 }) 129 130 _, _ = snsService.Publish(&sns.PublishInput{ 131 TopicArn: topic.TopicArn, 132 Message: aws.String("foobar"), 133 }) 134 135 messages, _ := sqsService.ReceiveMessage(&sqs.ReceiveMessageInput{ 136 QueueUrl: queueURL, 137 }) 138 fmt.Println("messages:", len(messages.Messages)) 139 140 var msg map[string]string 141 142 _ = json.Unmarshal([]byte(*messages.Messages[0].Body), &msg) 143 fmt.Println("message:", msg["Message"]) 144 145 // Output: 146 // queues: 1 147 // topics: 1 148 // messages: 1 149 // message: foobar 150 } 151 ```