Skip to content

Commit

Permalink
add cassettes to packer
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax committed Jan 13, 2025
1 parent e08ca19 commit 149b11e
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 56 deletions.
13 changes: 13 additions & 0 deletions builder/scaleway/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"flag"
"fmt"
"os"
"path/filepath"

"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/scaleway/packer-plugin-scaleway/internal/vcr"
"github.com/scaleway/scaleway-sdk-go/scw"
)

Expand Down Expand Up @@ -57,6 +59,17 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
clientOpts = append(clientOpts, scw.WithAPIURL(b.Config.APIURL))
}

if _, isSet := os.LookupEnv(vcr.UpdateCassettesEnvVariable); isSet {
client, cleanup, err := vcr.GetHTTPRecorder(filepath.Join("testdata", b.Config.ImageName+".cassette"), vcr.UpdateCassettes)
if err != nil {
ui.Error(err.Error())
return nil, err
}
defer cleanup()

clientOpts = append(clientOpts, scw.WithHTTPClient(client))
}

client, err := scw.NewClient(clientOpts...)
if err != nil {
ui.Error(err.Error())
Expand Down
2 changes: 1 addition & 1 deletion e2e_tests/checks/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package checks

import (
"context"
"e2e_tests/tester"
"fmt"

"github.com/scaleway/packer-plugin-scaleway/e2e_tests/tester"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
Expand Down
2 changes: 1 addition & 1 deletion e2e_tests/checks/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package checks

import (
"context"
"e2e_tests/tester"
"fmt"

"github.com/scaleway/packer-plugin-scaleway/e2e_tests/tester"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
Expand Down
2 changes: 1 addition & 1 deletion e2e_tests/checks/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package checks

import (
"context"
"e2e_tests/tester"
"fmt"

"github.com/scaleway/packer-plugin-scaleway/e2e_tests/tester"
block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
Expand Down
17 changes: 0 additions & 17 deletions e2e_tests/go.mod

This file was deleted.

20 changes: 0 additions & 20 deletions e2e_tests/go.sum

This file was deleted.

1 change: 1 addition & 0 deletions e2e_tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ TESTS=(

TEST_RESULT=0

rm ./tests.test
go test -c ./tests
./tests.test -test.v
TEST_RESULT=$?
Expand Down
5 changes: 2 additions & 3 deletions e2e_tests/tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
"os"
"testing"

"github.com/scaleway/packer-plugin-scaleway/internal/vcr"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/stretchr/testify/require"
)

const PackerCtxKey = "PACKER_CTX_KEY"

var UpdateCassettes = os.Getenv("PACKER_UPDATE_CASSETTES") == "true"

type PackerCtx struct {
ScwClient *scw.Client
ProjectID string
Expand Down Expand Up @@ -56,7 +55,7 @@ type TestConfig struct {
}

func Test(t *testing.T, config *TestConfig) {
httpClient, cleanup, err := getHTTPRecoder(t, ".", UpdateCassettes)
httpClient, cleanup, err := vcr.GetHTTPRecorder(vcr.GetTestFilePath(t, "."), vcr.UpdateCassettes)
require.NoError(t, err)
defer cleanup()

Expand Down
5 changes: 3 additions & 2 deletions e2e_tests/tests/complete_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package tests_test

import (
"e2e_tests/checks"
"e2e_tests/tester"
"testing"

"github.com/scaleway/packer-plugin-scaleway/e2e_tests/checks"
"github.com/scaleway/packer-plugin-scaleway/e2e_tests/tester"
"github.com/scaleway/scaleway-sdk-go/scw"
)

Expand All @@ -14,6 +14,7 @@ func TestComplete(t *testing.T) {
tester.Test(t, &tester.TestConfig{
Config: `
source "scaleway" "basic" {
communicator = "none"
commercial_type = "PLAY2-PICO"
zone = "fr-par-2"
image = "ubuntu_jammy"
Expand Down
5 changes: 3 additions & 2 deletions e2e_tests/tests/simple_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package tests_test

import (
"e2e_tests/checks"
"e2e_tests/tester"
"testing"

"github.com/scaleway/packer-plugin-scaleway/e2e_tests/checks"
"github.com/scaleway/packer-plugin-scaleway/e2e_tests/tester"
"github.com/scaleway/scaleway-sdk-go/scw"
)

Expand All @@ -14,6 +14,7 @@ func TestSimple(t *testing.T) {
tester.Test(t, &tester.TestConfig{
Config: `
source "scaleway" "basic" {
communicator = "none"
commercial_type = "PRO2-XXS"
zone = "fr-par-1"
image = "ubuntu_jammy"
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ require (
github.com/hashicorp/packer-plugin-sdk v0.5.4
github.com/mitchellh/mapstructure v1.5.0
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30
github.com/stretchr/testify v1.8.4
github.com/zclconf/go-cty v1.13.3
golang.org/x/crypto v0.32.0
gopkg.in/dnaeon/go-vcr.v4 v4.0.2
)

require (
Expand All @@ -28,6 +30,7 @@ require (
github.com/aws/aws-sdk-go v1.44.114 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dylanmei/iso8601 v0.1.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/go-jose/go-jose/v4 v4.0.1 // indirect
Expand Down Expand Up @@ -77,6 +80,7 @@ require (
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/packer-community/winrmcp v0.0.0-20180921211025-c76d91c1e7db // indirect
github.com/pkg/sftp v1.13.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
Expand All @@ -98,6 +102,7 @@ require (
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/zclconf/go-cty => github.com/nywilken/go-cty v1.13.3 // added by packer-sdc fix as noted in github.com/hashicorp/packer-plugin-sdk/issues/187
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/dnaeon/go-vcr.v4 v4.0.2 h1:7T5VYf2ifyK01ETHbJPl5A6XTpUljD4Trw3GEDcdedk=
gopkg.in/dnaeon/go-vcr.v4 v4.0.2/go.mod h1:65yxh9goQVrudqofKtHA4JNFWd6XZRkWfKN4YpMx7KI=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
22 changes: 13 additions & 9 deletions e2e_tests/tester/vcr.go → internal/vcr/vcr.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tester
package vcr

import (
"fmt"
Expand All @@ -11,7 +11,6 @@ import (
"testing"

"github.com/scaleway/scaleway-sdk-go/strcase"
"github.com/stretchr/testify/require"
"gopkg.in/dnaeon/go-vcr.v4/pkg/cassette"
"gopkg.in/dnaeon/go-vcr.v4/pkg/recorder"
)
Expand All @@ -22,6 +21,10 @@ var QueryMatcherIgnore = []string{
"project",
}

const UpdateCassettesEnvVariable = "PACKER_UPDATE_CASSETTES"

var UpdateCassettes = os.Getenv(UpdateCassettesEnvVariable) == "true"

// getTestFilePath returns a valid filename path based on the go test name and suffix. (Take care of non fs friendly char)
func getTestFilePath(t *testing.T, pkgFolder string, suffix string) string {
t.Helper()
Expand All @@ -41,6 +44,10 @@ func getTestFilePath(t *testing.T, pkgFolder string, suffix string) string {
return filepath.Join(pkgFolder, "testdata", fileName)
}

func GetTestFilePath(t *testing.T, pkgFolder string) string {
return getTestFilePath(t, pkgFolder, ".cassette")
}

// recorderAuthHook is a hook that will clean authorization tokens from cassette during record.
func recorderAuthHook(i *cassette.Interaction) error {
i.Request.Headers = i.Request.Headers.Clone()
Expand All @@ -67,30 +74,27 @@ func requestMatcher(actualRequest *http.Request, cassetteRequest cassette.Reques
actualURL.String() == cassetteURL.String()
}

// getHTTPRecoder creates a new httpClient that records all HTTP requests in a cassette.
// GetHTTPRecorder creates a new httpClient that records all HTTP requests in a cassette.
// This cassette is then replayed whenever tests are executed again. This means that once the
// requests are recorded in the cassette, no more real HTTP requests must be made to run the tests.
//
// It is important to add a `defer cleanup()` so the given cassette files are correctly
// closed and saved after the requests.
func getHTTPRecoder(t *testing.T, pkgFolder string, update bool) (client *http.Client, cleanup func(), err error) {
t.Helper()
func GetHTTPRecorder(cassetteFilePath string, update bool) (client *http.Client, cleanup func(), err error) {
recorderMode := recorder.ModeReplayOnly
if update {
recorderMode = recorder.ModeRecordOnly
}

cassetteFilePath := getTestFilePath(t, pkgFolder, ".cassette")
_, errorCassette := os.Stat(cassetteFilePath + ".yaml")
t.Logf("using %s.yaml", cassetteFilePath)

// If in record mode we check that the cassette exists
if recorderMode == recorder.ModeReplayOnly && errorCassette != nil {
return nil, nil, fmt.Errorf("cannot stat file %s.yaml while in replay mode", cassetteFilePath)
}

// Setup recorder and scw client
r, err := recorder.New(getTestFilePath(t, pkgFolder, ".cassette"),
r, err := recorder.New(cassetteFilePath,
recorder.WithMode(recorderMode),
recorder.WithSkipRequestLatency(true),
// Add a filter which removes Authorization headers from all requests:
Expand All @@ -104,6 +108,6 @@ func getHTTPRecoder(t *testing.T, pkgFolder string, update bool) (client *http.C
}(r)

return &http.Client{Transport: r}, func() {
require.NoError(t, r.Stop()) // Make sure recorder is stopped once done with it
r.Stop() // Make sure recorder is stopped once done with it
}, nil
}

0 comments on commit 149b11e

Please sign in to comment.