github.com/arunkumar7540/cli@v6.45.0+incompatible/integration/helpers/app.go (about) 1 package helpers 2 3 import ( 4 "archive/zip" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "math/rand" 9 "os" 10 "path/filepath" 11 "strings" 12 13 "github.com/onsi/gomega/gbytes" 14 15 . "github.com/onsi/gomega" 16 . "github.com/onsi/gomega/gexec" 17 "gopkg.in/yaml.v2" 18 ) 19 20 // WithHelloWorldApp creates a simple application to use with your CLI command 21 // (typically CF Push). When pushing, be aware of specifying '-b 22 // staticfile_buildpack" so that your app will correctly start up with the 23 // proper buildpack. 24 func WithHelloWorldApp(f func(dir string)) { 25 dir, err := ioutil.TempDir("", "simple-app") 26 Expect(err).ToNot(HaveOccurred()) 27 defer os.RemoveAll(dir) 28 29 tempfile := filepath.Join(dir, "index.html") 30 err = ioutil.WriteFile(tempfile, []byte(fmt.Sprintf("hello world %d", rand.Int())), 0666) 31 Expect(err).ToNot(HaveOccurred()) 32 33 err = ioutil.WriteFile(filepath.Join(dir, "Staticfile"), nil, 0666) 34 Expect(err).ToNot(HaveOccurred()) 35 36 f(dir) 37 } 38 39 // WithMultiEndpointApp creates a simple application to use with your CLI command 40 // (typically CF Push). It has multiple endpoints which are helpful when testing 41 // http healthchecks. 42 func WithMultiEndpointApp(f func(dir string)) { 43 dir, err := ioutil.TempDir("", "simple-app") 44 Expect(err).ToNot(HaveOccurred()) 45 defer os.RemoveAll(dir) 46 47 tempfile := filepath.Join(dir, "index.html") 48 err = ioutil.WriteFile(tempfile, []byte(fmt.Sprintf("hello world %d", rand.Int())), 0666) 49 Expect(err).ToNot(HaveOccurred()) 50 51 tempfile = filepath.Join(dir, "other_endpoint.html") 52 err = ioutil.WriteFile(tempfile, []byte("other endpoint"), 0666) 53 Expect(err).ToNot(HaveOccurred()) 54 55 tempfile = filepath.Join(dir, "third_endpoint.html") 56 err = ioutil.WriteFile(tempfile, []byte("third endpoint"), 0666) 57 Expect(err).ToNot(HaveOccurred()) 58 59 err = ioutil.WriteFile(filepath.Join(dir, "Staticfile"), nil, 0666) 60 Expect(err).ToNot(HaveOccurred()) 61 62 f(dir) 63 } 64 65 // WithNoResourceMatchedApp creates a simple application to use with your CLI 66 // command (typically CF Push). When pushing, be aware of specifying '-b 67 // staticfile_buildpack" so that your app will correctly start up with the 68 // proper buildpack. 69 func WithNoResourceMatchedApp(f func(dir string)) { 70 dir, err := ioutil.TempDir("", "simple-app") 71 Expect(err).ToNot(HaveOccurred()) 72 defer os.RemoveAll(dir) 73 74 tempfile := filepath.Join(dir, "index.html") 75 76 err = ioutil.WriteFile(tempfile, []byte(fmt.Sprintf("hello world %s", strings.Repeat("a", 65*1024))), 0666) 77 Expect(err).ToNot(HaveOccurred()) 78 79 f(dir) 80 } 81 82 // WithMultiBuildpackApp creates a multi-buildpack application to use with the CF push command. 83 func WithMultiBuildpackApp(f func(dir string)) { 84 f("../../assets/go_calls_ruby") 85 } 86 87 // WithProcfileApp creates an application to use with your CLI command 88 // that contains Procfile defining web and worker processes. 89 func WithProcfileApp(f func(dir string)) { 90 dir, err := ioutil.TempDir("", "simple-ruby-app") 91 Expect(err).ToNot(HaveOccurred()) 92 defer os.RemoveAll(dir) 93 94 err = ioutil.WriteFile(filepath.Join(dir, "Procfile"), []byte(`--- 95 web: ruby -run -e httpd . -p $PORT 96 console: bundle exec irb`, 97 ), 0666) 98 Expect(err).ToNot(HaveOccurred()) 99 100 err = ioutil.WriteFile(filepath.Join(dir, "Gemfile"), nil, 0666) 101 Expect(err).ToNot(HaveOccurred()) 102 103 err = ioutil.WriteFile(filepath.Join(dir, "Gemfile.lock"), []byte(` 104 GEM 105 specs: 106 107 PLATFORMS 108 ruby 109 110 DEPENDENCIES 111 112 BUNDLED WITH 113 1.15.0 114 `), 0666) 115 Expect(err).ToNot(HaveOccurred()) 116 117 f(dir) 118 } 119 120 // WithCrashingApp creates an application to use with your CLI command 121 // that will not successfully start its `web` process 122 func WithCrashingApp(f func(dir string)) { 123 dir, err := ioutil.TempDir("", "crashing-ruby-app") 124 Expect(err).ToNot(HaveOccurred()) 125 defer os.RemoveAll(dir) 126 127 err = ioutil.WriteFile(filepath.Join(dir, "Procfile"), []byte(`--- 128 web: bogus bogus`, 129 ), 0666) 130 Expect(err).ToNot(HaveOccurred()) 131 132 err = ioutil.WriteFile(filepath.Join(dir, "Gemfile"), nil, 0666) 133 Expect(err).ToNot(HaveOccurred()) 134 135 err = ioutil.WriteFile(filepath.Join(dir, "Gemfile.lock"), []byte(` 136 GEM 137 specs: 138 139 PLATFORMS 140 ruby 141 142 DEPENDENCIES 143 144 BUNDLED WITH 145 1.15.0 146 `), 0666) 147 Expect(err).ToNot(HaveOccurred()) 148 149 f(dir) 150 } 151 152 // WithBananaPantsApp creates a simple application to use with your CLI command 153 // (typically CF Push). When pushing, be aware of specifying '-b 154 // staticfile_buildpack" so that your app will correctly start up with the 155 // proper buildpack. 156 func WithBananaPantsApp(f func(dir string)) { 157 dir, err := ioutil.TempDir("", "simple-app") 158 Expect(err).ToNot(HaveOccurred()) 159 defer os.RemoveAll(dir) 160 161 tempfile := filepath.Join(dir, "index.html") 162 err = ioutil.WriteFile(tempfile, []byte("Banana Pants"), 0666) 163 Expect(err).ToNot(HaveOccurred()) 164 165 err = ioutil.WriteFile(filepath.Join(dir, "Staticfile"), nil, 0666) 166 Expect(err).ToNot(HaveOccurred()) 167 168 f(dir) 169 } 170 171 // AppGUID returns the GUID for an app in the currently targeted space. 172 func AppGUID(appName string) string { 173 session := CF("app", appName, "--guid") 174 Eventually(session).Should(Exit(0)) 175 return strings.TrimSpace(string(session.Out.Contents())) 176 } 177 178 // AppJSON returns the JSON representation of an app by name. 179 func AppJSON(appName string) string { 180 appGUID := AppGUID(appName) 181 session := CF("curl", fmt.Sprintf("/v3/apps/%s", appGUID)) 182 Eventually(session).Should(Exit(0)) 183 return strings.TrimSpace(string(session.Out.Contents())) 184 } 185 186 // WriteManifest will write out a YAML manifest file at the specified path. 187 func WriteManifest(path string, manifest map[string]interface{}) { 188 body, err := yaml.Marshal(manifest) 189 Expect(err).ToNot(HaveOccurred()) 190 err = ioutil.WriteFile(path, body, 0666) 191 Expect(err).ToNot(HaveOccurred()) 192 } 193 194 // Zipit zips the source into a .zip file in the target dir. 195 func Zipit(source, target, prefix string) error { 196 // Thanks to Svett Ralchev 197 // http://blog.ralch.com/tutorial/golang-working-with-zip/ 198 199 zipfile, err := os.Create(target) 200 if err != nil { 201 return err 202 } 203 defer zipfile.Close() 204 205 if prefix != "" { 206 _, err = io.WriteString(zipfile, prefix) 207 if err != nil { 208 return err 209 } 210 } 211 212 archive := zip.NewWriter(zipfile) 213 defer archive.Close() 214 215 err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error { 216 if err != nil { 217 return err 218 } 219 220 if path == source { 221 return nil 222 } 223 224 header, err := zip.FileInfoHeader(info) 225 if err != nil { 226 return err 227 } 228 header.Name, err = filepath.Rel(source, path) 229 if err != nil { 230 return err 231 } 232 233 header.Name = filepath.ToSlash(header.Name) 234 235 if info.IsDir() { 236 header.Name += "/" 237 header.SetMode(0755) 238 } else { 239 header.Method = zip.Deflate 240 header.SetMode(0744) 241 } 242 243 writer, err := archive.CreateHeader(header) 244 if err != nil { 245 return err 246 } 247 248 if info.IsDir() { 249 return nil 250 } 251 252 file, err := os.Open(path) 253 if err != nil { 254 return err 255 } 256 defer file.Close() 257 258 _, err = io.Copy(writer, file) 259 return err 260 }) 261 262 return err 263 } 264 265 // ConfirmStagingLogs checks session for output from NOAA client 266 // indicating that staging is working. 267 func ConfirmStagingLogs(session *Session) { 268 Eventually(session).Should(gbytes.Say(`(?i)Creating container|Successfully created container|Staging\.\.\.|Staging process started \.\.\.|Staging Complete|Exit status 0|Uploading droplet\.\.\.|Uploading complete`)) 269 }