forked from mistifyio/lochness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stubagent.go
126 lines (112 loc) · 3.13 KB
/
stubagent.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
122
123
124
125
126
package lochness
import (
"errors"
"math/rand"
"time"
"github.com/mistifyio/mistify-agent/client"
)
type (
// StubAgent is an Agenter with stubbed methods for testing
StubAgent struct {
context *Context
rand *rand.Rand
failPercent int
}
)
// NewStubAgent creates a new StubAgent instance within the context and
// initialies the random number generator for failures
func (context *Context) NewStubAgent(failPercent int) *StubAgent {
return &StubAgent{
context: context,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
failPercent: failPercent,
}
}
// randomError simulates failure for a given percent of the time
func (agent *StubAgent) randomError() error {
if agent.rand.Intn(100) < agent.failPercent {
return errors.New("Random Error")
}
return nil
}
// guestFromID creates a *client.Guest from a guestID using the datastore
func (agent *StubAgent) guestFromID(guestID string) (*client.Guest, error) {
g, err := agent.context.Guest(guestID)
if err != nil {
return nil, err
}
flavor, err := agent.context.Flavor(g.FlavorID)
if err != nil {
return nil, err
}
subnet, err := agent.context.Subnet(g.SubnetID)
if err != nil {
return nil, err
}
nic := client.Nic{
Name: "foobar",
Network: g.NetworkID,
Model: "foobar",
Mac: g.MAC.String(),
Address: g.IP.String(),
Netmask: subnet.CIDR.Mask.String(),
Gateway: subnet.Gateway.String(),
Device: "eth0",
}
disk := client.Disk{
Bus: "sata",
Device: "sda1",
Size: flavor.Disk,
Volume: "foo",
Image: "",
Source: "/dev/zvol/foo",
}
return &client.Guest{
ID: g.ID,
Type: g.Type,
Nics: []client.Nic{nic},
Disks: []client.Disk{disk},
State: "running",
Memory: uint(flavor.Memory),
CPU: uint(flavor.CPU),
VNC: 1337,
Metadata: g.Metadata,
}, nil
}
// CheckJobStatus looks up whether a guest job has been completed or not.
func (agent *StubAgent) CheckJobStatus(action, guestID, jobID string) (bool, error) {
if err := agent.randomError(); err != nil {
return true, err
}
return true, nil
}
// GetGuest is a stub for retrieving a guest via request to the agent.
func (agent *StubAgent) GetGuest(guestID string) (*client.Guest, error) {
if err := agent.randomError(); err != nil {
return nil, err
}
guest, err := agent.guestFromID(guestID)
return guest, err
}
// CreateGuest is a stub for creating a guest via request to the agent.
func (agent *StubAgent) CreateGuest(guestID string) (string, error) {
if err := agent.randomError(); err != nil {
return "", err
}
return "1234abcd-1234-abcd-1234-abcd1324abcd", nil
}
// DeleteGuest is a stub for deleting a guest via request to the agent.
func (agent *StubAgent) DeleteGuest(guestID string) (string, error) {
if err := agent.randomError(); err != nil {
return "", err
}
return "1234abcd-1234-abcd-1234-abcd1324abcd", nil
}
// GuestAction is a stub for issuing other basic guest actions via request to
// the agent
func (agent *StubAgent) GuestAction(guestID, actionName string) (string, error) {
if err := agent.randomError(); err != nil {
return "", err
}
return "1234abcd-1234-abcd-1234-abcd1324abcd", nil
}