Esempio n. 1
0
func NewFakeAWS(logger *AWSCallLogger) *FakeAWS {
	f := &FakeAWS{
		CloudFormation: NewFakeCloudFormation(logger),
		EC2:            NewFakeEC2(logger),
		IAM:            NewFakeIAM(logger),
	}
	f.servers = map[string]*httptest.Server{
		"cloudformation": httptest.NewServer(awsfaker.New(f.CloudFormation)),
		"ec2":            httptest.NewServer(awsfaker.New(f.EC2)),
		"iam":            httptest.NewServer(awsfaker.New(f.IAM)),
	}

	return f
}
Esempio n. 2
0
func Example() {
	// create a backend that implements the subset of the AWS API you need
	fakeBackend := &CloudFormationBackend{}

	// start a local HTTP server that dispatches requests to the backend
	fakeServer := httptest.NewServer(awsfaker.New(fakeBackend))

	// configure and use your client.  this might be a separate process,
	// with the endpoint override set via environment variable or other config
	client := cloudformation.New(session.New(&aws.Config{
		Credentials: credentials.NewStaticCredentials("some-access-key", "some-secret-key", ""),
		Region:      aws.String("some-region"),
		Endpoint:    aws.String(fakeServer.URL), // override the default AWS endpoint
	}))

	out, err := client.CreateStack(&cloudformation.CreateStackInput{
		StackName: aws.String("some-stack"),
	})
	if err != nil {
		panic(err)
	}
	fmt.Printf("[Client] CreateStack returned ID: %q\n", *out.StackId)

	_, err = client.CreateStack(&cloudformation.CreateStackInput{
		StackName: aws.String("some-stack"),
	})
	fmt.Printf("[Client] CreateStack returned error:\n %s\n", err)
	// Output:
	// [Server] CreateStack called on "some-stack"
	// [Client] CreateStack returned ID: "some-id"
	// [Server] CreateStack called on "some-stack"
	// [Client] CreateStack returned error:
	//  AlreadyExistsException: Stack [some-stack] already exists
	// 	status code: 400, request id:
}
Esempio n. 3
0
func (f *FakeEC2Backend) CreateKeyPair(input *ec2.CreateKeyPairInput) (*ec2.CreateKeyPairOutput, error) {
	f.CreateKeyPairCall.Receives = input
	return f.CreateKeyPairCall.ReturnsResult, f.CreateKeyPairCall.ReturnsError
}

var _ = Describe("Mocking out the EC2 service", func() {
	var (
		fakeBackend *FakeEC2Backend
		fakeServer  *httptest.Server
		client      *ec2.EC2
	)

	BeforeEach(func() {
		fakeBackend = &FakeEC2Backend{}
		fakeServer = httptest.NewServer(awsfaker.New(fakeBackend))
		client = ec2.New(newSession(fakeServer.URL))
	})

	AfterEach(func() {
		if fakeServer != nil {
			fakeServer.Close()
		}
	})

	It("should call the backend method", func() {
		client.CreateKeyPair(
			&ec2.CreateKeyPairInput{
				KeyName: aws.String("some-key-name"),
			})
		fakeAWSServer  *httptest.Server
	)

	BeforeEach(func() {
		var err error

		tmpDir, err = ioutil.TempDir("", "")
		Expect(err).NotTo(HaveOccurred())

		fakeBOSH = &fakeBOSHDirector{}
		fakeBOSHServer = httptest.NewServer(http.HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) {
			fakeBOSH.ServeHTTP(responseWriter, request)
		}))

		fakeAWS = awsbackend.New(fakeBOSHServer.URL)
		fakeAWSServer = httptest.NewServer(awsfaker.New(fakeAWS))
	})

	bblUp := func(bbl string) (*gexec.Session, error) {
		args := []string{
			fmt.Sprintf("--endpoint-override=%s", fakeAWSServer.URL),
			"--state-dir", tmpDir,
			"up",
			"--aws-access-key-id", "some-access-key",
			"--aws-secret-access-key", "some-access-secret",
			"--aws-region", "some-region",
		}
		session, err := gexec.Start(exec.Command(bbl, args...), GinkgoWriter, GinkgoWriter)
		return session, err
	}