~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/state/state_test.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
        "github.com/juju/utils"
22
22
        "github.com/juju/utils/arch"
23
23
        "github.com/juju/utils/series"
 
24
        "github.com/juju/version"
24
25
        gc "gopkg.in/check.v1"
25
26
        "gopkg.in/juju/charm.v6-unstable"
26
27
        "gopkg.in/mgo.v2"
36
37
        "github.com/juju/juju/state"
37
38
        "github.com/juju/juju/state/multiwatcher"
38
39
        statetesting "github.com/juju/juju/state/testing"
 
40
        "github.com/juju/juju/status"
39
41
        "github.com/juju/juju/storage/poolmanager"
40
42
        "github.com/juju/juju/storage/provider"
41
43
        "github.com/juju/juju/storage/provider/registry"
42
44
        "github.com/juju/juju/testcharms"
43
45
        "github.com/juju/juju/testing"
44
46
        "github.com/juju/juju/testing/factory"
45
 
        "github.com/juju/juju/version"
 
47
        jujuversion "github.com/juju/juju/version"
46
48
)
47
49
 
48
50
var goodPassword = "foo-12345678901234567890"
54
56
// asserting the behaviour of a given method in each state, and the unit quick-
55
57
// remove change caused many of these to fail.
56
58
func preventUnitDestroyRemove(c *gc.C, u *state.Unit) {
57
 
        err := u.SetAgentStatus(state.StatusIdle, "", nil)
 
59
        err := u.SetAgentStatus(status.StatusIdle, "", nil)
58
60
        c.Assert(err, jc.ErrorIsNil)
59
61
}
60
62
 
1876
1878
                c.Assert(env.Destroy(), gc.IsNil)
1877
1879
        }).Check()
1878
1880
        _, err = s.State.AddService(state.AddServiceArgs{Name: "s1", Owner: s.Owner.String(), Charm: charm})
1879
 
        c.Assert(err, gc.ErrorMatches, `cannot add service "s1": model is no longer alive`)
 
1881
        c.Assert(err, gc.ErrorMatches, `cannot add service "s1": model "testenv" is no longer alive`)
1880
1882
}
1881
1883
 
1882
1884
func (s *StateSuite) TestServiceNotFound(c *gc.C) {
1978
1980
                bindings:      map[string]string{"extra": "missing"},
1979
1981
                expectedError: `unknown endpoint "extra" not valid`,
1980
1982
        }, {
1981
 
                about:         "ensure network.DefaultSpace is not treated specially",
1982
 
                bindings:      map[string]string{"server": network.DefaultSpace},
1983
 
                expectedError: `unknown space "default" not valid`,
1984
 
        }, {
1985
1983
                about:         "extra endpoint not bound to a space",
1986
1984
                bindings:      map[string]string{"extra": ""},
1987
1985
                expectedError: `unknown endpoint "extra" not valid`,
2003
2001
                expectedError: `unknown space "invalid" not valid`,
2004
2002
        }, {
2005
2003
                about:         "known endpoint bound correctly and an extra endpoint",
2006
 
                bindings:      map[string]string{"server": "db", "foo": ""},
 
2004
                bindings:      map[string]string{"server": "db", "foo": "public"},
2007
2005
                expectedError: `unknown endpoint "foo" not valid`,
2008
2006
        }} {
2009
2007
                c.Logf("test #%d: %s", i, test.about)
2041
2039
                Name: "wordpress", Owner: s.Owner.String(), Charm: charm,
2042
2040
                Series: "centos7",
2043
2041
        })
2044
 
        c.Assert(err, gc.ErrorMatches, "cannot add service \"wordpress\": series \"centos7\" \\(OS \"CentOS\"\\) not supported by charm")
 
2042
        c.Assert(err, gc.ErrorMatches, `cannot add service "wordpress": series "centos7" \(OS \"CentOS"\) not supported by charm, supported series are "quantal"`)
2045
2043
}
2046
2044
 
2047
2045
func (s *StateSuite) TestAddServiceCompatibleOSWithSeriesInURL(c *gc.C) {
2055
2053
        c.Assert(err, jc.ErrorIsNil)
2056
2054
}
2057
2055
 
 
2056
func (s *StateSuite) TestAddServiceCompatibleOSWithNoExplicitSupportedSeries(c *gc.C) {
 
2057
        // If a charm doesn't declare any series, we can add it with any series we choose.
 
2058
        charm := s.AddSeriesCharm(c, "dummy", "")
 
2059
        _, err := s.State.AddService(state.AddServiceArgs{
 
2060
                Name: "wordpress", Owner: s.Owner.String(), Charm: charm,
 
2061
                Series: "quantal",
 
2062
        })
 
2063
        c.Assert(err, jc.ErrorIsNil)
 
2064
}
 
2065
 
2058
2066
func (s *StateSuite) TestAddServiceOSIncompatibleWithSupportedSeries(c *gc.C) {
2059
2067
        charm := state.AddTestingCharmMultiSeries(c, s.State, "multi-series")
2060
2068
        // A charm with supported series can only be force-deployed to series
2063
2071
                Name: "wordpress", Owner: s.Owner.String(), Charm: charm,
2064
2072
                Series: "centos7",
2065
2073
        })
