Enable patching out of test service server listing #8

Merged
merged 1 commit into from Apr 28, 2015
Jump to file or symbol
Failed to load files and symbols.
+134 −47
Split
@@ -323,13 +323,16 @@ type filter map[string]string
//
// This will match all servers with status "ACTIVE", and names starting
// with "foo".
-func (n *Nova) matchServers(f filter) []nova.ServerDetail {
+func (n *Nova) matchServers(f filter) ([]nova.ServerDetail, error) {
+ if err := n.ProcessFunctionHook(n, f); err != nil {
+ return nil, err
+ }
var servers []nova.ServerDetail
for _, server := range n.servers {
servers = append(servers, server)
}
if len(f) == 0 {
- return servers // empty filter matches everything
+ return servers, nil // empty filter matches everything
}
if status := f[nova.FilterStatus]; status != "" {
matched := []nova.ServerDetail{}
@@ -340,17 +343,15 @@ func (n *Nova) matchServers(f filter) []nova.ServerDetail {
}
if len(matched) == 0 {
// no match, so no need to look further
- return nil
+ return nil, nil
}
servers = matched
}
if nameRex := f[nova.FilterServer]; nameRex != "" {
matched := []nova.ServerDetail{}
rex, err := regexp.Compile(nameRex)
if err != nil {
- fmt.Printf("cannot compile regexp filter %q: %v\n", nameRex, err)
- // effectively nothing matches
- return nil
+ return nil, err
}
for _, server := range servers {
if rex.MatchString(server.Name) {
@@ -359,26 +360,29 @@ func (n *Nova) matchServers(f filter) []nova.ServerDetail {
}
if len(matched) == 0 {
// no match, here so ignore other results
- return nil
+ return nil, nil
}
servers = matched
}
- return servers
+ return servers, nil
// TODO(dimitern) - 2013-02-11 bug=1121690
// implement FilterFlavor, FilterImage, FilterMarker, FilterLimit and FilterChangesSince
}
// allServers returns a list of all existing servers.
// Filtering is supported, see filter type for more info.
-func (n *Nova) allServers(f filter) []nova.ServerDetail {
+func (n *Nova) allServers(f filter) ([]nova.ServerDetail, error) {
return n.matchServers(f)
}
// allServersAsEntities returns all servers as Entity structs.
// Filtering is supported, see filter type for more info.
-func (n *Nova) allServersAsEntities(f filter) []nova.Entity {
+func (n *Nova) allServersAsEntities(f filter) ([]nova.Entity, error) {
var entities []nova.Entity
- servers := n.matchServers(f)
+ servers, err := n.matchServers(f)
+ if err != nil {
+ return nil, err
+ }
for _, server := range servers {
entities = append(entities, nova.Entity{
Id: server.Id,
@@ -387,7 +391,7 @@ func (n *Nova) allServersAsEntities(f filter) []nova.Entity {
Links: server.Links,
})
}
- return entities
+ return entities, nil
}
// removeServer deletes an existing server.
@@ -612,7 +612,11 @@ func (n *Nova) handleRunServer(body []byte, w http.ResponseWriter, r *http.Reque
Addresses: make(map[string][]nova.IPAddress),
AvailabilityZone: req.Server.AvailabilityZone,
}
- nextServer := len(n.allServers(nil)) + 1
+ servers, err := n.allServers(nil)
+ if err != nil {
+ return err
+ }
+ nextServer := len(servers) + 1
n.buildServerLinks(&server)
// set some IP addresses
addr := fmt.Sprintf("127.10.0.%d", nextServer)
@@ -700,7 +704,10 @@ func (n *Nova) handleServers(w http.ResponseWriter, r *http.Request) error {
}
}
}
- entities := n.allServersAsEntities(f)
+ entities, err := n.allServersAsEntities(f)
+ if err != nil {
+ return err
+ }
if len(entities) == 0 {
entities = []nova.Entity{}
}
@@ -765,7 +772,10 @@ func (n *Nova) handleServersDetail(w http.ResponseWriter, r *http.Request) error
}
}
}
- servers := n.allServers(f)
+ servers, err := n.allServers(f)
+ if err != nil {
+ return err
+ }
if len(servers) == 0 {
servers = []nova.ServerDetail{}
}
@@ -16,6 +16,7 @@ import (
"gopkg.in/goose.v1/nova"
"gopkg.in/goose.v1/testing/httpsuite"
+ "gopkg.in/goose.v1/testservices/hook"
"gopkg.in/goose.v1/testservices/identityservice"
)
@@ -514,7 +515,8 @@ func (s *NovaHTTPSuite) TestGetFlavorsDetail(c *gc.C) {
}
func (s *NovaHTTPSuite) TestGetServers(c *gc.C) {
- entities := s.service.allServersAsEntities(nil)
+ entities, err := s.service.allServersAsEntities(nil)
+ c.Assert(err, gc.IsNil)
c.Assert(entities, gc.HasLen, 0)
var expected struct {
Servers []nova.Entity
@@ -535,7 +537,8 @@ func (s *NovaHTTPSuite) TestGetServers(c *gc.C) {
c.Assert(err, gc.IsNil)
defer s.service.removeServer(server.Id)
}
- entities = s.service.allServersAsEntities(nil)
+ entities, err = s.service.allServersAsEntities(nil)
+ c.Assert(err, gc.IsNil)
resp, err = s.authRequest("GET", "/servers", nil, nil)
c.Assert(err, gc.IsNil)
c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
@@ -556,7 +559,8 @@ func (s *NovaHTTPSuite) TestGetServers(c *gc.C) {
}
func (s *NovaHTTPSuite) TestGetServersWithFilters(c *gc.C) {
- entities := s.service.allServersAsEntities(nil)
+ entities, err := s.service.allServersAsEntities(nil)
+ c.Assert(err, gc.IsNil)
c.Assert(entities, gc.HasLen, 0)
var expected struct {
Servers []nova.Entity
@@ -588,6 +592,46 @@ func (s *NovaHTTPSuite) TestGetServersWithFilters(c *gc.C) {
c.Assert(expected.Servers[0].Name, gc.Equals, servers[0].Name)
}
+func (s *NovaHTTPSuite) TestGetServersWithBadFilter(c *gc.C) {
+ url := "/servers?name=(server"
+ resp, err := s.authRequest("GET", url, nil, nil)
+ c.Assert(err, gc.IsNil)
+ c.Assert(resp.StatusCode, gc.Equals, http.StatusInternalServerError)
+ type novaError struct {
+ Code int
+ Message string
+ }
+ var expected struct {
+ novaError `json:"computeFault"`
+ }
+ assertJSON(c, resp, &expected)
+ c.Check(expected.Code, gc.Equals, 500)
+ c.Check(expected.Message, gc.Matches, `error parsing.*\(server.*`)
+}
+
+func (s *NovaHTTPSuite) TestGetServersPatchMatch(c *gc.C) {
+ cleanup := s.service.RegisterControlPoint(
+ "matchServers",
+ func(sc hook.ServiceControl, args ...interface{}) error {
+ return fmt.Errorf("Unexpected error")
+ },
+ )
+ defer cleanup()
+ resp, err := s.authRequest("GET", "/servers", nil, nil)
+ c.Assert(err, gc.IsNil)
+ c.Assert(resp.StatusCode, gc.Equals, http.StatusInternalServerError)
+ type novaError struct {
+ Code int
+ Message string
+ }
+ var expected struct {
+ novaError `json:"computeFault"`
+ }
+ assertJSON(c, resp, &expected)
+ c.Check(expected.Code, gc.Equals, 500)
+ c.Check(expected.Message, gc.Equals, "Unexpected error")
+}
+
func (s *NovaHTTPSuite) TestNewUUID(c *gc.C) {
uuid, err := newUUID()
c.Assert(err, gc.IsNil)
@@ -619,7 +663,8 @@ func (s *NovaHTTPSuite) assertAddresses(c *gc.C, serverId string) {
}
func (s *NovaHTTPSuite) TestRunServer(c *gc.C) {
- entities := s.service.allServersAsEntities(nil)
+ entities, err := s.service.allServersAsEntities(nil)
+ c.Assert(err, gc.IsNil)
c.Assert(entities, gc.HasLen, 0)
var req struct {
Server struct {
@@ -712,7 +757,8 @@ func (s *NovaHTTPSuite) TestDeleteServer(c *gc.C) {
}
func (s *NovaHTTPSuite) TestGetServersDetail(c *gc.C) {
- servers := s.service.allServers(nil)
+ servers, err := s.service.allServers(nil)
+ c.Assert(err, gc.IsNil)
c.Assert(servers, gc.HasLen, 0)
var expected struct {
Servers []nova.ServerDetail `json:"servers"`
@@ -748,7 +794,8 @@ func (s *NovaHTTPSuite) TestGetServersDetail(c *gc.C) {
}
func (s *NovaHTTPSuite) TestGetServersDetailWithFilters(c *gc.C) {
- servers := s.service.allServers(nil)
+ servers, err := s.service.allServers(nil)
+ c.Assert(err, gc.IsNil)
c.Assert(servers, gc.HasLen, 0)
var expected struct {
Servers []nova.ServerDetail `json:"servers"`
Oops, something went wrong.