...
1
2
3 package warehouse
4
5 import (
6 "flag"
7 "fmt"
8 "net/http/httptest"
9 "testing"
10
11 "github.com/google/go-containerregistry/pkg/name"
12
13 "edge-infra.dev/pkg/f8n/warehouse/oci"
14 "edge-infra.dev/pkg/f8n/warehouse/oci/remote"
15 "edge-infra.dev/pkg/f8n/warehouse/whtest/registry"
16 "edge-infra.dev/test/f2"
17 "edge-infra.dev/test/f2/fctx"
18 "edge-infra.dev/test/f2/integration"
19 )
20
21
22
23
24
25
26 type Registry struct {
27
28
29 URL string
30
31
32 options []registry.Option
33
34
35 registry *httptest.Server
36 }
37
38
39 func New(opts ...registry.Option) *Registry {
40 return &Registry{
41 options: opts,
42 }
43 }
44
45
46
47 func FromContext(ctx fctx.Context) (*Registry, error) {
48 v := fctx.ValueFrom[Registry](ctx)
49 if v == nil {
50 return nil, fmt.Errorf("%w: warehouse.Registry extension", fctx.ErrNotFound)
51 }
52 return v, nil
53 }
54
55
56
57 func FromContextT(ctx fctx.Context, t *testing.T) *Registry {
58 return fctx.ValueFromT[Registry](ctx, t)
59 }
60
61
62 func (r *Registry) Push(a oci.Artifact, pkgName, tag string, opts ...remote.Option) error {
63
64
65 if integration.IsL1() {
66 opts = append(opts, remote.WithoutAuth())
67 }
68
69 refstr := fmt.Sprintf("%s/%s:%s", r.URL, pkgName, tag)
70 dst, err := name.ParseReference(refstr)
71 if err != nil {
72 return fmt.Errorf("failed to parse destination %s: %w", refstr, err)
73 }
74
75 return remote.Write(a, dst, opts...)
76 }
77
78 func (r *Registry) RegisterFns(f f2.Framework) {
79 switch {
80 case integration.IsL1():
81
82
83 f.Setup(func(ctx fctx.Context) (fctx.Context, error) {
84 u, registry, err := registry.New(r.options...)
85 if err != nil {
86 return ctx, err
87 }
88
89 r.registry = registry
90 r.URL = repoURL(u.Host, ctx.RunID)
91
92 return ctx, nil
93 })
94 f.Teardown(func(ctx fctx.Context) (fctx.Context, error) {
95 r.registry.Close()
96 return ctx, nil
97 })
98 case integration.IsL2():
99
100
101
102 f.Setup(func(ctx fctx.Context) (fctx.Context, error) {
103 if r.URL == "" {
104 return ctx, fmt.Errorf(
105 "%w: integration test requires --warehouse-repo", f2.ErrSkip,
106 )
107 }
108
109 r.URL = repoURL(r.URL, ctx.RunID)
110
111 return ctx, nil
112 })
113 }
114 }
115
116
117 func (r *Registry) BindFlags(fs *flag.FlagSet) {
118 fs.StringVar(&r.URL,
119 "warehouse-repo",
120 "",
121 "the OCI repo URL to use for remote operations. the test run ID is appended "+
122 "to the value to improve test isolation",
123 )
124 }
125
126
127 func (r *Registry) IntoContext(ctx fctx.Context) fctx.Context {
128 return fctx.ValueInto(ctx, r)
129 }
130
131
132
133 func repoURL(base, context string) string {
134 return fmt.Sprintf("%s/%s", base, context)
135 }
136
View as plain text