~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/juju/mock_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package juju_test
 
5
 
 
6
import (
 
7
        "net"
 
8
 
 
9
        "gopkg.in/juju/names.v2"
 
10
 
 
11
        "github.com/juju/juju/api"
 
12
        "github.com/juju/juju/network"
 
13
)
 
14
 
 
15
type mockAPIState struct {
 
16
        api.Connection
 
17
 
 
18
        // If non-nil, close is called when the Close method is called.
 
19
        close func(api.Connection) error
 
20
 
 
21
        addr          string
 
22
        apiHostPorts  [][]network.HostPort
 
23
        modelTag      string
 
24
        controllerTag string
 
25
}
 
26
 
 
27
type mockedStateFlags int
 
28
 
 
29
const (
 
30
        noFlags        mockedStateFlags = 0x0000
 
31
        mockedHostPort mockedStateFlags = 0x0001
 
32
        mockedModelTag mockedStateFlags = 0x0002
 
33
)
 
34
 
 
35
// mockedAPIState returns a mocked-up implementation
 
36
// of api.Connection. The logical OR of the flags specifies
 
37
// whether to include a fake host port and model tag
 
38
// in the result.
 
39
func mockedAPIState(flags mockedStateFlags) *mockAPIState {
 
40
        hasHostPort := flags&mockedHostPort == mockedHostPort
 
41
        hasModelTag := flags&mockedModelTag == mockedModelTag
 
42
        addr := ""
 
43
 
 
44
        apiHostPorts := [][]network.HostPort{}
 
45
        if hasHostPort {
 
46
                var apiAddrs []network.Address
 
47
                ipv4Address := network.NewAddress("0.1.2.3")
 
48
                ipv6Address := network.NewAddress("2001:db8::1")
 
49
                addr = net.JoinHostPort(ipv4Address.Value, "1234")
 
50
                apiAddrs = append(apiAddrs, ipv4Address, ipv6Address)
 
51
                apiHostPorts = [][]network.HostPort{
 
52
                        network.AddressesWithPort(apiAddrs, 1234),
 
53
                }
 
54
        }
 
55
        modelTag := ""
 
56
        if hasModelTag {
 
57
                modelTag = "model-df136476-12e9-11e4-8a70-b2227cce2b54"
 
58
        }
 
59
        return &mockAPIState{
 
60
                apiHostPorts:  apiHostPorts,
 
61
                modelTag:      modelTag,
 
62
                controllerTag: modelTag,
 
63
                addr:          addr,
 
64
        }
 
65
}
 
66
 
 
67
func (s *mockAPIState) Close() error {
 
68
        if s.close != nil {
 
69
                return s.close(s)
 
70
        }
 
71
        return nil
 
72
}
 
73
 
 
74
func (s *mockAPIState) Addr() string {
 
75
        return s.addr
 
76
}
 
77
 
 
78
func (s *mockAPIState) APIHostPorts() [][]network.HostPort {
 
79
        return s.apiHostPorts
 
80
}
 
81
 
 
82
func (s *mockAPIState) ModelTag() (names.ModelTag, error) {
 
83
        return names.ParseModelTag(s.modelTag)
 
84
}
 
85
 
 
86
func (s *mockAPIState) ControllerTag() (names.ModelTag, error) {
 
87
        return names.ParseModelTag(s.controllerTag)
 
88
}
 
89
 
 
90
func panicAPIOpen(apiInfo *api.Info, opts api.DialOpts) (api.Connection, error) {
 
91
        panic("api.Open called unexpectedly")
 
92
}