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

test: Add e2e test framework and update makefile and GHA workflow #185

Merged
merged 17 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
19 changes: 12 additions & 7 deletions .github/workflows/go-test-multiplatform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
go-version: ${{ needs.get-go-version.outputs.go-version }}
- run: |
echo "Testing with Go ${{ needs.get-go-version.outputs.go-version }}"
go test -race -count 1 ./... -timeout=3m
go test -race -count 1 ./builder/linode/... -timeout=3m

windows-go-tests:
needs:
Expand All @@ -57,7 +57,7 @@ jobs:
go-version: ${{ needs.get-go-version.outputs.go-version }}
- run: |
echo "Testing with Go ${{ needs.get-go-version.outputs.go-version }}"
go test -race -count 1 ./... -timeout=3m
go test -race -count 1 ./builder/linode/... -timeout=3m

linux-go-tests:
needs:
Expand All @@ -81,15 +81,17 @@ jobs:
report_filename=$(date +'%Y%m%d%H%M')_packer_test_report.xml
echo "REPORT_FILENAME=$report_filename" >> $GITHUB_ENV

- name: Run integration tests
- name: Run integration and unit tests
run: |
echo "Testing with Go ${{ needs.get-go-version.outputs.go-version }}"
if ! go test -race -count 1 ./... -timeout=3m -v | go-junit-report -set-exit-code > "$REPORT_FILENAME"; then
if ! make test | go-junit-report -set-exit-code > "$REPORT_FILENAME"; then
echo "EXIT_STATUS=1" >> $GITHUB_ENV
fi
cat "$REPORT_FILENAME"
env:
LINODE_TOKEN: ${{ secrets.LINODE_TOKEN }}

- name: Upload test report
- name: Upload test report as artifact
uses: actions/upload-artifact@v4
with:
name: test-report-file
Expand All @@ -111,6 +113,9 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: 'recursive'

# Download the artifact generated by the 'linux-go-tests' job
- name: Download test report
Expand All @@ -133,7 +138,7 @@ jobs:
- name: Add additional information to XML report
run: |
filename=$(ls | grep -E '^[0-9]{12}_packer_test_report\.xml$')
python scripts/add_to_xml_test_report.py \
python tod_scripts/add_to_xml_test_report.py \
--branch_name "${{ env.RELEASE_VERSION }}" \
--gha_run_id "$GITHUB_RUN_ID" \
--gha_run_number "$GITHUB_RUN_NUMBER" \
Expand All @@ -143,7 +148,7 @@ jobs:
- name: Upload test results to bucket
run: |
filename=$(ls | grep -E '^[0-9]{12}_packer_test_report\.xml$')
python3 scripts/test_report_upload_script.py "${filename}"
python3 tod_scripts/test_report_upload_script.py "${filename}"
env:
LINODE_CLI_OBJ_ACCESS_KEY: ${{ secrets.LINODE_CLI_OBJ_ACCESS_KEY }}
LINODE_CLI_OBJ_SECRET_KEY: ${{ secrets.LINODE_CLI_OBJ_SECRET_KEY }}
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tod_scripts"]
path = tod_scripts
url = https://github.com/linode/TOD-test-report-uploader.git
13 changes: 8 additions & 5 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NAME=linode
BINARY=packer-plugin-${NAME}
GOFMT_FILES?=$$(find . -name '*.go')
COUNT?=1
TEST?=$(shell go list ./...)
TEST?=$(shell go list ./builder/...)
HASHICORP_PACKER_PLUGIN_SDK_VERSION?=$(shell go list -m github.com/hashicorp/packer-plugin-sdk | cut -d " " -f2)

.PHONY: dev
Expand All @@ -14,17 +14,20 @@ dev: build
@mkdir -p ~/.packer.d/plugins/
@mv ${BINARY} ~/.packer.d/plugins/${BINARY}

test: fmtcheck
@go test -race -count $(COUNT) $(TEST) -timeout=3m
test: dev fmtcheck
@PACKER_ACC=1 go test -count $(COUNT) ./... -v -timeout=100m

install-packer-sdc: ## Install packer sofware development command
@go install github.com/hashicorp/packer-plugin-sdk/cmd/packer-sdc@${HASHICORP_PACKER_PLUGIN_SDK_VERSION}

plugin-check: install-packer-sdc build
@packer-sdc plugin-check ${BINARY}

testacc: dev
@PACKER_ACC=1 go test -count $(COUNT) -v $(TEST) -timeout=120m
unit-test: dev
@PACKER_ACC=1 go test -count $(COUNT) -v $(TEST) -timeout=10m

int-test: dev
@go test -v test/integration/e2e_test.go

generate: install-packer-sdc
@go generate ./...
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ require (
golang.org/x/oauth2 v0.16.0
)

require github.com/mitchellh/mapstructure v1.5.0
require (
github.com/mitchellh/mapstructure v1.5.0
github.com/stretchr/testify v1.8.4
)

