Skip to content

Commit

Permalink
dockerfile: port tests to txtar
Browse files Browse the repository at this point in the history
This makes things much simpler, IMO.

Signed-off-by: Hank Donnay <[email protected]>
  • Loading branch information
hdonnay committed Feb 8, 2024
1 parent ab19eb8 commit 345c85e
Show file tree
Hide file tree
Showing 32 changed files with 254 additions and 237 deletions.
68 changes: 32 additions & 36 deletions rhel/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,56 @@ import (
"bytes"
"context"
"encoding/json"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"golang.org/x/tools/txtar"
)

func TestGetLabels(t *testing.T) {
var errPrefix = []byte("error:")
ctx := context.Background()
td := os.DirFS("testdata")
de, err := fs.ReadDir(td, ".")

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 archive: %v", err)
}
defer f.Close()
w, err := td.Open(n + ".want")
if err != nil {
t.Fatal(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)
}
got, err := GetLabels(ctx, f)
if len(wantErr) == 0 {
if 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)
var got, want map[string]string
wantErr := bytes.HasPrefix(ar.Comment, errPrefix)
for _, f := range ar.Files {
switch f.Name {
case "Dockerfile":
got, err = GetLabels(ctx, bytes.NewReader(f.Data))
case "Want":
want = make(map[string]string)
if err := json.Unmarshal(f.Data, &want); err != nil {
t.Fatalf("unmarshaling wanted values: %v", err)
}
default:
t.Logf("skipping unknown file: %s", f.Name)
}
}

if wantErr {
got := err.Error()
want := string(bytes.TrimSpace(bytes.TrimPrefix(ar.Comment, errPrefix)))
if got != want {
t.Error(cmp.Diff(got, want))
}
return
}
if err != nil {
t.Errorf("error parsing labels: %v", err)
}
if !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
}
Expand Down
25 changes: 24 additions & 1 deletion rhel/dockerfile/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,31 @@ package dockerfile

import (
"bytes"
"path/filepath"
"testing"

"golang.org/x/tools/txtar"
)

func FuzzLex(f *testing.F) {
ms, err := filepath.Glob("testdata/*.txtar")
if err != nil {
f.Fatal(err)
}
for _, m := range ms {
ar, err := txtar.ParseFile(m)
if err != nil {
f.Fatalf("error parsing archive: %v", err)
}
File:
for _, af := range ar.Files {
if af.Name == "Dockerfile" {
f.Add(af.Data)
break File
}
}
}

f.Fuzz(func(t *testing.T, b []byte) {
l := newLexer()
l.Reset(bytes.NewReader(b))
Expand All @@ -22,4 +43,6 @@ func FuzzLex(f *testing.F) {
})
}

//go:generate sh -c "go run golang.org/x/tools/cmd/file2fuzz -o testdata/fuzz/FuzzLex $(ls -1 testdata/Dockerfile* | grep -v [.]want)"
// To add new files to the fuzz corpus:
//
// go run golang.org/x/tools/cmd/file2fuzz -o testdata/fuzz/FuzzLex [FILE]
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.

1 change: 0 additions & 1 deletion rhel/dockerfile/testdata/Dockerfile-InvalidLabel.want.err

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.

21 changes: 0 additions & 21 deletions rhel/dockerfile/testdata/Dockerfile-quay-quay-rhel8-v3.5.6-4.want

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.

22 changes: 0 additions & 22 deletions rhel/dockerfile/testdata/Dockerfile-ubi8-minimal-8.4-208.want

This file was deleted.

4 changes: 4 additions & 0 deletions rhel/dockerfile/testdata/InvalidLabel.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
error: invalid assignment syntax: `A=B C=D # Comments not allowed in this position`
-- Dockerfile --
LABEL A=B \
C=D # Comments not allowed in this position
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
-- Dockerfile --
FROM scratch

ENV A="B C" \
D=E

LABEL label="$A $D"
-- Want --
{
"label": "B C E"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- Dockerfile --
FROM registry.stage.redhat.io/rhel7/rhel:7.9

ENV container=docker
Expand Down Expand Up @@ -29,4 +30,19 @@ EXPOSE 4001 7001 2379 2380
ADD tmpfiles.template config.json.template service.template manifest.json /exports/

CMD ["/usr/bin/etcd-env.sh", "/usr/bin/etcd"]

-- Want --
{
"com.redhat.component": "etcd-container",
"description": "etcd is a distributed reliable key-value store for the most critical data of a distributed system.",
"install": "/usr/bin/docker run --rm $OPT1 --privileged -v /:/host -e HOST=/host -e NAME=$NAME -e IMAGE=$IMAGE $IMAGE $OPT2 /usr/bin/install.sh $OPT3",
"io.k8s.description": "etcd is a distributed reliable key-value store for the most critical data of a distributed system.",
"io.k8s.display-name": "etcd",
"io.openshift.expose-services": "2379:tcp,2380:tcp",
"io.openshift.tags": "etcd",
"name": "rhel7/etcd",
"run": "/usr/bin/docker run -d $OPT1 -p 4001:4001 -p 7001:7001 -p 2379:2379 -p 2380:2380 --name $NAME $IMAGE $OPT2 $OPT3",
"summary": "A highly-available key value store for shared configuration",
"uninstall": "/usr/bin/docker run --rm $OPT1 --privileged -v /:/host -e HOST=/host -e NAME=$NAME -e IMAGE=$IMAGE $IMAGE $OPT2 /usr/bin/uninstall.sh $OPT3",
"usage": "etcd -help ",
"version": "3.2.32"
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 345c85e

Please sign in to comment.