github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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  	gitjujutesting "github.com/juju/testing"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	"github.com/juju/juju/cmd/juju/model"
    13  	"github.com/juju/juju/jujuclient"
    14  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    15  	"github.com/juju/juju/testing"
    16  )
    17  
    18  type DumpDBCommandSuite struct {
    19  	testing.FakeJujuXDGDataHomeSuite
    20  	fake  fakeDumpDBClient
    21  	store *jujuclienttesting.MemStore
    22  }
    23  
    24  var _ = gc.Suite(&DumpDBCommandSuite{})
    25  
    26  func (s *DumpDBCommandSuite) SetUpTest(c *gc.C) {
    27  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    28  	s.fake.ResetCalls()
    29  	s.store = jujuclienttesting.NewMemStore()
    30  	s.store.CurrentControllerName = "testing"
    31  	s.store.Controllers["testing"] = jujuclient.ControllerDetails{}
    32  	s.store.Accounts["testing"] = jujuclient.AccountDetails{
    33  		User: "admin@local",
    34  	}
    35  	err := s.store.UpdateModel("testing", "admin@local/mymodel", jujuclient.ModelDetails{
    36  		testing.ModelTag.Id(),
    37  	})
    38  	c.Assert(err, jc.ErrorIsNil)
    39  	s.store.Models["testing"].CurrentModel = "admin@local/mymodel"
    40  }
    41  
    42  func (s *DumpDBCommandSuite) TestDumpDB(c *gc.C) {
    43  	ctx, err := testing.RunCommand(c, model.NewDumpDBCommandForTest(&s.fake, s.store))
    44  	c.Assert(err, jc.ErrorIsNil)
    45  	s.fake.CheckCalls(c, []gitjujutesting.StubCall{
    46  		{"DumpModelDB", []interface{}{testing.ModelTag}},
    47  		{"Close", nil},
    48  	})
    49  
    50  	out := testing.Stdout(ctx)
    51  	c.Assert(out, gc.Equals, `all-others: heaps of data
    52  models:
    53    name: testing
    54    uuid: fake-uuid
    55  `)
    56  }
    57  
    58  type fakeDumpDBClient struct {
    59  	gitjujutesting.Stub
    60  }
    61  
    62  func (f *fakeDumpDBClient) Close() error {
    63  	f.MethodCall(f, "Close")
    64  	return f.NextErr()
    65  }
    66  
    67  func (f *fakeDumpDBClient) DumpModelDB(model names.ModelTag) (map[string]interface{}, error) {
    68  	f.MethodCall(f, "DumpModelDB", model)
    69  	err := f.NextErr()
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	return map[string]interface{}{
    74  		"models": map[string]interface{}{
    75  			"name": "testing",
    76  			"uuid": "fake-uuid",
    77  		},
    78  		"all-others": "heaps of data",
    79  	}, nil
    80  }