github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/shared/manifest_diff_displayer_test.go (about)

     1  package shared_test
     2  
     3  import (
     4  	. "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/shared"
     5  	"github.com/LukasHeimann/cloudfoundrycli/v8/resources"
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/ui"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gbytes"
    11  )
    12  
    13  var _ = Describe("ManifestDiffDisplayer", func() {
    14  	var (
    15  		testUI    *ui.UI
    16  		displayer *ManifestDiffDisplayer
    17  	)
    18  
    19  	BeforeEach(func() {
    20  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    21  		displayer = NewManifestDiffDisplayer(testUI)
    22  	})
    23  
    24  	Describe("DisplayDiff", func() {
    25  		var (
    26  			rawManifest []byte
    27  			diff        resources.ManifestDiff
    28  			err         error
    29  		)
    30  
    31  		BeforeEach(func() {
    32  			rawManifest = []byte("")
    33  		})
    34  
    35  		JustBeforeEach(func() {
    36  			err = displayer.DisplayDiff(rawManifest, diff)
    37  		})
    38  
    39  		It("does not return an error", func() {
    40  			Expect(err).NotTo(HaveOccurred())
    41  		})
    42  
    43  		Context("No diffs", func() {
    44  			BeforeEach(func() {
    45  				diff = resources.ManifestDiff{}
    46  				rawManifest = []byte(`---
    47  version: 1
    48  applications:
    49  - name: app1
    50    buildpacks:
    51    - ruby_buildpack
    52    - java_buildpack
    53    env:
    54      VAR1: value1
    55      VAR2: value2
    56    routes:
    57    - route: route.example.com
    58    - route: another-route.example.com
    59    services:
    60    - my-service1
    61    - my-service2
    62    - name: my-service-with-arbitrary-params
    63      parameters:
    64        key1: value1
    65        key2: value2
    66    stack: cflinuxfs3
    67    metadata:
    68      annotations:
    69        contact: "bob@example.com jane@example.com"
    70      labels:
    71        sensitive: true
    72    processes:
    73    - type: web
    74      command: start-web.sh
    75      disk_quota: 512M
    76      health-check-http-endpoint: /healthcheck
    77      health-check-type: http
    78      health-check-invocation-timeout: 10
    79      instances: 3
    80      memory: 500M
    81      timeout: 10
    82    - type: worker
    83      command: start-worker.sh
    84      disk_quota: 1G
    85      health-check-type: process
    86      instances: 2
    87      memory: 256M
    88      timeout: 15`)
    89  			})
    90  
    91  			It("outputs the manifest without + or -", func() {
    92  				Expect(testUI.Out).To(Say(`---
    93    version: 1
    94    applications:
    95    - name: app1
    96      buildpacks:
    97      - ruby_buildpack
    98      - java_buildpack
    99      env:
   100        VAR1: value1
   101        VAR2: value2
   102      routes:
   103      - route: route.example.com
   104      - route: another-route.example.com
   105      services:
   106      - my-service1
   107      - my-service2
   108      - name: my-service-with-arbitrary-params
   109        parameters:
   110          key1: value1
   111          key2: value2
   112      stack: cflinuxfs3
   113      metadata:
   114        annotations:
   115          contact: "bob@example.com jane@example.com"
   116        labels:
   117          sensitive: true
   118      processes:
   119      - type: web
   120        command: start-web.sh
   121        disk_quota: 512M
   122        health-check-http-endpoint: /healthcheck
   123        health-check-type: http
   124        health-check-invocation-timeout: 10
   125        instances: 3
   126        memory: 500M
   127        timeout: 10
   128      - type: worker
   129        command: start-worker.sh
   130        disk_quota: 1G
   131        health-check-type: process
   132        instances: 2
   133        memory: 256M
   134        timeout: 15`))
   135  			})
   136  		})
   137  
   138  		Context("Operation kinds", func() {
   139  			When("adding a string value", func() {
   140  				BeforeEach(func() {
   141  					rawManifest = []byte(`---
   142  applications:
   143  - name: dora
   144    env:
   145      a: b
   146      r: m`)
   147  					diff = resources.ManifestDiff{
   148  						Diffs: []resources.Diff{
   149  							{Op: resources.AddOperation, Path: "/applications/0/env/r", Value: "m"},
   150  						},
   151  					}
   152  				})
   153  
   154  				It("outputs a diff indicating addition for a single line", func() {
   155  					Expect(testUI.Out).To(Say(`  ---
   156    applications:
   157    - name: dora
   158      env:
   159        a: b
   160  \+     r: m`))
   161  				})
   162  			})
   163  
   164  			When("adding a map value within an array", func() {
   165  				BeforeEach(func() {
   166  					rawManifest = []byte(`---
   167  applications:
   168  - name: dora
   169    env:
   170      a: b`)
   171  					diff = resources.ManifestDiff{
   172  						Diffs: []resources.Diff{
   173  							{Op: resources.AddOperation, Path: "/applications/0", Value: map[string]interface{}{
   174  								"name": "dora",
   175  								"env": map[string]interface{}{
   176  									"a": "b",
   177  								},
   178  							}},
   179  						},
   180  					}
   181  				})
   182  
   183  				It("outputs a diff indicating addition of a map type", func() {
   184  					Expect(testUI.Out).To(Say(`  ---
   185    applications:
   186  \+ - env:
   187  \+     a: b
   188  \+   name: dora`))
   189  				})
   190  			})
   191  
   192  			When("adding a map value within a map", func() {
   193  				BeforeEach(func() {
   194  					rawManifest = []byte(`---
   195  applications:
   196  - name: dora
   197    env:
   198      a: b`)
   199  					diff = resources.ManifestDiff{
   200  						Diffs: []resources.Diff{
   201  							{Op: resources.AddOperation, Path: "/applications/0/env", Value: map[string]interface{}{
   202  								"a": "b",
   203  							}},
   204  						},
   205  					}
   206  				})
   207  
   208  				It("outputs a diff indicating addition of a map type", func() {
   209  					Expect(testUI.Out).To(Say(`  ---
   210    applications:
   211    - name: dora
   212  \+   env:
   213  \+     a: b`))
   214  				})
   215  			})
   216  
   217  			When("adding an array value", func() {
   218  				BeforeEach(func() {
   219  					rawManifest = []byte(`---
   220  applications:
   221  - name: dora
   222    env:
   223      r: m
   224    routes:
   225    - route: route1.cli.fun
   226    - route: route2.cli.fun`)
   227  					diff = resources.ManifestDiff{
   228  						Diffs: []resources.Diff{
   229  							{
   230  								Op:   resources.AddOperation,
   231  								Path: "/applications/0/routes",
   232  								Value: []map[string]interface{}{
   233  									{
   234  										"route": "route1.cli.fun",
   235  									},
   236  									{
   237  										"route": "route2.cli.fun",
   238  									},
   239  								},
   240  							},
   241  						},
   242  					}
   243  				})
   244  
   245  				When("each element of the array is a map value", func() {
   246  					It("outputs a diff indicating addition for each map type", func() {
   247  						Expect(testUI.Out).To(Say(`  ---
   248    applications:
   249    - name: dora
   250      env:
   251        r: m
   252  \+   routes:
   253  \+   - route: route1.cli.fun
   254  \+   - route: route2.cli.fun`))
   255  					})
   256  				})
   257  			})
   258  
   259  			When("remove", func() {
   260  				BeforeEach(func() {
   261  					rawManifest = []byte(`---
   262  applications:
   263  - name: dora
   264    env:
   265      r: m`)
   266  					diff = resources.ManifestDiff{
   267  						Diffs: []resources.Diff{
   268  							{Op: resources.RemoveOperation, Path: "/applications/0/env/a", Was: "b"},
   269  						},
   270  					}
   271  				})
   272  
   273  				It("outputs correctly formatted diff with key removed", func() {
   274  					Expect(testUI.Out).To(Say(`  ---
   275    applications:
   276    - name: dora
   277      env:
   278        r: m
   279  -     a: b`))
   280  				})
   281  			})
   282  
   283  			When("replace", func() {
   284  				BeforeEach(func() {
   285  					rawManifest = []byte(`---
   286  applications:
   287  - name: dora
   288    env:
   289      a: c
   290      r: m`)
   291  					diff = resources.ManifestDiff{
   292  						Diffs: []resources.Diff{
   293  							{Op: resources.ReplaceOperation, Path: "/applications/0/env/a", Was: "b", Value: "c"},
   294  						},
   295  					}
   296  				})
   297  
   298  				It("outputs correctly formatted diff", func() {
   299  					Expect(testUI.Out).To(Say(`---
   300    applications:
   301    - name: dora
   302      env:
   303  -     a: b
   304  \+     a: c
   305        r: m
   306  `))
   307  				})
   308  			})
   309  		})
   310  
   311  		Context("when the YAML cannot be parsed", func() {
   312  			BeforeEach(func() {
   313  				diff = resources.ManifestDiff{
   314  					Diffs: []resources.Diff{
   315  						{Op: resources.ReplaceOperation, Path: "/applications/0/env/a", Was: "b", Value: "c"},
   316  					},
   317  				}
   318  				rawManifest = []byte(`not-real-yaml!`)
   319  			})
   320  
   321  			It("returns an error", func() {
   322  				Expect(err).To(MatchError("Unable to process manifest diff because its format is invalid."))
   323  			})
   324  		})
   325  	})
   326  })