-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_test.go
121 lines (100 loc) · 2.82 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
118
119
120
121
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package juju_test
import (
"github.com/juju/names/v4"
"github.com/juju/version/v2"
"github.com/juju/juju/api"
"github.com/juju/juju/core/network"
"github.com/juju/juju/testing"
)
type mockAPIState struct {
api.Connection
// If non-nil, close is called when the Close method is called.
close func(api.Connection) error
addr string
ipAddr string
apiHostPorts []network.MachineHostPorts
modelTag string
controllerTag string
publicDNSName 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.MachineHostPorts{}
if hasHostPort {
apiHostPorts = []network.MachineHostPorts{{
network.MachineHostPort{MachineAddress: network.NewMachineAddress("0.1.2.3"), NetPort: network.NetPort(1234)},
network.MachineHostPort{MachineAddress: network.NewMachineAddress("2001:db8::1"), NetPort: network.NetPort(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) IPAddr() string {
return s.ipAddr
}
func (s *mockAPIState) Addr() string {
return s.addr
}
func (s *mockAPIState) PublicDNSName() string {
return s.publicDNSName
}
func (s *mockAPIState) APIHostPorts() []network.MachineHostPorts {
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")
}