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

dockerfile: port to txtar & script tests #1224

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/quay/claircore

go 1.20
go 1.22

toolchain go1.22rc2

require (
github.com/Masterminds/semver v1.5.0
Expand Down Expand Up @@ -32,6 +34,7 @@ require (
golang.org/x/time v0.4.0
golang.org/x/tools v0.17.0
modernc.org/sqlite v1.28.0
rsc.io/script v0.0.1
)

require (
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -134,6 +135,7 @@ github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peK
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -188,6 +190,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
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/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
Expand Down Expand Up @@ -324,7 +327,9 @@ modernc.org/cc/v3 v3.40.0/go.mod h1:/bTg4dnWkSXowUO6ssQKnOV0yMVxDYNIsIrzqTFDGH0=
modernc.org/ccgo/v3 v3.16.13 h1:Mkgdzl46i5F/CNR/Kj80Ri59hC8TKAhZrYSaqvkwzUw=
modernc.org/ccgo/v3 v3.16.13/go.mod h1:2Quk+5YgpImhPjv2Qsob1DnZ/4som1lJTodubIcoUkY=
modernc.org/ccorpus v1.11.6 h1:J16RXiiqiCgua6+ZvQot4yUuUy8zxgqbqEEUuGPlISk=
modernc.org/ccorpus v1.11.6/go.mod h1:2gEUTrWqdpH2pXsmTM1ZkjeSrUWDpjMu2T6m29L/ErQ=
modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM=
modernc.org/httpfs v1.0.6/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM=
modernc.org/libc v1.29.0 h1:tTFRFq69YKCF2QyGNuRUQxKBm1uZZLubf6Cjh/pVHXs=
modernc.org/libc v1.29.0/go.mod h1:DaG/4Q3LRRdqpiLyP0C2m1B8ZMGkQ+cCgOIjEtQlYhQ=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
Expand All @@ -338,6 +343,10 @@ modernc.org/sqlite v1.28.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0
modernc.org/strutil v1.1.3 h1:fNMm+oJklMGYfU9Ylcywl0CO5O6nTfaowNsh2wpPjzY=
modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw=
modernc.org/tcl v1.15.2 h1:C4ybAYCGJw968e+Me18oW55kD/FexcHbqH2xak1ROSY=
modernc.org/tcl v1.15.2/go.mod h1:3+k/ZaEbKrC8ePv8zJWPtBSW0V7Gg9g8rkmhI1Kfs3c=
modernc.org/token v1.0.1 h1:A3qvTqOwexpfZZeyI0FeGPDlSWX5pjZu9hF4lU+EKWg=
modernc.org/token v1.0.1/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
modernc.org/z v1.7.3 h1:zDJf6iHjrnB+WRD88stbXokugjyc0/pB91ri1gO6LZY=
modernc.org/z v1.7.3/go.mod h1:Ipv4tsdxZRbQyLq9Q1M6gdbkxYzdlrciF2Hi/lS7nWE=
rsc.io/script v0.0.1 h1:t8dtrXAUACmR2OV0OXuQ9otT/5x2F/QqsnssYhtcziM=
rsc.io/script v0.0.1/go.mod h1:CQ30lgzFbVimcUVFHfewSNOAv0ag6UQ0b0nIWeN22Fc=
124 changes: 89 additions & 35 deletions rhel/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,124 @@
package dockerfile

import (
"bufio"
"bytes"
"context"
"encoding/json"
"io/fs"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"golang.org/x/tools/txtar"
"rsc.io/script"
"rsc.io/script/scripttest"
)

func TestGetLabels(t *testing.T) {
ctx := context.Background()
td := os.DirFS("testdata")
de, err := fs.ReadDir(td, ".")
e := script.NewEngine()
e.Cmds = scripttest.DefaultCmds()
e.Conds = scripttest.DefaultConds()
e.Cmds["GetLabels"] = CmdGetLabels
const defaultScript = `# Check for expected JSON output.
GetLabels
cmp Got Want
`

ms, err := filepath.Glob("testdata/*.txtar")
if err != nil {
t.Fatal(err)
}
for _, de := range de {
n := de.Name()
if !strings.HasPrefix(n, "Dockerfile") ||
strings.HasSuffix(n, ".want") ||
strings.HasSuffix(n, ".want.err") {
continue
}
t.Run(n, func(t *testing.T) {
f, err := td.Open(n)
for _, m := range ms {
t.Run(strings.TrimSuffix(filepath.Base(m), filepath.Ext(m)), func(t *testing.T) {
ar, err := txtar.ParseFile(m)
if err != nil {
t.Fatal(err)
t.Fatalf("error parsing %q: %v", m, err)
}
defer f.Close()
w, err := td.Open(n + ".want")
wd := t.TempDir()
s, err := script.NewState(ctx, wd, nil)
if err != nil {
t.Fatal(err)
t.Fatalf("error constructing *State: %v", err)
}
defer w.Close()
wantErr, _ := fs.ReadFile(td, n+".want.err")

want := make(map[string]string)
if err := json.NewDecoder(w).Decode(&want); err != nil {
t.Error(err)
if err := s.ExtractFiles(ar); err != nil {
t.Fatalf("error with ExtractFiles: %v", err)
}
got, err := GetLabels(ctx, f)
if len(wantErr) == 0 {
if err != nil {
log := new(strings.Builder)
defer func() {
if err := s.CloseAndWait(log); err != nil {
t.Error(err)
}
} else {
if err == nil {
t.Error("got nil, wanted error")
} else {
if got, want := err.Error(), string(bytes.TrimSpace(wantErr)); got != want {
t.Errorf("got: %+#q, want: %+#q", got, want)
}
if log.Len() > 0 {
t.Log(strings.TrimSuffix(log.String(), "\n"))
}
}()
sr := bytes.NewBuffer(ar.Comment)
if sr.Len() == 0 {
m = "(default script)"
sr = bytes.NewBufferString(defaultScript)
}

if !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
if err := e.Execute(s, m, bufio.NewReader(sr), log); err != nil {
t.Error(err)
}
})
}
}

var CmdGetLabels = script.Command(
script.CmdUsage{
Summary: "parse a Dockerfile and return the labels of the named files",
Args: "[input]",
Detail: []string{
"If a filename is not given, 'Dockerfile' is assumed.",
"The result is JSON-encoded with indents of two spaces and written to the file 'Got'.",
"If there's an error, it is printed to stderr.",
},
},
func(s *script.State, args ...string) (script.WaitFunc, error) {
name := "Dockerfile"
switch len(args) {
case 0:
case 1:
name = args[0]
default:
return nil, errors.New("bad number of arguments: want at most 1")
}

in, err := os.Open(filepath.Join(s.Getwd(), name))
if err != nil {
return nil, err
}
out, err := os.Create(filepath.Join(s.Getwd(), "Got"))
if err != nil {
in.Close()
return nil, err
}
enc := json.NewEncoder(out)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")

return func(s *script.State) (stdout, stderr string, err error) {
defer in.Close()
defer out.Close()
var got map[string]string
got, err = GetLabels(s.Context(), in)
if err == nil {
if encErr := enc.Encode(got); encErr != nil {
err = fmt.Errorf("json error: %w", encErr)
}
} else {
stderr = err.Error() + "\n"
err = errors.New("GetLabels failed")
}
return
}, nil
},
)

func TestSplit(t *testing.T) {
for _, p := range []struct {
In string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- Dockerfile --
# Normal comments are fine
FROM scratch

Expand All @@ -7,3 +8,8 @@ LABEL \
C=D
# This comment is stripped despite the apparent continuation

-- Want --
{
"A": "B",
"C": "D"
}
4 changes: 0 additions & 4 deletions rhel/dockerfile/testdata/Dockerfile-Comments.want

This file was deleted.

2 changes: 0 additions & 2 deletions rhel/dockerfile/testdata/Dockerfile-InvalidLabel

This file was deleted.

4 changes: 0 additions & 4 deletions rhel/dockerfile/testdata/Dockerfile-InvalidLabel.want

This file was deleted.

This file was deleted.

15 changes: 0 additions & 15 deletions rhel/dockerfile/testdata/Dockerfile-etcd-rhel7.want

This file was deleted.

3 changes: 0 additions & 3 deletions rhel/dockerfile/testdata/Dockerfile-issue526.want

This file was deleted.

20 changes: 0 additions & 20 deletions rhel/dockerfile/testdata/Dockerfile-nodejs10.want

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

20 changes: 0 additions & 20 deletions rhel/dockerfile/testdata/Dockerfile-ubi7-7.9-516.want

This file was deleted.

Loading
Loading