github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/provider/common/destroy_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common_test 5 6 import ( 7 "fmt" 8 "strings" 9 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/environs" 14 "github.com/juju/juju/instance" 15 "github.com/juju/juju/provider/common" 16 "github.com/juju/juju/testing" 17 ) 18 19 type DestroySuite struct { 20 testing.BaseSuite 21 } 22 23 var _ = gc.Suite(&DestroySuite{}) 24 25 func (s *DestroySuite) TestCannotGetInstances(c *gc.C) { 26 env := &mockEnviron{ 27 allInstances: func() ([]instance.Instance, error) { 28 return nil, fmt.Errorf("nope") 29 }, 30 config: configGetter(c), 31 } 32 err := common.Destroy(env) 33 c.Assert(err, gc.ErrorMatches, "nope") 34 } 35 36 func (s *DestroySuite) TestCannotStopInstances(c *gc.C) { 37 env := &mockEnviron{ 38 allInstances: func() ([]instance.Instance, error) { 39 return []instance.Instance{ 40 &mockInstance{id: "one"}, 41 &mockInstance{id: "another"}, 42 }, nil 43 }, 44 stopInstances: func(ids []instance.Id) error { 45 c.Assert(ids, gc.HasLen, 2) 46 c.Assert(ids[0], gc.Equals, instance.Id("one")) 47 c.Assert(ids[1], gc.Equals, instance.Id("another")) 48 return fmt.Errorf("nah") 49 }, 50 config: configGetter(c), 51 } 52 err := common.Destroy(env) 53 c.Assert(err, gc.ErrorMatches, "nah") 54 } 55 56 func (s *DestroySuite) TestSuccessWhenStorageErrors(c *gc.C) { 57 // common.Destroy doesn't touch storage anymore, so 58 // failing storage should not affect success. 59 env := &mockEnviron{ 60 storage: &mockStorage{removeAllErr: fmt.Errorf("noes!")}, 61 allInstances: func() ([]instance.Instance, error) { 62 return []instance.Instance{ 63 &mockInstance{id: "one"}, 64 &mockInstance{id: "another"}, 65 }, nil 66 }, 67 stopInstances: func(ids []instance.Id) error { 68 c.Assert(ids, gc.HasLen, 2) 69 c.Assert(ids[0], gc.Equals, instance.Id("one")) 70 c.Assert(ids[1], gc.Equals, instance.Id("another")) 71 return nil 72 }, 73 config: configGetter(c), 74 } 75 err := common.Destroy(env) 76 c.Assert(err, jc.ErrorIsNil) 77 } 78 79 func (s *DestroySuite) TestSuccess(c *gc.C) { 80 stor := newStorage(s, c) 81 err := stor.Put("somewhere", strings.NewReader("stuff"), 5) 82 c.Assert(err, jc.ErrorIsNil) 83 84 env := &mockEnviron{ 85 storage: stor, 86 allInstances: func() ([]instance.Instance, error) { 87 return []instance.Instance{ 88 &mockInstance{id: "one"}, 89 }, nil 90 }, 91 stopInstances: func(ids []instance.Id) error { 92 c.Assert(ids, gc.HasLen, 1) 93 c.Assert(ids[0], gc.Equals, instance.Id("one")) 94 return nil 95 }, 96 config: configGetter(c), 97 } 98 err = common.Destroy(env) 99 c.Assert(err, jc.ErrorIsNil) 100 101 // common.Destroy doesn't touch storage anymore. 102 r, err := stor.Get("somewhere") 103 c.Assert(err, jc.ErrorIsNil) 104 r.Close() 105 } 106 107 func (s *DestroySuite) TestSuccessWhenNoInstances(c *gc.C) { 108 stor := newStorage(s, c) 109 err := stor.Put("elsewhere", strings.NewReader("stuff"), 5) 110 c.Assert(err, jc.ErrorIsNil) 111 112 env := &mockEnviron{ 113 storage: stor, 114 allInstances: func() ([]instance.Instance, error) { 115 return nil, environs.ErrNoInstances 116 }, 117 config: configGetter(c), 118 } 119 err = common.Destroy(env) 120 c.Assert(err, jc.ErrorIsNil) 121 }