Exemplo n.º 1
0
func TestPeers(t *testing.T) {
	g := Goblin(t)

	g.Describe(`SortDroplets`, func() {
		region := &godo.Region{Slug: `nyc1`}
		droplet := godo.Droplet{Region: region}
		var output map[string][]string

		g.BeforeEach(func() {
			output = SortDroplets([]godo.Droplet{droplet})
		})

		g.Describe(`without a private network`, func() {
			g.Before(func() {
				droplet.Networks = &godo.Networks{
					V4: []godo.NetworkV4{
						godo.NetworkV4{IPAddress: `192.168.0.0`, Type: `public`},
					},
				}
			})

			g.It(`is not included in the output`, func() {
				_, exists := output[region.Slug]
				g.Assert(exists).Equal(false)
			})
		})

		g.Describe(`with a private network`, func() {
			g.Before(func() {
				droplet.Networks = &godo.Networks{
					V4: []godo.NetworkV4{
						godo.NetworkV4{IPAddress: `192.168.0.0`, Type: `private`},
					},
				}
			})

			g.It(`is included in the output`, func() {
				g.Assert(output[region.Slug]).Equal([]string{`192.168.0.0`})
			})
		})
	})

	g.Describe(`DropletList`, func() {
		var sds *stubDropletService
		g.BeforeEach(func() {
			sds = &stubDropletService{}
		})

		g.Describe(`with no droplets`, func() {
			g.BeforeEach(func() {
				sds.list = func(a *godo.ListOptions) ([]godo.Droplet, *godo.Response, error) {
					resp := &godo.Response{}
					resp.Links = nil
					return []godo.Droplet{}, resp, nil
				}
			})

			g.It(`returns no droplets`, func() {
				drops, err := DropletList(sds)
				g.Assert(drops).Equal([]godo.Droplet{})
				g.Assert(err).Equal(nil)
			})
		})

		g.Describe(`with a single page of droplets`, func() {
			g.BeforeEach(func() {
				sds.list = func(a *godo.ListOptions) ([]godo.Droplet, *godo.Response, error) {
					resp := &godo.Response{}
					resp.Links = nil
					return []godo.Droplet{{Name: `foobar`}}, resp, nil
				}
			})

			g.It(`returns the droplets`, func() {
				drops, err := DropletList(sds)
				g.Assert(drops).Equal([]godo.Droplet{{Name: `foobar`}})
				g.Assert(err).Equal(nil)
			})
		})

		g.Describe(`with a multiple pages of droplets`, func() {
			g.BeforeEach(func() {
				sds.list = func(a *godo.ListOptions) ([]godo.Droplet, *godo.Response, error) {
					resp := &godo.Response{}
					drops := []godo.Droplet{}
					if a.Page == 0 {
						resp.Links = &godo.Links{Pages: &godo.Pages{Next: `http://example.com/droplets?page=2`, Last: `http://example.com/droplets?page=2`}}
						drops = append(drops, godo.Droplet{Name: `firstPage`})
					} else {
						resp.Links = &godo.Links{Pages: &godo.Pages{Prev: `http://example.com/droplets?page=1`}}
						drops = append(drops, godo.Droplet{Name: `secondPage`})
					}
					return drops, resp, nil
				}
			})

			g.It(`returns the droplets`, func() {
				drops, err := DropletList(sds)
				g.Assert(drops).Equal([]godo.Droplet{{Name: `firstPage`}, {Name: `secondPage`}})
				g.Assert(err).Equal(nil)
			})
		})
	})
}