github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/manifest/generate_manifest_test.go (about)

     1  package manifest_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  
     8  	. "github.com/cloudfoundry/cli/cf/manifest"
     9  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  type outputs struct {
    15  	contents []string
    16  	cursor   int
    17  }
    18  
    19  var _ = Describe("generate_manifest", func() {
    20  	var (
    21  		m   AppManifest
    22  		err error
    23  	)
    24  
    25  	BeforeEach(func() {
    26  		_, err = os.Stat("./output.yml")
    27  		Ω(err).To(HaveOccurred())
    28  
    29  		m = NewGenerator()
    30  		m.FileSavePath("./output.yml")
    31  	})
    32  
    33  	AfterEach(func() {
    34  		err = os.Remove("./output.yml")
    35  		Ω(err).ToNot(HaveOccurred())
    36  	})
    37  
    38  	It("creates a new file at a given path", func() {
    39  		m.Save()
    40  
    41  		_, err = os.Stat("./output.yml")
    42  		Ω(err).ToNot(HaveOccurred())
    43  	})
    44  
    45  	It("starts the manifest with 3 dashes (---), followed by 'applications'", func() {
    46  		m.Save()
    47  
    48  		contents := getYamlContent("./output.yml")
    49  
    50  		Ω(contents[0]).To(Equal("---"))
    51  		Ω(contents[1]).To(Equal("applications:"))
    52  	})
    53  
    54  	It("creates entry under the given app name", func() {
    55  		m.Memory("app1", 128)
    56  		m.Memory("app2", 64)
    57  		m.Save()
    58  
    59  		//outputs.ContainSubstring assert orders
    60  		cmdOutput := &outputs{
    61  			contents: getYamlContent("./output.yml"),
    62  			cursor:   0,
    63  		}
    64  
    65  		Ω(cmdOutput.ContainsSubstring("- name: app1")).To(BeTrue())
    66  		Ω(cmdOutput.ContainsSubstring("  memory: 128M")).To(BeTrue())
    67  
    68  		Ω(cmdOutput.ContainsSubstring("- name: app2")).To(BeTrue())
    69  		Ω(cmdOutput.ContainsSubstring("  memory: 64M")).To(BeTrue())
    70  	})
    71  
    72  	It("prefixes each service with '-'", func() {
    73  		m.Service("app1", "service1")
    74  		m.Service("app1", "service2")
    75  		m.Service("app1", "service3")
    76  		m.Save()
    77  
    78  		contents := getYamlContent("./output.yml")
    79  
    80  		Ω(contents).To(ContainSubstrings(
    81  			[]string{"  services:"},
    82  			[]string{"- service1"},
    83  			[]string{"- service2"},
    84  			[]string{"- service3"},
    85  		))
    86  	})
    87  
    88  	It("generates a manifest containing all the attributes", func() {
    89  		m.Memory("app1", 128)
    90  		m.StartupCommand("app1", "run main.go")
    91  		m.Service("app1", "service1")
    92  		m.EnvironmentVars("app1", "foo", "boo")
    93  		m.HealthCheckTimeout("app1", 100)
    94  		m.Instances("app1", 3)
    95  		m.Domain("app1", "foo", "blahblahblah.com")
    96  		err := m.Save()
    97  		Ω(err).NotTo(HaveOccurred())
    98  
    99  		Ω(getYamlContent("./output.yml")).To(ContainSubstrings(
   100  			[]string{"- name: app1"},
   101  			[]string{"  memory: 128M"},
   102  			[]string{"  command: run main.go"},
   103  			[]string{"  services:"},
   104  			[]string{"  - service1"},
   105  			[]string{"  env:"},
   106  			[]string{"    foo: boo"},
   107  			[]string{"  timeout: 100"},
   108  			[]string{"  instances: 3"},
   109  			[]string{"  host: foo"},
   110  			[]string{"  domain: blahblahblah.com"},
   111  		))
   112  	})
   113  
   114  })
   115  
   116  func getYamlContent(path string) []string {
   117  	b, err := ioutil.ReadFile(path)
   118  	Ω(err).ToNot(HaveOccurred())
   119  
   120  	return strings.Split(string(b), "\n")
   121  }
   122  
   123  func (o *outputs) ContainsSubstring(str string) bool {
   124  	for i := o.cursor; i < len(o.contents)-1; i++ {
   125  		if strings.Contains(o.contents[i], str) {
   126  			o.cursor = i
   127  			return true
   128  		}
   129  	}
   130  	return false
   131  }