github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/integration/helpers/app.go (about)

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