Skip to content

Commit

Permalink
spread: add tests that test/illustrate how openstaack.FindImage() works
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Sep 25, 2023
1 parent 6c293f0 commit 2d98f1b
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions spread/openstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,20 @@ func makeGlanceImageDetails(imgs []string) []glance.ImageDetail {
return out
}

func (s *openstackFindImageSuite) TestOpenstackFindImageHappy(c *C) {
var fakeOpenstackImageListMadeUp = []string{
"ubuntu-22.04",
"ubuntu-20.04",
"fedora-38",
}

func (s *openstackFindImageSuite) TestOpenstackFindImage(c *C) {
for _, tc := range []struct {
imageName string
availableImages []string
expectFind bool
}{
{"ubuntu-22.04", []string{"fedora-35", "tumbleweed-1"}, false},
{"ubuntu-22.04", []string{"fedora-35", "ubuntu-22.04"}, true},
{"ubuntu-14.04", fakeOpenstackImageListMadeUp, false},
{"ubuntu-22.04", fakeOpenstackImageListMadeUp, true},
} {
s.fakeImageClient.res = makeGlanceImageDetails(tc.availableImages)
idt, err := s.opst.FindImage(tc.imageName)
Expand All @@ -151,3 +157,40 @@ func (s *openstackFindImageSuite) TestOpenstackFindImageHappy(c *C) {
}
}
}

var fakeOpenstackImageList = []string{
// from real "openstack image list" output but put here unordered
"auto-sync/ubuntu-bionic-18.04-amd64-server-20210928-disk1.img",
"auto-sync/ubuntu-bionic-18.04-amd64-server-20230530-disk1.img",
"auto-sync/ubuntu-bionic-18.04-amd64-server-20210614-disk1.img",
// made-up: contains search
"auto-sync/ubuntu-22.04-something",
// made-up
"fedora-35",
"tumbleweed-1",
}

func (s *openstackFindImageSuite) TestOpenstackFindImageComplex(c *C) {
for _, tc := range []struct {
imageName string
availableImages []string
expectedImageName string
}{
// trivial
{"fedora-35", fakeOpenstackImageList, "fedora-35"},
// simple string match of name
{"ubuntu-22.04", fakeOpenstackImageList, "auto-sync/ubuntu-22.04-something"},
// complex sorting based on date of the images
{"ubuntu-bionic-18.04-amd64", fakeOpenstackImageList, "auto-sync/ubuntu-bionic-18.04-amd64-server-20210928-disk1.img"},
} {
s.fakeImageClient.res = makeGlanceImageDetails(tc.availableImages)
idt, err := s.opst.FindImage(tc.imageName)
if tc.expectedImageName != "" {
c.Check(err, IsNil, Commentf("%s", tc))
c.Check(idt.Name, Equals, tc.expectedImageName)
} else {

c.Check(err, ErrorMatches, `cannot find matching image for `+tc.imageName, Commentf("%s", tc))
}
}
}

0 comments on commit 2d98f1b

Please sign in to comment.