github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/helpers/app.go (about)

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