github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/maas/maas2storage_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package maas 5 6 import ( 7 "bytes" 8 "io/ioutil" 9 10 "github.com/juju/errors" 11 "github.com/juju/gomaasapi" 12 "github.com/juju/testing" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 ) 16 17 type maas2StorageSuite struct { 18 maas2Suite 19 } 20 21 var _ = gc.Suite(&maas2StorageSuite{}) 22 23 func makeCall(funcName string, args ...interface{}) testing.StubCall { 24 return testing.StubCall{funcName, args} 25 } 26 27 func checkCalls(c *gc.C, stub *testing.Stub, calls ...testing.StubCall) { 28 stub.CheckCalls(c, calls) 29 } 30 31 func (s *maas2StorageSuite) makeStorage(c *gc.C, controller gomaasapi.Controller) *maas2Storage { 32 env := s.makeEnviron(c, controller) 33 env.uuid = "prefix" 34 storage, ok := NewStorage(env).(*maas2Storage) 35 c.Assert(ok, jc.IsTrue) 36 return storage 37 } 38 39 func (s *maas2StorageSuite) TestGetNoSuchFile(c *gc.C) { 40 storage := s.makeStorage(c, newFakeControllerWithErrors( 41 errors.New("This file no existence"), 42 )) 43 _, err := storage.Get("grasshopper.avi") 44 c.Assert(err, gc.ErrorMatches, "This file no existence") 45 } 46 47 func (s *maas2StorageSuite) TestGetReadFails(c *gc.C) { 48 storage := s.makeStorage(c, newFakeControllerWithFiles( 49 &fakeFile{name: "prefix-grasshopper.avi", error: errors.New("read error")}, 50 )) 51 _, err := storage.Get("grasshopper.avi") 52 c.Assert(err, gc.ErrorMatches, "read error") 53 } 54 55 func (s *maas2StorageSuite) TestGetNotFound(c *gc.C) { 56 storage := s.makeStorage(c, newFakeControllerWithErrors( 57 gomaasapi.NewNoMatchError("wee"), 58 )) 59 _, err := storage.Get("grasshopper.avi") 60 c.Assert(err, jc.Satisfies, errors.IsNotFound) 61 } 62 63 func (s *maas2StorageSuite) TestGetSuccess(c *gc.C) { 64 controller := newFakeControllerWithFiles( 65 &fakeFile{name: "prefix-grasshopper.avi", contents: []byte("The man in the high castle")}, 66 ) 67 storage := s.makeStorage(c, controller) 68 reader, err := storage.Get("grasshopper.avi") 69 c.Assert(err, jc.ErrorIsNil) 70 defer reader.Close() 71 result, err := ioutil.ReadAll(reader) 72 c.Assert(err, jc.ErrorIsNil) 73 c.Assert(result, gc.DeepEquals, []byte("The man in the high castle")) 74 controller.Stub.CheckCall(c, 0, "GetFile", "prefix-grasshopper.avi") 75 } 76 77 func (s *maas2StorageSuite) TestListError(c *gc.C) { 78 storage := s.makeStorage(c, newFakeControllerWithErrors( 79 errors.New("couldn't list files"), 80 )) 81 _, err := storage.List("american-territories") 82 c.Assert(err, gc.ErrorMatches, "couldn't list files") 83 } 84 85 func (s *maas2StorageSuite) TestListSuccess(c *gc.C) { 86 controller := newFakeControllerWithFiles( 87 &fakeFile{name: "prefix-julianna"}, 88 &fakeFile{name: "prefix-frank"}, 89 ) 90 storage := s.makeStorage(c, controller) 91 result, err := storage.List("grasshopper") 92 c.Assert(err, jc.ErrorIsNil) 93 c.Assert(result, jc.DeepEquals, []string{"frank", "julianna"}) 94 controller.Stub.CheckCall(c, 0, "Files", "prefix-grasshopper") 95 } 96 97 func (s *maas2StorageSuite) TestURLError(c *gc.C) { 98 controller := newFakeControllerWithErrors(errors.New("no such file")) 99 storage := s.makeStorage(c, controller) 100 _, err := storage.URL("grasshopper.avi") 101 c.Assert(err, gc.ErrorMatches, "no such file") 102 } 103 104 func (s *maas2StorageSuite) TestURLSuccess(c *gc.C) { 105 controller := newFakeControllerWithFiles( 106 &fakeFile{name: "prefix-grasshopper.avi", url: "heavy lies"}, 107 ) 108 storage := s.makeStorage(c, controller) 109 result, err := storage.URL("grasshopper.avi") 110 c.Assert(err, jc.ErrorIsNil) 111 c.Assert(result, gc.Equals, "heavy lies") 112 checkCalls(c, controller.Stub, makeCall("GetFile", "prefix-grasshopper.avi")) 113 } 114 115 func (s *maas2StorageSuite) TestPut(c *gc.C) { 116 controller := newFakeControllerWithErrors(errors.New("oh no!")) 117 storage := s.makeStorage(c, controller) 118 reader := bytes.NewReader([]byte{}) 119 err := storage.Put("riff", reader, 10) 120 c.Assert(err, gc.ErrorMatches, "oh no!") 121 checkCalls(c, controller.Stub, makeCall("AddFile", gomaasapi.AddFileArgs{ 122 Filename: "prefix-riff", 123 Reader: reader, 124 Length: 10, 125 })) 126 } 127 128 func (s *maas2StorageSuite) TestRemoveNoSuchFile(c *gc.C) { 129 controller := newFakeControllerWithErrors(errors.New("oh no!")) 130 storage := s.makeStorage(c, controller) 131 err := storage.Remove("FIOS") 132 c.Assert(err, gc.ErrorMatches, "oh no!") 133 } 134 135 func (s *maas2StorageSuite) TestRemoveErrorFromDelete(c *gc.C) { 136 controller := newFakeControllerWithFiles( 137 &fakeFile{name: "prefix-FIOS", error: errors.New("protected")}, 138 ) 139 storage := s.makeStorage(c, controller) 140 err := storage.Remove("FIOS") 141 c.Assert(err, gc.ErrorMatches, "protected") 142 checkCalls(c, controller.Stub, makeCall("GetFile", "prefix-FIOS")) 143 } 144 145 func (s *maas2StorageSuite) TestRemoveAll(c *gc.C) { 146 controller := newFakeControllerWithFiles( 147 &fakeFile{name: "prefix-zack"}, 148 &fakeFile{name: "prefix-kevin", error: errors.New("oops")}, 149 &fakeFile{name: "prefix-jim"}, 150 &fakeFile{name: "prefix-riff"}, 151 ) 152 storage := s.makeStorage(c, controller) 153 err := storage.RemoveAll() 154 c.Assert(err, gc.ErrorMatches, "cannot delete all provider state: oops") 155 controller.Stub.CheckCall(c, 0, "Files", "prefix-") 156 157 deleteds := make([]bool, 4) 158 for i, file := range controller.files { 159 deleteds[i] = file.(*fakeFile).deleted 160 } 161 c.Assert(deleteds, jc.DeepEquals, []bool{true, true, true, true}) 162 }