github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+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" 14 . "github.com/onsi/gomega/gbytes" 15 . "github.com/onsi/gomega/gexec" 16 yaml "gopkg.in/yaml.v2" 17 ) 18 19 func WithManifest(manifest map[string]interface{}, f func(manifestDir string)) { 20 dir, err := ioutil.TempDir("", "simple-app") 21 Expect(err).ToNot(HaveOccurred()) 22 defer os.RemoveAll(dir) 23 24 WriteManifest(filepath.Join(dir, "manifest.yml"), manifest) 25 f(dir) 26 } 27 28 // WithHelloWorldApp creates a simple application to use with your CLI command 29 // (typically CF Push). When pushing, be aware of specifying '-b 30 // staticfile_buildpack" so that your app will correctly start up with the 31 // proper buildpack. 32 func WithHelloWorldApp(f func(dir string)) { 33 dir, err := ioutil.TempDir("", "simple-app") 34 Expect(err).ToNot(HaveOccurred()) 35 defer os.RemoveAll(dir) 36 37 tempfile := filepath.Join(dir, "index.html") 38 err = ioutil.WriteFile(tempfile, []byte(fmt.Sprintf("hello world %d", rand.Int())), 0666) 39 Expect(err).ToNot(HaveOccurred()) 40 41 err = ioutil.WriteFile(filepath.Join(dir, "Staticfile"), nil, 0666) 42 Expect(err).ToNot(HaveOccurred()) 43 44 f(dir) 45 } 46 47 // WithNoResourceMatchedApp creates a simple application to use with your CLI 48 // command (typically CF Push). When pushing, be aware of specifying '-b 49 // staticfile_buildpack" so that your app will correctly start up with the 50 // proper buildpack. 51 func WithNoResourceMatchedApp(f func(dir string)) { 52 dir, err := ioutil.TempDir("", "simple-app") 53 Expect(err).ToNot(HaveOccurred()) 54 defer os.RemoveAll(dir) 55 56 tempfile := filepath.Join(dir, "index.html") 57 58 err = ioutil.WriteFile(tempfile, []byte(fmt.Sprintf("hello world %s", strings.Repeat("a", 65*1024))), 0666) 59 Expect(err).ToNot(HaveOccurred()) 60 61 f(dir) 62 } 63 64 func WithMultiBuildpackApp(f func(dir string)) { 65 f("../assets/go_calls_ruby") 66 } 67 68 // WithProcfileApp creates an application to use with your CLI command 69 // that contains Procfile defining web and worker processes. 70 func WithProcfileApp(f func(dir string)) { 71 dir, err := ioutil.TempDir("", "simple-ruby-app") 72 Expect(err).ToNot(HaveOccurred()) 73 defer os.RemoveAll(dir) 74 75 err = ioutil.WriteFile(filepath.Join(dir, "Procfile"), []byte(`--- 76 web: ruby -run -e httpd . -p $PORT 77 console: bundle exec irb`, 78 ), 0666) 79 Expect(err).ToNot(HaveOccurred()) 80 81 err = ioutil.WriteFile(filepath.Join(dir, "Gemfile"), nil, 0666) 82 Expect(err).ToNot(HaveOccurred()) 83 84 err = ioutil.WriteFile(filepath.Join(dir, "Gemfile.lock"), []byte(` 85 GEM 86 specs: 87 88 PLATFORMS 89 ruby 90 91 DEPENDENCIES 92 93 BUNDLED WITH 94 1.15.0 95 `), 0666) 96 Expect(err).ToNot(HaveOccurred()) 97 98 f(dir) 99 } 100 101 func WithCrashingApp(f func(dir string)) { 102 dir, err := ioutil.TempDir("", "crashing-ruby-app") 103 Expect(err).ToNot(HaveOccurred()) 104 defer os.RemoveAll(dir) 105 106 err = ioutil.WriteFile(filepath.Join(dir, "Procfile"), []byte(`--- 107 web: bogus bogus`, 108 ), 0666) 109 Expect(err).ToNot(HaveOccurred()) 110 111 err = ioutil.WriteFile(filepath.Join(dir, "Gemfile"), nil, 0666) 112 Expect(err).ToNot(HaveOccurred()) 113 114 err = ioutil.WriteFile(filepath.Join(dir, "Gemfile.lock"), []byte(` 115 GEM 116 specs: 117 118 PLATFORMS 119 ruby 120 121 DEPENDENCIES 122 123 BUNDLED WITH 124 1.15.0 125 `), 0666) 126 Expect(err).ToNot(HaveOccurred()) 127 128 f(dir) 129 } 130 131 // WithBananaPantsApp creates a simple application to use with your CLI command 132 // (typically CF Push). When pushing, be aware of specifying '-b 133 // staticfile_buildpack" so that your app will correctly start up with the 134 // proper buildpack. 135 func WithBananaPantsApp(f func(dir string)) { 136 dir, err := ioutil.TempDir("", "simple-app") 137 Expect(err).ToNot(HaveOccurred()) 138 defer os.RemoveAll(dir) 139 140 tempfile := filepath.Join(dir, "index.html") 141 err = ioutil.WriteFile(tempfile, []byte("Banana Pants"), 0666) 142 Expect(err).ToNot(HaveOccurred()) 143 144 err = ioutil.WriteFile(filepath.Join(dir, "Staticfile"), nil, 0666) 145 Expect(err).ToNot(HaveOccurred()) 146 147 f(dir) 148 } 149 150 // AppGUID returns the GUID for an app in the currently targeted space. 151 func AppGUID(appName string) string { 152 session := CF("app", appName, "--guid") 153 Eventually(session).Should(Exit(0)) 154 return strings.TrimSpace(string(session.Out.Contents())) 155 } 156 157 func WriteManifest(path string, manifest map[string]interface{}) { 158 body, err := yaml.Marshal(manifest) 159 Expect(err).ToNot(HaveOccurred()) 160 err = ioutil.WriteFile(path, body, 0666) 161 Expect(err).ToNot(HaveOccurred()) 162 } 163 164 func ConfirmStagingLogs(session *Session) { 165 Eventually(session).Should(Say("(?i)Creating container|Successfully created container|Staging\\.\\.\\.|Staging process started \\.\\.\\.|Staging Complete")) 166 } 167 168 // Zipit zips the source into a .zip file in the target dir 169 func Zipit(source, target, prefix string) error { 170 // Thanks to Svett Ralchev 171 // http://blog.ralch.com/tutorial/golang-working-with-zip/ 172 173 zipfile, err := os.Create(target) 174 if err != nil { 175 return err 176 } 177 defer zipfile.Close() 178 179 if prefix != "" { 180 _, err = io.WriteString(zipfile, prefix) 181 if err != nil { 182 return err 183 } 184 } 185 186 archive := zip.NewWriter(zipfile) 187 defer archive.Close() 188 189 err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error { 190 if err != nil { 191 return err 192 } 193 194 if path == source { 195 return nil 196 } 197 198 header, err := zip.FileInfoHeader(info) 199 if err != nil { 200 return err 201 } 202 header.Name, err = filepath.Rel(source, path) 203 if err != nil { 204 return err 205 } 206 207 header.Name = filepath.ToSlash(header.Name) 208 209 if info.IsDir() { 210 header.Name += "/" 211 header.SetMode(0755) 212 } else { 213 header.Method = zip.Deflate 214 header.SetMode(0744) 215 } 216 217 writer, err := archive.CreateHeader(header) 218 if err != nil { 219 return err 220 } 221 222 if info.IsDir() { 223 return nil 224 } 225 226 file, err := os.Open(path) 227 if err != nil { 228 return err 229 } 230 defer file.Close() 231 232 _, err = io.Copy(writer, file) 233 return err 234 }) 235 236 return err 237 }