/
agent_test.go
82 lines (70 loc) · 2.58 KB
/
agent_test.go
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
81
82
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package agent
import (
"github.com/juju/cmd"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils/series"
gc "gopkg.in/check.v1"
worker "gopkg.in/juju/worker.v1"
"github.com/juju/juju/cmd/jujud/agent/agenttest"
cmdutil "github.com/juju/juju/cmd/jujud/util"
imagetesting "github.com/juju/juju/environs/imagemetadata/testing"
"github.com/juju/juju/juju/paths"
"github.com/juju/juju/mongo"
"github.com/juju/juju/network"
coretesting "github.com/juju/juju/testing"
"github.com/juju/juju/worker/proxyupdater"
)
type acCreator func() (cmd.Command, AgentConf)
// CheckAgentCommand is a utility function for verifying that common agent
// options are handled by a Command; it returns an instance of that
// command pre-parsed, with any mandatory flags added.
func CheckAgentCommand(c *gc.C, create acCreator, args []string) cmd.Command {
com, conf := create()
err := coretesting.InitCommand(com, args)
dataDir, err := paths.DataDir(series.MustHostSeries())
c.Assert(err, jc.ErrorIsNil)
c.Assert(conf.DataDir(), gc.Equals, dataDir)
badArgs := append(args, "--data-dir", "")
com, _ = create()
err = coretesting.InitCommand(com, badArgs)
c.Assert(err, gc.ErrorMatches, "--data-dir option must be set")
args = append(args, "--data-dir", "jd")
com, conf = create()
c.Assert(coretesting.InitCommand(com, args), gc.IsNil)
c.Assert(conf.DataDir(), gc.Equals, "jd")
return com
}
// ParseAgentCommand is a utility function that inserts the always-required args
// before parsing an agent command and returning the result.
func ParseAgentCommand(ac cmd.Command, args []string) error {
common := []string{
"--data-dir", "jd",
}
return coretesting.InitCommand(ac, append(common, args...))
}
// AgentSuite is a fixture to be used by agent test suites.
type AgentSuite struct {
agenttest.AgentSuite
}
func (s *AgentSuite) SetUpSuite(c *gc.C) {
s.JujuConnSuite.SetUpSuite(c)
s.PatchValue(&cmdutil.EnsureMongoServer, func(mongo.EnsureServerParams) error {
return nil
})
}
func (s *AgentSuite) SetUpTest(c *gc.C) {
s.JujuConnSuite.SetUpTest(c)
// Set API host ports so FindTools/Tools API calls succeed.
hostPorts := [][]network.HostPort{
network.NewHostPorts(1234, "0.1.2.3"),
}
err := s.State.SetAPIHostPorts(hostPorts)
c.Assert(err, jc.ErrorIsNil)
s.PatchValue(&proxyupdater.NewWorker, func(proxyupdater.Config) (worker.Worker, error) {
return newDummyWorker(), nil
})
// Tests should not try to use internet. Ensure base url is empty.
imagetesting.PatchOfficialDataSources(&s.CleanupSuite, "")
}