|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "e2e_tests/tester" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/scaleway/scaleway-sdk-go/api/instance/v1" |
| 9 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 10 | +) |
| 11 | + |
| 12 | +var _ tester.PackerCheck = (*ImageCheck)(nil) |
| 13 | + |
| 14 | +func Image(zone scw.Zone, name string) *ImageCheck { |
| 15 | + return &ImageCheck{ |
| 16 | + zone: zone, |
| 17 | + imageName: name, |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +type ImageCheck struct { |
| 22 | + zone scw.Zone |
| 23 | + imageName string |
| 24 | + |
| 25 | + rootVolumeType *string |
| 26 | +} |
| 27 | + |
| 28 | +func (c *ImageCheck) RootVolumeType(rootVolumeType string) *ImageCheck { |
| 29 | + c.rootVolumeType = &rootVolumeType |
| 30 | + |
| 31 | + return c |
| 32 | +} |
| 33 | + |
| 34 | +func (c *ImageCheck) Check(ctx context.Context) error { |
| 35 | + testCtx := tester.ExtractCtx(ctx) |
| 36 | + api := instance.NewAPI(testCtx.ScwClient) |
| 37 | + |
| 38 | + resp, err := api.ListImages(&instance.ListImagesRequest{ |
| 39 | + Name: &c.imageName, |
| 40 | + Zone: c.zone, |
| 41 | + }, scw.WithAllPages(), scw.WithContext(ctx)) |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("failed to list images: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + if len(resp.Images) == 0 { |
| 47 | + return fmt.Errorf("image %s not found", c.imageName) |
| 48 | + } |
| 49 | + |
| 50 | + if len(resp.Images) > 1 { |
| 51 | + return fmt.Errorf("multiple images found with name %s", c.imageName) |
| 52 | + } |
| 53 | + |
| 54 | + image := resp.Images[0] |
| 55 | + |
| 56 | + if image.Name != c.imageName { |
| 57 | + return fmt.Errorf("image name %s does not match expected %s", image.Name, c.imageName) |
| 58 | + } |
| 59 | + |
| 60 | + if c.rootVolumeType != nil { |
| 61 | + if string(image.RootVolume.VolumeType) != *c.rootVolumeType { |
| 62 | + return fmt.Errorf("image root volume type %s does not match expected %s", image.RootVolume.VolumeType, *c.rootVolumeType) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return nil |
| 67 | +} |
0 commit comments