github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/worker/gclient/client_test.go (about)

     1  package gclient_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  
     9  	"code.cloudfoundry.org/garden"
    10  	"github.com/pf-qiu/concourse/v6/atc/worker/gclient"
    11  	"github.com/pf-qiu/concourse/v6/atc/worker/gclient/connection/connectionfakes"
    12  )
    13  
    14  var _ = Describe("Client", func() {
    15  	var client gclient.Client
    16  
    17  	var fakeConnection *connectionfakes.FakeConnection
    18  
    19  	BeforeEach(func() {
    20  		fakeConnection = new(connectionfakes.FakeConnection)
    21  	})
    22  
    23  	JustBeforeEach(func() {
    24  		client = gclient.NewClient(fakeConnection)
    25  	})
    26  
    27  	Describe("Capacity", func() {
    28  		BeforeEach(func() {
    29  			fakeConnection.CapacityReturns(
    30  				garden.Capacity{
    31  					MemoryInBytes: 1111,
    32  					DiskInBytes:   2222,
    33  					MaxContainers: 42,
    34  				},
    35  				nil,
    36  			)
    37  		})
    38  
    39  		It("sends a capacity request and returns the capacity", func() {
    40  			capacity, err := client.Capacity()
    41  			Ω(err).ShouldNot(HaveOccurred())
    42  			Ω(capacity.MemoryInBytes).Should(Equal(uint64(1111)))
    43  			Ω(capacity.DiskInBytes).Should(Equal(uint64(2222)))
    44  		})
    45  
    46  		Context("when getting capacity fails", func() {
    47  			disaster := errors.New("oh no!")
    48  
    49  			BeforeEach(func() {
    50  				fakeConnection.CapacityReturns(garden.Capacity{}, disaster)
    51  			})
    52  
    53  			It("returns the error", func() {
    54  				_, err := client.Capacity()
    55  				Ω(err).Should(Equal(disaster))
    56  			})
    57  		})
    58  	})
    59  
    60  	Describe("BulkInfo", func() {
    61  		expectedBulkInfo := map[string]garden.ContainerInfoEntry{
    62  			"handle1": garden.ContainerInfoEntry{
    63  				Info: garden.ContainerInfo{
    64  					State: "container1State",
    65  				},
    66  			},
    67  			"handle2": garden.ContainerInfoEntry{
    68  				Info: garden.ContainerInfo{
    69  					State: "container1State",
    70  				},
    71  			},
    72  		}
    73  		handles := []string{"handle1", "handle2"}
    74  
    75  		It("gets info for the requested containers", func() {
    76  			fakeConnection.BulkInfoReturns(expectedBulkInfo, nil)
    77  
    78  			bulkInfo, err := client.BulkInfo(handles)
    79  			Ω(err).ShouldNot(HaveOccurred())
    80  
    81  			Ω(fakeConnection.BulkInfoCallCount()).Should(Equal(1))
    82  			Ω(fakeConnection.BulkInfoArgsForCall(0)).Should(Equal(handles))
    83  			Ω(bulkInfo).Should(Equal(expectedBulkInfo))
    84  		})
    85  
    86  		Context("when there is a error with the connection", func() {
    87  			expectedBulkInfo := map[string]garden.ContainerInfoEntry{}
    88  
    89  			BeforeEach(func() {
    90  				fakeConnection.BulkInfoReturns(expectedBulkInfo, errors.New("Oh noes!"))
    91  			})
    92  
    93  			It("returns the error", func() {
    94  				_, err := client.BulkInfo(handles)
    95  				Ω(err).Should(MatchError("Oh noes!"))
    96  			})
    97  		})
    98  	})
    99  
   100  	Describe("BulkMetrics", func() {
   101  		expectedBulkMetrics := map[string]garden.ContainerMetricsEntry{
   102  			"handle1": garden.ContainerMetricsEntry{
   103  				Metrics: garden.Metrics{
   104  					DiskStat: garden.ContainerDiskStat{
   105  						TotalInodesUsed:     1,
   106  						TotalBytesUsed:      2,
   107  						ExclusiveBytesUsed:  3,
   108  						ExclusiveInodesUsed: 4,
   109  					},
   110  				},
   111  			},
   112  			"handle2": garden.ContainerMetricsEntry{
   113  				Metrics: garden.Metrics{
   114  					DiskStat: garden.ContainerDiskStat{
   115  						TotalInodesUsed:     5,
   116  						TotalBytesUsed:      6,
   117  						ExclusiveBytesUsed:  7,
   118  						ExclusiveInodesUsed: 8,
   119  					},
   120  				},
   121  			},
   122  		}
   123  		handles := []string{"handle1", "handle2"}
   124  
   125  		It("gets info for the requested containers", func() {
   126  			fakeConnection.BulkMetricsReturns(expectedBulkMetrics, nil)
   127  
   128  			bulkInfo, err := client.BulkMetrics(handles)
   129  			Ω(err).ShouldNot(HaveOccurred())
   130  
   131  			Ω(fakeConnection.BulkMetricsCallCount()).Should(Equal(1))
   132  			Ω(fakeConnection.BulkMetricsArgsForCall(0)).Should(Equal(handles))
   133  			Ω(bulkInfo).Should(Equal(expectedBulkMetrics))
   134  		})
   135  
   136  		Context("when there is a error with the connection", func() {
   137  			expectedBulkMetrics := map[string]garden.ContainerMetricsEntry{}
   138  
   139  			BeforeEach(func() {
   140  				fakeConnection.BulkMetricsReturns(expectedBulkMetrics, errors.New("Oh noes!"))
   141  			})
   142  
   143  			It("returns the error", func() {
   144  				_, err := client.BulkMetrics(handles)
   145  				Ω(err).Should(MatchError("Oh noes!"))
   146  			})
   147  		})
   148  	})
   149  
   150  	Describe("Create", func() {
   151  		It("sends a create request and returns a container", func() {
   152  			spec := garden.ContainerSpec{
   153  				RootFSPath: "/some/roofs",
   154  			}
   155  
   156  			fakeConnection.CreateReturns("some-handle", nil)
   157  
   158  			container, err := client.Create(spec)
   159  			Ω(err).ShouldNot(HaveOccurred())
   160  			Ω(container).ShouldNot(BeNil())
   161  
   162  			actualSpec := fakeConnection.CreateArgsForCall(0)
   163  			Ω(actualSpec).Should(Equal(spec))
   164  
   165  			Ω(container.Handle()).Should(Equal("some-handle"))
   166  		})
   167  
   168  		Context("when there is a connection error", func() {
   169  			disaster := errors.New("oh no!")
   170  
   171  			BeforeEach(func() {
   172  				fakeConnection.CreateReturns("", disaster)
   173  			})
   174  
   175  			It("returns it", func() {
   176  				_, err := client.Create(garden.ContainerSpec{})
   177  				Ω(err).Should(Equal(disaster))
   178  			})
   179  		})
   180  	})
   181  
   182  	Describe("Containers", func() {
   183  		It("sends a list request and returns all containers", func() {
   184  			fakeConnection.ListReturns([]string{"handle-a", "handle-b"}, nil)
   185  
   186  			props := garden.Properties{"foo": "bar"}
   187  
   188  			containers, err := client.Containers(props)
   189  			Ω(err).ShouldNot(HaveOccurred())
   190  
   191  			actualProps := fakeConnection.ListArgsForCall(0)
   192  			Ω(actualProps).Should(Equal(props))
   193  
   194  			Ω(containers).Should(HaveLen(2))
   195  			Ω(containers[0].Handle()).Should(Equal("handle-a"))
   196  			Ω(containers[1].Handle()).Should(Equal("handle-b"))
   197  		})
   198  
   199  		Context("when there is a connection error", func() {
   200  			disaster := errors.New("oh no!")
   201  
   202  			BeforeEach(func() {
   203  				fakeConnection.ListReturns(nil, disaster)
   204  			})
   205  
   206  			It("returns it", func() {
   207  				_, err := client.Containers(nil)
   208  				Ω(err).Should(Equal(disaster))
   209  			})
   210  		})
   211  	})
   212  
   213  	Describe("Destroy", func() {
   214  		It("sends a destroy request", func() {
   215  			err := client.Destroy("some-handle")
   216  			Ω(err).ShouldNot(HaveOccurred())
   217  			actualHandle := fakeConnection.DestroyArgsForCall(0)
   218  			Ω(actualHandle).Should(Equal("some-handle"))
   219  		})
   220  
   221  		Context("when there is a connection error", func() {
   222  			disaster := errors.New("oh no!")
   223  
   224  			BeforeEach(func() {
   225  				fakeConnection.DestroyReturns(disaster)
   226  			})
   227  
   228  			It("returns it", func() {
   229  				err := client.Destroy("some-handle")
   230  				Ω(err).Should(Equal(disaster))
   231  			})
   232  		})
   233  	})
   234  
   235  	Describe("Lookup", func() {
   236  		It("sends a list request", func() {
   237  			fakeConnection.ListReturns([]string{"some-handle", "some-other-handle"}, nil)
   238  
   239  			container, err := client.Lookup("some-handle")
   240  			Ω(err).ShouldNot(HaveOccurred())
   241  
   242  			Ω(container.Handle()).Should(Equal("some-handle"))
   243  		})
   244  
   245  		Context("when the container is not found", func() {
   246  			BeforeEach(func() {
   247  				fakeConnection.ListReturns([]string{"some-other-handle"}, nil)
   248  			})
   249  
   250  			It("returns ContainerNotFoundError", func() {
   251  				_, err := client.Lookup("some-handle")
   252  				Ω(err).Should(MatchError(garden.ContainerNotFoundError{Handle: "some-handle"}))
   253  			})
   254  		})
   255  
   256  		Context("when there is a connection error", func() {
   257  			disaster := errors.New("oh no!")
   258  
   259  			BeforeEach(func() {
   260  				fakeConnection.ListReturns(nil, disaster)
   261  			})
   262  
   263  			It("returns it", func() {
   264  				_, err := client.Lookup("some-handle")
   265  				Ω(err).Should(Equal(disaster))
   266  			})
   267  		})
   268  	})
   269  })