Skip to content

Commit

Permalink
fix execution with cassettes
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax committed Jan 13, 2025
1 parent 149b11e commit 1da5458
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 28 deletions.
6 changes: 3 additions & 3 deletions e2e_tests/GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
build-binary:
make -C .. build

link-binary:
ln -fs ../packer-plugin-scaleway
install-plugin:
packer plugins install -path ../packer-plugin-scaleway "github.com/scaleway/scaleway"

test: build-binary link-binary
test: build-binary install-plugin
./test.sh
22 changes: 17 additions & 5 deletions e2e_tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@
# - Run packer e2e tests
# - Tries to delete project

PROJECT_ID=$(scw account project create -otemplate="{{.ID}}")
export SCW_DEFAULT_PROJECT_ID=$PROJECT_ID
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

echo Running tests with new project $SCW_DEFAULT_PROJECT_ID

TESTS=(
# simple
Expand All @@ -18,10 +27,13 @@ TESTS=(
TEST_RESULT=0

rm ./tests.test
go test -c ./tests
go test -c ../internal/tests
./tests.test -test.v
TEST_RESULT=$?

./clean.sh $SCW_DEFAULT_PROJECT_ID
if [ "$PACKER_UPDATE_CASSETTES" == "true" ]
then
./clean.sh $SCW_DEFAULT_PROJECT_ID
fi

exit $TEST_RESULT
2 changes: 1 addition & 1 deletion e2e_tests/checks/image.go → internal/checks/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

"github.com/scaleway/packer-plugin-scaleway/e2e_tests/tester"
"github.com/scaleway/packer-plugin-scaleway/internal/tester"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

"github.com/scaleway/packer-plugin-scaleway/e2e_tests/tester"
"github.com/scaleway/packer-plugin-scaleway/internal/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 → internal/checks/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

"github.com/scaleway/packer-plugin-scaleway/e2e_tests/tester"
"github.com/scaleway/packer-plugin-scaleway/internal/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
File renamed without changes.
File renamed without changes.
24 changes: 17 additions & 7 deletions e2e_tests/tester/tester.go → internal/tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tester
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"testing"
Expand All @@ -19,24 +20,33 @@ type PackerCtx struct {
ProjectID string
}

func NewTestContext(ctx context.Context, httpClient *http.Client) (context.Context, error) {
func getActiveProfile() *scw.Profile {
cfg, err := scw.LoadConfig()
if err != nil {
return nil, err
return &scw.Profile{}
}
activeProfile, err := cfg.GetActiveProfile()
if err != nil {
return nil, err
return &scw.Profile{}
}

return activeProfile
}

func NewTestContext(ctx context.Context, httpClient *http.Client) (context.Context, error) {
activeProfile := getActiveProfile()
profile := scw.MergeProfiles(activeProfile, scw.LoadEnvProfile())
client, err := scw.NewClient(scw.WithProfile(profile), scw.WithHTTPClient(httpClient))
if err != nil {
return nil, err
return nil, fmt.Errorf("error creating scw client: %w", err)
}
projectID, exists := client.GetDefaultProjectID()
if !exists {
return nil, errors.New("error getting default project ID")
if vcr.UpdateCassettes == false {
projectID = "11111111-1111-1111-1111-111111111111"
} else {
return nil, errors.New("error getting default project ID")
}
}

return context.WithValue(ctx, PackerCtxKey, &PackerCtx{
Expand All @@ -61,15 +71,15 @@ func Test(t *testing.T, config *TestConfig) {

ctx := context.Background()
ctx, err = NewTestContext(ctx, httpClient)
require.Nil(t, err)
require.NoError(t, err)

// Create TMP Dir
tmpDir, err := os.MkdirTemp(os.TempDir(), "packer_e2e_test")
require.NoError(t, err)
t.Logf("Created tmp dir: %s", tmpDir)

err = packerExec(tmpDir, config.Config)
require.NoError(t, err, "error executing packer command")
require.NoError(t, err, "error executing packer command: %s", err)

for i, check := range config.Checks {
t.Logf("Running check %d/%d", i+1, len(config.Checks))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package tests_test
import (
"testing"

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package tests_test
import (
"testing"

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

Expand Down
33 changes: 27 additions & 6 deletions internal/vcr/vcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -58,17 +59,37 @@ func recorderAuthHook(i *cassette.Interaction) error {
return nil
}

// stripRandomNumbers receive a string with format "%s-%d" and will strip the last number element when split by dash.
func stripRandomNumbers(s string) string {
elems := strings.Split(s, "-")
if _, err := strconv.ParseInt(elems[len(elems)-1], 10, 64); err == nil {
// Last elem is an int
elems = elems[:len(elems)-1]
}

return strings.Join(elems, "-")
}

func cleanUrlValues(values url.Values, u *url.URL) url.Values {
for _, query := range QueryMatcherIgnore {
values.Del(query)
}
for key, query := range values {
if key == "name" && len(query) > 0 {
query[0] = stripRandomNumbers(query[0])
}
}

return values
}

func requestMatcher(actualRequest *http.Request, cassetteRequest cassette.Request) bool {
cassetteURL, _ := url.Parse(cassetteRequest.URL)
actualURL := actualRequest.URL
cassetteQueryValues := cassetteURL.Query()
actualQueryValues := actualURL.Query()
for _, query := range QueryMatcherIgnore {
actualQueryValues.Del(query)
cassetteQueryValues.Del(query)
}
actualURL.RawQuery = actualQueryValues.Encode()
cassetteURL.RawQuery = cassetteQueryValues.Encode()
actualURL.RawQuery = cleanUrlValues(actualQueryValues, cassetteURL).Encode()
cassetteURL.RawQuery = cleanUrlValues(cassetteQueryValues, cassetteURL).Encode()

return actualRequest.Method == cassetteRequest.Method &&
actualURL.String() == cassetteURL.String()
Expand Down
27 changes: 27 additions & 0 deletions internal/vcr/vcr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package vcr

import "testing"

func Test_stripRandomNumbers(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
{
name: "packer snapshot",
args: args{"snapshot-packer-1736526252"},
want: "snapshot-packer",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := stripRandomNumbers(tt.args.s); got != tt.want {
t.Errorf("stripRandomNumbers() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 1da5458

Please sign in to comment.