/
aws_ec2.go
62 lines (51 loc) · 1.49 KB
/
aws_ec2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// Copyright 2020 Joyent, Inc.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
package test
import (
"fmt"
"net"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/awstesting/unit"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gin-gonic/gin"
"github.com/joyent/triton-shim/server"
)
// GetEC2Svc will start a shim server and return an ec2 service that points to
// the shim server.
func GetEC2Svc(t *testing.T, runTest func(ec2Svc *ec2.EC2)) {
// Switch to test mode so you don't get such noisy output
gin.SetMode(gin.TestMode)
engine := server.Setup()
listener, err := net.Listen("tcp", ":0")
if err != nil {
t.Errorf("listen error %v", err)
}
go func() {
// service connections
err := engine.RunListener(listener)
if err != nil {
t.Errorf("listen error %v", err)
}
}()
// Load session from shared config
awsConfig := aws.Config{
Region: unit.Session.Config.Region,
DisableSSL: aws.Bool(true),
Endpoint: aws.String(fmt.Sprintf("http://%s", listener.Addr().String())),
Credentials: unit.Session.Config.Credentials,
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Config: awsConfig,
}))
// Create new EC2 client
ec2Svc := ec2.New(sess)
runTest(ec2Svc)
}