Skip to content

Commit

Permalink
Work around MAAS 1.9.4 bug
Browse files Browse the repository at this point in the history
Handle a JSON deserialisation error caused by the API version requiring
login in MAAS 1.9.4 - this is fixed in 1.9.5, but we shouldn't break
this for 1.9.4 users right now.

MAAS bug: https://bugs.launchpad.net/maas/+bug/1583715
Fixes https://bugs.launchpad.net/juju/+bug/1680046
  • Loading branch information
babbageclunk committed Apr 6, 2017
1 parent eada3f3 commit 5428ae9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,14 @@ func indicatesUnsupportedVersion(err error) bool {
code := serverErr.StatusCode
return code == http.StatusNotFound || code == http.StatusGone
}
// Workaround for bug in MAAS 1.9.4 - instead of a 404 we get a
// redirect to the HTML login page, which doesn't parse as JSON.
// https://bugs.launchpad.net/maas/+bug/1583715
if syntaxErr, ok := errors.Cause(err).(*json.SyntaxError); ok {
logger.Criticalf("%#v", *syntaxErr)
message := "invalid character '<' looking for beginning of value"
return syntaxErr.Offset == 1 && syntaxErr.Error() == message
}
return false
}

Expand Down
19 changes: 19 additions & 0 deletions controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,25 @@ func (s *controllerSuite) TestNewController404(c *gc.C) {
c.Assert(err, jc.Satisfies, IsUnsupportedVersionError)
}

func (s *controllerSuite) TestNewControllerWith194Bug(c *gc.C) {
// 1.9.4 has a bug where if you ask for /api/2.0/version/ without
// being logged in (rather than OAuth connection) it redirects you
// to the login page. This is fixed in 1.9.5, but we should work
// around it anyway. https://bugs.launchpad.net/maas/+bug/1583715
server := NewSimpleServer()
server.AddGetResponse("/api/2.0/users/?op=whoami", http.StatusOK, "the answer to all your prayers")
server.AddGetResponse("/api/2.0/version/", http.StatusOK, "<html><head>")
server.Start()
defer server.Close()

controller, err := NewController(ControllerArgs{
BaseURL: server.URL,
APIKey: "fake:as:key",
})
c.Assert(controller, gc.IsNil)
c.Assert(err, jc.Satisfies, IsUnsupportedVersionError)
}

func (s *controllerSuite) TestBootResources(c *gc.C) {
controller := s.getController(c)
resources, err := controller.BootResources()
Expand Down

0 comments on commit 5428ae9

Please sign in to comment.