forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Examples of testing with Pulumi (pulumi#608)
* .NET unit test * Add an example of PaC-based testing * Add a TypeScript unit test * Add a python test * Add an integration test
- Loading branch information
1 parent
d617a14
commit b95e810
Showing
57 changed files
with
2,112 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.pulumi/ | ||
**/bin/ | ||
node_modules/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Integration Testing of Pulumi programs in Go | ||
|
||
This integration test treats infrastucture deployed by a Pulumi program as a "black box". If deploys the infrastructure, retrieves an endpoint from stack outputs, sends an HTTP request to the endpoint, validates the response, and tears down the infrastructure again. | ||
|
||
This test deploys a static website as an AWS S3 bucket and checks that the site is reachable. | ||
|
||
## Prerequisites | ||
|
||
1. [Install Go](https://golang.org/doc/install). | ||
2. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/). | ||
3. [Configure Pulumi to Use AWS](https://www.pulumi.com/docs/intro/cloud-providers/aws/setup/) (if your AWS CLI is configured, no further changes are required). | ||
|
||
## Running the tests | ||
|
||
Run the tests: | ||
|
||
``` | ||
$ go test | ||
... | ||
PASS | ||
ok github.com/pulumi/examples/testing-integration 65.749s | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright 2016-2020, Pulumi Corporation. All rights reserved. | ||
|
||
package examples | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"path" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pulumi/pulumi/pkg/testing/integration" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestS3Website(t *testing.T) { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
t.FailNow() | ||
} | ||
|
||
test := integration.ProgramTestOptions{ | ||
Dir: path.Join(cwd, "program"), | ||
Quick: true, | ||
SkipRefresh: true, | ||
Config: map[string]string{ | ||
"aws:region": "us-west-1", | ||
}, | ||
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) { | ||
assertHTTPResult(t, "http:https://"+stack.Outputs["websiteUrl"].(string), nil, func(body string) bool { | ||
return assert.Contains(t, body, "Hello, Pulumi!") | ||
}) | ||
}, | ||
} | ||
integration.ProgramTest(t, &test) | ||
} | ||
|
||
func assertHTTPResult(t *testing.T, output interface{}, headers map[string]string, check func(string) bool) bool { | ||
return assertHTTPResultWithRetry(t, output, headers, 5*time.Minute, check) | ||
} | ||
|
||
func assertHTTPResultWithRetry(t *testing.T, output interface{}, headers map[string]string, maxWait time.Duration, check func(string) bool) bool { | ||
return assertHTTPResultShapeWithRetry(t, output, headers, maxWait, func(string) bool { return true }, check) | ||
} | ||
|
||
func assertHTTPResultShapeWithRetry(t *testing.T, output interface{}, headers map[string]string, maxWait time.Duration, | ||
ready func(string) bool, check func(string) bool) bool { | ||
hostname, ok := output.(string) | ||
if !assert.True(t, ok, fmt.Sprintf("expected `%s` output", output)) { | ||
return false | ||
} | ||
|
||
if !(strings.HasPrefix(hostname, "http:https://") || strings.HasPrefix(hostname, "https://")) { | ||
hostname = fmt.Sprintf("http:https://%s", hostname) | ||
} | ||
|
||
startTime := time.Now() | ||
count, sleep := 0, 0 | ||
for { | ||
now := time.Now() | ||
req, err := http.NewRequest("GET", hostname, nil) | ||
if !assert.NoError(t, err) { | ||
return false | ||
} | ||
|
||
for k, v := range headers { | ||
// Host header cannot be set via req.Header.Set(), and must be set | ||
// directly. | ||
if strings.ToLower(k) == "host" { | ||
req.Host = v | ||
continue | ||
} | ||
req.Header.Set(k, v) | ||
} | ||
|
||
client := &http.Client{Timeout: time.Second * 10} | ||
resp, err := client.Do(req) | ||
if err == nil && resp.StatusCode == 200 { | ||
if !assert.NotNil(t, resp.Body, "resp.body was nil") { | ||
return false | ||
} | ||
|
||
// Read the body | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if !assert.NoError(t, err) { | ||
return false | ||
} | ||
|
||
bodyText := string(body) | ||
|
||
// Even if we got 200 and a response, it may not be ready for assertion yet - that's specific per test. | ||
if ready(bodyText) { | ||
// Verify it matches expectations | ||
return check(bodyText) | ||
} | ||
} | ||
if now.Sub(startTime) >= maxWait { | ||
fmt.Printf("Timeout after %v. Unable to http.get %v successfully.", maxWait, hostname) | ||
return false | ||
} | ||
count++ | ||
// delay 10s, 20s, then 30s and stay at 30s | ||
if sleep > 30 { | ||
sleep = 30 | ||
} else { | ||
sleep += 10 | ||
} | ||
time.Sleep(time.Duration(sleep) * time.Second) | ||
fmt.Printf("Http Error: %v\n", err) | ||
fmt.Printf(" Retry: %v, elapsed wait: %v, max wait %v\n", count, now.Sub(startTime), maxWait) | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module github.com/pulumi/examples/testing-integration | ||
|
||
go 1.13 | ||
|
||
require ( | ||
github.com/pkg/errors v0.8.1 | ||
github.com/pulumi/pulumi v1.13.0 | ||
github.com/stretchr/testify v1.4.1-0.20191106224347-f1bd0923b832 | ||
) | ||
|
||
replace github.com/Azure/go-autorest => github.com/Azure/go-autorest v12.4.3+incompatible |
Oops, something went wrong.