2066
 
        c.Assert(err, gc.ErrorMatches, "cannot add service \"wordpress\": series \"centos7\" \\(OS \"CentOS\"\\) not supported by charm")
 
2074
        c.Assert(err, gc.ErrorMatches, `cannot add service "wordpress": series "centos7" \(OS "CentOS"\) not supported by charm, supported series are "precise, trusty"`)
2067
2075
}
2068
2076
 
2069
2077
func (s *StateSuite) TestAllServices(c *gc.C) {
3007
3015
}
3008
3016
 
3009
3017
func (s *StateSuite) TestWatchForModelConfigChanges(c *gc.C) {
3010
 
        cur := version.Current
 
3018
        cur := jujuversion.Current
3011
3019
        err := statetesting.SetAgentVersion(s.State, cur)
3012
3020
        c.Assert(err, jc.ErrorIsNil)
3013
3021
        w := s.State.WatchForModelConfigChanges()
3219
3227
        }
3220
3228
}
3221
3229
 
3222
 
func testSetAgentCompatPassword(c *gc.C, entity state.Authenticator) {
3223
 
        // In Juju versions 1.16 and older we used UserPasswordHash(password,CompatSalt)
3224
 
        // for Machine and Unit agents. This was determined to be overkill
3225
 
        // (since we know that Unit agents will actually use
3226
 
        // utils.RandomPassword() and get 18 bytes of entropy, and thus won't
3227
 
        // be brute-forced.)
3228
 
        c.Assert(entity.PasswordValid(goodPassword), jc.IsFalse)
3229
 
        agentHash := utils.AgentPasswordHash(goodPassword)
3230
 
        err := state.SetPasswordHash(entity, agentHash)
3231
 
        c.Assert(err, jc.ErrorIsNil)
3232
 
        c.Assert(entity.PasswordValid(goodPassword), jc.IsTrue)
3233
 
        c.Assert(entity.PasswordValid(alternatePassword), jc.IsFalse)
3234
 
        c.Assert(state.GetPasswordHash(entity), gc.Equals, agentHash)
3235
 
 
3236
 
        backwardsCompatibleHash := utils.UserPasswordHash(goodPassword, utils.CompatSalt)
3237
 
        c.Assert(backwardsCompatibleHash, gc.Not(gc.Equals), agentHash)
3238
 
        err = state.SetPasswordHash(entity, backwardsCompatibleHash)
3239
 
        c.Assert(err, jc.ErrorIsNil)
3240
 
        c.Assert(entity.PasswordValid(alternatePassword), jc.IsFalse)
3241
 
        c.Assert(state.GetPasswordHash(entity), gc.Equals, backwardsCompatibleHash)
3242
 
        // After succeeding to log in with the old compatible hash, the db
3243
 
        // should be updated with the new hash
3244
 
        c.Assert(entity.PasswordValid(goodPassword), jc.IsTrue)
3245
 
        c.Assert(state.GetPasswordHash(entity), gc.Equals, agentHash)
3246
 
        c.Assert(entity.PasswordValid(goodPassword), jc.IsTrue)
3247
 
 
3248
 
        // Agents are unable to set short passwords
3249
 
        err = entity.SetPassword("short")
3250
 
        c.Check(err, gc.ErrorMatches, "password is only 5 bytes long, and is not a valid Agent password")
3251
 
        // Grandfather clause. Agents that have short passwords are allowed if
3252
 
        // it was done in the compatHash form
3253
 
        agentHash = utils.AgentPasswordHash("short")
3254
 
        backwardsCompatibleHash = utils.UserPasswordHash("short", utils.CompatSalt)
3255
 
        err = state.SetPasswordHash(entity, backwardsCompatibleHash)
3256
 
        c.Assert(err, jc.ErrorIsNil)
3257
 
        c.Assert(entity.PasswordValid("short"), jc.IsTrue)
3258
 
        // We'll still update the hash, but now it points to the hash of the
3259
 
        // shorter password. Agents still can't set the password to it
3260
 
        c.Assert(state.GetPasswordHash(entity), gc.Equals, agentHash)
3261
 
        // Still valid with the shorter password
3262
 
        c.Assert(entity.PasswordValid("short"), jc.IsTrue)
3263
 
}
3264
 
 
3265
3230
type entity interface {
3266
3231
        state.Entity
3267
3232
        state.Lifer
3807
3772
 
3808
3773
func (s *StateSuite) TestSetEnvironAgentVersionOnOtherEnviron(c *gc.C) {
3809
3774
        current := version.MustParseBinary("1.24.7-trusty-amd64")
3810
 
        s.PatchValue(&version.Current, current.Number)
 
3775
        s.PatchValue(&jujuversion.Current, current.Number)
3811
3776
        s.PatchValue(&arch.HostArch, func() string { return current.Arch })
3812
3777
        s.PatchValue(&series.HostSeries, func() string { return current.Series })
3813
3778
 
3823
3788
        assertAgentVersion(c, otherSt, lower.Number.String())
3824
3789
 
3825
3790
        // Set other environ version == server environ version
3826
 
        err = otherSt.SetModelAgentVersion(version.Current)
 
3791
        err = otherSt.SetModelAgentVersion(jujuversion.Current)
3827
3792
        c.Assert(err, jc.ErrorIsNil)
3828
 
        assertAgentVersion(c, otherSt, version.Current.String())
 
3793
        assertAgentVersion(c, otherSt, jujuversion.Current.String())
3829
3794
 
3830
3795
        // Set other environ version to > server environ version
3831
3796
        err = otherSt.SetModelAgentVersion(higher.Number)
3832
3797
        expected := fmt.Sprintf("a hosted model cannot have a higher version than the server model: %s > %s",
3833
3798
                higher.Number,
3834
 
                version.Current,
 
3799
                jujuversion.Current,
3835
3800
        )
3836
3801
        c.Assert(err, gc.ErrorMatches, expected)
3837
3802
}
4637
4602
        c.Assert(err, gc.ErrorMatches, `"invalid-id" is not a valid machine id`)
4638
4603
}
4639
4604
 
 
4605
func (s *StateSuite) TestSetOrGetMongoSpaceNameSets(c *gc.C) {
 
4606
        info, err := s.State.ControllerInfo()
 
4607
        c.Assert(err, jc.ErrorIsNil)
 
4608
        c.Assert(info.MongoSpaceName, gc.Equals, "")
 
4609
        c.Assert(info.MongoSpaceState, gc.Equals, state.MongoSpaceUnknown)
 
4610
 
 
4611
        spaceName := network.SpaceName("foo")
 
4612
 
 
4613
        name, err := s.State.SetOrGetMongoSpaceName(spaceName)
 
4614
        c.Assert(err, jc.ErrorIsNil)
 
4615
        c.Assert(name, gc.Equals, spaceName)
 
4616
 
 
4617
        info, err = s.State.ControllerInfo()
 
4618
        c.Assert(err, jc.ErrorIsNil)
 
4619
        c.Assert(info.MongoSpaceName, gc.Equals, string(spaceName))
 
4620
        c.Assert(info.MongoSpaceState, gc.Equals, state.MongoSpaceValid)
 
4621
}
 
