generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: add end to end tests * add test script * add complete test delete resources across all zones add new asserts * add more asserts * Use go tests instead of a main * run packer from go tests and merge *_test.go with packer files * add cassettes to test checks * add cassettes to packer * fix execution with cassettes * improve doc * add cassettes * run e2e tests in CI * fix tests in CI * lint * split test and e2e_test in makefile * add e2e test to ci * skip broken test
- Loading branch information
Showing
23 changed files
with
3,298 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
build-binary: | ||
make -C .. build | ||
|
||
install-plugin: | ||
packer plugins install -path ../packer-plugin-scaleway "github.com/scaleway/scaleway" | ||
|
||
test: build-binary install-plugin | ||
./test.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# End-to-end tests | ||
|
||
This folder contains scripts and a makefile to help run end to end tests. | ||
|
||
## Write a test | ||
|
||
Create a new test in `../internal/tests`. | ||
|
||
## Cassettes | ||
|
||
To run easily in a CI, tests can be run while recording http requests. This allows pipelines to test without token by using recorded requests. | ||
|
||
- To record cassettes, you must set `PACKER_UPDATE_CASSETTES=true`. | ||
- To use recorded cassettes, you must set `PACKER_UPDATE_CASSETTES=false` | ||
|
||
## Running tests | ||
|
||
`PACKER_UPDATE_CASSETTES=true make test` | ||
|
||
Test script will create a new project for you then run all tests before cleaning up the project | ||
|
||
## Test environment | ||
|
||
Tests should run in an empty project. Tests will check for non deleted resources and a non-empty project will interfere with the checks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env bash | ||
|
||
## This script will clean a project and its resources. | ||
|
||
if [ "$#" -ne 1 ] | ||
then | ||
echo "Usage: $0 [project-id]" | ||
exit 1 | ||
fi | ||
|
||
export SCW_DEFAULT_PROJECT_ID=$1 | ||
|
||
# Clean images | ||
scw instance image list zone=all project-id="$SCW_DEFAULT_PROJECT_ID" -otemplate="zone={{.Zone}} {{.ID}}" | xargs -L1 -P1 scw instance image delete with-snapshots=true | ||
# Clean volumes | ||
scw instance volume list zone=all project-id="$SCW_DEFAULT_PROJECT_ID" -otemplate="zone={{.Zone}} {{.ID}}" | xargs -L1 -P1 scw instance volume delete | ||
|
||
# A security group will be created alongside the server during packer execution. | ||
# We need to delete this security group before deleting the project | ||
scw instance security-group list zone=all project-id="$SCW_DEFAULT_PROJECT_ID" -otemplate="zone={{.Zone}} {{.ID}}" | xargs -L1 -P1 scw instance security-group delete | ||
|
||
scw account project delete project-id="$SCW_DEFAULT_PROJECT_ID" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env bash | ||
|
||
## This script will: | ||
# - create a temporary project, configure it in environment | ||
# - Run packer e2e tests | ||
# - Tries to delete project | ||
|
||
if [ "$PACKER_UPDATE_CASSETTES" == "true" ] | ||
then | ||
PROJECT_ID=$(scw account project create -otemplate="{{.ID}}") | ||
export SCW_DEFAULT_PROJECT_ID=$PROJECT_ID | ||
|
||
echo Running tests with new project $SCW_DEFAULT_PROJECT_ID | ||
else | ||
export SCW_ACCESS_KEY=SCWXXXXXXXXXXXXXFAKE | ||
export SCW_SECRET_KEY=11111111-1111-1111-1111-111111111111 | ||
export SCW_DEFAULT_PROJECT_ID=11111111-1111-1111-1111-111111111111 | ||
echo Using cassettes, no test project was created | ||
fi | ||
|
||
go test ../internal/tests -v | ||
TEST_RESULT=$? | ||
|
||
if [ "$PACKER_UPDATE_CASSETTES" == "true" ] | ||
then | ||
./clean.sh $SCW_DEFAULT_PROJECT_ID | ||
fi | ||
|
||
exit $TEST_RESULT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env bash | ||
|
||
export SCW_DEFAULT_PROJECT_ID=c276a890-0f62-4d02-a5c5-9f86d615a029 | ||
|
||
scw instance image list zone=all project-id="$SCW_DEFAULT_PROJECT_ID" -otemplate="zone={{.Zone}} {{.ID}}" | xargs -L1 -P1 scw instance image delete with-snapshots=true | ||
|
||
# A security group will be created alongside the server during packer execution. | ||
# We need to delete this security group before deleting the project | ||
scw instance security-group list zone=all project-id="$SCW_DEFAULT_PROJECT_ID" -otemplate="zone={{.Zone}} {{.ID}}" | xargs -L1 -P1 scw instance security-group delete | ||
|
||
scw account project delete project-id="$SCW_DEFAULT_PROJECT_ID" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/scaleway/packer-plugin-scaleway/internal/tester" | ||
"github.com/scaleway/scaleway-sdk-go/api/instance/v1" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
var _ tester.PackerCheck = (*ImageCheck)(nil) | ||
|
||
func Image(zone scw.Zone, name string) *ImageCheck { | ||
return &ImageCheck{ | ||
zone: zone, | ||
imageName: name, | ||
} | ||
} | ||
|
||
type ImageCheck struct { | ||
zone scw.Zone | ||
imageName string | ||
|
||
rootVolumeType *string | ||
size *scw.Size | ||
} | ||
|
||
func (c *ImageCheck) RootVolumeType(rootVolumeType string) *ImageCheck { | ||
c.rootVolumeType = &rootVolumeType | ||
|
||
return c | ||
} | ||
|
||
func (c *ImageCheck) SizeInGb(size uint64) *ImageCheck { | ||
c.size = scw.SizePtr(scw.Size(size) * scw.GB) | ||
|
||
return c | ||
} | ||
|
||
func (c *ImageCheck) Check(ctx context.Context) error { | ||
testCtx := tester.ExtractCtx(ctx) | ||
api := instance.NewAPI(testCtx.ScwClient) | ||
|
||
resp, err := api.ListImages(&instance.ListImagesRequest{ | ||
Name: &c.imageName, | ||
Zone: c.zone, | ||
Project: &testCtx.ProjectID, | ||
}, scw.WithAllPages(), scw.WithContext(ctx)) | ||
if err != nil { | ||
return fmt.Errorf("failed to list images: %w", err) | ||
} | ||
|
||
if len(resp.Images) == 0 { | ||
return fmt.Errorf("image %s not found, no images found", c.imageName) | ||
} | ||
|
||
if len(resp.Images) > 1 { | ||
return fmt.Errorf("multiple images found with name %s", c.imageName) | ||
} | ||
|
||
image := resp.Images[0] | ||
|
||
if image.Name != c.imageName { | ||
return fmt.Errorf("image name %s does not match expected %s", image.Name, c.imageName) | ||
} | ||
|
||
if c.rootVolumeType != nil && string(image.RootVolume.VolumeType) != *c.rootVolumeType { | ||
return fmt.Errorf("image root volume type %s does not match expected %s", image.RootVolume.VolumeType, *c.rootVolumeType) | ||
} | ||
|
||
if c.size != nil && image.RootVolume.Size != *c.size { | ||
return fmt.Errorf("image size %d does not match expected %d", image.RootVolume.Size, *c.size) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/scaleway/packer-plugin-scaleway/internal/tester" | ||
"github.com/scaleway/scaleway-sdk-go/api/instance/v1" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
type SnapshotCheck struct { | ||
zone scw.Zone | ||
snapshotName string | ||
|
||
size *scw.Size | ||
} | ||
|
||
func (c *SnapshotCheck) SizeInGB(size uint64) *SnapshotCheck { | ||
c.size = scw.SizePtr(scw.Size(size) * scw.GB) | ||
|
||
return c | ||
} | ||
|
||
func (c *SnapshotCheck) Check(ctx context.Context) error { | ||
testCtx := tester.ExtractCtx(ctx) | ||
api := instance.NewAPI(testCtx.ScwClient) | ||
|
||
resp, err := api.ListSnapshots(&instance.ListSnapshotsRequest{ | ||
Zone: c.zone, | ||
Name: &c.snapshotName, | ||
Project: &testCtx.ProjectID, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(resp.Snapshots) == 0 { | ||
return fmt.Errorf("snapshot %s not found, no snapshots found", c.snapshotName) | ||
} | ||
|
||
if len(resp.Snapshots) > 1 { | ||
return fmt.Errorf("multiple snapshots found with name %s", c.snapshotName) | ||
} | ||
|
||
snapshot := resp.Snapshots[0] | ||
|
||
if snapshot.Name != c.snapshotName { | ||
return fmt.Errorf("snapshot name %s does not match expected snapshot name %s", snapshot.Name, c.snapshotName) | ||
} | ||
|
||
if c.size != nil && snapshot.Size != *c.size { | ||
return fmt.Errorf("snapshot size %d does not match expected size %d", snapshot.Size, *c.size) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Snapshot(zone scw.Zone, snapshotName string) *SnapshotCheck { | ||
return &SnapshotCheck{ | ||
zone: zone, | ||
snapshotName: snapshotName, | ||
} | ||
} |
Oops, something went wrong.