forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing_test.go
116 lines (94 loc) · 2.67 KB
/
testing_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package service
import (
"github.com/juju/errors"
"github.com/juju/os/series"
"github.com/juju/testing"
"github.com/juju/utils"
"github.com/juju/version"
gc "gopkg.in/check.v1"
"github.com/juju/juju/service/common"
svctesting "github.com/juju/juju/service/common/testing"
)
// Stub stubs out the external functions used in the service package.
type Stub struct {
*testing.Stub
Version version.Binary
Service Service
}
// GetVersion stubs out .
func (s *Stub) GetVersion() version.Binary {
s.AddCall("GetVersion")
// Pop the next error off the queue, even though we don't use it.
s.NextErr()
return s.Version
}
// DiscoverService stubs out service.DiscoverService.
func (s *Stub) DiscoverService(name string) (Service, error) {
s.AddCall("DiscoverService", name)
return s.Service, s.NextErr()
}
// BaseSuite is the base test suite for the application package.
type BaseSuite struct {
testing.IsolationSuite
Dirname string
Name string
Conf common.Conf
Failure error
Stub *testing.Stub
Service *svctesting.FakeService
Patched *Stub
}
func (s *BaseSuite) SetUpTest(c *gc.C) {
s.IsolationSuite.SetUpTest(c)
s.Dirname = c.MkDir()
s.Name = "juju-agent-machine-0"
s.Conf = common.Conf{
Desc: "some service",
ExecStart: "/bin/jujud machine 0",
}
s.Failure = errors.New("<failed>")
s.Service = svctesting.NewFakeService(s.Name, s.Conf)
s.Stub = &s.Service.Stub
s.Patched = &Stub{Stub: s.Stub}
s.PatchValue(&discoverService, s.Patched.DiscoverService)
}
func (s *BaseSuite) PatchAttempts(retries int) {
s.PatchValue(&installStartRetryAttempts, utils.AttemptStrategy{
Min: retries,
})
}
func (s *BaseSuite) PatchSeries(ser string) {
s.PatchValue(&series.MustHostSeries, func() string { return ser })
}
func NewDiscoveryCheck(name string, running bool, failure error) discoveryCheck {
return discoveryCheck{
name: name,
isRunning: func() (bool, error) {
return running, failure
},
}
}
func (s *BaseSuite) PatchLocalDiscovery(checks ...discoveryCheck) {
s.PatchValue(&discoveryFuncs, checks)
}
func (s *BaseSuite) PatchLocalDiscoveryDisable() {
s.PatchLocalDiscovery()
}
func (s *BaseSuite) PatchLocalDiscoveryNoMatch(expected string) {
// TODO(ericsnow) Pull from a list of supported init systems.
names := []string{
InitSystemUpstart,
InitSystemSystemd,
InitSystemWindows,
}
var checks []discoveryCheck
for _, name := range names {
checks = append(checks, NewDiscoveryCheck(name, name == expected, nil))
}
s.PatchLocalDiscovery(checks...)
}
func (s *BaseSuite) CheckFailure(c *gc.C, err error) {
c.Check(errors.Cause(err), gc.Equals, s.Failure)
}