code.cloudfoundry.org/cli@v7.1.0+incompatible/actor/v7pushaction/resource_match_test.go (about)

     1  package v7pushaction_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/sharedaction"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	. "code.cloudfoundry.org/cli/actor/v7pushaction"
     9  	"code.cloudfoundry.org/cli/actor/v7pushaction/v7pushactionfakes"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("MatchResources", func() {
    16  	var (
    17  		actor       *Actor
    18  		fakeV7Actor *v7pushactionfakes.FakeV7Actor
    19  
    20  		resources  []sharedaction.V3Resource
    21  		executeErr error
    22  
    23  		matched   []sharedaction.V3Resource
    24  		unmatched []sharedaction.V3Resource
    25  		warnings  Warnings
    26  	)
    27  
    28  	BeforeEach(func() {
    29  		actor, fakeV7Actor, _ = getTestPushActor()
    30  	})
    31  
    32  	JustBeforeEach(func() {
    33  		matched, unmatched, warnings, executeErr = actor.MatchResources(resources)
    34  	})
    35  
    36  	When("all the resources are unmatched", func() {
    37  		BeforeEach(func() {
    38  			resources = []sharedaction.V3Resource{
    39  				{Checksum: ccv3.Checksum{Value: "file-1"}},
    40  				{Checksum: ccv3.Checksum{Value: "file-2"}},
    41  				{Checksum: ccv3.Checksum{Value: "file-3"}},
    42  			}
    43  
    44  			fakeV7Actor.ResourceMatchReturns(
    45  				[]sharedaction.V3Resource{},
    46  				v7action.Warnings{"all-unmatched-warning"},
    47  				nil,
    48  			)
    49  		})
    50  
    51  		It("returns an empty slice of matches and a complete slice of unmatches (in the order provided)", func() {
    52  			Expect(executeErr).To(BeNil())
    53  			Expect(matched).To(BeEmpty())
    54  			Expect(unmatched).To(Equal(resources))
    55  			Expect(warnings).To(Equal(Warnings{"all-unmatched-warning"}))
    56  		})
    57  	})
    58  
    59  	When("all the resources are matched", func() {
    60  		BeforeEach(func() {
    61  			resources = []sharedaction.V3Resource{
    62  				{Checksum: ccv3.Checksum{Value: "file-1"}},
    63  				{Checksum: ccv3.Checksum{Value: "file-2"}},
    64  				{Checksum: ccv3.Checksum{Value: "file-3"}},
    65  			}
    66  
    67  			fakeV7Actor.ResourceMatchReturns(
    68  				resources,
    69  				v7action.Warnings{"all-unmatched-warning"},
    70  				nil,
    71  			)
    72  		})
    73  
    74  		It("returns a complete slice of matches and an empty slice of unmatches (in the order provided)", func() {
    75  			Expect(executeErr).To(BeNil())
    76  			Expect(matched).To(Equal(resources))
    77  			Expect(unmatched).To(BeEmpty())
    78  			Expect(warnings).To(Equal(Warnings{"all-unmatched-warning"}))
    79  		})
    80  	})
    81  
    82  	When("some of the resources are matched", func() {
    83  		var expectedMatches []sharedaction.V3Resource
    84  		var expectedUnmatches []sharedaction.V3Resource
    85  
    86  		BeforeEach(func() {
    87  			resources = []sharedaction.V3Resource{
    88  				{Checksum: ccv3.Checksum{Value: "file-1"}},
    89  				{Checksum: ccv3.Checksum{Value: "file-2"}},
    90  				{Checksum: ccv3.Checksum{Value: "file-3"}},
    91  			}
    92  
    93  			expectedMatches = []sharedaction.V3Resource{
    94  				{Checksum: ccv3.Checksum{Value: "file-1"}},
    95  				{Checksum: ccv3.Checksum{Value: "file-3"}},
    96  			}
    97  
    98  			expectedUnmatches = []sharedaction.V3Resource{
    99  				{Checksum: ccv3.Checksum{Value: "file-2"}},
   100  			}
   101  
   102  			fakeV7Actor.ResourceMatchReturns(
   103  				expectedMatches,
   104  				v7action.Warnings{"all-unmatched-warning"},
   105  				nil,
   106  			)
   107  		})
   108  
   109  		It("returns a slice of matches and a slice of unmatches (in the order provided)", func() {
   110  			Expect(executeErr).To(BeNil())
   111  			Expect(matched).To(Equal(expectedMatches))
   112  			Expect(unmatched).To(Equal(expectedUnmatches))
   113  			Expect(warnings).To(Equal(Warnings{"all-unmatched-warning"}))
   114  		})
   115  	})
   116  
   117  	When("v7actor.ResourceMatch returns an error", func() {
   118  		BeforeEach(func() {
   119  			fakeV7Actor.ResourceMatchReturns(
   120  				nil,
   121  				v7action.Warnings{
   122  					"error-response-warning-1",
   123  					"error-response-warning-2",
   124  				},
   125  				errors.New("error-response-text"),
   126  			)
   127  		})
   128  
   129  		It("returns the same error", func() {
   130  			Expect(executeErr).To(MatchError("error-response-text"))
   131  			Expect(warnings).To(ConsistOf(
   132  				"error-response-warning-1",
   133  				"error-response-warning-2",
   134  			))
   135  		})
   136  	})
   137  })