Skip to content

Commit

Permalink
feat: add cymbalshop e2e test (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
caetano-colin authored Nov 22, 2024
1 parent 03b2db6 commit 8296254
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 4 deletions.
15 changes: 12 additions & 3 deletions build/int.cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,20 @@ steps:
- appinfra-apply
- fleetscope-verify

- id: app-e2e
- id: cymbal-bank-e2e
name: "gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS"
args:
["/bin/bash", "-c", "sleep 60s && cft test run TestAppE2EMultitenent --stage verify --verbose"]
["/bin/bash", "-c", "sleep 60 && cft test run TestCymbalBankE2E --stage verify --verbose"]
waitFor:
- appsource-verify-cymbal-bank

- id: cymbal-shop-e2e
name: "gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS"
args:
["/bin/bash", "-c", "cft test run TestCymbalShopE2E --stage verify --verbose"]
waitFor:
- appsource-verify-cymbal-shop

- id: appinfra-teardown
name: "gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS"
args:
Expand All @@ -177,7 +185,8 @@ steps:
"cft test run TestAppInfra --stage teardown --verbose",
]
waitFor:
- app-e2e
- cymbal-bank-e2e
- cymbal-shop-e2e
- appsource-verify-cymbal-bank
- appsource-verify-cymbal-shop
- appinfra-verify
Expand Down
2 changes: 1 addition & 1 deletion test/integration/cymbal-bank/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
maxRetries int = 30
)

func TestAppE2EMultitenent(t *testing.T) {
func TestCymbalBankE2E(t *testing.T) {
multitenant := tft.NewTFBlueprintTest(t, tft.WithTFDir("../../../2-multitenant/envs/development"))
t.Run("End to end tests Cymbal Bank Multitenant", func(t *testing.T) {
jar, err := cookiejar.New(nil)
Expand Down
111 changes: 111 additions & 0 deletions test/integration/cymbal-shop/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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"
"net/http"
"net/http/cookiejar"
"strings"
"testing"
"time"

"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
"github.com/gruntwork-io/terratest/modules/shell"

"github.com/gruntwork-io/terratest/modules/retry"
)

const (
sleepBetweenRetries time.Duration = time.Duration(60) * time.Second
maxRetries int = 30
namespace = "cymbalshops-development"
service = "frontend-external"
)

func getServiceIpAddress(t *testing.T, serviceName string, namespace string) (string, error) {
cmd := fmt.Sprintf("get service %s -n %s -o jsonpath='{.status.loadBalancer.ingress[0].ip}'", serviceName, namespace)
args := strings.Fields(cmd)
kubectlCmd := shell.Command{
Command: "kubectl",
Args: args,
}
return shell.RunCommandAndGetStdOutE(t, kubectlCmd)
}
func connectToFleet(t *testing.T, clusterName string, location string, project string) {
gcloud.Runf(t, "container fleet memberships get-credentials %s --location=%s --project=%s", clusterName, location, project)
}

func TestCymbalShopE2E(t *testing.T) {
multitenant := tft.NewTFBlueprintTest(t, tft.WithTFDir("../../../2-multitenant/envs/development"))
// retrieve cluster location and fleet membership from 2-multitenant
clusterProjectId := multitenant.GetJsonOutput("cluster_project_id").String()
clusterLocation := multitenant.GetJsonOutput("cluster_regions").Array()[0].String()
clusterMembership := multitenant.GetJsonOutput("cluster_membership_ids").Array()[0].String()

// extract clusterName from fleet membership id
splitClusterMembership := strings.Split(clusterMembership, "/")
clusterName := splitClusterMembership[len(splitClusterMembership)-1]

connectToFleet(t, clusterName, clusterLocation, clusterProjectId)
t.Run("Cymbal-Shop End-to-End Test", func(t *testing.T) {
jar, err := cookiejar.New(nil)
if err != nil {
t.Fatal(err)
}
client := &http.Client{
Jar: jar,
}
ctx := context.Background()

ipAddress, err := getServiceIpAddress(t, service, namespace)
if err != nil {
t.Fatal(err)
}

// Test webserver is avaliable
heartbeat := func() (string, error) {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://%s", ipAddress), nil)
if err != nil {
return "", err
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
fmt.Println(resp)
return "", err
}
return fmt.Sprint(resp.StatusCode), err
}
statusCode, _ := retry.DoWithRetryE(
t,
fmt.Sprintf("Checking: %s", ipAddress),
maxRetries,
sleepBetweenRetries,
heartbeat,
)
if err != nil {
t.Fatalf("Error: webserver (%s) not ready after %d attemps, status code: %q",
ipAddress,
maxRetries,
statusCode,
)
}
})
}

0 comments on commit 8296254

Please sign in to comment.