Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor smoke tests #2721

Merged
merged 3 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@
.idea/
~/.*
*.test
# Don't add zipped assets. Use the runtime zipping logic in the e2e suite instead.
tests/e2e/assets/*.zip
dorifi
tests/assets/dorifi/dorifi
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ generate-fakes:

fmt: install-gofumpt install-shfmt
$(GOFUMPT) -w .
$(SHFMT) -f . | grep -v '^tests/vendor' | grep -v '^tests/e2e/assets/vendored' | xargs $(SHFMT) -w -i 2 -ci
$(SHFMT) -f . | grep -v '^tests/vendor' | xargs $(SHFMT) -w -i 2 -ci

vet: ## Run go vet against code.
go vet ./...
Expand All @@ -62,8 +62,15 @@ test-tools:
test-e2e: build-dorifi
./scripts/run-tests.sh tests/e2e

test-crds: build-dorifi
./tests/crds/run-tests.sh

test-smoke: build-dorifi
./tests/smoke/run-tests.sh


build-dorifi:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ../dorifi/dorifi -C tests/e2e/assets/golang .
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ../dorifi/dorifi -C tests/assets/golang .

GOFUMPT = $(shell go env GOPATH)/bin/gofumpt
install-gofumpt:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/crds/crds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ var _ = Describe("Using the k8s API directly", Ordered, func() {
Eventually(cf.Cf("target", "-o", orgDisplayName, "-s", spaceDisplayName)).Should(Exit(0))

Eventually(
cf.Cf("push", PrefixedGUID("crds-test-app"), "-p", "../smoke/assets/test-node-app", "--no-start"), // This could be any app
cf.Cf("push", PrefixedGUID("crds-test-app"), "-p", "../assets/dorifi", "--no-start"), // This could be any app
"20s",
).Should(Exit(0))
})
Expand Down
14 changes: 0 additions & 14 deletions tests/crds/run-crds-tests-kind.sh

This file was deleted.

20 changes: 20 additions & 0 deletions tests/crds/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

export CRDS_TEST_API_ENDPOINT="${CRDS_TEST_API_ENDPOINT:-https://localhost}"
export CRDS_TEST_SKIP_SSL="${CRDS_TEST_SKIP_SSL:-true}"
export CRDS_TEST_CLI_USER="${CRDS_TEST_CLI_USER:-}"

if [[ -z "${CRDS_TEST_CLI_USER:-}" ]]; then
export CRDS_TEST_CLI_USER="crd-cli-test-user"
"${SCRIPT_DIR}/../../scripts/create-new-user.sh" "$CRDS_TEST_CLI_USER"
fi

pushd "${SCRIPT_DIR}"
{
go run github.com/onsi/ginkgo/v2/ginkgo
}
popd
4 changes: 2 additions & 2 deletions tests/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ var _ = SynchronizedBeforeSuite(func() []byte {
// The DEFAULT_APP_BITS_PATH and DEFAULT_APP_RESPONSE environment variables are a workaround to allow e2e tests to run
// with a different app in these environments.
// See https://github.com/cloudfoundry/korifi/issues/2355 for refactoring ideas
DefaultAppBitsFile: zipAsset(getEnv("DEFAULT_APP_BITS_PATH", "assets/dorifi")),
MultiProcessAppBitsFile: zipAsset("assets/multi-process"),
DefaultAppBitsFile: zipAsset(getEnv("DEFAULT_APP_BITS_PATH", "../assets/dorifi")),
MultiProcessAppBitsFile: zipAsset("../assets/multi-process"),
}

bs, err := json.Marshal(sharedData)
Expand Down
9 changes: 7 additions & 2 deletions tests/matchers/jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ func MatchJSONPath(path string, expected any) *JSONPathMatcher {
}

func (m *JSONPathMatcher) Match(actual interface{}) (bool, error) {
bs, ok := actual.([]byte)
if !ok {
var bs []byte
switch a := actual.(type) {
case []byte:
bs = a
case string:
bs = []byte(a)
default:
return false, fmt.Errorf("found %T, expected []byte", actual)
}

Expand Down
4 changes: 0 additions & 4 deletions tests/smoke/assets/test-node-app/package.json

This file was deleted.

11 changes: 0 additions & 11 deletions tests/smoke/assets/test-node-app/server.js

This file was deleted.

14 changes: 0 additions & 14 deletions tests/smoke/run-smoke-tests-kind.sh

This file was deleted.

17 changes: 17 additions & 0 deletions tests/smoke/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

export SMOKE_TEST_USER="${SMOKE_TEST_USER:-cf-admin}"
export SMOKE_TEST_APPS_DOMAIN="${SMOKE_TEST_APPS_DOMAIN:-apps-127-0-0-1.nip.io}"
export SMOKE_TEST_APP_ROUTE_PROTOCOL="${SMOKE_TEST_APP_ROUTE_PROTOCOL:-https}"
export SMOKE_TEST_API_ENDPOINT="${SMOKE_TEST_API_ENDPOINT:-https://localhost}"
export SMOKE_TEST_SKIP_SSL="${SMOKE_TEST_SKIP_SSL:-true}"

pushd "${SCRIPT_DIR}"
{
go run github.com/onsi/ginkgo/v2/ginkgo
}
popd
67 changes: 67 additions & 0 deletions tests/smoke/smoke_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,82 @@
package smoke_test

import (
"os"
"testing"
"time"

"github.com/cloudfoundry/cf-test-helpers/cf"
"github.com/cloudfoundry/cf-test-helpers/generator"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/types"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)

const NamePrefix = "cf-on-k8s-smoke"

var (
orgName string
spaceName string
appName string
appsDomain string
appRouteProtocol string
)

func TestSmoke(t *testing.T) {
RegisterFailHandler(Fail)
SetDefaultEventuallyTimeout(10 * time.Minute)
RunSpecs(t, "Smoke Tests Suite")
}

var _ = BeforeSuite(func() {
apiArguments := []string{"api", GetRequiredEnvVar("SMOKE_TEST_API_ENDPOINT")}
skipSSL := os.Getenv("SMOKE_TEST_SKIP_SSL") == "true"
if skipSSL {
apiArguments = append(apiArguments, "--skip-ssl-validation")
}

Eventually(cf.Cf(apiArguments...)).Should(Exit(0))

loginAs(GetRequiredEnvVar("SMOKE_TEST_USER"))

appRouteProtocol = GetDefaultedEnvVar("SMOKE_TEST_APP_ROUTE_PROTOCOL", "https")
appsDomain = GetRequiredEnvVar("SMOKE_TEST_APPS_DOMAIN")
orgName = generator.PrefixedRandomName(NamePrefix, "org")
spaceName = generator.PrefixedRandomName(NamePrefix, "space")
appName = generator.PrefixedRandomName(NamePrefix, "app")

Eventually(cf.Cf("create-org", orgName)).Should(Exit(0))
Eventually(cf.Cf("create-space", "-o", orgName, spaceName)).Should(Exit(0))
Eventually(cf.Cf("target", "-o", orgName, "-s", spaceName)).Should(Exit(0))

Eventually(
cf.Cf("push", appName, "-p", "../assets/dorifi"),
).Should(Exit(0))
})

var _ = AfterSuite(func() {
if CurrentSpecReport().State.Is(types.SpecStateFailed) {
printAppReport(appName)
}

if orgName != "" {
Eventually(func() *Session {
return cf.Cf("delete-org", orgName, "-f").Wait()
}, 2*time.Minute, 1*time.Second).Should(Exit(0))
}
})

func GetRequiredEnvVar(envVarName string) string {
value, ok := os.LookupEnv(envVarName)
Expect(ok).To(BeTrue(), envVarName+" environment variable is required, but was not provided.")
return value
}

func GetDefaultedEnvVar(envVarName, defaultValue string) string {
value, ok := os.LookupEnv(envVarName)
if !ok {
return defaultValue
}
return value
}
Loading
Loading