Skip to content

Commit

Permalink
feat: first version of E2E tests (#180)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Peabody <[email protected]>
  • Loading branch information
amandakarina and apeabody authored Jul 15, 2024
1 parent e54e3dc commit 44f4ffc
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 12 deletions.
16 changes: 11 additions & 5 deletions build/int.cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,26 @@ steps:
waitFor:
- appinfra-apply

- id: appsource-init
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestSourceCymbalBank --stage init --verbose']
waitFor:
- appinfra-apply
- id: appsource-verify
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestSourceCymbalBank --stage verify --verbose']
waitFor:
- appinfra-apply
- id: wait-warmup
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'echo "waiting warmup." && sleep 2m']
waitFor:
- appsource-verify
- id: app-e2e
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestAppE2E --stage verify --verbose']
waitFor:
- wait-warmup
- id: appinfra-teardown
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestAppInfra --stage teardown --verbose']
waitFor:
- app-e2e
- appsource-verify
- appinfra-verify
- appfactory-verify
Expand Down
32 changes: 32 additions & 0 deletions test/integration/appsource/assets/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
labels:
application: cymbal-bank
environment: development
team: frontend
tier: web
annotations:
kubernetes.io/ingress.global-static-ip-name: frontend-ip
kubernetes.io/ingress.class: gce
name: frontend
spec:
defaultBackend:
service:
name: frontend
port:
number: 80
22 changes: 22 additions & 0 deletions test/integration/appsource/assets/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
- destinationrule.yaml
- ingress.yaml
components:
- ../../../../components/development
7 changes: 7 additions & 0 deletions test/integration/appsource/cymbal_bank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ func TestSourceCymbalBank(t *testing.T) {
t.Fatal(err)
}

if mapPath == "frontend" {
err = cp.Copy("assets/", fmt.Sprintf("%s/src/%s/k8s/overlays/development/", tmpDirApp, mapPath))
if err != nil {
t.Fatal(err)
}
}

gitAppRun("add", ".")
gitApp.CommitWithMsg("initial commit", []string{"--allow-empty"})
gitAppRun("push", "--all", "google", "-f")
Expand Down
182 changes: 182 additions & 0 deletions test/integration/cymbal-bank/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cymbalbank_e2e

import (
"context"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"testing"

"github.com/google/uuid"

"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
)

func TestAppE2E(t *testing.T) {
multitenant := tft.NewTFBlueprintTest(t, tft.WithTFDir("../../../2-multitenant/envs/development"))
t.Run("End to end tests", func(t *testing.T) {
jar, err := cookiejar.New(nil)
if err != nil {
t.Fatal(err)
}
client := &http.Client{
Jar: jar,
}
ctx := context.Background()
ipAddress := multitenant.GetJsonOutput("app_ip_addresses").Get("cymbal-bank.frontend-ip").String()
resp, err := login(ctx, client, ipAddress)
fmt.Printf("Login resp: %v \n", resp)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
fmt.Println(resp)
t.Fatal(err)
}

resp, err = home(ctx, client, ipAddress)
fmt.Printf("Home resp: %v \n", resp)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
fmt.Println(resp)
t.Fatal(err)
}

resp, err = deposit(ctx, client, ipAddress)
fmt.Printf("Deposit resp: %v \n", resp)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
fmt.Println(resp)
t.Fatal(err)
}

_, err = checkTransaction(ctx, client, ipAddress, "5,230.00", "DEPOSIT")
if err != nil {
t.Fatal(err)
}
resp, err = transfer(ctx, client, ipAddress)
fmt.Printf("Transfer resp: %v \n", resp)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
fmt.Println(resp)
t.Fatal(err)
}
_, err = checkTransaction(ctx, client, ipAddress, "320.98", "PAYMENT")
if err != nil {
t.Fatal(err)
}
})
}

func login(ctx context.Context, c *http.Client, ipAddress string) (*http.Response, error) {
fmt.Println("APP Login.")
loginUrl := fmt.Sprintf("http://%s/login", ipAddress)
form := make(url.Values)
form.Add("username", "testuser")
form.Add("password", "bankofanthos")
b := strings.NewReader(form.Encode())
req, err := http.NewRequestWithContext(ctx, "POST", loginUrl, b)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return c.Do(req)
}

