@@ -10,77 +10,136 @@ import (
1010 "github.com/cloudfoundry/cf-test-helpers/v2/cf"
1111 . "github.com/onsi/gomega"
1212 . "github.com/onsi/gomega/gexec"
13+ "os"
14+ "path/filepath"
1315)
1416
1517type LifeCycle interface {
16- Prepare (string , string , string ) (string , string )
18+ CreateAppArgs () []string
19+ PushArgs () []string
1720}
1821
1922type BuildpackLifecycles struct {}
2023type CNBLifecycles struct {}
2124type DockerLifecycles struct {}
2225
23- func (b * BuildpackLifecycles ) Prepare (serviceName , appName , appFeatureFlag string ) (string , string ) {
24-
25- Expect (cf .Cf ("create-app" , appName ).Wait ()).To (Exit (0 ))
26- appGuid , serviceGuid := LifeCycleCommon (serviceName , appName , appFeatureFlag )
27-
28- Expect (cf .Cf (app_helpers .CatnipWithArgs (
29- appName ,
30- "-m" , DEFAULT_MEMORY_LIMIT )... ,
31- ).Wait (Config .CfPushTimeoutDuration ())).To (Exit (0 ))
32-
33- return appGuid , serviceGuid
34-
26+ func (b * BuildpackLifecycles ) CreateAppArgs () []string {
27+ return []string {}
3528}
3629
37- func (c * CNBLifecycles ) Prepare (serviceName , appName , appFeatureFlag string ) (string , string ) {
30+ func (b * BuildpackLifecycles ) PushArgs () []string {
31+ return []string {
32+ "--buildpack" , Config .GetBinaryBuildpackName (),
33+ "-p" , assets .NewAssets ().Catnip ,
34+ "-c" , "./catnip" ,
35+ }
36+ }
3837
39- Expect (cf .Cf ("create-app" , appName , "--app-type" , "cnb" , "--buildpack" , Config .GetGoBuildpackName ()).Wait ()).To (Exit (0 ))
40- appGuid , serviceGuid := LifeCycleCommon (serviceName , appName , appFeatureFlag )
38+ func (c * CNBLifecycles ) CreateAppArgs () []string {
39+ return []string {
40+ "--app-type" , "cnb" ,
41+ "--buildpack" , Config .GetCNBGoBuildpackName (),
42+ }
43+ }
4144
42- Expect (cf .Cf (
43- "push" ,
44- appName ,
45+ func (c * CNBLifecycles ) PushArgs () []string {
46+ return []string {
4547 "--lifecycle" , "cnb" ,
4648 "--buildpack" , Config .GetCNBGoBuildpackName (),
47- "-m" , DEFAULT_MEMORY_LIMIT ,
4849 "-p" , assets .NewAssets ().CatnipSrc ,
49- ).Wait (Config .CfPushTimeoutDuration ())).To (Exit (0 ))
50-
51- return appGuid , serviceGuid
50+ }
5251}
5352
54- func (d * DockerLifecycles ) Prepare (serviceName , appName , appFeatureFlag string ) (string , string ) {
55-
56- Expect (cf .Cf ("create-app" , appName , "--app-type" , "docker" ).Wait ()).To (Exit (0 ))
57- appGuid , serviceGuid := LifeCycleCommon (serviceName , appName , appFeatureFlag )
53+ func (d * DockerLifecycles ) CreateAppArgs () []string {
54+ return []string {
55+ "--app-type" , "docker" ,
56+ }
57+ }
5858
59- Expect (cf .Cf (
60- "push" ,
61- appName ,
59+ func (d * DockerLifecycles ) PushArgs () []string {
60+ return []string {
6261 "--docker-image" , Config .GetCatnipDockerAppImage (),
63- "-m" , DEFAULT_MEMORY_LIMIT ,
64- ).Wait (Config .CfPushTimeoutDuration ())).To (Exit (0 ))
65- return appGuid , serviceGuid
62+ }
6663}
6764
6865const (
6966 TAGS = "list, of, tags"
7067 CREDS = `{"username": "admin", "password":"pa55woRD"}`
7168)
7269
73- func LifeCycleCommon (serviceName , appName , appFeatureFlag string ) (string , string ) {
70+ func Prepare (appName , serviceName , appFeatureFlag string , lifecycle LifeCycle ) (string , string ) {
71+ appGuid := CreateApp (appName , lifecycle .CreateAppArgs ()... )
72+ serviceGuid := CreateUpsi (serviceName )
73+ BindUpsi (appName , serviceName )
74+ EnableFeatureViaAPI (appGuid , appFeatureFlag )
75+
76+ pushArgs := append ([]string {appName }, lifecycle .PushArgs ()... )
77+ Push (pushArgs ... )
78+
79+ return appGuid , serviceGuid
80+ }
81+
82+ func PrepareWithManifest (appName , serviceName , appFeatureFlag string , lifecycle LifeCycle ) (string , string ) {
83+ serviceGuid := CreateUpsi (serviceName )
84+ manifestFile := CreateManifest (appName , serviceName , appFeatureFlag )
85+
86+ pushArgs := append ([]string {"-f" , manifestFile }, lifecycle .PushArgs ()... )
87+ Push (pushArgs ... )
88+
89+ return app_helpers .GetAppGuid (appName ), serviceGuid
90+ }
91+
92+ func CreateApp (appName string , args ... string ) string {
93+ createAppArgs := []string {
94+ "create-app" , appName ,
95+ }
96+ createAppArgs = append (createAppArgs , args ... )
97+ Expect (cf .Cf (createAppArgs ... ).Wait ()).To (Exit (0 ))
98+ appGuid := app_helpers .GetAppGuid (appName )
7499
100+ return appGuid
101+ }
102+
103+ func CreateUpsi (serviceName string ) string {
75104 Expect (cf .Cf ("create-user-provided-service" , serviceName , "-p" , CREDS , "-t" , TAGS ).Wait ()).To (Exit (0 ))
76105 serviceGuid := services .GetServiceInstanceGuid (serviceName )
77106
78- appGuid := app_helpers .GetAppGuid (appName )
107+ return serviceGuid
108+ }
79109
80- appFeatureUrl := fmt .Sprintf ("/v3/apps/%s/features/%s" , appGuid , appFeatureFlag )
81- Expect (cf .Cf ("curl" , appFeatureUrl , "-X" , "PATCH" , "-d" , `{"enabled": true}` ).Wait ()).To (Exit (0 ))
110+ func CreateManifest (appName , serviceName , appFeatureFlag string ) string {
111+ tmpdir , err := os .MkdirTemp (os .TempDir (), appName )
112+ Expect (err ).ToNot (HaveOccurred ())
113+
114+ manifestFile := filepath .Join (tmpdir , "manifest.yml" )
115+ manifestContent := fmt .Sprintf (`---
116+ applications:
117+ - name: %s
118+ features:
119+ %s: true
120+ services:
121+ - %s
122+ ` , appName , appFeatureFlag , serviceName )
123+ err = os .WriteFile (manifestFile , []byte (manifestContent ), 0644 )
124+ Expect (err ).ToNot (HaveOccurred ())
125+
126+ return manifestFile
127+ }
82128
129+ func BindUpsi (appName , serviceName string ) {
83130 Expect (cf .Cf ("bind-service" , appName , serviceName ).Wait ()).To (Exit (0 ))
131+ }
84132
85- return appGuid , serviceGuid
133+ func EnableFeatureViaAPI (appGuid , appFeatureFlag string ) {
134+ appFeatureUrl := fmt .Sprintf ("/v3/apps/%s/features/%s" , appGuid , appFeatureFlag )
135+ Expect (cf .Cf ("curl" , appFeatureUrl , "-X" , "PATCH" , "-d" , `{"enabled": true}` ).Wait ()).To (Exit (0 ))
136+ }
137+
138+ func Push (args ... string ) {
139+ pushArgs := []string {
140+ "push" ,
141+ "-m" , DEFAULT_MEMORY_LIMIT ,
142+ }
143+ pushArgs = append (pushArgs , args ... )
144+ Expect (cf .Cf (pushArgs ... ).Wait (Config .CfPushTimeoutDuration ())).To (Exit (0 ))
86145}
0 commit comments