Skip to content

Commit

Permalink
test: use original components.json for prefetch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron Meissner committed Oct 11, 2024
1 parent e650215 commit 1f76658
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 867 deletions.
8 changes: 2 additions & 6 deletions vhdbuilder/prefetch/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ SHELL = bash
all: generate

.PHONY: generate
generate: generate-testdata
generate:
REGENERATE_CONTAINER_IMAGE_PREFETCH_TESTDATA="true" go test ./...

.PHONY: test
test:
go test ./...

.PHONY: generate-testdata
generate-testdata:
@bash ./hack/generate-testdata.sh
go test ./...
19 changes: 0 additions & 19 deletions vhdbuilder/prefetch/hack/generate-testdata.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
// GeneratePrefetchScript generates the container image prefetch script based on the specified component list.
func GeneratePrefetchScript(list *components.List) ([]byte, error) {
if list == nil {
return nil, fmt.Errorf("components list generate opt must be non-nil")
return nil, fmt.Errorf("components list must be non-nil")
}
var args TemplateArgs
for _, image := range list.Images {
Expand Down
21 changes: 16 additions & 5 deletions vhdbuilder/prefetch/internal/containerimage/containerimage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package containerimage_test

import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
Expand All @@ -13,23 +14,25 @@ import (

const (
testDataPath = "testdata/"
artifactsRelPath = "parts/linux/cloud-init/artifacts"
regenerateTestData = "REGENERATE_CONTAINER_IMAGE_PREFETCH_TESTDATA"
)

var (
componentsTestDataPath = filepath.Join(testDataPath, "components.json")
prefetchScriptTestDataPath = filepath.Join(testDataPath, "prefetch.sh")
)

func TestContianerImage(t *testing.T) {
componentsPath := resolveComponentsBasePath(t)

if strings.EqualFold(os.Getenv(regenerateTestData), "true") {
generate(t)
generate(t, componentsPath)
}

expectedContent, err := os.ReadFile(prefetchScriptTestDataPath)
assert.NoError(t, err)

list, err := components.ParseList(componentsTestDataPath)
list, err := components.ParseList(componentsPath)
assert.NoError(t, err)

actualContent, err := containerimage.GeneratePrefetchScript(list)
Expand All @@ -38,13 +41,13 @@ func TestContianerImage(t *testing.T) {
assert.Equal(t, expectedContent, actualContent)
}

func generate(t *testing.T) {
func generate(t *testing.T, componentsPath string) {
t.Log("generating container image prefetch.sh testdata...")

err := os.MkdirAll(testDataPath, os.ModePerm)
assert.NoError(t, err)

list, err := components.ParseList(componentsTestDataPath)
list, err := components.ParseList(componentsPath)
assert.NoError(t, err)

content, err := containerimage.GeneratePrefetchScript(list)
Expand All @@ -53,3 +56,11 @@ func generate(t *testing.T) {
err = os.WriteFile(prefetchScriptTestDataPath, content, os.ModePerm)
assert.NoError(t, err)
}

func resolveComponentsBasePath(t *testing.T) string {
// this is hack until we can get rid of storing static testdata altogether
repoBasePath, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
assert.NoError(t, err, "unable to determine repo root with git rev-parse")
basePath := strings.ReplaceAll(string(repoBasePath), "\n", "")
return filepath.Join(filepath.Join(basePath, artifactsRelPath), "components.json")
}
Loading

0 comments on commit 1f76658

Please sign in to comment.