github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/controller/undertaker/mock_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package undertaker_test 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 "gopkg.in/juju/names.v2" 11 12 "github.com/juju/juju/apiserver/facades/controller/undertaker" 13 "github.com/juju/juju/core/status" 14 "github.com/juju/juju/environs/config" 15 "github.com/juju/juju/state" 16 ) 17 18 // mockState implements State interface and allows inspection of called 19 // methods. 20 type mockState struct { 21 model *mockModel 22 removed bool 23 isSystem bool 24 25 watcher state.NotifyWatcher 26 } 27 28 var _ undertaker.State = (*mockState)(nil) 29 30 func newMockState(modelOwner names.UserTag, modelName string, isSystem bool) *mockState { 31 model := mockModel{ 32 owner: modelOwner, 33 name: modelName, 34 uuid: "9d3d3b19-2b0c-4a3f-acde-0b1645586a72", 35 life: state.Alive, 36 } 37 38 st := &mockState{ 39 model: &model, 40 isSystem: isSystem, 41 watcher: &mockWatcher{ 42 changes: make(chan struct{}, 1), 43 }, 44 } 45 return st 46 } 47 48 func (m *mockState) EnsureModelRemoved() error { 49 if !m.removed { 50 return errors.New("found documents for model") 51 } 52 return nil 53 } 54 55 func (m *mockState) RemoveDyingModel() error { 56 if m.model.life == state.Alive { 57 return errors.New("model not dying or dead") 58 } 59 m.removed = true 60 return nil 61 } 62 63 func (m *mockState) ProcessDyingModel() error { 64 if m.model.life != state.Dying { 65 return errors.New("model is not dying") 66 } 67 m.model.life = state.Dead 68 return nil 69 } 70 71 func (m *mockState) IsController() bool { 72 return m.isSystem 73 } 74 75 func (m *mockState) Model() (undertaker.Model, error) { 76 return m.model, nil 77 } 78 79 func (m *mockState) ModelConfig() (*config.Config, error) { 80 return &config.Config{}, nil 81 } 82 83 func (m *mockState) FindEntity(tag names.Tag) (state.Entity, error) { 84 if tag.Kind() == names.ModelTagKind && tag.Id() == m.model.UUID() { 85 return m.model, nil 86 } 87 return nil, errors.NotFoundf("entity with tag %q", tag.String()) 88 } 89 90 func (m *mockState) WatchModelEntityReferences(mUUID string) state.NotifyWatcher { 91 return m.watcher 92 } 93 94 func (m *mockState) ModelUUID() string { 95 return m.model.UUID() 96 } 97 98 // mockModel implements Model interface and allows inspection of called 99 // methods. 100 type mockModel struct { 101 tod time.Time 102 owner names.UserTag 103 life state.Life 104 name string 105 uuid string 106 107 status status.Status 108 statusInfo string 109 statusData map[string]interface{} 110 } 111 112 var _ undertaker.Model = (*mockModel)(nil) 113 114 func (m *mockModel) Owner() names.UserTag { 115 return m.owner 116 } 117 118 func (m *mockModel) Life() state.Life { 119 return m.life 120 } 121 122 func (m *mockModel) Tag() names.Tag { 123 return names.NewModelTag(m.uuid) 124 } 125 126 func (m *mockModel) Name() string { 127 return m.name 128 } 129 130 func (m *mockModel) UUID() string { 131 return m.uuid 132 } 133 134 func (m *mockModel) Destroy() error { 135 m.life = state.Dying 136 return nil 137 } 138 139 func (m *mockModel) SetStatus(sInfo status.StatusInfo) error { 140 m.status = sInfo.Status 141 m.statusInfo = sInfo.Message 142 m.statusData = sInfo.Data 143 return nil 144 } 145 146 type mockWatcher struct { 147 state.NotifyWatcher 148 changes chan struct{} 149 } 150 151 func (w *mockWatcher) Changes() <-chan struct{} { 152 return w.changes 153 }