github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/util/manifestparser/parser_test.go (about)

     1  package manifestparser_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	. "code.cloudfoundry.org/cli/util/manifestparser"
     8  
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Parser", func() {
    14  	var parser *Parser
    15  
    16  	BeforeEach(func() {
    17  		parser = NewParser()
    18  	})
    19  
    20  	Describe("NewParser", func() {
    21  		It("returns a parser", func() {
    22  			Expect(parser).ToNot(BeNil())
    23  		})
    24  	})
    25  
    26  	Describe("Parse", func() {
    27  		var (
    28  			manifestPath string
    29  			manifest     map[string]interface{}
    30  
    31  			executeErr error
    32  		)
    33  
    34  		JustBeforeEach(func() {
    35  			tmpfile, err := ioutil.TempFile("", "")
    36  			Expect(err).ToNot(HaveOccurred())
    37  			manifestPath = tmpfile.Name()
    38  			Expect(tmpfile.Close()).ToNot(HaveOccurred())
    39  
    40  			WriteManifest(manifestPath, manifest)
    41  
    42  			executeErr = parser.Parse(manifestPath)
    43  		})
    44  
    45  		AfterEach(func() {
    46  			Expect(os.RemoveAll(manifestPath)).ToNot(HaveOccurred())
    47  		})
    48  
    49  		Context("when given a valid manifest file", func() {
    50  			BeforeEach(func() {
    51  				manifest = map[string]interface{}{
    52  					"applications": []map[string]string{
    53  						{
    54  							"name": "app-1",
    55  						},
    56  						{
    57  							"name": "app-2",
    58  						},
    59  					},
    60  				}
    61  			})
    62  
    63  			It("returns nil and sets the applications", func() {
    64  				Expect(executeErr).ToNot(HaveOccurred())
    65  			})
    66  		})
    67  
    68  		Context("when given an invalid manifest file", func() {
    69  			BeforeEach(func() {
    70  				manifest = map[string]interface{}{}
    71  			})
    72  
    73  			It("returns an error", func() {
    74  				Expect(executeErr).To(MatchError("must have at least one application"))
    75  			})
    76  		})
    77  	})
    78  
    79  	Describe("AppNames", func() {
    80  		Context("when given a valid manifest file", func() {
    81  			BeforeEach(func() {
    82  				parser.Applications = []Application{{Name: "app-1"}, {Name: "app-2"}}
    83  			})
    84  
    85  			It("gets the app names", func() {
    86  				appNames := parser.AppNames()
    87  				Expect(appNames).To(ConsistOf("app-1", "app-2"))
    88  			})
    89  		})
    90  	})
    91  
    92  	Describe("RawManifest", func() {
    93  		Context("when given an app name", func() {
    94  			Context("when app is successfully marshalled", func() {
    95  				var manifestPath string
    96  
    97  				BeforeEach(func() {
    98  					tmpfile, err := ioutil.TempFile("", "")
    99  					Expect(err).ToNot(HaveOccurred())
   100  					manifestPath = tmpfile.Name()
   101  					Expect(tmpfile.Close()).ToNot(HaveOccurred())
   102  
   103  					manifest := map[string]interface{}{
   104  						"applications": []map[string]string{
   105  							{
   106  								"name": "app-1",
   107  							},
   108  							{
   109  								"name": "app-2",
   110  							},
   111  						},
   112  					}
   113  					WriteManifest(manifestPath, manifest)
   114  
   115  					executeErr := parser.Parse(manifestPath)
   116  					Expect(executeErr).ToNot(HaveOccurred())
   117  				})
   118  
   119  				AfterEach(func() {
   120  					Expect(os.RemoveAll(manifestPath)).ToNot(HaveOccurred())
   121  				})
   122  
   123  				It("gets the app's manifest", func() {
   124  					rawManifest, err := parser.RawManifest("app-1")
   125  					Expect(err).ToNot(HaveOccurred())
   126  					Expect(rawManifest).To(MatchYAML(`---
   127  applications:
   128  - name: app-1
   129  
   130  - name: app-2
   131  `))
   132  
   133  					rawManifest, err = parser.RawManifest("app-2")
   134  					Expect(err).ToNot(HaveOccurred())
   135  					Expect(rawManifest).To(MatchYAML(`---
   136  applications:
   137  - name: app-1
   138  
   139  - name: app-2
   140  `))
   141  				})
   142  			})
   143  
   144  			PContext("when app marshalling errors", func() {
   145  				It("returns an error", func() {})
   146  			})
   147  		})
   148  	})
   149  })