@@ -5,123 +5,67 @@ import (
5
5
"crypto/tls"
6
6
"fmt"
7
7
"net/http"
8
- "os"
9
8
"strings"
10
9
"time"
11
10
11
+ . "code.cloudfoundry.org/korifi/tests/matchers"
12
12
"github.com/cloudfoundry/cf-test-helpers/cf"
13
13
"github.com/cloudfoundry/cf-test-helpers/generator"
14
14
. "github.com/onsi/ginkgo/v2"
15
- "github.com/onsi/ginkgo/v2/types"
16
15
. "github.com/onsi/gomega"
16
+ "github.com/onsi/gomega/gbytes"
17
17
. "github.com/onsi/gomega/gexec"
18
+ "github.com/onsi/gomega/types"
18
19
)
19
20
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
-
36
21
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
+ ))
69
28
})
29
+ })
70
30
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
+ })
75
39
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 ))
81
44
})
45
+ })
82
46
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 () {
85
49
serviceName := generator .PrefixedRandomName (NamePrefix , "svc" )
86
50
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 ))
98
54
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
+ })
121
58
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
+ ))
125
69
})
126
70
})
127
71
})
@@ -149,3 +93,16 @@ func loginAs(user string) {
149
93
loginSession := cf .CfWithStdin (bytes .NewBufferString (user + "\n \n " ), "login" )
150
94
Eventually (loginSession ).Should (Exit (0 ))
151
95
}
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