github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/stacks/stacks_test.go (about)

     1  package stacks_test
     2  
     3  import (
     4  	"net/http"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     8  	"code.cloudfoundry.org/cli/cf/errors"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/net"
    11  
    12  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    13  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    14  
    15  	"github.com/onsi/gomega/ghttp"
    16  
    17  	. "code.cloudfoundry.org/cli/cf/api/stacks"
    18  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("StacksRepo", func() {
    24  	var (
    25  		testServer *ghttp.Server
    26  		configRepo coreconfig.ReadWriter
    27  		repo       StackRepository
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		configRepo = testconfig.NewRepositoryWithDefaults()
    32  		configRepo.SetAccessToken("BEARER my_access_token")
    33  
    34  		gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
    35  		repo = NewCloudControllerStackRepository(configRepo, gateway)
    36  	})
    37  
    38  	BeforeEach(func() {
    39  		testServer = ghttp.NewServer()
    40  		configRepo.SetAPIEndpoint(testServer.URL())
    41  	})
    42  
    43  	AfterEach(func() {
    44  		if testServer != nil {
    45  			testServer.Close()
    46  		}
    47  	})
    48  
    49  	Describe("FindByName", func() {
    50  		Context("when a stack exists", func() {
    51  			BeforeEach(func() {
    52  				testServer.AppendHandlers(
    53  					ghttp.CombineHandlers(
    54  						ghttp.VerifyRequest("GET", "/v2/stacks", "q=name%3Alinux"),
    55  						ghttp.RespondWith(http.StatusOK, `{
    56  							"resources": [
    57  								{
    58  									"metadata": { "guid": "custom-linux-guid" },
    59  									"entity": { "name": "custom-linux" }
    60  								}
    61  							]
    62  						}`),
    63  					),
    64  				)
    65  			})
    66  
    67  			It("tries to find the stack", func() {
    68  				repo.FindByName("linux")
    69  				Expect(testServer.ReceivedRequests()).To(HaveLen(1))
    70  			})
    71  
    72  			It("returns the stack", func() {
    73  				stack, err := repo.FindByName("linux")
    74  				Expect(err).NotTo(HaveOccurred())
    75  				Expect(stack).To(Equal(models.Stack{
    76  					Name: "custom-linux",
    77  					GUID: "custom-linux-guid",
    78  				}))
    79  			})
    80  		})
    81  
    82  		Context("when the stack cannot be found", func() {
    83  			BeforeEach(func() {
    84  				testServer.AppendHandlers(
    85  					ghttp.CombineHandlers(
    86  						ghttp.VerifyRequest("GET", "/v2/stacks", "q=name%3Alinux"),
    87  						ghttp.RespondWith(http.StatusOK, `{
    88  							"resources": []
    89  						}`),
    90  					),
    91  				)
    92  			})
    93  
    94  			It("tries to find the stack", func() {
    95  				repo.FindByName("linux")
    96  				Expect(testServer.ReceivedRequests()).To(HaveLen(1))
    97  			})
    98  
    99  			It("returns an error", func() {
   100  				_, err := repo.FindByName("linux")
   101  				Expect(err).To(BeAssignableToTypeOf(&errors.ModelNotFoundError{}))
   102  			})
   103  		})
   104  	})
   105  
   106  	Describe("FindAll", func() {
   107  		BeforeEach(func() {
   108  			testServer.AppendHandlers(
   109  				ghttp.CombineHandlers(
   110  					ghttp.VerifyRequest("GET", "/v2/stacks"),
   111  					ghttp.RespondWith(http.StatusOK, `{
   112  							"next_url": "/v2/stacks?page=2",
   113  							"resources": [
   114  								{
   115  									"metadata": {
   116  										"guid": "stack-guid-1",
   117  										"url": "/v2/stacks/stack-guid-1",
   118  										"created_at": "2013-08-31 01:32:40 +0000",
   119  										"updated_at": "2013-08-31 01:32:40 +0000"
   120  									},
   121  									"entity": {
   122  										"name": "lucid64",
   123  										"description": "Ubuntu 10.04"
   124  									}
   125  								}
   126  							]
   127  						}`),
   128  				),
   129  				ghttp.CombineHandlers(
   130  					ghttp.VerifyRequest("GET", "/v2/stacks"),
   131  					ghttp.RespondWith(http.StatusOK, `{
   132  							"resources": [
   133  								{
   134  									"metadata": {
   135  										"guid": "stack-guid-2",
   136  										"url": "/v2/stacks/stack-guid-2",
   137  										"created_at": "2013-08-31 01:32:40 +0000",
   138  										"updated_at": "2013-08-31 01:32:40 +0000"
   139  									},
   140  									"entity": {
   141  										"name": "lucid64custom",
   142  										"description": "Fake Ubuntu 10.04"
   143  									}
   144  								}
   145  							]
   146  						}`),
   147  				),
   148  			)
   149  		})
   150  
   151  		It("tries to find all stacks", func() {
   152  			repo.FindAll()
   153  			Expect(testServer.ReceivedRequests()).To(HaveLen(2))
   154  		})
   155  
   156  		It("returns the stacks it found", func() {
   157  			stacks, err := repo.FindAll()
   158  			Expect(err).NotTo(HaveOccurred())
   159  			Expect(stacks).To(ConsistOf([]models.Stack{
   160  				{
   161  					GUID:        "stack-guid-1",
   162  					Name:        "lucid64",
   163  					Description: "Ubuntu 10.04",
   164  				},
   165  				{
   166  					GUID:        "stack-guid-2",
   167  					Name:        "lucid64custom",
   168  					Description: "Fake Ubuntu 10.04",
   169  				},
   170  			}))
   171  		})
   172  	})
   173  
   174  	Describe("FindByGUID", func() {
   175  		Context("when a stack with that GUID can be found", func() {
   176  			BeforeEach(func() {
   177  				testServer.AppendHandlers(
   178  					ghttp.CombineHandlers(
   179  						ghttp.VerifyRequest("GET", "/v2/stacks/the-stack-guid"),
   180  						ghttp.RespondWith(http.StatusOK, `{
   181  						  "metadata": {
   182  						    "guid": "the-stack-guid",
   183  						    "url": "/v2/stacks/the-stack-guid",
   184  						    "created_at": "2016-01-26T22:20:04Z",
   185  						    "updated_at": null
   186  						  },
   187  						  "entity": {
   188  						    "name": "the-stack-name",
   189  						    "description": "the-stack-description"
   190  						  }
   191  						}`),
   192  					),
   193  				)
   194  			})
   195  
   196  			It("tries to find the stack", func() {
   197  				repo.FindByGUID("the-stack-guid")
   198  				Expect(testServer.ReceivedRequests()).To(HaveLen(1))
   199  			})
   200  
   201  			It("returns a stack", func() {
   202  				stack, err := repo.FindByGUID("the-stack-guid")
   203  				Expect(err).NotTo(HaveOccurred())
   204  				Expect(stack).To(Equal(models.Stack{
   205  					GUID:        "the-stack-guid",
   206  					Name:        "the-stack-name",
   207  					Description: "the-stack-description",
   208  				}))
   209  			})
   210  		})
   211  
   212  		Context("when a stack with that GUID cannot be found", func() {
   213  			BeforeEach(func() {
   214  				testServer.AppendHandlers(
   215  					ghttp.CombineHandlers(
   216  						ghttp.VerifyRequest("GET", "/v2/stacks/the-stack-guid"),
   217  						ghttp.RespondWith(http.StatusNotFound, `{
   218  							"code": 250003,
   219  							"description": "The stack could not be found: ea541500-a1bd-4ac2-8ab1-f38ed3a483ad",
   220  							"error_code": "CF-StackNotFound"
   221  						}`),
   222  					),
   223  				)
   224  			})
   225  
   226  			It("returns an error", func() {
   227  				_, err := repo.FindByGUID("the-stack-guid")
   228  				Expect(err).To(HaveOccurred())
   229  				Expect(err).To(BeAssignableToTypeOf(&errors.HTTPNotFoundError{}))
   230  			})
   231  		})
   232  	})
   233  
   234  	Context("when finding the stack results in an error", func() {
   235  		BeforeEach(func() {
   236  			testServer.Close()
   237  			testServer = nil
   238  		})
   239  
   240  		It("returns an error", func() {
   241  			_, err := repo.FindByGUID("the-stack-guid")
   242  			Expect(err).To(HaveOccurred())
   243  			Expect(err.Error()).To(ContainSubstring("Error retrieving stacks: "))
   244  		})
   245  	})
   246  })