4622
 
 
4623
func (s *StateSuite) TestSetOrGetMongoSpaceNameDoesNotReplaceValidSpace(c *gc.C) {
 
4624
        spaceName := network.SpaceName("foo")
 
4625
        name, err := s.State.SetOrGetMongoSpaceName(spaceName)
 
4626
        c.Assert(err, jc.ErrorIsNil)
 
4627
        c.Assert(name, gc.Equals, spaceName)
 
4628
 
 
4629
        name, err = s.State.SetOrGetMongoSpaceName(network.SpaceName("bar"))
 
4630
        c.Assert(err, jc.ErrorIsNil)
 
4631
        c.Assert(name, gc.Equals, spaceName)
 
4632
 
 
4633
        info, err := s.State.ControllerInfo()
 
4634
        c.Assert(err, jc.ErrorIsNil)
 
4635
        c.Assert(info.MongoSpaceName, gc.Equals, string(spaceName))
 
4636
        c.Assert(info.MongoSpaceState, gc.Equals, state.MongoSpaceValid)
 
4637
}
 
4638
 
 
4639
func (s *StateSuite) TestSetMongoSpaceStateSetsValidStates(c *gc.C) {
 
4640
        mongoStates := []state.MongoSpaceStates{
 
4641
                state.MongoSpaceUnknown,
 
4642
                state.MongoSpaceValid,
 
4643
                state.MongoSpaceInvalid,
 
4644
                state.MongoSpaceUnsupported,
 
4645
        }
 
4646
        for _, st := range mongoStates {
 
4647
                err := s.State.SetMongoSpaceState(st)
 
4648
                c.Assert(err, jc.ErrorIsNil)
 
4649
                info, err := s.State.ControllerInfo()
 
4650
                c.Assert(err, jc.ErrorIsNil)
 
4651
                c.Assert(info.MongoSpaceState, gc.Equals, st)
 
4652
        }
 
4653
}
 
4654
 
 
4655
func (s *StateSuite) TestSetMongoSpaceStateErrorOnInvalidStates(c *gc.C) {
 
4656
        err := s.State.SetMongoSpaceState(state.MongoSpaceStates("bad"))
 
4657
        c.Assert(err, gc.ErrorMatches, "mongoSpaceState: bad not valid")
 
4658
        info, err := s.State.ControllerInfo()
 
4659
        c.Assert(err, jc.ErrorIsNil)
 
4660
        c.Assert(info.MongoSpaceState, gc.Equals, state.MongoSpaceUnknown)
 
4661
}
 
4662
 
4640
4663
type SetAdminMongoPasswordSuite struct {
4641
4664
        testing.BaseSuite
4642
4665
}