require (
cloud.google.com/go v0.110.2 // indirect
Expand All @@ -28,6 +31,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.14.1 // indirect
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
Expand Down Expand Up @@ -79,6 +83,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 +103,7 @@ require (
google.golang.org/grpc v1.56.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.66.6 // 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
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWH
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dylanmei/iso8601 v0.1.0 h1:812NGQDBcqquTfH5Yeo7lwR0nzx/cKdsmf3qMjPURUI=
github.com/dylanmei/iso8601 v0.1.0/go.mod h1:w9KhXSgIyROl1DefbMYIE7UVSIvELTbMrCfx+QkYnoQ=
github.com/dylanmei/winrmtest v0.0.0-20210303004826-fbc9ae56efb6 h1:zWydSUQBJApHwpQ4guHi+mGyQN/8yN6xbKWdDtL3ZNM=
Expand Down Expand Up @@ -292,6 +293,7 @@ github.com/pkg/sftp v1.13.2 h1:taJnKntsWgU+qae21Rx52lIwndAdKrj0mfUNQsz1z4Q=
github.com/pkg/sftp v1.13.2/go.mod h1:LzqnAvaD5TWeNBsZpfKxSYn1MbjWwOsCIAFFJbpIsK8=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand Down Expand Up @@ -331,6 +333,7 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/ugorji/go v1.2.6/go.mod h1:anCg0y61KIhDlPZmnH+so+RQbysYVyDko0IMgJv0Nn0=
github.com/ugorji/go/codec v1.2.6 h1:7kbGefxLoDBuYXOms4yD7223OpNMMPNPZxXk5TvFcyQ=
Expand Down
37 changes: 0 additions & 37 deletions scripts/add_to_xml_test_report.py

This file was deleted.

81 changes: 0 additions & 81 deletions scripts/test_report_upload_script.py

This file was deleted.

88 changes: 88 additions & 0 deletions test/integration/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package integration

import (
"context"
"fmt"
"net/http"
"os"
"os/exec"
"strings"
"testing"

"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2"
)

const (
packerTemplate = "template/test_image_template.json"
)

func TestBuildPackerImage(t *testing.T) {
linodeToken := os.Getenv("LINODE_TOKEN")

if linodeToken == "" {
t.Fatal("Linode token is not set. Please set LINODE_TOKEN as environment variable.")
}

// Run the Packer build command from terminal
cmd := exec.Command("packer", "build", packerTemplate)
output, err := cmd.CombinedOutput()
// Check if the Packer build was successful
if err != nil {
t.Fatalf("Error building Packer image: %v\nOutput:\n%s", err, output)
}

// Assert the output contains expected strings
expectedSubstring := "Builds finished. The artifacts of successful builds"
assert.True(t, strings.Contains(string(output), expectedSubstring), "Expected successful build output to contain: %s", expectedSubstring)

// Assert other fields
err = assertLinodeImage("test-packer-image-", t)

if err != nil {
t.Fatalf("Error asserting Linode builder image: %v", err)
}
}

func assertLinodeImage(imageLabelPrefix string, t *testing.T) error {
linodeToken := os.Getenv("LINODE_TOKEN")

tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: linodeToken})

oauth2Client := &http.Client{
Transport: &oauth2.Transport{
Source: tokenSource,
},
}

linodeClient := linodego.NewClient(oauth2Client)

images, err := linodeClient.ListImages(context.Background(), nil)
if err != nil {
return fmt.Errorf("error listing Linode images: %v", err)
}

// Find the desired image by label prefix
var targetImage *linodego.Image
for _, image := range images {
if image.Label != "" && strings.HasPrefix(image.Label, imageLabelPrefix) {
targetImage = &image
break
}
}

if targetImage == nil {
return fmt.Errorf("image with label prefix '%s' not found", imageLabelPrefix)
}

assert.Equal(t, "manual", targetImage.Type, "unexpected instance type")
expectedInstanceIDFormat := "private/"
assert.True(t, strings.HasPrefix(targetImage.ID, expectedInstanceIDFormat), "unexpected instance ID prefix")
expectedInstanceLabel := "test-packer-image-"
assert.True(t, strings.HasPrefix(targetImage.Label, expectedInstanceLabel), "unexpected instance label prefix")
expectedImageDescription := "My Test Image Description"
assert.Equal(t, expectedImageDescription, targetImage.Description, "unexpected image description")

return nil
}
18 changes: 18 additions & 0 deletions test/integration/template/test_image_template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"variables": {
"linode_token": "{{env `LINODE_TOKEN`}}"
},
"builders": [{
"type": "linode",
"linode_token": "{{ user `linode_token` }}",
"image": "linode/debian9",
"region": "us-east",
"instance_type": "g6-nanode-1",
"instance_label": "test-instance-{{timestamp}}",

"image_label": "test-packer-image-{{timestamp}}",
"image_description": "My Test Image Description",

"ssh_username": "root"
}]
}
1 change: 1 addition & 0 deletions tod_scripts
Submodule tod_scripts added at eec4b9
Loading