github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/state/resources_persistence_staged_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/mgo.v2/txn" 12 13 "github.com/juju/juju/state/statetest" 14 ) 15 16 var _ = gc.Suite(&StagedResourceSuite{}) 17 18 type StagedResourceSuite struct { 19 testing.IsolationSuite 20 21 stub *testing.Stub 22 base *statetest.StubPersistence 23 } 24 25 func (s *StagedResourceSuite) SetUpTest(c *gc.C) { 26 s.IsolationSuite.SetUpTest(c) 27 28 s.stub = &testing.Stub{} 29 s.base = statetest.NewStubPersistence(s.stub) 30 s.base.ReturnApplicationExistsOps = []txn.Op{{ 31 C: "application", 32 Id: "a-application", 33 Assert: txn.DocExists, 34 }} 35 } 36 37 func (s *StagedResourceSuite) newStagedResource(c *gc.C, serviceID, name string) (*StagedResource, resourceDoc) { 38 stored, doc := newPersistenceResource(c, serviceID, name) 39 ignoredErr := errors.New("<never reached>") 40 s.stub.SetErrors(nil, nil, ignoredErr) 41 staged := &StagedResource{ 42 base: s.base, 43 id: stored.ID, 44 stored: stored, 45 } 46 return staged, doc 47 } 48 49 func (s *StagedResourceSuite) TestStageOkay(c *gc.C) { 50 staged, doc := s.newStagedResource(c, "a-application", "spam") 51 doc.DocID += "#staged" 52 ignoredErr := errors.New("<never reached>") 53 s.stub.SetErrors(nil, nil, nil, ignoredErr) 54 55 err := staged.stage() 56 c.Assert(err, jc.ErrorIsNil) 57 58 s.stub.CheckCallNames(c, "Run", "ApplicationExistsOps", "RunTransaction") 59 s.stub.CheckCall(c, 2, "RunTransaction", []txn.Op{{ 60 C: "resources", 61 Id: "resource#a-application/spam#staged", 62 Assert: txn.DocMissing, 63 Insert: &doc, 64 }, { 65 C: "application", 66 Id: "a-application", 67 Assert: txn.DocExists, 68 }}) 69 } 70 71 func (s *StagedResourceSuite) TestStageExists(c *gc.C) { 72 staged, doc := s.newStagedResource(c, "a-application", "spam") 73 doc.DocID += "#staged" 74 ignoredErr := errors.New("<never reached>") 75 s.stub.SetErrors(nil, nil, txn.ErrAborted, nil, nil, ignoredErr) 76 77 err := staged.stage() 78 c.Assert(err, jc.ErrorIsNil) 79 80 s.stub.CheckCallNames(c, "Run", "ApplicationExistsOps", "RunTransaction", "ApplicationExistsOps", "RunTransaction") 81 s.stub.CheckCall(c, 2, "RunTransaction", []txn.Op{{ 82 C: "resources", 83 Id: "resource#a-application/spam#staged", 84 Assert: txn.DocMissing, 85 Insert: &doc, 86 }, { 87 C: "application", 88 Id: "a-application", 89 Assert: txn.DocExists, 90 }}) 91 s.stub.CheckCall(c, 4, "RunTransaction", []txn.Op{{ 92 C: "resources", 93 Id: "resource#a-application/spam#staged", 94 Assert: &doc, 95 }, { 96 C: "application", 97 Id: "a-application", 98 Assert: txn.DocExists, 99 }}) 100 } 101 102 func (s *StagedResourceSuite) TestUnstageOkay(c *gc.C) { 103 staged, _ := s.newStagedResource(c, "a-application", "spam") 104 ignoredErr := errors.New("<never reached>") 105 s.stub.SetErrors(nil, nil, ignoredErr) 106 107 err := staged.Unstage() 108 c.Assert(err, jc.ErrorIsNil) 109 110 s.stub.CheckCallNames(c, "Run", "RunTransaction") 111 s.stub.CheckCall(c, 1, "RunTransaction", []txn.Op{{ 112 C: "resources", 113 Id: "resource#a-application/spam#staged", 114 Remove: true, 115 }}) 116 } 117 118 func (s *StagedResourceSuite) TestActivateOkay(c *gc.C) { 119 staged, doc := s.newStagedResource(c, "a-application", "spam") 120 ignoredErr := errors.New("<never reached>") 121 s.stub.SetErrors(nil, nil, nil, nil, nil, ignoredErr) 122 123 err := staged.Activate() 124 c.Assert(err, jc.ErrorIsNil) 125 126 s.stub.CheckCallNames(c, "Run", "ApplicationExistsOps", "One", "IncCharmModifiedVersionOps", "RunTransaction") 127 s.stub.CheckCall(c, 3, "IncCharmModifiedVersionOps", "a-application") 128 s.stub.CheckCall(c, 4, "RunTransaction", []txn.Op{{ 129 C: "resources", 130 Id: "resource#a-application/spam", 131 Assert: txn.DocMissing, 132 Insert: &doc, 133 }, { 134 C: "application", 135 Id: "a-application", 136 Assert: txn.DocExists, 137 }, { 138 C: "resources", 139 Id: "resource#a-application/spam#staged", 140 Remove: true, 141 }}) 142 } 143 144 func (s *StagedResourceSuite) TestActivateExists(c *gc.C) { 145 staged, doc := s.newStagedResource(c, "a-application", "spam") 146 ignoredErr := errors.New("<never reached>") 147 s.stub.SetErrors(nil, nil, nil, nil, txn.ErrAborted, nil, nil, nil, nil, ignoredErr) 148 149 err := staged.Activate() 150 c.Assert(err, jc.ErrorIsNil) 151 152 s.stub.CheckCallNames(c, "Run", "ApplicationExistsOps", "One", "IncCharmModifiedVersionOps", "RunTransaction", "ApplicationExistsOps", "One", "IncCharmModifiedVersionOps", "RunTransaction") 153 s.stub.CheckCall(c, 3, "IncCharmModifiedVersionOps", "a-application") 154 s.stub.CheckCall(c, 4, "RunTransaction", []txn.Op{{ 155 C: "resources", 156 Id: "resource#a-application/spam", 157 Assert: txn.DocMissing, 158 Insert: &doc, 159 }, { 160 C: "application", 161 Id: "a-application", 162 Assert: txn.DocExists, 163 }, { 164 C: "resources", 165 Id: "resource#a-application/spam#staged", 166 Remove: true, 167 }}) 168 s.stub.CheckCall(c, 7, "IncCharmModifiedVersionOps", "a-application") 169 s.stub.CheckCall(c, 8, "RunTransaction", []txn.Op{{ 170 C: "resources", 171 Id: "resource#a-application/spam", 172 Assert: txn.DocExists, 173 Remove: true, 174 }, { 175 C: "resources", 176 Id: "resource#a-application/spam", 177 Assert: txn.DocMissing, 178 Insert: &doc, 179 }, { 180 C: "application", 181 Id: "a-application", 182 Assert: txn.DocExists, 183 }, { 184 C: "resources", 185 Id: "resource#a-application/spam#staged", 186 Remove: true, 187 }}) 188 }