func home(ctx context.Context, c *http.Client, ipAddress string) (*http.Response, error) {

homeUrl := fmt.Sprintf("http://%s/home", ipAddress)
req, err := http.NewRequestWithContext(ctx, "GET", homeUrl, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return c.Do(req)
}

func deposit(ctx context.Context, c *http.Client, ipAddress string) (*http.Response, error) {
fmt.Println("APP Deposit.")
depositUrl := fmt.Sprintf("http://%s/deposit", ipAddress)
form := make(url.Values)
form.Add("account", "{\"account_num\": \"9099791699\", \"routing_num\": \"808889588\"}")
form.Add("external_account_num", "")
form.Add("external_routing_num", "")
form.Add("external_label", "")
form.Add("amount", "5230.00")
form.Add("uuid", uuid.NewString())
req, err := http.NewRequestWithContext(ctx, "POST", depositUrl, strings.NewReader(form.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
fmt.Printf("Deposit value: %s \n", form.Get("amount"))
return c.Do(req)
}

func checkTransaction(ctx context.Context, c *http.Client, ipAddress string, amount string, typeTransaction string) (*http.Response, error) {
resp, err := home(ctx, c, ipAddress)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
fmt.Println(resp)
return nil, err
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
bodyString := string(bodyBytes)
if typeTransaction == "DEPOSIT" {
amount = fmt.Sprintf("+$%s", amount)
} else if typeTransaction == "PAYMENT" {
amount = fmt.Sprintf("-$%s", amount)
}
if !strings.Contains(bodyString, amount) {
fmt.Printf("Error on %s!\n", typeTransaction)
return nil, fmt.Errorf("Error on %s!\n", typeTransaction)
}
return resp, nil
}

func transfer(ctx context.Context, c *http.Client, ipAddress string) (*http.Response, error) {
fmt.Println("APP Transfer.")
transferUrl := fmt.Sprintf("http://%s/payment", ipAddress)
form := make(url.Values)
form.Add("account_num", "1077441377")
form.Add("contact_account_num", "")
form.Add("contact_label", "")
form.Add("amount", "320.98")
form.Add("uuid", uuid.NewString())
req, err := http.NewRequestWithContext(ctx, "POST", transferUrl, strings.NewReader(form.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
fmt.Printf("Transfer value: %s \n", form.Get("amount"))
return c.Do(req)
}
6 changes: 3 additions & 3 deletions test/integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ go 1.22

require (
github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test v0.16.0
github.com/google/uuid v1.6.0
github.com/gruntwork-io/terratest v0.46.16
github.com/otiai10/copy v1.14.0
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.1
)
Expand All @@ -30,7 +32,6 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand All @@ -52,7 +53,6 @@ require (
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.2-0.20210821155943-2d9075ca8770 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/otiai10/copy v1.14.0
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
Expand All @@ -64,7 +64,7 @@ require (
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions test/integration/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o=
github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
github.com/googleapis/enterprise-certificate-proxy v0.1.0/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
github.com/googleapis/enterprise-certificate-proxy v0.2.0/go.mod h1:8C0jb7/mgJe/9KK8Lm7X9ctZC2t60YyIpYEI16jx0Qg=
Expand Down Expand Up @@ -599,8 +599,8 @@ golang.org/x/oauth2 v0.0.0-20220822191816-0ebed06d0094/go.mod h1:h4gKUeWbJ4rQPri
golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
golang.org/x/oauth2 v0.1.0/go.mod h1:G9FE4dLTsbXUu90h/Pf85g4w1D+SSAgR+q46nJZ8M4A=
golang.org/x/oauth2 v0.16.0 h1:aDkGMBSYxElaoP81NpoUoz2oo2R2wHdZpGToUxfyQrQ=
golang.org/x/oauth2 v0.16.0/go.mod h1:hqZ+0LWXsiVoZpeld6jVt06P3adbS2Uu911W1SsJv2o=
golang.org/x/oauth2 v0.12.0 h1:smVPGxink+n1ZI5pkQa8y6fZT0RW0MgCO5bFpepy4B4=
golang.org/x/oauth2 v0.12.0/go.mod h1:A74bZ3aGXgCY0qaIC9Ahg6Lglin4AMAco8cIv9baba4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down

0 comments on commit 44f4ffc

Please sign in to comment.