github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/util/manifestparser/manifest_test.go (about)

     1  package manifestparser_test
     2  
     3  import (
     4  	. "code.cloudfoundry.org/cli/util/manifestparser"
     5  	. "github.com/onsi/ginkgo"
     6  	. "github.com/onsi/gomega"
     7  	"gopkg.in/yaml.v2"
     8  )
     9  
    10  var _ = Describe("Manifest", func() {
    11  	var manifest Manifest
    12  
    13  	BeforeEach(func() {
    14  		manifest = Manifest{}
    15  	})
    16  
    17  	Describe("AppNames", func() {
    18  		When("given a valid manifest file", func() {
    19  			BeforeEach(func() {
    20  				manifest.Applications = []Application{
    21  					{Name: "app-1"},
    22  					{Name: "app-2"},
    23  				}
    24  			})
    25  
    26  			It("gets the app names", func() {
    27  				appNames := manifest.AppNames()
    28  				Expect(appNames).To(ConsistOf("app-1", "app-2"))
    29  			})
    30  		})
    31  	})
    32  
    33  	Describe("ContainsMultipleApps", func() {
    34  		When("given a valid manifest file with multiple apps", func() {
    35  			BeforeEach(func() {
    36  				manifest.Applications = []Application{
    37  					{Name: "app-1"},
    38  					{Name: "app-2"}}
    39  			})
    40  
    41  			It("returns true", func() {
    42  				Expect(manifest.ContainsMultipleApps()).To(BeTrue())
    43  			})
    44  		})
    45  
    46  		When("given a valid manifest file with a single app", func() {
    47  			BeforeEach(func() {
    48  				manifest.Applications = []Application{{Name: "app-1"}}
    49  			})
    50  
    51  			It("returns false", func() {
    52  				Expect(manifest.ContainsMultipleApps()).To(BeFalse())
    53  			})
    54  		})
    55  	})
    56  
    57  	Describe("ContainsPrivateDockerImages", func() {
    58  		When("the manifest contains a docker image", func() {
    59  			When("the image is public", func() {
    60  				BeforeEach(func() {
    61  					manifest.Applications = []Application{
    62  						{Name: "app-1", Docker: &Docker{Image: "image-1"}},
    63  						{Name: "app-2", Docker: &Docker{Image: "image-2"}}}
    64  				})
    65  
    66  				It("returns false", func() {
    67  					Expect(manifest.ContainsPrivateDockerImages()).To(BeFalse())
    68  				})
    69  			})
    70  
    71  			When("the image is private", func() {
    72  				BeforeEach(func() {
    73  					manifest.Applications = []Application{
    74  						{Name: "app-1", Docker: &Docker{Image: "image-1"}},
    75  						{Name: "app-2", Docker: &Docker{Image: "image-2", Username: "user"}},
    76  					}
    77  				})
    78  
    79  				It("returns true", func() {
    80  					Expect(manifest.ContainsPrivateDockerImages()).To(BeTrue())
    81  				})
    82  			})
    83  		})
    84  
    85  		When("the manifest does not contain a docker image", func() {
    86  			BeforeEach(func() {
    87  				manifest.Applications = []Application{
    88  					{Name: "app-1"},
    89  					{Name: "app-2"},
    90  				}
    91  			})
    92  
    93  			It("returns false", func() {
    94  				Expect(manifest.ContainsPrivateDockerImages()).To(BeFalse())
    95  			})
    96  		})
    97  	})
    98  
    99  	Describe("HasAppWithNoName", func() {
   100  		It("returns true when there is an app with no name", func() {
   101  			manifest.Applications = []Application{
   102  				{Name: "some-app"},
   103  				{},
   104  			}
   105  
   106  			Expect(manifest.HasAppWithNoName()).To(BeTrue())
   107  		})
   108  
   109  		It("returns false when all apps have names", func() {
   110  			manifest.Applications = []Application{
   111  				{Name: "some-app"},
   112  				{Name: "some-other-app"},
   113  			}
   114  
   115  			Expect(manifest.HasAppWithNoName()).To(BeFalse())
   116  		})
   117  	})
   118  
   119  	Describe("GetFirstAppWebProcess", func() {
   120  		BeforeEach(func() {
   121  			manifest.Applications = []Application{
   122  				{
   123  					Processes: []Process{
   124  						{Type: "worker"},
   125  						{Type: "web", Memory: "1G"},
   126  					},
   127  				},
   128  				{
   129  					Processes: []Process{
   130  						{Type: "worker2"},
   131  						{Type: "web", Memory: "2G"},
   132  					},
   133  				},
   134  			}
   135  		})
   136  
   137  		It("returns the first app's web process", func() {
   138  			Expect(manifest.GetFirstAppWebProcess()).To(Equal(&Process{
   139  				Type: "web", Memory: "1G",
   140  			}))
   141  		})
   142  
   143  		It("returns nil if there is no first-app-web-process", func() {
   144  			manifest.Applications = []Application{
   145  				{
   146  					Name: "app1",
   147  				},
   148  			}
   149  
   150  			Expect(manifest.GetFirstAppWebProcess()).To(BeNil())
   151  		})
   152  	})
   153  
   154  	Describe("GetFirstApp", func() {
   155  		BeforeEach(func() {
   156  			manifest.Applications = []Application{
   157  				{
   158  					Name: "app1",
   159  				},
   160  				{
   161  					Name: "app2",
   162  				},
   163  			}
   164  		})
   165  
   166  		It("returns the first app", func() {
   167  			Expect(manifest.GetFirstApp()).To(Equal(&Application{
   168  				Name: "app1",
   169  			}))
   170  		})
   171  	})
   172  
   173  	Describe("has the correct attributes", func() {
   174  		It("it unmarshals with the version", func() {
   175  			manifestBytes := []byte(`---
   176  applications: []
   177  version: 151
   178  `)
   179  			err := yaml.Unmarshal(manifestBytes, &manifest)
   180  			Expect(err).ToNot(HaveOccurred())
   181  
   182  			Expect(manifest.Version).To(Equal(151))
   183  		})
   184  
   185  		It("it marshals with the version", func() {
   186  			manifest = Manifest{
   187  				Applications: []Application{},
   188  				Version:      151,
   189  			}
   190  			expectedManifestBytes := []byte(`applications: []
   191  version: 151
   192  `)
   193  			actualBytes, err := yaml.Marshal(manifest)
   194  
   195  			Expect(err).ToNot(HaveOccurred())
   196  			Expect(string(actualBytes)).To(Equal(string(expectedManifestBytes)))
   197  		})
   198  	})
   199  })