1package compute_test
2
3import (
4	"errors"
5
6	"github.com/genevieve/leftovers/gcp/compute"
7	"github.com/genevieve/leftovers/gcp/compute/fakes"
8	. "github.com/onsi/ginkgo"
9	. "github.com/onsi/gomega"
10	gcpcompute "google.golang.org/api/compute/v1"
11)
12
13var _ = Describe("UrlMaps", func() {
14	var (
15		client *fakes.UrlMapsClient
16		logger *fakes.Logger
17
18		urlMaps compute.UrlMaps
19	)
20
21	BeforeEach(func() {
22		client = &fakes.UrlMapsClient{}
23		logger = &fakes.Logger{}
24
25		urlMaps = compute.NewUrlMaps(client, logger)
26	})
27
28	Describe("List", func() {
29		var filter string
30
31		BeforeEach(func() {
32			logger.PromptWithDetailsCall.Returns.Proceed = true
33			client.ListUrlMapsCall.Returns.Output = &gcpcompute.UrlMapList{
34				Items: []*gcpcompute.UrlMap{{
35					Name: "banana-url-map",
36				}},
37			}
38			filter = "banana"
39		})
40
41		It("lists, filters, and prompts for url maps to delete", func() {
42			list, err := urlMaps.List(filter)
43			Expect(err).NotTo(HaveOccurred())
44
45			Expect(client.ListUrlMapsCall.CallCount).To(Equal(1))
46
47			Expect(logger.PromptWithDetailsCall.CallCount).To(Equal(1))
48			Expect(logger.PromptWithDetailsCall.Receives.Type).To(Equal("Url Map"))
49			Expect(logger.PromptWithDetailsCall.Receives.Name).To(Equal("banana-url-map"))
50
51			Expect(list).To(HaveLen(1))
52		})
53
54		Context("when the client fails to list url maps", func() {
55			BeforeEach(func() {
56				client.ListUrlMapsCall.Returns.Error = errors.New("some error")
57			})
58
59			It("returns the error", func() {
60				_, err := urlMaps.List(filter)
61				Expect(err).To(MatchError("List Url Maps: some error"))
62			})
63		})
64
65		Context("when the url map name does not contain the filter", func() {
66			It("does not add it to the list", func() {
67				list, err := urlMaps.List("grape")
68				Expect(err).NotTo(HaveOccurred())
69
70				Expect(logger.PromptWithDetailsCall.CallCount).To(Equal(0))
71				Expect(list).To(HaveLen(0))
72			})
73		})
74
75		Context("when the user says no to the prompt", func() {
76			BeforeEach(func() {
77				logger.PromptWithDetailsCall.Returns.Proceed = false
78			})
79
80			It("does not add it to the list", func() {
81				list, err := urlMaps.List(filter)
82				Expect(err).NotTo(HaveOccurred())
83
84				Expect(list).To(HaveLen(0))
85			})
86		})
87	})
88})
89