github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/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  	gc "launchpad.net/gocheck"
    11  
    12  	"launchpad.net/juju-core/environs"
    13  	"launchpad.net/juju-core/errors"
    14  	"launchpad.net/juju-core/instance"
    15  	"launchpad.net/juju-core/provider/common"
    16  	jc "launchpad.net/juju-core/testing/checkers"
    17  	"launchpad.net/juju-core/testing/testbase"
    18  )
    19  
    20  type DestroySuite struct {
    21  	testbase.LoggingSuite
    22  }
    23  
    24  var _ = gc.Suite(&DestroySuite{})
    25  
    26  func (s *DestroySuite) TestCannotGetInstances(c *gc.C) {
    27  	env := &mockEnviron{
    28  		allInstances: func() ([]instance.Instance, error) {
    29  			return nil, fmt.Errorf("nope")
    30  		},
    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(instances []instance.Instance) error {
    45  			c.Assert(instances, gc.HasLen, 2)
    46  			c.Assert(instances[0].Id(), gc.Equals, instance.Id("one"))
    47  			c.Assert(instances[1].Id(), gc.Equals, instance.Id("another"))
    48  			return fmt.Errorf("nah")
    49  		},
    50  	}
    51  	err := common.Destroy(env)
    52  	c.Assert(err, gc.ErrorMatches, "nah")
    53  }
    54  
    55  func (s *DestroySuite) TestCannotTrashStorage(c *gc.C) {
    56  	env := &mockEnviron{
    57  		storage: &mockStorage{removeAllErr: fmt.Errorf("noes!")},
    58  		allInstances: func() ([]instance.Instance, error) {
    59  			return []instance.Instance{
    60  				&mockInstance{id: "one"},
    61  				&mockInstance{id: "another"},
    62  			}, nil
    63  		},
    64  		stopInstances: func(instances []instance.Instance) error {
    65  			c.Assert(instances, gc.HasLen, 2)
    66  			c.Assert(instances[0].Id(), gc.Equals, instance.Id("one"))
    67  			c.Assert(instances[1].Id(), gc.Equals, instance.Id("another"))
    68  			return nil
    69  		},
    70  	}
    71  	err := common.Destroy(env)
    72  	c.Assert(err, gc.ErrorMatches, "noes!")
    73  }
    74  
    75  func (s *DestroySuite) TestSuccess(c *gc.C) {
    76  	stor := newStorage(s, c)
    77  	err := stor.Put("somewhere", strings.NewReader("stuff"), 5)
    78  	c.Assert(err, gc.IsNil)
    79  
    80  	env := &mockEnviron{
    81  		storage: stor,
    82  		allInstances: func() ([]instance.Instance, error) {
    83  			return []instance.Instance{
    84  				&mockInstance{id: "one"},
    85  			}, nil
    86  		},
    87  		stopInstances: func(instances []instance.Instance) error {
    88  			c.Assert(instances, gc.HasLen, 1)
    89  			c.Assert(instances[0].Id(), gc.Equals, instance.Id("one"))
    90  			return nil
    91  		},
    92  	}
    93  	err = common.Destroy(env)
    94  	c.Assert(err, gc.IsNil)
    95  	_, err = stor.Get("somewhere")
    96  	c.Assert(err, jc.Satisfies, errors.IsNotFoundError)
    97  }
    98  
    99  func (s *DestroySuite) TestCannotTrashStorageWhenNoInstances(c *gc.C) {
   100  	env := &mockEnviron{
   101  		storage: &mockStorage{removeAllErr: fmt.Errorf("noes!")},
   102  		allInstances: func() ([]instance.Instance, error) {
   103  			return nil, environs.ErrNoInstances
   104  		},
   105  	}
   106  	err := common.Destroy(env)
   107  	c.Assert(err, gc.ErrorMatches, "noes!")
   108  }
   109  
   110  func (s *DestroySuite) TestSuccessWhenNoInstances(c *gc.C) {
   111  	stor := newStorage(s, c)
   112  	err := stor.Put("elsewhere", strings.NewReader("stuff"), 5)
   113  	c.Assert(err, gc.IsNil)
   114  
   115  	env := &mockEnviron{
   116  		storage: stor,
   117  		allInstances: func() ([]instance.Instance, error) {
   118  			return nil, environs.ErrNoInstances
   119  		},
   120  	}
   121  	err = common.Destroy(env)
   122  	c.Assert(err, gc.IsNil)
   123  	_, err = stor.Get("elsewhere")
   124  	c.Assert(err, jc.Satisfies, errors.IsNotFoundError)
   125  }