Skip to content

Commit 1f31ffb

Browse files
committed
fixup! wip: fixture: store test fixtures in OCI artifacts
Signed-off-by: Hank Donnay <[email protected]>
1 parent 2051eb7 commit 1f31ffb

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

test/fixture/fixture.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323
)
2424

2525
func lookupCaller(t *testing.T) string {
26-
pc, _, _, ok := runtime.Caller(3)
26+
const module = `github.com/quay/claircore/`
27+
pc, _, _, ok := runtime.Caller(2)
2728
if !ok {
2829
t.Fatal("unable to get caller")
2930
}
@@ -33,14 +34,13 @@ func lookupCaller(t *testing.T) string {
3334
if idx == -1 {
3435
t.Fatalf("weird name: %q", name)
3536
}
36-
name = name[:idx]
37-
return name
37+
return strings.TrimPrefix(name[:idx], module)
3838
}
3939

4040
var (
4141
// Alternatively, use ghcr.io ?
4242
Registry = `quay.io`
43-
Namespace = `projectclair`
43+
Namespace = `projectquay`
4444
Tag = `latest`
4545
)
4646

@@ -69,6 +69,7 @@ const (
6969
// Fetch ...
7070
func Fetch[V Value, K Kind[V]](ctx context.Context, t *testing.T) []V {
7171
t.Helper()
72+
repo := lookupCaller(t)
7273
dir := filepath.Join(integration.PackageCacheDir(t), "fixtures")
7374
if err := os.Mkdir(dir, 0755); err != nil && !errors.Is(err, fs.ErrExist) {
7475
t.Fatal(err)
@@ -78,14 +79,15 @@ func Fetch[V Value, K Kind[V]](ctx context.Context, t *testing.T) []V {
7879

7980
c := regclient.New(
8081
regclient.WithFS(sys),
82+
regclient.WithDockerCreds(),
8183
)
8284
tgt, err := ref.New("ocidir://" + Tag)
8385
if err != nil {
8486
t.Fatal(err)
8587
}
8688
t.Run("FetchFixtures", func(t *testing.T) {
8789
integration.Skip(t)
88-
name := fmt.Sprintf("%s/%s/%s:%s", Registry, Namespace, lookupCaller(t), Tag)
90+
name := fmt.Sprintf("%s/%s/%s:%s", Registry, Namespace, repo, Tag)
8991
t.Logf("pulling fixtues referencing %q", name)
9092
remote, err := ref.New(name)
9193
if err != nil {
@@ -105,11 +107,13 @@ func Fetch[V Value, K Kind[V]](ctx context.Context, t *testing.T) []V {
105107
t.Fatalf("unexpected error with local files: %v", err)
106108
}
107109

108-
list, err := c.ReferrerList(ctx, tgt, scheme.WithReferrerAT(elem.ArtifactType()))
110+
at := elem.ArtifactType()
111+
list, err := c.ReferrerList(ctx, tgt, scheme.WithReferrerAT(at))
109112
if err != nil {
110113
t.Fatal(err)
111114
}
112115
if list.IsEmpty() {
116+
t.Logf("no manifests of type %q for %s", at, tgt.CommonName())
113117
return nil
114118
}
115119

@@ -120,14 +124,15 @@ func Fetch[V Value, K Kind[V]](ctx context.Context, t *testing.T) []V {
120124
if err != nil {
121125
t.Fatalf("error fetching blob: %v", err)
122126
}
127+
t.Logf("found descriptor: %v", d)
123128
// Don't worry too much about the Reader; all tests are transitory, man.
124129
var m oci.Manifest
125130
if err := json.NewDecoder(rd).Decode(&m); err != nil {
126131
t.Fatalf("unexpected error decoding descriptor data: %v", err)
127132
}
128133
rd.Close()
129134
var k K = &out[i]
130-
if err := k.Load(ctx, dir, local, tgt, &m); err != nil {
135+
if err := k.Load(ctx, t, dir, local, tgt, &m); err != nil {
131136
t.Fatalf("tk: %v", err)
132137
}
133138
}

test/fixture/fixture_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ import (
66
)
77

88
func TestLookup(t *testing.T) {
9-
const want = `github.com/quay/claircore/test/fixture`
9+
const want = `test/fixture`
1010
var got string
1111
// Simulate getting called from a top-level test:
1212
func() {
13-
func() {
14-
got = lookupCaller(t)
15-
}()
13+
got = lookupCaller(t)
1614
}()
1715
t.Logf("got: %q, want: %q", got, want)
1816
if got != want {

test/fixture/kinds.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const artifactTmpl = `application/vnd.claircore.test-fixture.%s.config.v1`
2222
type Kind[T Value] interface {
2323
*T
2424
// Load takes an OCIDir so that we can _know_ the path of blobs.
25-
Load(context.Context, string, *ocidir.OCIDir, ref.Ref, *oci.Manifest) error
25+
Load(context.Context, *testing.T, string, *ocidir.OCIDir, ref.Ref, *oci.Manifest) error
2626
ArtifactType() string
2727
}
2828

@@ -54,7 +54,7 @@ type Indexer struct {
5454
}
5555

5656
func (*Indexer) ArtifactType() string { return fmt.Sprintf(artifactTmpl, `indexer`) }
57-
func (i *Indexer) Load(ctx context.Context, root string, dir *ocidir.OCIDir, r ref.Ref, m *oci.Manifest) error {
57+
func (i *Indexer) Load(ctx context.Context, t *testing.T, root string, dir *ocidir.OCIDir, r ref.Ref, m *oci.Manifest) error {
5858
if m.ArtifactType != i.ArtifactType() {
5959
return ErrBadArtifactType
6060
}
@@ -94,6 +94,7 @@ func (i *Indexer) Load(ctx context.Context, root string, dir *ocidir.OCIDir, r r
9494
return err
9595
}
9696
default: // Skip
97+
t.Logf("skipping artifact: %s", l.ArtifactType)
9798
}
9899
}
99100
return nil
@@ -102,28 +103,28 @@ func (i *Indexer) Load(ctx context.Context, root string, dir *ocidir.OCIDir, r r
102103
type Matcher struct{}
103104

104105
func (*Matcher) ArtifactType() string { return fmt.Sprintf(artifactTmpl, `matcher`) }
105-
func (ma *Matcher) Load(ctx context.Context, root string, dir *ocidir.OCIDir, r ref.Ref, m *oci.Manifest) error {
106+
func (ma *Matcher) Load(ctx context.Context, t *testing.T, root string, dir *ocidir.OCIDir, r ref.Ref, m *oci.Manifest) error {
106107
panic("TODO: implement")
107108
}
108109

109110
type Updater struct{}
110111

111112
func (*Updater) ArtifactType() string { return fmt.Sprintf(artifactTmpl, `updater`) }
112-
func (u *Updater) Load(ctx context.Context, root string, dir *ocidir.OCIDir, r ref.Ref, m *oci.Manifest) error {
113+
func (u *Updater) Load(ctx context.Context, t *testing.T, root string, dir *ocidir.OCIDir, r ref.Ref, m *oci.Manifest) error {
113114
panic("TODO: implement")
114115
}
115116

116117
type MatcherFlow struct{}
117118

118119
func (*MatcherFlow) ArtifactType() string { return fmt.Sprintf(artifactTmpl, `matcher-flow`) }
119-
func (mf *MatcherFlow) Load(ctx context.Context, root string, dir *ocidir.OCIDir, r ref.Ref, m *oci.Manifest) error {
120+
func (mf *MatcherFlow) Load(ctx context.Context, t *testing.T, root string, dir *ocidir.OCIDir, r ref.Ref, m *oci.Manifest) error {
120121
panic("TODO: implement")
121122
}
122123

123124
type Integration struct{}
124125

125126
func (*Integration) ArtifactType() string { return fmt.Sprintf(artifactTmpl, `integration`) }
126-
func (i *Integration) Load(ctx context.Context, root string, dir *ocidir.OCIDir, r ref.Ref, m *oci.Manifest) error {
127+
func (i *Integration) Load(ctx context.Context, t *testing.T, root string, dir *ocidir.OCIDir, r ref.Ref, m *oci.Manifest) error {
127128
panic("TODO: implement")
128129
}
129130

0 commit comments

Comments
 (0)