Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Use a lxc client library helper to detect Not Found errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wallyworld committed Sep 8, 2021
1 parent 6bcbb0b commit 030a669
Show file tree
Hide file tree
Showing 10 changed files with 1,095 additions and 645 deletions.
3 changes: 2 additions & 1 deletion container/lxd/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package lxd_test

import (
"errors"
"net/http"
"time"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -441,7 +442,7 @@ func (s *containerSuite) TestRemoveContainersSuccessWithNotFound(c *gc.C) {
exp.DeleteContainer("c1").Return(deleteOp, nil)
exp.GetContainerState("c2").Return(&api.ContainerState{StatusCode: api.Started}, lxdtesting.ETag, nil)
exp.UpdateContainerState("c2", stopReq, lxdtesting.ETag).Return(stopOp, nil)
exp.DeleteContainer("c2").Return(deleteOp, errors.New("not found"))
exp.DeleteContainer("c2").Return(deleteOp, api.StatusErrorf(http.StatusNotFound, ""))

jujuSvr, err := lxd.NewServer(cSvr)
c.Assert(err, jc.ErrorIsNil)
Expand Down
9 changes: 5 additions & 4 deletions container/lxd/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package lxd_test

import (
"errors"
"net/http"

"github.com/golang/mock/gomock"
jc "github.com/juju/testing/checkers"
Expand Down Expand Up @@ -77,7 +78,7 @@ func (s *imageSuite) TestFindImageLocalServerUnknownSeries(c *gc.C) {
ctrl := gomock.NewController(c)
defer ctrl.Finish()
iSvr := s.NewMockServer(ctrl)
iSvr.EXPECT().GetImageAlias("juju/pldlinux/"+s.Arch()).Return(nil, lxdtesting.ETag, errors.New("not found"))
iSvr.EXPECT().GetImageAlias("juju/pldlinux/"+s.Arch()).Return(nil, lxdtesting.ETag, lxdapi.StatusErrorf(http.StatusNotFound, ""))

jujuSvr, err := lxd.NewServer(iSvr)
c.Assert(err, jc.ErrorIsNil)
Expand All @@ -101,8 +102,8 @@ func (s *imageSuite) TestFindImageRemoteServers(c *gc.C) {
image := lxdapi.Image{Filename: "this-is-our-image"}
alias := lxdapi.ImageAliasesEntry{ImageAliasesEntryPut: lxdapi.ImageAliasesEntryPut{Target: "foo-remote-target"}}
gomock.InOrder(
iSvr.EXPECT().GetImageAlias("juju/xenial/"+s.Arch()).Return(nil, lxdtesting.ETag, errors.New("not found")),
rSvr1.EXPECT().GetImageAlias("xenial/"+s.Arch()).Return(nil, lxdtesting.ETag, errors.New("not found")),
iSvr.EXPECT().GetImageAlias("juju/xenial/"+s.Arch()).Return(nil, lxdtesting.ETag, lxdapi.StatusErrorf(http.StatusNotFound, "")),
rSvr1.EXPECT().GetImageAlias("xenial/"+s.Arch()).Return(nil, lxdtesting.ETag, lxdapi.StatusErrorf(http.StatusNotFound, "")),
rSvr2.EXPECT().GetImageAlias("xenial/"+s.Arch()).Return(&alias, lxdtesting.ETag, nil),
rSvr2.EXPECT().GetImage("foo-remote-target").Return(&image, lxdtesting.ETag, nil),
)
Expand Down Expand Up @@ -170,7 +171,7 @@ func (s *imageSuite) TestFindImageRemoteServersNotFound(c *gc.C) {

alias := lxdapi.ImageAliasesEntry{ImageAliasesEntryPut: lxdapi.ImageAliasesEntryPut{Target: "foo-remote-target"}}
gomock.InOrder(
iSvr.EXPECT().GetImageAlias("juju/bionic/"+s.Arch()).Return(nil, lxdtesting.ETag, errors.New("not found")),
iSvr.EXPECT().GetImageAlias("juju/bionic/"+s.Arch()).Return(nil, lxdtesting.ETag, lxdapi.StatusErrorf(http.StatusNotFound, "")),
rSvr.EXPECT().GetImageAlias("bionic/"+s.Arch()).Return(&alias, lxdtesting.ETag, nil),
rSvr.EXPECT().GetImage("foo-remote-target").Return(
nil, lxdtesting.ETag, errors.New("failed to retrieve image")),
Expand Down
29 changes: 15 additions & 14 deletions container/lxd/mocks/clock_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions container/lxd/mocks/snap_manager_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion container/lxd/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package lxd_test

import (
"errors"
"net/http"

"github.com/golang/mock/gomock"
jc "github.com/juju/testing/checkers"
Expand Down Expand Up @@ -271,7 +272,7 @@ func (s *networkSuite) TestVerifyNetworkDeviceNotPresentCreated(c *gc.C) {
NetworkPut: lxdapi.NetworkPut{Config: netConf},
}
gomock.InOrder(
cSvr.EXPECT().GetNetwork(network.DefaultLXDBridge).Return(nil, "", errors.New("not found")),
cSvr.EXPECT().GetNetwork(network.DefaultLXDBridge).Return(nil, "", lxdapi.StatusErrorf(http.StatusNotFound, "")),
cSvr.EXPECT().CreateNetwork(netCreateReq).Return(nil),
cSvr.EXPECT().GetNetwork(network.DefaultLXDBridge).Return(newNet, "", nil),
cSvr.EXPECT().UpdateProfile("default", defaultLegacyProfileWithNIC().Writable(), lxdtesting.ETag).Return(nil),
Expand Down
5 changes: 4 additions & 1 deletion container/lxd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package lxd

import (
"net/http"

"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/utils/v2/arch"
Expand Down Expand Up @@ -319,5 +321,6 @@ func (s *Server) SupportedArches() []string {
// IsLXDNotFound checks if an error from the LXD API indicates that a requested
// entity was not found.
func IsLXDNotFound(err error) bool {
return err != nil && err.Error() == "not found"
_, result := api.StatusErrorMatch(err, http.StatusNotFound)
return result
}
Loading

0 comments on commit 030a669

Please sign in to comment.