github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/model/dumpdb_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for info. 3 4 package model_test 5 6 import ( 7 "github.com/juju/cmd/cmdtesting" 8 gitjujutesting "github.com/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/juju/names.v2" 12 13 "github.com/juju/juju/cmd/juju/model" 14 coremodel "github.com/juju/juju/core/model" 15 "github.com/juju/juju/jujuclient" 16 "github.com/juju/juju/testing" 17 ) 18 19 type DumpDBCommandSuite struct { 20 testing.FakeJujuXDGDataHomeSuite 21 fake fakeDumpDBClient 22 store *jujuclient.MemStore 23 } 24 25 var _ = gc.Suite(&DumpDBCommandSuite{}) 26 27 func (s *DumpDBCommandSuite) SetUpTest(c *gc.C) { 28 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 29 s.fake.ResetCalls() 30 s.store = jujuclient.NewMemStore() 31 s.store.CurrentControllerName = "testing" 32 s.store.Controllers["testing"] = jujuclient.ControllerDetails{} 33 s.store.Accounts["testing"] = jujuclient.AccountDetails{ 34 User: "admin", 35 } 36 err := s.store.UpdateModel("testing", "admin/mymodel", jujuclient.ModelDetails{ 37 ModelUUID: testing.ModelTag.Id(), 38 ModelType: coremodel.IAAS, 39 }) 40 c.Assert(err, jc.ErrorIsNil) 41 s.store.Models["testing"].CurrentModel = "admin/mymodel" 42 } 43 44 func (s *DumpDBCommandSuite) TestDumpDB(c *gc.C) { 45 ctx, err := cmdtesting.RunCommand(c, model.NewDumpDBCommandForTest(&s.fake, s.store)) 46 c.Assert(err, jc.ErrorIsNil) 47 s.fake.CheckCalls(c, []gitjujutesting.StubCall{ 48 {"DumpModelDB", []interface{}{testing.ModelTag}}, 49 {"Close", nil}, 50 }) 51 52 out := cmdtesting.Stdout(ctx) 53 c.Assert(out, gc.Equals, `all-others: heaps of data 54 models: 55 name: testing 56 uuid: fake-uuid 57 `) 58 } 59 60 type fakeDumpDBClient struct { 61 gitjujutesting.Stub 62 } 63 64 func (f *fakeDumpDBClient) Close() error { 65 f.MethodCall(f, "Close") 66 return f.NextErr() 67 } 68 69 func (f *fakeDumpDBClient) DumpModelDB(model names.ModelTag) (map[string]interface{}, error) { 70 f.MethodCall(f, "DumpModelDB", model) 71 err := f.NextErr() 72 if err != nil { 73 return nil, err 74 } 75 return map[string]interface{}{ 76 "models": map[string]interface{}{ 77 "name": "testing", 78 "uuid": "fake-uuid", 79 }, 80 "all-others": "heaps of data", 81 }, nil 82 }