github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/container/lxd/image_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lxd_test
     5  
     6  import (
     7  	"errors"
     8  
     9  	"github.com/golang/mock/gomock"
    10  	jc "github.com/juju/testing/checkers"
    11  	lxdclient "github.com/lxc/lxd/client"
    12  	lxdapi "github.com/lxc/lxd/shared/api"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/container/lxd"
    16  	lxdtesting "github.com/juju/juju/container/lxd/testing"
    17  )
    18  
    19  var _ = gc.Suite(&imageSuite{})
    20  
    21  type imageSuite struct {
    22  	lxdtesting.BaseSuite
    23  }
    24  
    25  func (s *imageSuite) patch(remotes map[string]lxdclient.ImageServer) {
    26  	lxd.PatchConnectRemote(s, remotes)
    27  }
    28  
    29  func (s *imageSuite) TestCopyImageUsesPassedCallback(c *gc.C) {
    30  	ctrl := gomock.NewController(c)
    31  	defer ctrl.Finish()
    32  	iSvr := s.NewMockServer(ctrl)
    33  
    34  	copyOp := lxdtesting.NewMockRemoteOperation(ctrl)
    35  	copyOp.EXPECT().Wait().Return(nil).AnyTimes()
    36  	copyOp.EXPECT().GetTarget().Return(&lxdapi.Operation{StatusCode: lxdapi.Success}, nil)
    37  	copyOp.EXPECT().AddHandler(gomock.Any()).Return(nil, nil)
    38  
    39  	image := lxdapi.Image{Filename: "this-is-our-image"}
    40  	aliases := []lxdapi.ImageAlias{{Name: "local/image/alias"}}
    41  	req := &lxdclient.ImageCopyArgs{Aliases: aliases}
    42  	iSvr.EXPECT().CopyImage(iSvr, image, req).Return(copyOp, nil)
    43  
    44  	jujuSvr, err := lxd.NewServer(iSvr)
    45  	c.Assert(err, jc.ErrorIsNil)
    46  
    47  	sourced := lxd.SourcedImage{
    48  		Image:     &image,
    49  		LXDServer: iSvr,
    50  	}
    51  	err = jujuSvr.CopyRemoteImage(sourced, []string{"local/image/alias"}, lxdtesting.NoOpCallback)
    52  	c.Assert(err, jc.ErrorIsNil)
    53  }
    54  
    55  func (s *imageSuite) TestFindImageLocalServer(c *gc.C) {
    56  	ctrl := gomock.NewController(c)
    57  	defer ctrl.Finish()
    58  	iSvr := s.NewMockServer(ctrl)
    59  
    60  	alias := &lxdapi.ImageAliasesEntry{ImageAliasesEntryPut: lxdapi.ImageAliasesEntryPut{Target: "foo-target"}}
    61  	image := lxdapi.Image{Filename: "this-is-our-image"}
    62  	gomock.InOrder(
    63  		iSvr.EXPECT().GetImageAlias("juju/xenial/"+s.Arch()).Return(alias, lxdtesting.ETag, nil),
    64  		iSvr.EXPECT().GetImage("foo-target").Return(&image, lxdtesting.ETag, nil),
    65  	)
    66  
    67  	jujuSvr, err := lxd.NewServer(iSvr)
    68  	c.Assert(err, jc.ErrorIsNil)
    69  
    70  	found, err := jujuSvr.FindImage("xenial", s.Arch(), []lxd.ServerSpec{{}}, false, nil)
    71  	c.Assert(err, jc.ErrorIsNil)
    72  	c.Check(found.LXDServer, gc.Equals, iSvr)
    73  	c.Check(*found.Image, gc.DeepEquals, image)
    74  }
    75  
    76  func (s *imageSuite) TestFindImageLocalServerUnknownSeries(c *gc.C) {
    77  	ctrl := gomock.NewController(c)
    78  	defer ctrl.Finish()
    79  	iSvr := s.NewMockServer(ctrl)
    80  	iSvr.EXPECT().GetImageAlias("juju/pldlinux/"+s.Arch()).Return(nil, lxdtesting.ETag, nil)
    81  
    82  	jujuSvr, err := lxd.NewServer(iSvr)
    83  	c.Assert(err, jc.ErrorIsNil)
    84  
    85  	_, err = jujuSvr.FindImage("pldlinux", s.Arch(), []lxd.ServerSpec{{}}, false, nil)
    86  	c.Check(err, gc.ErrorMatches, `.*series: "pldlinux".*`)
    87  }
    88  
    89  func (s *imageSuite) TestFindImageRemoteServers(c *gc.C) {
    90  	ctrl := gomock.NewController(c)
    91  	defer ctrl.Finish()
    92  	iSvr := s.NewMockServer(ctrl)
    93  
    94  	rSvr1 := lxdtesting.NewMockImageServer(ctrl)
    95  	rSvr2 := lxdtesting.NewMockImageServer(ctrl)
    96  	s.patch(map[string]lxdclient.ImageServer{
    97  		"server-that-wont-work": rSvr1,
    98  		"server-that-has-image": rSvr2,
    99  	})
   100  
   101  	image := lxdapi.Image{Filename: "this-is-our-image"}
   102  	alias := lxdapi.ImageAliasesEntry{ImageAliasesEntryPut: lxdapi.ImageAliasesEntryPut{Target: "foo-remote-target"}}
   103  	gomock.InOrder(
   104  		iSvr.EXPECT().GetImageAlias("juju/xenial/"+s.Arch()).Return(nil, lxdtesting.ETag, nil),
   105  		rSvr1.EXPECT().GetImageAlias("xenial/"+s.Arch()).Return(nil, lxdtesting.ETag, nil),
   106  		rSvr2.EXPECT().GetImageAlias("xenial/"+s.Arch()).Return(&alias, lxdtesting.ETag, nil),
   107  		rSvr2.EXPECT().GetImage("foo-remote-target").Return(&image, lxdtesting.ETag, nil),
   108  	)
   109  
   110  	jujuSvr, err := lxd.NewServer(iSvr)
   111  	c.Assert(err, jc.ErrorIsNil)
   112  
   113  	remotes := []lxd.ServerSpec{
   114  		{Name: "server-that-wont-work", Protocol: lxd.LXDProtocol},
   115  		{Name: "server-that-has-image", Protocol: lxd.SimpleStreamsProtocol},
   116  		{Name: "server-that-should-not-be-touched", Protocol: lxd.LXDProtocol},
   117  	}
   118  	found, err := jujuSvr.FindImage("xenial", s.Arch(), remotes, false, nil)
   119  	c.Assert(err, jc.ErrorIsNil)
   120  	c.Check(found.LXDServer, gc.Equals, rSvr2)
   121  	c.Check(*found.Image, gc.DeepEquals, image)
   122  }
   123  
   124  func (s *imageSuite) TestFindImageRemoteServersCopyLocalNoCallback(c *gc.C) {
   125  	ctrl := gomock.NewController(c)
   126  	defer ctrl.Finish()
   127  	iSvr := s.NewMockServer(ctrl)
   128  
   129  	rSvr := lxdtesting.NewMockImageServer(ctrl)
   130  	s.patch(map[string]lxdclient.ImageServer{
   131  		"server-that-has-image": rSvr,
   132  	})
   133  
   134  	copyOp := lxdtesting.NewMockRemoteOperation(ctrl)
   135  	copyOp.EXPECT().Wait().Return(nil).AnyTimes()
   136  	copyOp.EXPECT().GetTarget().Return(&lxdapi.Operation{StatusCode: lxdapi.Success}, nil)
   137  
   138  	localAlias := "juju/xenial/" + s.Arch()
   139  	image := lxdapi.Image{Filename: "this-is-our-image"}
   140  	alias := lxdapi.ImageAliasesEntry{ImageAliasesEntryPut: lxdapi.ImageAliasesEntryPut{Target: "foo-remote-target"}}
   141  	copyReq := &lxdclient.ImageCopyArgs{Aliases: []lxdapi.ImageAlias{{Name: localAlias}}}
   142  	gomock.InOrder(
   143  		iSvr.EXPECT().GetImageAlias(localAlias).Return(nil, lxdtesting.ETag, nil),
   144  		rSvr.EXPECT().GetImageAlias("xenial/"+s.Arch()).Return(&alias, lxdtesting.ETag, nil),
   145  		rSvr.EXPECT().GetImage("foo-remote-target").Return(&image, lxdtesting.ETag, nil),
   146  		iSvr.EXPECT().CopyImage(rSvr, image, copyReq).Return(copyOp, nil),
   147  	)
   148  
   149  	jujuSvr, err := lxd.NewServer(iSvr)
   150  	c.Assert(err, jc.ErrorIsNil)
   151  
   152  	remotes := []lxd.ServerSpec{
   153  		{Name: "server-that-has-image", Protocol: lxd.SimpleStreamsProtocol},
   154  	}
   155  	found, err := jujuSvr.FindImage("xenial", s.Arch(), remotes, true, nil)
   156  	c.Assert(err, jc.ErrorIsNil)
   157  	c.Check(found.LXDServer, gc.Equals, iSvr)
   158  	c.Check(*found.Image, gc.DeepEquals, image)
   159  }
   160  
   161  func (s *imageSuite) TestFindImageRemoteServersNotFound(c *gc.C) {
   162  	ctrl := gomock.NewController(c)
   163  	defer ctrl.Finish()
   164  	iSvr := s.NewMockServer(ctrl)
   165  
   166  	rSvr := lxdtesting.NewMockImageServer(ctrl)
   167  	s.patch(map[string]lxdclient.ImageServer{
   168  		"server-that-has-image": rSvr,
   169  	})
   170  
   171  	alias := lxdapi.ImageAliasesEntry{ImageAliasesEntryPut: lxdapi.ImageAliasesEntryPut{Target: "foo-remote-target"}}
   172  	gomock.InOrder(
   173  		iSvr.EXPECT().GetImageAlias("juju/bionic/"+s.Arch()).Return(nil, lxdtesting.ETag, nil),
   174  		rSvr.EXPECT().GetImageAlias("bionic/"+s.Arch()).Return(&alias, lxdtesting.ETag, nil),
   175  		rSvr.EXPECT().GetImage("foo-remote-target").Return(
   176  			nil, lxdtesting.ETag, errors.New("failed to retrieve image")),
   177  	)
   178  
   179  	jujuSvr, err := lxd.NewServer(iSvr)
   180  	c.Assert(err, jc.ErrorIsNil)
   181  
   182  	remotes := []lxd.ServerSpec{{Name: "server-that-has-image", Protocol: lxd.SimpleStreamsProtocol}}
   183  	_, err = jujuSvr.FindImage("bionic", s.Arch(), remotes, false, nil)
   184  	c.Assert(err, gc.ErrorMatches, ".*failed to retrieve image.*")
   185  }
   186  
   187  func (s *imageSuite) TestSeriesRemoteAliasesNotSupported(c *gc.C) {
   188  	_, err := lxd.SeriesRemoteAliases("centos7", "arm64")
   189  	c.Assert(err, gc.ErrorMatches, `series "centos7" not supported`)
   190  
   191  	_, err = lxd.SeriesRemoteAliases("opensuseleap", "s390x")
   192  	c.Assert(err, gc.ErrorMatches, `series "opensuseleap" not supported`)
   193  }