Skip to content

Commit 4b41a25

Browse files
committed
test: add end to end tests
1 parent 0753293 commit 4b41a25

File tree

10 files changed

+256
-0
lines changed

10 files changed

+256
-0
lines changed

e2e_tests/GNUmakefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
build-binary:
2+
make -C .. build
3+
4+
link-binary:
5+
ln -fs ../packer-plugin-scaleway
6+
7+
test-simple: build-binary link-binary
8+
packer build ./simple/build_scaleway.pkr.hcl
9+
go run ./simple/test.go
10+
11+
test:

e2e_tests/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# End to end tests
2+
3+
This folder contains packer projects with each one go binaries that will check the behavior of the packer plugin.
4+
5+
## Test environment
6+
7+
- Tests should run in an empty project. Tests will check for non deleted resources and a non-empty project will interfere with the checks.
8+
-
9+

e2e_tests/checks/image.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}

e2e_tests/checks/volume.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package checks
2+
3+
import (
4+
"context"
5+
"e2e_tests/tester"
6+
"fmt"
7+
8+
block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
9+
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
11+
)
12+
13+
var _ tester.PackerCheck = (*NoVolumesCheck)(nil)
14+
15+
type NoVolumesCheck struct {
16+
zone scw.Zone
17+
}
18+
19+
func (c *NoVolumesCheck) Check(ctx context.Context) error {
20+
testCtx := tester.ExtractCtx(ctx)
21+
instanceAPI := instance.NewAPI(testCtx.ScwClient)
22+
blockAPI := block.NewAPI(testCtx.ScwClient)
23+
24+
resp, err := instanceAPI.ListVolumes(&instance.ListVolumesRequest{
25+
Zone: c.zone,
26+
}, scw.WithContext(ctx))
27+
if err != nil {
28+
return fmt.Errorf("failed to list instance volumes: %w", err)
29+
}
30+
31+
if len(resp.Volumes) != 0 {
32+
return fmt.Errorf("expected 0 instance volumes, got %d", len(resp.Volumes))
33+
}
34+
35+
blockResp, err := blockAPI.ListVolumes(&block.ListVolumesRequest{
36+
Zone: c.zone,
37+
}, scw.WithContext(ctx))
38+
if err != nil {
39+
return fmt.Errorf("failed to list block volumes: %w", err)
40+
}
41+
42+
if len(blockResp.Volumes) != 0 {
43+
return fmt.Errorf("expected 0 block volumes, got %d", len(blockResp.Volumes))
44+
}
45+
46+
return nil
47+
}
48+
49+
// NoVolume checks that the current project does not contain any volume, block or instance.
50+
func NoVolume(zone scw.Zone) *NoVolumesCheck {
51+
return &NoVolumesCheck{
52+
zone: zone,
53+
}
54+
}

e2e_tests/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module e2e_tests
2+
3+
go 1.23.4
4+
5+
require github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30
6+
7+
require gopkg.in/yaml.v2 v2.4.0 // indirect

e2e_tests/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30 h1:yoKAVkEVwAqbGbR8n87rHQ1dulL25rKloGadb3vm770=
2+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30/go.mod h1:sH0u6fq6x4R5M7WxkoQFY/o7UaiItec0o1LinLCJNq8=
3+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
4+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
6+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
packer {
2+
required_plugins {
3+
}
4+
}
5+
6+
source "scaleway" "basic" {
7+
commercial_type = "PRO2-XXS"
8+
zone = "fr-par-1"
9+
image = "ubuntu_jammy"
10+
image_name = "temp-build-packer"
11+
ssh_username = "root"
12+
remove_volume = true
13+
}
14+
15+
build {
16+
sources = ["source.scaleway.basic"]
17+
}

e2e_tests/simple/test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"e2e_tests/checks"
6+
"e2e_tests/tester"
7+
8+
"github.com/scaleway/scaleway-sdk-go/scw"
9+
)
10+
11+
func main() {
12+
zone := scw.ZoneFrPar1
13+
14+
tester.Run(context.Background(),
15+
checks.Image(zone, "temp-build-packer").
16+
RootVolumeType("unified"),
17+
checks.NoVolume(zone),
18+
)
19+
}

e2e_tests/tester/checks.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package tester
2+
3+
import (
4+
"context"
5+
)
6+
7+
// PackerCheck represents a check for a scaleway resource
8+
type PackerCheck interface {
9+
Check(ctx context.Context) error
10+
}

e2e_tests/tester/tester.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package tester
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"github.com/scaleway/scaleway-sdk-go/scw"
10+
)
11+
12+
const PackerCtxKey = "PACKER_CTX_KEY"
13+
14+
type PackerCtx struct {
15+
ScwClient *scw.Client
16+
}
17+
18+
func NewContext(ctx context.Context) (context.Context, error) {
19+
cfg, err := scw.LoadConfig()
20+
if err != nil {
21+
return nil, err
22+
}
23+
activeProfile, err := cfg.GetActiveProfile()
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
profile := scw.MergeProfiles(activeProfile, scw.LoadEnvProfile())
29+
client, err := scw.NewClient(scw.WithProfile(profile))
30+
if err != nil {
31+
return nil, err
32+
}
33+
return context.WithValue(ctx, PackerCtxKey, &PackerCtx{client}), nil
34+
}
35+
36+
func ExtractCtx(ctx context.Context) *PackerCtx {
37+
return ctx.Value(PackerCtxKey).(*PackerCtx)
38+
}
39+
40+
func Run(ctx context.Context, packerChecks ...PackerCheck) {
41+
log.Println("Running tests...")
42+
ctx, err := NewContext(ctx)
43+
if err != nil {
44+
panic(err)
45+
}
46+
47+
for i, check := range packerChecks {
48+
log.Println("Running test", i)
49+
err := check.Check(ctx)
50+
if err != nil {
51+
log.Fatalln(fmt.Sprintf("Packer check %d failed:", i), err)
52+
}
53+
}
54+
55+
os.Exit(0)
56+
}

0 commit comments

Comments
 (0)