~axwalk/juju-core/lp1303195-manual-ubuntuuser-bash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package testing_test

import (
	stdtesting "testing"

	"labix.org/v2/mgo/bson"
	gc "launchpad.net/gocheck"

	"launchpad.net/juju-core/testing"
	"launchpad.net/juju-core/testing/testbase"
)

type mgoSuite struct {
	testbase.LoggingSuite
	testing.MgoSuite
}

var _ = gc.Suite(&mgoSuite{})

func TestMgoSuite(t *stdtesting.T) {
	testing.MgoTestPackage(t)
}

func (s *mgoSuite) SetUpSuite(c *gc.C) {
	s.LoggingSuite.SetUpSuite(c)
	s.MgoSuite.SetUpSuite(c)
}

func (s *mgoSuite) TearDownSuite(c *gc.C) {
	s.LoggingSuite.TearDownSuite(c)
	s.MgoSuite.TearDownSuite(c)
}

func (s *mgoSuite) SetUpTest(c *gc.C) {
	s.LoggingSuite.SetUpTest(c)
	s.MgoSuite.SetUpTest(c)
}

func (s *mgoSuite) TearDownTest(c *gc.C) {
	s.LoggingSuite.TearDownTest(c)
	s.MgoSuite.TearDownTest(c)
}

func (s *mgoSuite) TestResetWhenUnauthorized(c *gc.C) {
	session := testing.MgoServer.MustDial()
	defer session.Close()
	err := session.DB("admin").AddUser("admin", "foo", false)
	if err != nil && err.Error() != "need to login" {
		c.Assert(err, gc.IsNil)
	}
	// The test will fail if the reset does not succeed
}

func (s *mgoSuite) TestStartAndClean(c *gc.C) {
	c.Assert(testing.MgoServer.Addr(), gc.Not(gc.Equals), "")

	session := testing.MgoServer.MustDial()
	defer session.Close()
	menu := session.DB("food").C("menu")
	err := menu.Insert(
		bson.D{{"spam", "lots"}},
		bson.D{{"eggs", "fried"}},
	)
	c.Assert(err, gc.IsNil)
	food := make([]map[string]string, 0)
	err = menu.Find(nil).All(&food)
	c.Assert(err, gc.IsNil)
	c.Assert(food, gc.HasLen, 2)
	c.Assert(food[0]["spam"], gc.Equals, "lots")
	c.Assert(food[1]["eggs"], gc.Equals, "fried")

	testing.MgoServer.Reset()
	morefood := make([]map[string]string, 0)
	err = menu.Find(nil).All(&morefood)
	c.Assert(err, gc.IsNil)
	c.Assert(morefood, gc.HasLen, 0)
}