Skip to content

Commit bccf423

Browse files
Share test assets in all e2e tests
Also improve the structure of the smoke test Co-authored-by: Georgi Sabev <georgethebeatle@gmail.com>
1 parent 6422edd commit bccf423

File tree

14 files changed

+131
-120
lines changed

14 files changed

+131
-120
lines changed

.gitignore

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44
.idea/
55
~/.*
66
*.test
7-
# Don't add zipped assets. Use the runtime zipping logic in the e2e suite instead.
8-
tests/e2e/assets/*.zip
9-
dorifi
7+
tests/assets/dorifi/dorifi

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ generate-fakes:
3737

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

4242
vet: ## Run go vet against code.
4343
go vet ./...
@@ -63,7 +63,7 @@ test-e2e: build-dorifi
6363
./scripts/run-tests.sh tests/e2e
6464

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

6868
GOFUMPT = $(shell go env GOPATH)/bin/gofumpt
6969
install-gofumpt:
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/crds/crds_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ var _ = Describe("Using the k8s API directly", Ordered, func() {
112112
Eventually(cf.Cf("target", "-o", orgDisplayName, "-s", spaceDisplayName)).Should(Exit(0))
113113

114114
Eventually(
115-
cf.Cf("push", PrefixedGUID("crds-test-app"), "-p", "../smoke/assets/test-node-app", "--no-start"), // This could be any app
115+
cf.Cf("push", PrefixedGUID("crds-test-app"), "-p", "../assets/dorifi", "--no-start"), // This could be any app
116116
"20s",
117117
).Should(Exit(0))
118118
})

tests/e2e/e2e_suite_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ var _ = SynchronizedBeforeSuite(func() []byte {
302302
// The DEFAULT_APP_BITS_PATH and DEFAULT_APP_RESPONSE environment variables are a workaround to allow e2e tests to run
303303
// with a different app in these environments.
304304
// See https://github.com/cloudfoundry/korifi/issues/2355 for refactoring ideas
305-
DefaultAppBitsFile: zipAsset(getEnv("DEFAULT_APP_BITS_PATH", "assets/dorifi")),
306-
MultiProcessAppBitsFile: zipAsset("assets/multi-process"),
305+
DefaultAppBitsFile: zipAsset(getEnv("DEFAULT_APP_BITS_PATH", "../assets/dorifi")),
306+
MultiProcessAppBitsFile: zipAsset("../assets/multi-process"),
307307
}
308308

309309
bs, err := json.Marshal(sharedData)

tests/smoke/assets/test-node-app/package.json

-4
This file was deleted.

tests/smoke/assets/test-node-app/server.js

-11
This file was deleted.

tests/smoke/smoke_suite_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,86 @@
11
package smoke_test
22

33
import (
4+
"os"
45
"testing"
56
"time"
67

8+
"github.com/cloudfoundry/cf-test-helpers/cf"
9+
"github.com/cloudfoundry/cf-test-helpers/generator"
710
. "github.com/onsi/ginkgo/v2"
11+
"github.com/onsi/ginkgo/v2/types"
812
. "github.com/onsi/gomega"
13+
. "github.com/onsi/gomega/gexec"
14+
)
15+
16+
const NamePrefix = "cf-on-k8s-smoke"
17+
18+
var (
19+
orgName string
20+
spaceName string
21+
appName string
22+
appsDomain string
23+
appRouteProtocol string
924
)
1025

1126
func TestSmoke(t *testing.T) {
1227
RegisterFailHandler(Fail)
1328
SetDefaultEventuallyTimeout(10 * time.Minute)
1429
RunSpecs(t, "Smoke Tests Suite")
1530
}
31+
32+
var _ = BeforeSuite(func() {
33+
apiArguments := []string{"api", GetRequiredEnvVar("SMOKE_TEST_API_ENDPOINT")}
34+
skipSSL := os.Getenv("SMOKE_TEST_SKIP_SSL") == "true"
35+
if skipSSL {
36+
apiArguments = append(apiArguments, "--skip-ssl-validation")
37+
}
38+
39+
Eventually(cf.Cf(apiArguments...)).Should(Exit(0))
40+
41+
loginAs(GetRequiredEnvVar("SMOKE_TEST_USER"))
42+
43+
appRouteProtocol = GetDefaultedEnvVar("SMOKE_TEST_APP_ROUTE_PROTOCOL", "https")
44+
appsDomain = GetRequiredEnvVar("SMOKE_TEST_APPS_DOMAIN")
45+
orgName = generator.PrefixedRandomName(NamePrefix, "org")
46+
spaceName = generator.PrefixedRandomName(NamePrefix, "space")
47+
appName = generator.PrefixedRandomName(NamePrefix, "app")
48+
49+
Eventually(cf.Cf("create-org", orgName)).Should(Exit(0))
50+
Eventually(cf.Cf("create-space", "-o", orgName, spaceName)).Should(Exit(0))
51+
Eventually(cf.Cf("target", "-o", orgName, "-s", spaceName)).Should(Exit(0))
52+
53+
Eventually(
54+
cf.Cf("push", appName, "-p", "../assets/dorifi"),
55+
).Should(Exit(0))
56+
})
57+
58+
var _ = AfterSuite(func() {
59+
if CurrentSpecReport().State.Is(types.SpecStateFailed) {
60+
printAppReport(appName)
61+
}
62+
63+
if orgName != "" {
64+
Eventually(func() *Session {
65+
return cf.Cf("delete-org", orgName, "-f").Wait()
66+
}, 2*time.Minute, 1*time.Second).Should(Exit(0))
67+
}
68+
})
69+
70+
func GetRequiredEnvVar(envVarName string) string {
71+
value, ok := os.LookupEnv(envVarName)
72+
Expect(ok).To(BeTrue(), envVarName+" environment variable is required, but was not provided.")
73+
return value
74+
}
75+
76+
func GetDefaultedEnvVar(envVarName, defaultValue string) string {
77+
value, ok := os.LookupEnv(envVarName)
78+
if !ok {
79+
return defaultValue
80+
}
81+
return value
82+
}
83+
84+
func getSpaceName() string {
85+
return spaceName
86+
}

tests/smoke/smoke_test.go

+54-97
Original file line numberDiff line numberDiff line change
@@ -5,123 +5,67 @@ import (
55
"crypto/tls"
66
"fmt"
77
"net/http"
8-
"os"
98
"strings"
109
"time"
1110

11+
. "code.cloudfoundry.org/korifi/tests/matchers"
1212
"github.com/cloudfoundry/cf-test-helpers/cf"
1313
"github.com/cloudfoundry/cf-test-helpers/generator"
1414
. "github.com/onsi/ginkgo/v2"
15-
"github.com/onsi/ginkgo/v2/types"
1615
. "github.com/onsi/gomega"
16+
"github.com/onsi/gomega/gbytes"
1717
. "github.com/onsi/gomega/gexec"
18+
"github.com/onsi/gomega/types"
1819
)
1920

20-
const NamePrefix = "cf-on-k8s-smoke"
21-
22-
func GetRequiredEnvVar(envVarName string) string {
23-
value, ok := os.LookupEnv(envVarName)
24-
Expect(ok).To(BeTrue(), envVarName+" environment variable is required, but was not provided.")
25-
return value
26-
}
27-
28-
func GetDefaultedEnvVar(envVarName, defaultValue string) string {
29-
value, ok := os.LookupEnv(envVarName)
30-
if !ok {
31-
return defaultValue
32-
}
33-
return value
34-
}
35-
3621
var _ = Describe("Smoke Tests", func() {
37-
When("running cf push", func() {
38-
var (
39-
orgName string
40-
appName string
41-
appsDomain, appRouteProtocol string
42-
)
43-
44-
BeforeEach(func() {
45-
apiArguments := []string{"api", GetRequiredEnvVar("SMOKE_TEST_API_ENDPOINT")}
46-
skipSSL := os.Getenv("SMOKE_TEST_SKIP_SSL") == "true"
47-
if skipSSL {
48-
apiArguments = append(apiArguments, "--skip-ssl-validation")
49-
}
50-
51-
By("targetting the API")
52-
cfAPI := cf.Cf(apiArguments...)
53-
Eventually(cfAPI).Should(Exit(0))
54-
55-
By("logging in")
56-
loginAs(GetRequiredEnvVar("SMOKE_TEST_USER"))
57-
58-
appRouteProtocol = GetDefaultedEnvVar("SMOKE_TEST_APP_ROUTE_PROTOCOL", "https")
59-
appsDomain = GetRequiredEnvVar("SMOKE_TEST_APPS_DOMAIN")
60-
orgName = generator.PrefixedRandomName(NamePrefix, "org")
61-
spaceName := generator.PrefixedRandomName(NamePrefix, "space")
62-
63-
By("creating an org")
64-
Eventually(cf.Cf("create-org", orgName)).Should(Exit(0))
65-
By("creating a space")
66-
Eventually(cf.Cf("create-space", "-o", orgName, spaceName)).Should(Exit(0))
67-
By("targetting the org")
68-
Eventually(cf.Cf("target", "-o", orgName, "-s", spaceName)).Should(Exit(0))
22+
Describe("cf push", func() {
23+
It("runs the app", func() {
24+
appResponseShould("/", SatisfyAll(
25+
HaveHTTPStatus(http.StatusOK),
26+
HaveHTTPBody(ContainSubstring("Hi, I'm Dorifi!")),
27+
))
6928
})
29+
})
7030

71-
AfterEach(func() {
72-
if CurrentSpecReport().State.Is(types.SpecStateFailed) {
73-
printAppReport(appName)
74-
}
31+
Describe("cf logs", func() {
32+
It("prints app logs", func() {
33+
Eventually(func(g Gomega) {
34+
cfLogs := cf.Cf("logs", appName, "--recent")
35+
g.Expect(cfLogs.Wait().Out).To(gbytes.Say("Listening on port 8080"))
36+
}, 2*time.Minute, 2*time.Second).Should(Succeed())
37+
})
38+
})
7539

76-
if orgName != "" {
77-
Eventually(func() *Session {
78-
return cf.Cf("delete-org", orgName, "-f").Wait()
79-
}, 2*time.Minute, 1*time.Second).Should(Exit(0))
80-
}
40+
Describe("cf run-task", func() {
41+
It("succeeds", func() {
42+
cfRunTask := cf.Cf("run-task", appName, "-c", `echo "Hello from the task"`)
43+
Eventually(cfRunTask).Should(Exit(0))
8144
})
45+
})
8246

83-
It("creates a routable app pod in Kubernetes from a source-based app", func() {
84-
appName = generator.PrefixedRandomName(NamePrefix, "app")
47+
Describe("cf bind-service", func() {
48+
BeforeEach(func() {
8549
serviceName := generator.PrefixedRandomName(NamePrefix, "svc")
8650

87-
By("pushing an unstarted app")
88-
cfPush := cf.Cf("push", appName, "-p", "assets/test-node-app", "--no-start")
89-
Eventually(cfPush).Should(Exit(0))
90-
91-
By("creating a user-provided service instance")
92-
cfCreateService := cf.Cf("create-user-provided-service", serviceName, "-p", `{"key1":"value1","key2":"value2"}`)
93-
Eventually(cfCreateService).Should(Exit(0))
94-
95-
By("binding the service to the app")
96-
cfBindService := cf.Cf("bind-service", appName, serviceName)
97-
Eventually(cfBindService).Should(Exit(0))
51+
Eventually(
52+
cf.Cf("create-user-provided-service", serviceName, "-p", `{"key1":"value1","key2":"value2"}`),
53+
).Should(Exit(0))
9854

99-
By("staging and starting the app")
100-
cfStart := cf.Cf("start", appName)
101-
Eventually(cfStart).Should(Exit(0))
102-
103-
By("sending a request to the app")
104-
var httpClient http.Client
105-
httpClient.Transport = &http.Transport{
106-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
107-
}
108-
109-
Eventually(func(g Gomega) {
110-
resp, err := httpClient.Get(fmt.Sprintf("%s://%s.%s", appRouteProtocol, appName, appsDomain))
111-
g.Expect(err).NotTo(HaveOccurred())
112-
g.Expect(resp).To(HaveHTTPStatus(http.StatusOK))
113-
g.Expect(resp).To(HaveHTTPBody(ContainSubstring("Hello World")))
114-
}, 5*time.Minute, 30*time.Second).Should(Succeed())
115-
116-
By("checking the app logs")
117-
Eventually(func(g Gomega) {
118-
cfLogs := cf.Cf("logs", appName, "--recent")
119-
g.Expect(string(cfLogs.Wait().Out.Contents())).To(ContainSubstring("Console output from test-node-app"))
120-
}, 2*time.Minute, 2*time.Second).Should(Succeed())
55+
Eventually(cf.Cf("bind-service", appName, serviceName)).Should(Exit(0))
56+
Eventually(cf.Cf("restart", appName)).Should(Exit(0))
57+
})
12158

122-
By("running a task")
123-
cfRunTask := cf.Cf("run-task", appName, "-c", `echo "Hello from the task"`)
124-
Eventually(cfRunTask).Should(Exit(0))
59+
It("binds the service to the app", func() {
60+
appResponseShould("/env.json", SatisfyAll(
61+
HaveHTTPStatus(http.StatusOK),
62+
HaveHTTPBody(
63+
MatchJSONPath("$.VCAP_SERVICES", SatisfyAll(
64+
MatchJSONPath(`$["user-provided"][0].credentials.key1`, "value1"),
65+
MatchJSONPath(`$["user-provided"][0].credentials.key2`, "value2"),
66+
)),
67+
),
68+
))
12569
})
12670
})
12771
})
@@ -149,3 +93,16 @@ func loginAs(user string) {
14993
loginSession := cf.CfWithStdin(bytes.NewBufferString(user+"\n\n"), "login")
15094
Eventually(loginSession).Should(Exit(0))
15195
}
96+
97+
func appResponseShould(requestPath string, matchExpectations types.GomegaMatcher) {
98+
var httpClient http.Client
99+
httpClient.Transport = &http.Transport{
100+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
101+
}
102+
103+
Eventually(func(g Gomega) {
104+
resp, err := httpClient.Get(fmt.Sprintf("%s://%s.%s%s", appRouteProtocol, appName, appsDomain, requestPath))
105+
g.Expect(err).NotTo(HaveOccurred())
106+
g.Expect(resp).To(matchExpectations)
107+
}, 5*time.Minute, 30*time.Second).Should(Succeed())
108+
}

0 commit comments

Comments
 (0)