Skip to content

Commit

Permalink
Add agent-version to model info as optional parameter and update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasiamac committed May 29, 2017
1 parent 776f6f4 commit a429b3a
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 21 deletions.
3 changes: 3 additions & 0 deletions api/base/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package base
import (
"time"

"github.com/juju/version"

"github.com/juju/juju/instance"
"github.com/juju/juju/status"
)
Expand Down Expand Up @@ -57,6 +59,7 @@ type ModelInfo struct {
Status Status
Users []UserInfo
Machines []Machine
AgentVersion *version.Number
}

// Status represents the status of a machine, application, or unit.
Expand Down
40 changes: 29 additions & 11 deletions api/modelmanager/modelinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/juju/juju/api/modelmanager"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/testing"
"github.com/juju/juju/version"
)

type modelInfoSuite struct {
Expand All @@ -31,25 +32,42 @@ func (s *modelInfoSuite) assertResponse(c *gc.C, result interface{}) *params.Mod
return result.(*params.ModelInfoResults)
}

func (s *modelInfoSuite) TestModelInfo(c *gc.C) {
results := params.ModelInfoResults{
Results: []params.ModelInfoResult{{
Result: &params.ModelInfo{Name: "name", UUID: "etc."},
}, {
Error: &params.Error{Message: "woop"},
}},
}
func (s *modelInfoSuite) assertExpectedModelInfo(c *gc.C, expectedInfo params.ModelInfoResults) {
apiCaller := basetesting.APICallerFunc(
func(objType string, version int, id, request string, a, result interface{}) error {
s.checkCall(c, objType, id, request)
resp := s.assertResponse(c, result)
*resp = results
*resp = expectedInfo
return nil
})
client := modelmanager.NewClient(apiCaller)
info, err := client.ModelInfo([]names.ModelTag{testing.ModelTag, testing.ModelTag})
input := []names.ModelTag{}
for i := 0; i < len(expectedInfo.Results); i++ {
input = append(input, testing.ModelTag)
}
info, err := client.ModelInfo(input)
c.Assert(err, jc.ErrorIsNil)
c.Assert(info, jc.DeepEquals, results.Results)
c.Assert(info, jc.DeepEquals, expectedInfo.Results)
}

func (s *modelInfoSuite) TestModelInfo(c *gc.C) {
results := params.ModelInfoResults{
Results: []params.ModelInfoResult{{
Result: &params.ModelInfo{Name: "name", UUID: "etc."},
}, {
Error: &params.Error{Message: "woop"},
}},
}
s.assertExpectedModelInfo(c, results)
}

func (s *modelInfoSuite) TestModelInfoWithAgentVersion(c *gc.C) {
results := params.ModelInfoResults{
Results: []params.ModelInfoResult{{
Result: &params.ModelInfo{Name: "name", UUID: "etc.", AgentVersion: &version.Current},
}},
}
s.assertExpectedModelInfo(c, results)
}

func (s *modelInfoSuite) TestInvalidResultCount(c *gc.C) {
Expand Down
3 changes: 3 additions & 0 deletions api/modelmanager/modelmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func convertParamsModelInfo(modelInfo params.ModelInfo) (base.ModelInfo, error)
Owner: ownerTag.Id(),
Life: string(modelInfo.Life),
}
if modelInfo.AgentVersion != nil {
result.AgentVersion = modelInfo.AgentVersion
}
result.Status = base.Status{
Status: modelInfo.Status.Status,
Info: modelInfo.Status.Info,
Expand Down
3 changes: 3 additions & 0 deletions apiserver/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ func (c *Client) ModelInfo() (params.ModelInfo, error) {
OwnerTag: model.Owner().String(),
Life: params.Life(model.Life().String()),
}
if agentVersion, exists := conf.AgentVersion(); exists {
info.AgentVersion = &agentVersion
}
if tag, ok := model.CloudCredential(); ok {
info.CloudCredentialTag = tag.String()
}
Expand Down
2 changes: 2 additions & 0 deletions apiserver/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func (s *serverSuite) TestModelInfo(c *gc.C) {
c.Assert(info.UUID, gc.Equals, model.UUID())
c.Assert(info.OwnerTag, gc.Equals, model.Owner().String())
c.Assert(info.Life, gc.Equals, params.Alive)
expectedAgentVersion, _ := conf.AgentVersion()
c.Assert(info.AgentVersion, gc.DeepEquals, &expectedAgentVersion)
// The controller UUID is not returned by the ModelInfo endpoint on the
// Client facade.
c.Assert(info.ControllerUUID, gc.Equals, "")
Expand Down
3 changes: 3 additions & 0 deletions apiserver/modelmanager/modelinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ func (s *modelInfoSuite) setAPIUser(c *gc.C, user names.UserTag) {

func (s *modelInfoSuite) TestModelInfo(c *gc.C) {
info := s.getModelInfo(c, s.st.model.cfg.UUID())
expectedAgentVersion, exists := s.st.model.cfg.AgentVersion()
c.Assert(exists, jc.IsTrue)
c.Assert(info, jc.DeepEquals, params.ModelInfo{
Name: "testenv",
UUID: s.st.model.cfg.UUID(),
Expand Down Expand Up @@ -194,6 +196,7 @@ func (s *modelInfoSuite) TestModelInfo(c *gc.C) {
Level: "essential",
Owner: "user",
},
AgentVersion: &expectedAgentVersion,
})
s.st.CheckCalls(c, []gitjujutesting.StubCall{
{"ControllerTag", nil},
Expand Down
3 changes: 3 additions & 0 deletions apiserver/modelmanager/modelmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,9 @@ func (m *ModelManagerAPI) getModelInfo(tag names.ModelTag) (params.ModelInfo, er
if err == nil {
info.ProviderType = cfg.Type()
info.DefaultSeries = config.PreferredSeries(cfg)
if agentVersion, exists := cfg.AgentVersion(); exists {
info.AgentVersion = &agentVersion
}
}

status, err := model.Status()
Expand Down
3 changes: 3 additions & 0 deletions apiserver/params/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ type ModelInfo struct {

// SLA contains the information about the SLA for the model, if set.
SLA *ModelSLAInfo `json:"sla"`

// AgentVersion is the agent version for this model.
AgentVersion *version.Number `json:"agent-version"`
}

// ModelSLAInfo describes the SLA info for a model.
Expand Down
4 changes: 4 additions & 0 deletions cmd/juju/common/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type ModelInfo struct {
Machines map[string]ModelMachineInfo `json:"machines,omitempty" yaml:"machines,omitempty"`
SLA string `json:"sla,omitempty" yaml:"sla,omitempty"`
SLAOwner string `json:"sla-owner,omitempty" yaml:"sla-owner,omitempty"`
AgentVersion string `json:"agent-version,omitempty" yaml:"agent-version,omitempty"`
}

// ModelMachineInfo contains information about a machine in a model.
Expand Down Expand Up @@ -86,6 +87,9 @@ func ModelInfoFromParams(info params.ModelInfo, now time.Time) (ModelInfo, error
Cloud: cloudTag.Id(),
CloudRegion: info.CloudRegion,
}
if info.AgentVersion != nil {
modelInfo.AgentVersion = info.AgentVersion.String()
}
// Although this may be more performance intensive, we have to use reflection
// since structs containing map[string]interface {} cannot be compared, i.e
// cannot use simple '==' here.
Expand Down
11 changes: 8 additions & 3 deletions cmd/juju/controller/addmodel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
gitjujutesting "github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils"
"github.com/juju/version"
gc "gopkg.in/check.v1"
"gopkg.in/juju/names.v2"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -42,11 +43,15 @@ var _ = gc.Suite(&AddModelSuite{})

func (s *AddModelSuite) SetUpTest(c *gc.C) {
s.FakeJujuXDGDataHomeSuite.SetUpTest(c)

agentVersion, err := version.Parse("2.2-rc1")
c.Assert(err, jc.ErrorIsNil)
s.fakeAddModelAPI = &fakeAddClient{
model: base.ModelInfo{
Name: "test",
UUID: "fake-model-uuid",
Owner: "ignored-for-now",
Name: "test",
UUID: "fake-model-uuid",
Owner: "ignored-for-now",
AgentVersion: &agentVersion,
},
}
s.fakeCloudAPI = &fakeCloudAPI{
Expand Down
35 changes: 30 additions & 5 deletions cmd/juju/controller/listmodels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/juju/cmd"
"github.com/juju/cmd/cmdtesting"
jc "github.com/juju/testing/checkers"
"github.com/juju/version"
gc "gopkg.in/check.v1"
"gopkg.in/juju/names.v2"

Expand Down Expand Up @@ -64,18 +65,20 @@ func (f *fakeModelMgrAPIClient) ModelInfo(tags []names.ModelTag) ([]params.Model
if f.infos != nil {
return f.infos, nil
}
agentVersion, _ := version.Parse("2.2-rc1")
results := make([]params.ModelInfoResult, len(tags))
for i, tag := range tags {
for _, model := range f.models {
if model.UUID != tag.Id() {
continue
}
result := &params.ModelInfo{
Name: model.Name,
UUID: model.UUID,
OwnerTag: names.NewUserTag(model.Owner).String(),
CloudTag: "cloud-dummy",
Status: params.EntityStatus{},
Name: model.Name,
UUID: model.UUID,
OwnerTag: names.NewUserTag(model.Owner).String(),
CloudTag: "cloud-dummy",
Status: params.EntityStatus{},
AgentVersion: &agentVersion,
}
switch model.Name {
case "test-model1":
Expand Down Expand Up @@ -268,6 +271,7 @@ func (s *ModelsSuite) TestModelsError(c *gc.C) {
}

func createBasicModelInfo() *params.ModelInfo {
agentVersion, _ := version.Parse("2.2-rc1")
return &params.ModelInfo{
Name: "basic-model",
UUID: testing.ModelTag.Id(),
Expand All @@ -276,6 +280,7 @@ func createBasicModelInfo() *params.ModelInfo {
Life: params.Dead,
CloudTag: names.NewCloudTag("altostratus").String(),
CloudRegion: "mid-level",
AgentVersion: &agentVersion,
}
}

Expand Down Expand Up @@ -315,3 +320,23 @@ owner/basic-model altostratus/mid-level - 2 - - never
`[1:])
}

func (s *ModelsSuite) assertAgentVersionPresent(c *gc.C, testInfo *params.ModelInfo, checker gc.Checker) {
s.api.infos = []params.ModelInfoResult{
params.ModelInfoResult{Result: testInfo},
}
context, err := cmdtesting.RunCommand(c, s.newCommand(), "--format=yaml")
c.Assert(err, jc.ErrorIsNil)
c.Assert(cmdtesting.Stdout(context), checker, "agent-version")
}

func (s *ModelsSuite) TestListModelsWithAgent(c *gc.C) {
basicInfo := createBasicModelInfo()
s.assertAgentVersionPresent(c, basicInfo, jc.Contains)
}

func (s *ModelsSuite) TestListModelsWithNoAgent(c *gc.C) {
basicInfo := createBasicModelInfo()
basicInfo.AgentVersion = nil
s.assertAgentVersionPresent(c, basicInfo, gc.Not(jc.Contains))
}
40 changes: 40 additions & 0 deletions cmd/juju/model/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/juju/errors"
gitjujutesting "github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/version"
gc "gopkg.in/check.v1"
"gopkg.in/juju/names.v2"

Expand Down Expand Up @@ -472,6 +473,45 @@ func (s *ShowCommandSuite) TestShowBasicWithSLAIncompleteModelsJson(c *gc.C) {
s.assertShowOutput(c, "json")
}

func (s *ShowCommandSuite) TestShowModelWithAgentVersionInJson(c *gc.C) {
s.expectedDisplay = "{\"basic-model\":{\"name\":\"basic-model\"," +
"\"model-uuid\":\"deadbeef-0bad-400d-8000-4b1d0d06f00d\"," +
"\"controller-uuid\":\"deadbeef-1bad-500d-9000-4b1d0d06f00d\"," +
"\"controller-name\":\"testing\",\"owner\":\"owner\"," +
"\"cloud\":\"altostratus\",\"region\":\"mid-level\"," +
"\"life\":\"dead\",\"agent-version\":\"2.2-rc1\"}}\n"
s.assertShowModelWithAgent(c, "json")
}

func (s *ShowCommandSuite) TestShowModelWithAgentVersionInYaml(c *gc.C) {
s.expectedDisplay = `
basic-model:
name: basic-model
model-uuid: deadbeef-0bad-400d-8000-4b1d0d06f00d
controller-uuid: deadbeef-1bad-500d-9000-4b1d0d06f00d
controller-name: testing
owner: owner
cloud: altostratus
region: mid-level
life: dead
agent-version: 2.2-rc1
`[1:]
s.assertShowModelWithAgent(c, "yaml")
}

func (s *ShowCommandSuite) assertShowModelWithAgent(c *gc.C, format string) {
// Since most of the tests in this suite already test model infos without
// agent version, all we need to do here is to test one with it.
agentVersion, err := version.Parse("2.2-rc1")
c.Assert(err, jc.ErrorIsNil)
basicTestInfo := createBasicModelInfo()
basicTestInfo.AgentVersion = &agentVersion
s.fake.infos = []params.ModelInfoResult{
params.ModelInfoResult{Result: basicTestInfo},
}
s.assertShowOutput(c, format)
}

func (s *ShowCommandSuite) newShowCommand() cmd.Command {
return model.NewShowCommandForTest(&s.fake, noOpRefresh, s.store)
}
Expand Down
8 changes: 6 additions & 2 deletions featuretests/cmd_juju_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package featuretests

import (
"fmt"
"os"
"reflect"
"time"
Expand All @@ -30,6 +31,7 @@ import (
"github.com/juju/juju/status"
"github.com/juju/juju/testing"
"github.com/juju/juju/testing/factory"
"github.com/juju/juju/version"
)

type cmdControllerSuite struct {
Expand Down Expand Up @@ -111,7 +113,7 @@ func (s *cmdControllerSuite) TestListModelsYAML(c *gc.C) {
two := uint64(2)
s.Factory.MakeMachine(c, &factory.MachineParams{Characteristics: &instance.HardwareCharacteristics{CpuCores: &two}})
context := s.run(c, "list-models", "--format=yaml")
c.Assert(cmdtesting.Stdout(context), gc.Matches, `
expectedOutput := `
models:
- name: controller
model-uuid: deadbeef-0bad-400d-8000-4b1d0d06f00d
Expand All @@ -136,8 +138,10 @@ models:
"1":
cores: 2
sla: unsupported
agent-version: %v
current-model: controller
`[1:])
`[1:]
c.Assert(cmdtesting.Stdout(context), gc.Matches, fmt.Sprintf(expectedOutput, version.Current))
}

func (s *cmdControllerSuite) TestListDeadModels(c *gc.C) {
Expand Down

0 comments on commit a429b3a

Please sign in to comment.