| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package cloudapi_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"io/ioutil" |
| 11 |
"log" |
| 12 |
"net/http" |
| 13 |
"net/http/httptest" |
| 14 |
"os" |
| 15 |
"strings" |
| 16 |
"time" |
| 17 |
|
| 18 |
gc "launchpad.net/gocheck" |
| 19 |
|
| 20 |
"github.com/joyent/gocommon/client" |
| 21 |
"github.com/joyent/gosdc/cloudapi" |
| 22 |
lc "github.com/joyent/gosdc/localservices/cloudapi" |
| 23 |
"github.com/joyent/gosign/auth" |
| 24 |
"github.com/julienschmidt/httprouter" |
| 25 |
) |
| 26 |
|
| 27 |
var privateKey []byte |
| 28 |
|
| 29 |
func registerLocalTests(keyName string) { |
| 30 |
var localKeyFile string |
| 31 |
if keyName == "" { |
| 32 |
localKeyFile = os.Getenv("HOME") + "/.ssh/id_rsa" |
| 33 |
} else { |
| 34 |
localKeyFile = keyName |
| 35 |
} |
| 36 |
privateKey, _ = ioutil.ReadFile(localKeyFile) |
| 37 |
|
| 38 |
gc.Suite(&LocalTests{}) |
| 39 |
} |
| 40 |
|
| 41 |
type LocalTests struct { |
| 42 |
|
| 43 |
creds *auth.Credentials |
| 44 |
testClient *cloudapi.Client |
| 45 |
Server *httptest.Server |
| 46 |
Mux *httprouter.Router |
| 47 |
oldHandler http.Handler |
| 48 |
cloudapi *lc.CloudAPI |
| 49 |
} |
| 50 |
|
| 51 |
func (s *LocalTests) SetUpSuite(c *gc.C) { |
| 52 |
|
| 53 |
s.Server = httptest.NewServer(nil) |
| 54 |
s.oldHandler = s.Server.Config.Handler |
| 55 |
s.Mux = httprouter.New() |
| 56 |
s.Server.Config.Handler = s.Mux |
| 57 |
|
| 58 |
|
| 59 |
authentication, err := auth.NewAuth("localtest", string(privateKey), "rsa-sha256") |
| 60 |
c.Assert(err, gc.IsNil) |
| 61 |
|
| 62 |
s.creds = &auth.Credentials{ |
| 63 |
UserAuthentication: authentication, |
| 64 |
SdcKeyId: "", |
| 65 |
SdcEndpoint: auth.Endpoint{URL: s.Server.URL}, |
| 66 |
} |
| 67 |
s.cloudapi = lc.New(s.creds.SdcEndpoint.URL, s.creds.UserAuthentication.User) |
| 68 |
s.cloudapi.SetupHTTP(s.Mux) |
| 69 |
} |
| 70 |
|
| 71 |
func (s *LocalTests) TearDownSuite(c *gc.C) { |
| 72 |
s.Mux = nil |
| 73 |
s.Server.Config.Handler = s.oldHandler |
| 74 |
s.Server.Close() |
| 75 |
} |
| 76 |
|
| 77 |
func (s *LocalTests) SetUpTest(c *gc.C) { |
| 78 |
client := client.NewClient(s.creds.SdcEndpoint.URL, cloudapi.DefaultAPIVersion, s.creds, log.New(os.Stderr, "", log.LstdFlags)) |
| 79 |
c.Assert(client, gc.NotNil) |
| 80 |
s.testClient = cloudapi.New(client) |
| 81 |
c.Assert(s.testClient, gc.NotNil) |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
func (s *LocalTests) createKey(c *gc.C) { |
| 86 |
key, err := s.testClient.CreateKey(cloudapi.CreateKeyOpts{Name: "fake-key", Key: testKey}) |
| 87 |
c.Assert(err, gc.IsNil) |
| 88 |
c.Assert(key, gc.DeepEquals, &cloudapi.Key{Name: "fake-key", Fingerprint: "", Key: testKey}) |
| 89 |
} |
| 90 |
|
| 91 |
func (s *LocalTests) deleteKey(c *gc.C) { |
| 92 |
err := s.testClient.DeleteKey("fake-key") |
| 93 |
c.Assert(err, gc.IsNil) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
func (s *LocalTests) createMachine(c *gc.C) *cloudapi.Machine { |
| 98 |
machine, err := s.testClient.CreateMachine(cloudapi.CreateMachineOpts{Package: localPackageName, Image: localImageID}) |
| 99 |
c.Assert(err, gc.IsNil) |
| 100 |
c.Assert(machine, gc.NotNil) |
| 101 |
|
| 102 |
|
| 103 |
for !s.pollMachineState(c, machine.Id, "running") { |
| 104 |
time.Sleep(1 * time.Second) |
| 105 |
} |
| 106 |
|
| 107 |
return machine |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
func (s *LocalTests) pollMachineState(c *gc.C, machineId, state string) bool { |
| 112 |
machineConfig, err := s.testClient.GetMachine(machineId) |
| 113 |
c.Assert(err, gc.IsNil) |
| 114 |
return strings.EqualFold(machineConfig.State, state) |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
func (s *LocalTests) deleteMachine(c *gc.C, machineId string) { |
| 119 |
err := s.testClient.StopMachine(machineId) |
| 120 |
c.Assert(err, gc.IsNil) |
| 121 |
|
| 122 |
|
| 123 |
for !s.pollMachineState(c, machineId, "stopped") { |
| 124 |
time.Sleep(1 * time.Second) |
| 125 |
} |
| 126 |
|
| 127 |
err = s.testClient.DeleteMachine(machineId) |
| 128 |
c.Assert(err, gc.IsNil) |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
func (s *LocalTests) listMachines(c *gc.C, filter *cloudapi.Filter) { |
| 133 |
var contains bool |
| 134 |
testMachine := s.createMachine(c) |
| 135 |
defer s.deleteMachine(c, testMachine.Id) |
| 136 |
|
| 137 |
machines, err := s.testClient.ListMachines(filter) |
| 138 |
c.Assert(err, gc.IsNil) |
| 139 |
c.Assert(machines, gc.NotNil) |
| 140 |
for _, m := range machines { |
| 141 |
if m.Id == testMachine.Id { |
| 142 |
contains = true |
| 143 |
break |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
if !contains { |
| 149 |
c.Fatalf("Obtained machines [%v] do not contain test machine [%v]", machines, *testMachine) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
func (s *LocalTests) createFirewallRule(c *gc.C) *cloudapi.FirewallRule { |
| 155 |
fwRule, err := s.testClient.CreateFirewallRule(cloudapi.CreateFwRuleOpts{Enabled: false, Rule: testFwRule}) |
| 156 |
c.Assert(err, gc.IsNil) |
| 157 |
c.Assert(fwRule, gc.NotNil) |
| 158 |
c.Assert(fwRule.Rule, gc.Equals, testFwRule) |
| 159 |
c.Assert(fwRule.Enabled, gc.Equals, false) |
| 160 |
time.Sleep(10 * time.Second) |
| 161 |
|
| 162 |
return fwRule |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
func (s *LocalTests) deleteFwRule(c *gc.C, fwRuleId string) { |
| 167 |
err := s.testClient.DeleteFirewallRule(fwRuleId) |
| 168 |
c.Assert(err, gc.IsNil) |
| 169 |
} |