forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_test.go
117 lines (97 loc) · 2.67 KB
/
mock_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
117
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package juju_test
import (
"net"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/api"
"github.com/juju/juju/network"
"github.com/juju/juju/testing"
"github.com/juju/version"
)
type mockAPIState struct {
api.Connection
// If non-nil, close is called when the Close method is called.
close func(api.Connection) error
addr string
apiHostPorts [][]network.HostPort
modelTag string
controllerTag string
}
type mockedStateFlags int
const (
noFlags mockedStateFlags = 0x0000
mockedHostPort mockedStateFlags = 0x0001
mockedModelTag mockedStateFlags = 0x0002
)
// mockedAPIState returns a mocked-up implementation
// of api.Connection. The logical OR of the flags specifies
// whether to include a fake host port and model tag
// in the result.
func mockedAPIState(flags mockedStateFlags) *mockAPIState {
hasHostPort := flags&mockedHostPort == mockedHostPort
hasModelTag := flags&mockedModelTag == mockedModelTag
addr := ""
apiHostPorts := [][]network.HostPort{}
if hasHostPort {
var apiAddrs []network.Address
ipv4Address := network.NewAddress("0.1.2.3")
ipv6Address := network.NewAddress("2001:db8::1")
addr = net.JoinHostPort(ipv4Address.Value, "1234")
apiAddrs = append(apiAddrs, ipv4Address, ipv6Address)
apiHostPorts = [][]network.HostPort{
network.AddressesWithPort(apiAddrs, 1234),
}
}
modelTag := ""
if hasModelTag {
modelTag = "model-df136476-12e9-11e4-8a70-b2227cce2b54"
}
return &mockAPIState{
apiHostPorts: apiHostPorts,
modelTag: modelTag,
controllerTag: testing.ControllerTag.Id(),
addr: addr,
}
}
func (s *mockAPIState) Close() error {
if s.close != nil {
return s.close(s)
}
return nil
}
func (s *mockAPIState) ServerVersion() (version.Number, bool) {
return version.MustParse("1.2.3"), true
}
func (s *mockAPIState) Addr() string {
return s.addr
}
func (s *mockAPIState) APIHostPorts() [][]network.HostPort {
return s.apiHostPorts
}
func (s *mockAPIState) ModelTag() (names.ModelTag, bool) {
if s.modelTag == "" {
return names.ModelTag{}, false
}
t, err := names.ParseModelTag(s.modelTag)
if err != nil {
panic("bad model tag")
}
return t, true
}
func (s *mockAPIState) ControllerTag() names.ControllerTag {
t, err := names.ParseControllerTag(s.controllerTag)
if err != nil {
panic("bad controller tag")
}
return t
}
func (s *mockAPIState) AuthTag() names.Tag {
return names.NewUserTag("admin")
}
func (s *mockAPIState) ControllerAccess() string {
return "superuser"
}
func panicAPIOpen(apiInfo *api.Info, opts api.DialOpts) (api.Connection, error) {
panic("api.Open called unexpectedly")
}