github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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  // CreateApp creates an empty app in CloudController with no package or droplet
    21  func CreateApp(app string) {
    22  	Eventually(CF("create-app", app)).Should(Exit(0))
    23  }
    24  
    25  // QuickDeleteApp deletes the app with the given name, if provided, using
    26  // 'cf curl /v3/app... -X DELETE'.
    27  func QuickDeleteApp(appName string) {
    28  	guid := AppGUID(appName)
    29  	url := fmt.Sprintf("/v3/apps/%s", guid)
    30  	session := CF("curl", "-X", "DELETE", url)
    31  	Eventually(session).Should(Exit(0))
    32  }
    33  
    34  // WithHelloWorldApp creates a simple application to use with your CLI command
    35  // (typically CF Push). When pushing, be aware of specifying '-b
    36  // staticfile_buildpack" so that your app will correctly start up with the
    37  // proper buildpack.
    38  func WithHelloWorldApp(f func(dir string)) {
    39  	dir := TempDirAbsolutePath("", "simple-app")
    40  	defer os.RemoveAll(dir)
    41  
    42  	tempfile := filepath.Join(dir, "index.html")
    43  	err := ioutil.WriteFile(tempfile, []byte(fmt.Sprintf("hello world %d", rand.Int())), 0666)
    44  	Expect(err).ToNot(HaveOccurred())
    45  
    46  	err = ioutil.WriteFile(filepath.Join(dir, "Staticfile"), nil, 0666)
    47  	Expect(err).ToNot(HaveOccurred())
    48  
    49  	f(dir)
    50  }
    51  
    52  // WithMultiEndpointApp creates a simple application to use with your CLI command
    53  // (typically CF Push). It has multiple endpoints which are helpful when testing
    54  // http healthchecks.
    55  func WithMultiEndpointApp(f func(dir string)) {
    56  	dir := TempDirAbsolutePath("", "simple-app")
    57  	defer os.RemoveAll(dir)
    58  
    59  	tempfile := filepath.Join(dir, "index.html")
    60  	err := ioutil.WriteFile(tempfile, []byte(fmt.Sprintf("hello world %d", rand.Int())), 0666)
    61  	Expect(err).ToNot(HaveOccurred())
    62  
    63  	tempfile = filepath.Join(dir, "other_endpoint.html")
    64  	err = ioutil.WriteFile(tempfile, []byte("other endpoint"), 0666)
    65  	Expect(err).ToNot(HaveOccurred())
    66  
    67  	tempfile = filepath.Join(dir, "third_endpoint.html")
    68  	err = ioutil.WriteFile(tempfile, []byte("third endpoint"), 0666)
    69  	Expect(err).ToNot(HaveOccurred())
    70  
    71  	err = ioutil.WriteFile(filepath.Join(dir, "Staticfile"), nil, 0666)
    72  	Expect(err).ToNot(HaveOccurred())
    73  
    74  	f(dir)
    75  }
    76  
    77  func WithSidecarApp(f func(dir string), appName string) {
    78  	withSidecarManifest := func(dir string) {
    79  		err := ioutil.WriteFile(filepath.Join(dir, "manifest.yml"), []byte(fmt.Sprintf(`---
    80  applications:
    81    - name: %s
    82      sidecars:
    83      - name: sidecar_name
    84        process_types: [web]
    85        command: sleep infinity`,
    86  			appName)), 0666)
    87  		Expect(err).ToNot(HaveOccurred())
    88  
    89  		f(dir)
    90  	}
    91  
    92  	WithHelloWorldApp(withSidecarManifest)
    93  }
    94  
    95  func WithTaskApp(f func(dir string), appName string) {
    96  	withTaskManifest := func(dir string) {
    97  		err := ioutil.WriteFile(filepath.Join(dir, "manifest.yml"), []byte(fmt.Sprintf(`---
    98  applications:
    99  - name: %s
   100    processes:
   101    - type: task
   102      command: echo hi`,
   103  			appName)), 0666)
   104  		Expect(err).ToNot(HaveOccurred())
   105  
   106  		f(dir)
   107  	}
   108  
   109  	WithHelloWorldApp(withTaskManifest)
   110  }
   111  
   112  // WithNoResourceMatchedApp creates a simple application to use with your CLI
   113  // command (typically CF Push). When pushing, be aware of specifying '-b
   114  // staticfile_buildpack" so that your app will correctly start up with the
   115  // proper buildpack.
   116  func WithNoResourceMatchedApp(f func(dir string)) {
   117  	dir := TempDirAbsolutePath("", "simple-app")
   118  	defer os.RemoveAll(dir)
   119  
   120  	tempfile := filepath.Join(dir, "index.html")
   121  
   122  	err := ioutil.WriteFile(tempfile, []byte(fmt.Sprintf("hello world %s", strings.Repeat("a", 65*1024))), 0666)
   123  	Expect(err).ToNot(HaveOccurred())
   124  
   125  	f(dir)
   126  }
   127  
   128  // WithMultiBuildpackApp creates a multi-buildpack application to use with the CF push command.
   129  func WithMultiBuildpackApp(f func(dir string)) {
   130  	f("../../assets/go_calls_ruby")
   131  }
   132  
   133  // WithProcfileApp creates an application to use with your CLI command
   134  // that contains Procfile defining web and worker processes.
   135  func WithProcfileApp(f func(dir string)) {
   136  	dir := TempDirAbsolutePath("", "simple-ruby-app")
   137  	defer os.RemoveAll(dir)
   138  
   139  	err := ioutil.WriteFile(filepath.Join(dir, "Procfile"), []byte(`---
   140  web: ruby -run -e httpd . -p $PORT
   141  console: bundle exec irb`,
   142  	), 0666)
   143  	Expect(err).ToNot(HaveOccurred())
   144  
   145  	err = ioutil.WriteFile(filepath.Join(dir, "Gemfile"), nil, 0666)
   146  	Expect(err).ToNot(HaveOccurred())
   147  
   148  	err = ioutil.WriteFile(filepath.Join(dir, "Gemfile.lock"), []byte(`
   149  GEM
   150    specs:
   151  
   152  PLATFORMS
   153    ruby
   154  
   155  DEPENDENCIES
   156  
   157  BUNDLED WITH
   158     1.15.0
   159  	`), 0666)
   160  	Expect(err).ToNot(HaveOccurred())
   161  
   162  	f(dir)
   163  }
   164  
   165  // WithCrashingApp creates an application to use with your CLI command
   166  // that will not successfully start its `web` process
   167  func WithCrashingApp(f func(dir string)) {
   168  	dir := TempDirAbsolutePath("", "crashing-ruby-app")
   169  	defer os.RemoveAll(dir)
   170  
   171  	err := ioutil.WriteFile(filepath.Join(dir, "Procfile"), []byte(`---
   172  web: bogus bogus`,
   173  	), 0666)
   174  	Expect(err).ToNot(HaveOccurred())
   175  
   176  	err = ioutil.WriteFile(filepath.Join(dir, "Gemfile"), nil, 0666)
   177  	Expect(err).ToNot(HaveOccurred())
   178  
   179  	err = ioutil.WriteFile(filepath.Join(dir, "Gemfile.lock"), []byte(`
   180  GEM
   181    specs:
   182  
   183  PLATFORMS
   184    ruby
   185  
   186  DEPENDENCIES
   187  
   188  BUNDLED WITH
   189     1.15.0
   190  	`), 0666)
   191  	Expect(err).ToNot(HaveOccurred())
   192  
   193  	f(dir)
   194  }
   195  
   196  // WithBananaPantsApp creates a simple application to use with your CLI command
   197  // (typically CF Push). When pushing, be aware of specifying '-b
   198  // staticfile_buildpack" so that your app will correctly start up with the
   199  // proper buildpack.
   200  func WithBananaPantsApp(f func(dir string)) {
   201  	dir := TempDirAbsolutePath("", "simple-app")
   202  	defer os.RemoveAll(dir)
   203  
   204  	tempfile := filepath.Join(dir, "index.html")
   205  	err := ioutil.WriteFile(tempfile, []byte("Banana Pants"), 0666)
   206  	Expect(err).ToNot(HaveOccurred())
   207  
   208  	err = ioutil.WriteFile(filepath.Join(dir, "Staticfile"), nil, 0666)
   209  	Expect(err).ToNot(HaveOccurred())
   210  
   211  	f(dir)
   212  }
   213  
   214  func WithEmptyFilesApp(f func(dir string)) {
   215  	dir := TempDirAbsolutePath("", "simple-app")
   216  	defer os.RemoveAll(dir)
   217  
   218  	tempfile := filepath.Join(dir, "index.html")
   219  	err := ioutil.WriteFile(tempfile, nil, 0666)
   220  	Expect(err).ToNot(HaveOccurred())
   221  
   222  	err = ioutil.WriteFile(filepath.Join(dir, "Staticfile"), nil, 0666)
   223  	Expect(err).ToNot(HaveOccurred())
   224  
   225  	f(dir)
   226  }
   227  
   228  // AppGUID returns the GUID for an app in the currently targeted space.
   229  func AppGUID(appName string) string {
   230  	session := CF("app", appName, "--guid")
   231  	Eventually(session).Should(Exit(0))
   232  	return strings.TrimSpace(string(session.Out.Contents()))
   233  }
   234  
   235  // AppJSON returns the JSON representation of an app by name.
   236  func AppJSON(appName string) string {
   237  	appGUID := AppGUID(appName)
   238  	session := CF("curl", fmt.Sprintf("/v3/apps/%s", appGUID))
   239  	Eventually(session).Should(Exit(0))
   240  	return strings.TrimSpace(string(session.Out.Contents()))
   241  }
   242  
   243  // WriteManifest will write out a YAML manifest file at the specified path.
   244  func WriteManifest(path string, manifest map[string]interface{}) {
   245  	body, err := yaml.Marshal(manifest)
   246  	Expect(err).ToNot(HaveOccurred())
   247  	err = ioutil.WriteFile(path, body, 0666)
   248  	Expect(err).ToNot(HaveOccurred())
   249  }
   250  
   251  // Zipit zips the source into a .zip file in the target dir.
   252  func Zipit(source, target, prefix string) error {
   253  	// Thanks to Svett Ralchev
   254  	// http://blog.ralch.com/tutorial/golang-working-with-zip/
   255  
   256  	zipfile, err := os.Create(target)
   257  	if err != nil {
   258  		return err
   259  	}
   260  	defer zipfile.Close()
   261  
   262  	if prefix != "" {
   263  		_, err = io.WriteString(zipfile, prefix)
   264  		if err != nil {
   265  			return err
   266  		}
   267  	}
   268  
   269  	archive := zip.NewWriter(zipfile)
   270  	defer archive.Close()
   271  
   272  	err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
   273  		if err != nil {
   274  			return err
   275  		}
   276  
   277  		if path == source {
   278  			return nil
   279  		}
   280  
   281  		header, err := zip.FileInfoHeader(info)
   282  		if err != nil {
   283  			return err
   284  		}
   285  		header.Name, err = filepath.Rel(source, path)
   286  		if err != nil {
   287  			return err
   288  		}
   289  
   290  		header.Name = filepath.ToSlash(header.Name)
   291  
   292  		if info.IsDir() {
   293  			header.Name += "/"
   294  			header.SetMode(0755)
   295  		} else {
   296  			header.Method = zip.Deflate
   297  			header.SetMode(0744)
   298  		}
   299  
   300  		writer, err := archive.CreateHeader(header)
   301  		if err != nil {
   302  			return err
   303  		}
   304  
   305  		if info.IsDir() {
   306  			return nil
   307  		}
   308  
   309  		file, err := os.Open(path)
   310  		if err != nil {
   311  			return err
   312  		}
   313  		defer file.Close()
   314  
   315  		_, err = io.Copy(writer, file)
   316  		return err
   317  	})
   318  
   319  	return err
   320  }
   321  
   322  // ConfirmStagingLogs checks session for output from NOAA client
   323  // indicating that staging is working.
   324  func ConfirmStagingLogs(session *Session) {
   325  	Eventually(session).Should(gbytes.Say(`(?i)Creating container|Successfully created container|Staging\.\.\.|Staging process started \.\.\.|Staging Complete|Exit status 0|Uploading droplet\.\.\.|Uploading complete`))
   326  }