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: rewrite scripts in Go #3968

Merged
merged 5 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 0 additions & 21 deletions integration/scripts/download-images.sh

This file was deleted.

24 changes: 0 additions & 24 deletions integration/scripts/download-vm-images.sh

This file was deleted.

112 changes: 112 additions & 0 deletions magefiles/fixture.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package main

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/google/go-containerregistry/pkg/crane"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/magefile/mage/sh"
)

func fixtureContainerImages() error {
const (
testImages = "ghcr.io/aquasecurity/trivy-test-images"
dir = "integration/testdata/fixtures/images/"
)
if err := os.MkdirAll(dir, 0750); err != nil {
return err
}
tags, err := crane.ListTags(testImages)
if err != nil {
return err
}
for _, tag := range tags {
fileName := tag + ".tar.gz"
filePath := filepath.Join(dir, fileName)
if exists(filePath) {
continue
}
fmt.Printf("Downloading %s...\n", tag)
imgName := fmt.Sprintf("%s:%s", testImages, tag)
img, err := crane.Pull(imgName)
if err != nil {
return err
}
tarPath := strings.TrimSuffix(filePath, ".gz")
if err = crane.Save(img, imgName, tarPath); err != nil {
return err
}
if err = sh.Run("gzip", tarPath); err != nil {
return err
}
}
return nil
}

func fixtureVMImages() error {
const (
testVMImages = "ghcr.io/aquasecurity/trivy-test-vm-images"
titleAnnotation = "org.opencontainers.image.title"
dir = "integration/testdata/fixtures/vm-images/"
)
if err := os.MkdirAll(dir, 0750); err != nil {
return err
}
tags, err := crane.ListTags(testVMImages)
if err != nil {
return err
}
for _, tag := range tags {
img, err := crane.Pull(fmt.Sprintf("%s:%s", testVMImages, tag))
if err != nil {
return err
}

manifest, err := img.Manifest()
if err != nil {
return err
}

layers, err := img.Layers()
if err != nil {
return err
}

for i, layer := range layers {
fileName, ok := manifest.Layers[i].Annotations[titleAnnotation]
if !ok {
continue
}
filePath := filepath.Join(dir, fileName)
if exists(filePath) {
return nil
}
fmt.Printf("Downloading %s...\n", fileName)
if err = saveLayer(layer, filePath); err != nil {
return err
}
}
}
return nil
}

func saveLayer(layer v1.Layer, filePath string) error {
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()

c, err := layer.Compressed()
if err != nil {
return err
}
if _, err = io.Copy(f, c); err != nil {
return err
}
return nil
}
20 changes: 2 additions & 18 deletions magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ func (Tool) Wire() error {
return sh.Run("go", "install", "github.com/google/wire/cmd/wire@v0.5.0")
}

// Crane installs crane
func (Tool) Crane() error {
if exists(filepath.Join(GOBIN, "crane")) {
return nil
}
return sh.Run("go", "install", "github.com/google/go-containerregistry/cmd/crane@v0.9.0")
}

// GolangciLint installs golangci-lint
func (Tool) GolangciLint() error {
const version = "v1.52.2"
Expand Down Expand Up @@ -179,20 +171,12 @@ type Test mg.Namespace

// FixtureContainerImages downloads and extracts required images
func (Test) FixtureContainerImages() error {
mg.Deps(Tool{}.Crane)
if err := os.MkdirAll(filepath.Join("integration", "testdata", "fixtures", "images"), 0750); err != nil {
return err
}

downloadScript := filepath.Join("integration", "scripts", "download-images.sh")
return sh.Run(downloadScript)
return fixtureContainerImages()
}

// FixtureVMImages downloads and extracts required VM images
func (Test) FixtureVMImages() error {
mg.Deps(Tool{}.Crane)
downloadScript := filepath.Join("integration", "scripts", "download-vm-images.sh")
return sh.Run(downloadScript)
return fixtureVMImages()
}

// GenerateModules compiles WASM modules for unit tests
Expand Down