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

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	. "code.cloudfoundry.org/cli/actor/v2action"
    10  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("Resource Actions", func() {
    17  	var (
    18  		actor                     *Actor
    19  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    20  		srcDir                    string
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    25  		actor = NewActor(fakeCloudControllerClient, nil, nil)
    26  
    27  		var err error
    28  		srcDir, err = ioutil.TempDir("", "resource-actions-test")
    29  		Expect(err).ToNot(HaveOccurred())
    30  
    31  		subDir := filepath.Join(srcDir, "level1", "level2")
    32  		err = os.MkdirAll(subDir, 0777)
    33  		Expect(err).ToNot(HaveOccurred())
    34  
    35  		err = ioutil.WriteFile(filepath.Join(subDir, "tmpFile1"), []byte("why hello"), 0600)
    36  		Expect(err).ToNot(HaveOccurred())
    37  
    38  		err = ioutil.WriteFile(filepath.Join(srcDir, "tmpFile2"), []byte("Hello, Binky"), 0600)
    39  		Expect(err).ToNot(HaveOccurred())
    40  
    41  		err = ioutil.WriteFile(filepath.Join(srcDir, "tmpFile3"), []byte("Bananarama"), 0600)
    42  		Expect(err).ToNot(HaveOccurred())
    43  	})
    44  
    45  	AfterEach(func() {
    46  		Expect(os.RemoveAll(srcDir)).ToNot(HaveOccurred())
    47  	})
    48  
    49  	Describe("ResourceMatch", func() {
    50  		var (
    51  			allResources []Resource
    52  
    53  			matchedResources   []Resource
    54  			unmatchedResources []Resource
    55  			warnings           Warnings
    56  			executeErr         error
    57  		)
    58  
    59  		JustBeforeEach(func() {
    60  			matchedResources, unmatchedResources, warnings, executeErr = actor.ResourceMatch(allResources)
    61  		})
    62  
    63  		When("given folders", func() {
    64  			BeforeEach(func() {
    65  				allResources = []Resource{
    66  					{Filename: "folder-1", Mode: DefaultFolderPermissions},
    67  					{Filename: "folder-2", Mode: DefaultFolderPermissions},
    68  					{Filename: "folder-1/folder-3", Mode: DefaultFolderPermissions},
    69  				}
    70  			})
    71  
    72  			It("does not send folders", func() {
    73  				Expect(executeErr).ToNot(HaveOccurred())
    74  				Expect(fakeCloudControllerClient.UpdateResourceMatchCallCount()).To(Equal(0))
    75  			})
    76  
    77  			It("returns all folders [in order] in unmatchedResources", func() {
    78  				Expect(executeErr).ToNot(HaveOccurred())
    79  				Expect(unmatchedResources).To(Equal(allResources))
    80  			})
    81  		})
    82  
    83  		When("given files", func() {
    84  			BeforeEach(func() {
    85  				allResources = []Resource{
    86  					{Filename: "file-1", Mode: 0744, Size: 11, SHA1: "some-sha-1"},
    87  					{Filename: "file-2", Mode: 0744, Size: 0, SHA1: "some-sha-2"},
    88  					{Filename: "file-3", Mode: 0744, Size: 13, SHA1: "some-sha-3"},
    89  					{Filename: "file-4", Mode: 0744, Size: 14, SHA1: "some-sha-4"},
    90  					{Filename: "file-5", Mode: 0744, Size: 15, SHA1: "some-sha-5"},
    91  				}
    92  			})
    93  
    94  			It("sends non-zero sized files", func() {
    95  				Expect(executeErr).ToNot(HaveOccurred())
    96  				Expect(fakeCloudControllerClient.UpdateResourceMatchCallCount()).To(Equal(1))
    97  				Expect(fakeCloudControllerClient.UpdateResourceMatchArgsForCall(0)).To(ConsistOf(
    98  					ccv2.Resource{Filename: "file-1", Mode: 0744, Size: 11, SHA1: "some-sha-1"},
    99  					ccv2.Resource{Filename: "file-3", Mode: 0744, Size: 13, SHA1: "some-sha-3"},
   100  					ccv2.Resource{Filename: "file-4", Mode: 0744, Size: 14, SHA1: "some-sha-4"},
   101  					ccv2.Resource{Filename: "file-5", Mode: 0744, Size: 15, SHA1: "some-sha-5"},
   102  				))
   103  			})
   104  
   105  			When("none of the files are matched", func() {
   106  				It("returns all files [in order] in unmatchedResources", func() {
   107  					Expect(executeErr).ToNot(HaveOccurred())
   108  					Expect(unmatchedResources).To(Equal(allResources))
   109  				})
   110  			})
   111  
   112  			When("some files are matched", func() {
   113  				BeforeEach(func() {
   114  					fakeCloudControllerClient.UpdateResourceMatchReturns(
   115  						[]ccv2.Resource{
   116  							ccv2.Resource{Size: 14, SHA1: "some-sha-4"},
   117  							ccv2.Resource{Size: 13, SHA1: "some-sha-3"},
   118  						},
   119  						ccv2.Warnings{"warnings-1", "warnings-2"},
   120  						nil,
   121  					)
   122  				})
   123  
   124  				It("returns all the unmatched files [in order] in unmatchedResources", func() {
   125  					Expect(executeErr).ToNot(HaveOccurred())
   126  					Expect(unmatchedResources).To(ConsistOf(
   127  						Resource{Filename: "file-1", Mode: 0744, Size: 11, SHA1: "some-sha-1"},
   128  						Resource{Filename: "file-2", Mode: 0744, Size: 0, SHA1: "some-sha-2"},
   129  						Resource{Filename: "file-5", Mode: 0744, Size: 15, SHA1: "some-sha-5"},
   130  					))
   131  				})
   132  
   133  				It("returns all the matched files [in order] in matchedResources", func() {
   134  					Expect(executeErr).ToNot(HaveOccurred())
   135  					Expect(matchedResources).To(ConsistOf(
   136  						Resource{Filename: "file-3", Mode: 0744, Size: 13, SHA1: "some-sha-3"},
   137  						Resource{Filename: "file-4", Mode: 0744, Size: 14, SHA1: "some-sha-4"},
   138  					))
   139  				})
   140  
   141  				It("returns the warnings", func() {
   142  					Expect(warnings).To(ConsistOf("warnings-1", "warnings-2"))
   143  				})
   144  			})
   145  		})
   146  
   147  		When("sending a large number of files/folders", func() {
   148  			BeforeEach(func() {
   149  				fakeCloudControllerClient.UpdateResourceMatchReturnsOnCall(
   150  					0, nil, ccv2.Warnings{"warnings-1"}, nil,
   151  				)
   152  				fakeCloudControllerClient.UpdateResourceMatchReturnsOnCall(
   153  					1, nil, ccv2.Warnings{"warnings-2"}, nil,
   154  				)
   155  
   156  				allResources = []Resource{} // empties to prevent test pollution
   157  				for i := 0; i < MaxResourceMatchChunkSize+2; i++ {
   158  					allResources = append(allResources, Resource{Filename: "file", Mode: 0744, Size: 11, SHA1: "some-sha"})
   159  				}
   160  			})
   161  
   162  			It("chunks the CC API calls by MaxResourceMatchChunkSize", func() {
   163  				Expect(executeErr).ToNot(HaveOccurred())
   164  				Expect(warnings).To(ConsistOf("warnings-1", "warnings-2"))
   165  
   166  				Expect(fakeCloudControllerClient.UpdateResourceMatchCallCount()).To(Equal(2))
   167  				Expect(fakeCloudControllerClient.UpdateResourceMatchArgsForCall(0)).To(HaveLen(MaxResourceMatchChunkSize))
   168  				Expect(fakeCloudControllerClient.UpdateResourceMatchArgsForCall(1)).To(HaveLen(2))
   169  			})
   170  
   171  		})
   172  
   173  		When("the CC API returns an error", func() {
   174  			var expectedErr error
   175  
   176  			BeforeEach(func() {
   177  				expectedErr = errors.New("things are taking tooooooo long")
   178  				fakeCloudControllerClient.UpdateResourceMatchReturnsOnCall(
   179  					0, nil, ccv2.Warnings{"warnings-1"}, nil,
   180  				)
   181  				fakeCloudControllerClient.UpdateResourceMatchReturnsOnCall(
   182  					1, nil, ccv2.Warnings{"warnings-2"}, expectedErr,
   183  				)
   184  
   185  				allResources = []Resource{} // empties to prevent test pollution
   186  				for i := 0; i < MaxResourceMatchChunkSize+2; i++ {
   187  					allResources = append(allResources, Resource{Filename: "file", Mode: 0744, Size: 11, SHA1: "some-sha"})
   188  				}
   189  			})
   190  
   191  			It("returns all warnings and errors", func() {
   192  				Expect(executeErr).To(MatchError(expectedErr))
   193  				Expect(warnings).To(ConsistOf("warnings-1", "warnings-2"))
   194  			})
   195  		})
   196  	})
   197  })