Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nightly e2e test #55

Merged
merged 11 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Nightly Tests

on:
schedule:
# Will run at 00:00 every day
- cron: "0 0 * * *"

jobs:
nightly:
strategy:
matrix:
go-version: [ 1.18.x ]
platform: [ ubuntu-latest ]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- name: Checkout
uses: actions/checkout@v2
- name: Build binaries
run: make build
- name: Run e2e test
run: go test -count 1 -v ./builder/scaleway/builder_acc_test.go -timeout=120m
env:
PACKER_ACC: 1
PACKER_LOG: 1
SCW_DEBUG: 1
SCW_ACCESS_KEY: ${{ secrets.SCW_ACCESS_KEY }}
SCW_SECRET_KEY: ${{ secrets.SCW_SECRET_KEY }}
SCW_DEFAULT_ORGANIZATION_ID: ${{ secrets.SCW_DEFAULT_ORGANIZATION_ID }}
SCW_DEFAULT_PROJECT_ID: ${{ secrets.SCW_DEFAULT_PROJECT_ID }}
8 changes: 8 additions & 0 deletions builder/scaleway/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package scaleway
import (
"context"
"errors"
"flag"
"fmt"
"os"

"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/communicator"
Expand All @@ -19,6 +21,8 @@ import (
// BuilderId is the unique id for the builder
const BuilderId = "hashicorp.scaleway"

var nightly = flag.Bool("nightly", os.Getenv("PACKER_ACC") == "1", "Run nightly tests")
yfodil marked this conversation as resolved.
Show resolved Hide resolved

type Builder struct {
config Config
runner multistep.Runner
Expand Down Expand Up @@ -91,6 +95,10 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
new(stepImage),
}

if *nightly {
yfodil marked this conversation as resolved.
Show resolved Hide resolved
steps = append(steps, new(stepSweep))
}

b.runner = commonsteps.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(ctx, state)

Expand Down
52 changes: 52 additions & 0 deletions builder/scaleway/builder_acc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package scaleway

import (
"fmt"
"os"
"os/exec"
"testing"

"github.com/hashicorp/packer-plugin-sdk/acctest"
)

// Run with: PACKER_ACC=1 go test -count 1 -v ./builder/scaleway/builder_acc_test.go -timeout=120m
func TestAccScalewayBuilder(t *testing.T) {
if skip := testAccPreCheck(t); skip {
return
}
acctest.TestPlugin(t, &acctest.PluginTestCase{
Name: "test-scaleway-builder-basic",
Template: testBuilderAccBasic,
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}
return nil
},
})
}

func testAccPreCheck(t *testing.T) bool {
if os.Getenv(acctest.TestEnvVar) == "" {
t.Skipf("Acceptance tests skipped unless env '%s' set",
acctest.TestEnvVar)
return true
}
return false
}

const testBuilderAccBasic = `
source "scaleway" "basic" {
commercial_type = "DEV1-S"
image = "ubuntu_focal"
image_name = "nightly"
ssh_username = "root"
zone = "fr-par-1"
}

build {
sources = ["source.scaleway.basic"]
}
`
48 changes: 48 additions & 0 deletions builder/scaleway/step_sweep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package scaleway

import (
"context"
"fmt"

"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

type stepSweep struct{}

func (s *stepSweep) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
// nothing to do ... only cleanup interests us
return multistep.ActionContinue
}

func (s *stepSweep) Cleanup(state multistep.StateBag) {
instanceAPI := instance.NewAPI(state.Get("client").(*scw.Client))
ui := state.Get("ui").(packersdk.Ui)
snapshotID := state.Get("snapshot_id").(string)
imageID := state.Get("image_id").(string)

ui.Say("Deleting Image...")

err := instanceAPI.DeleteImage(&instance.DeleteImageRequest{
ImageID: imageID,
})
if err != nil {
err := fmt.Errorf("error deleting image: %s", err)
state.Put("error", err)
ui.Error(fmt.Sprintf("Error deleting image: %s. (Ignored)", err))
}

ui.Say("Deleting Snapshot...")

err = instanceAPI.DeleteSnapshot(&instance.DeleteSnapshotRequest{
SnapshotID: snapshotID,
})
if err != nil {
err := fmt.Errorf("error deleting snapshot: %s", err)
state.Put("error", err)
ui.Error(fmt.Sprintf("Error deleting snapshot: %s. (Ignored)", err))
}

}