|
| 1 | +// Copyright 2016-2020, Pulumi Corporation. All rights reserved. |
| 2 | + |
| 3 | +package examples |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "path" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/pulumi/pulumi/pkg/testing/integration" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | +) |
| 18 | + |
| 19 | +func TestS3Website(t *testing.T) { |
| 20 | + cwd, err := os.Getwd() |
| 21 | + if err != nil { |
| 22 | + t.FailNow() |
| 23 | + } |
| 24 | + |
| 25 | + test := integration.ProgramTestOptions{ |
| 26 | + Dir: path.Join(cwd, "program"), |
| 27 | + Quick: true, |
| 28 | + SkipRefresh: true, |
| 29 | + Config: map[string]string{ |
| 30 | + "aws:region": "us-west-1", |
| 31 | + }, |
| 32 | + ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) { |
| 33 | + assertHTTPResult(t, "http://"+stack.Outputs["websiteUrl"].(string), nil, func(body string) bool { |
| 34 | + return assert.Contains(t, body, "Hello, Pulumi!") |
| 35 | + }) |
| 36 | + }, |
| 37 | + } |
| 38 | + integration.ProgramTest(t, &test) |
| 39 | +} |
| 40 | + |
| 41 | +func assertHTTPResult(t *testing.T, output interface{}, headers map[string]string, check func(string) bool) bool { |
| 42 | + return assertHTTPResultWithRetry(t, output, headers, 5*time.Minute, check) |
| 43 | +} |
| 44 | + |
| 45 | +func assertHTTPResultWithRetry(t *testing.T, output interface{}, headers map[string]string, maxWait time.Duration, check func(string) bool) bool { |
| 46 | + return assertHTTPResultShapeWithRetry(t, output, headers, maxWait, func(string) bool { return true }, check) |
| 47 | +} |
| 48 | + |
| 49 | +func assertHTTPResultShapeWithRetry(t *testing.T, output interface{}, headers map[string]string, maxWait time.Duration, |
| 50 | + ready func(string) bool, check func(string) bool) bool { |
| 51 | + hostname, ok := output.(string) |
| 52 | + if !assert.True(t, ok, fmt.Sprintf("expected `%s` output", output)) { |
| 53 | + return false |
| 54 | + } |
| 55 | + |
| 56 | + if !(strings.HasPrefix(hostname, "http://") || strings.HasPrefix(hostname, "https://")) { |
| 57 | + hostname = fmt.Sprintf("http://%s", hostname) |
| 58 | + } |
| 59 | + |
| 60 | + startTime := time.Now() |
| 61 | + count, sleep := 0, 0 |
| 62 | + for { |
| 63 | + now := time.Now() |
| 64 | + req, err := http.NewRequest("GET", hostname, nil) |
| 65 | + if !assert.NoError(t, err) { |
| 66 | + return false |
| 67 | + } |
| 68 | + |
| 69 | + for k, v := range headers { |
| 70 | + // Host header cannot be set via req.Header.Set(), and must be set |
| 71 | + // directly. |
| 72 | + if strings.ToLower(k) == "host" { |
| 73 | + req.Host = v |
| 74 | + continue |
| 75 | + } |
| 76 | + req.Header.Set(k, v) |
| 77 | + } |
| 78 | + |
| 79 | + client := &http.Client{Timeout: time.Second * 10} |
| 80 | + resp, err := client.Do(req) |
| 81 | + if err == nil && resp.StatusCode == 200 { |
| 82 | + if !assert.NotNil(t, resp.Body, "resp.body was nil") { |
| 83 | + return false |
| 84 | + } |
| 85 | + |
| 86 | + // Read the body |
| 87 | + defer resp.Body.Close() |
| 88 | + body, err := ioutil.ReadAll(resp.Body) |
| 89 | + if !assert.NoError(t, err) { |
| 90 | + return false |
| 91 | + } |
| 92 | + |
| 93 | + bodyText := string(body) |
| 94 | + |
| 95 | + // Even if we got 200 and a response, it may not be ready for assertion yet - that's specific per test. |
| 96 | + if ready(bodyText) { |
| 97 | + // Verify it matches expectations |
| 98 | + return check(bodyText) |
| 99 | + } |
| 100 | + } |
| 101 | + if now.Sub(startTime) >= maxWait { |
| 102 | + fmt.Printf("Timeout after %v. Unable to http.get %v successfully.", maxWait, hostname) |
| 103 | + return false |
| 104 | + } |
| 105 | + count++ |
| 106 | + // delay 10s, 20s, then 30s and stay at 30s |
| 107 | + if sleep > 30 { |
| 108 | + sleep = 30 |
| 109 | + } else { |
| 110 | + sleep += 10 |
| 111 | + } |
| 112 | + time.Sleep(time.Duration(sleep) * time.Second) |
| 113 | + fmt.Printf("Http Error: %v\n", err) |
| 114 | + fmt.Printf(" Retry: %v, elapsed wait: %v, max wait %v\n", count, now.Sub(startTime), maxWait) |
| 115 | + } |
| 116 | + |
| 117 | + return false |
| 118 | +} |
0 commit comments