Debian Codegraph @

cmd/ocisrv/main_test.go

raw symbols SCIP
1 // Copyright 2023 CUE Labs AG
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package main
16
17 import (
18 "bytes"
19 "context"
20 "fmt"
21 "io"
22 "net"
23 "net/http"
24 "os"
25 "os/signal"
26 "path/filepath"
27 "testing"
28 "time"
29
30 "cuelabs.dev/go/oci/ociregistry"
31 "cuelabs.dev/go/oci/ociregistry/ociclient"
32 "github.com/opencontainers/go-digest"
33 "github.com/rogpeppe/go-internal/testscript"
34 "github.com/rogpeppe/retry"
35 )
36
37 func init() {
38 writeNetAddr = writeNetAddrForTest
39
40 // Process the interrupt signal sent by testscript
41 // to ensure a clean exit.
42 sigc := make(chan os.Signal, 1)
43 signal.Notify(sigc, os.Interrupt)
44 go func() {
45 <-sigc
46 os.Exit(0)
47 }()
48 }
49
50 func TestMain(m *testing.M) {
51 os.Exit(testscript.RunMain(m, map[string]func() int{
52 "ocisrv": func() int {
53 main()
54 return 0
55 },
56 }))
57 }
58
59 func TestScript(t *testing.T) {
60 testscript.Run(t, testscript.Params{
61 Dir: "testdata",
62 Setup: func(env *testscript.Env) error {
63 env.Setenv("ADDR_FILE", filepath.Join(env.WorkDir, "listen-addr"))
64 return nil
65 },
66 Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
67 "pushblob": cmdPushBlob,
68 },
69 })
70 }
71
72 func cmdPushBlob(ts *testscript.TestScript, neg bool, args []string) {
73 if len(args) != 3 {
74 ts.Fatalf("usage: pushblob $repo $blobfile $digest")
75 }
76 repo, blobFile, dg := args[0], args[1], args[2]
77 data, err := os.ReadFile(ts.MkAbs(blobFile))
78 ts.Check(err)
79 r, err := connect(ts)
80 ts.Check(err)
81 if !neg && digest.FromBytes(data) != digest.Digest(dg) {
82 ts.Fatalf("blob digest mismatch")
83 }
84
85 _, err = r.PushBlob(context.Background(), repo, ociregistry.Descriptor{
86 Size: int64(len(data)),
87 Digest: digest.Digest(dg),
88 }, bytes.NewReader(data))
89 if neg {
90 if err == nil {
91 ts.Fatalf("unexpected success")
92 }
93 // TODO it would be nice if we could write the error to stderr
94 // so it could be checked by testscript commands.
95 ts.Logf("error: %v", err)
96 return
97 }
98 ts.Check(err)
99 }
100
101 func cmdGetBlob(ts *testscript.TestScript, neg bool, args []string) {
102 if len(args) != 2 {
103 ts.Fatalf("usage: getblob $repo $digest")
104 }
105 repo, dg := args[0], args[1]
106 r, err := connect(ts)
107 ts.Check(err)
108 rd, err := r.GetBlob(context.Background(), repo, digest.Digest(dg))
109 if neg {
110 if err == nil {
111 ts.Fatalf("unexpected success")
112 }
113 // TODO it would be nice if we could write the error to stderr
114 // so it could be checked by testscript commands.
115 ts.Logf("error: %v", err)
116 return
117 }
118 data, err := io.ReadAll(rd)
119 ts.Check(err)
120 _, err = ts.Stdout().Write(data)
121 ts.Check(err)
122 }
123
124 var waitStrategy = retry.Strategy{
125 Delay: time.Millisecond,
126 MaxDelay: 20 * time.Millisecond,
127 MaxDuration: 500 * time.Millisecond,
128 }
129
130 func connect(ts *testscript.TestScript) (ociregistry.Interface, error) {
131 addrFile := ts.Getenv("ADDR_FILE")
132 if addrFile == "" {
133 return nil, fmt.Errorf("$ADDR_FILE not set")
134 }
135 var addr string
136 for it := waitStrategy.Start(); ; {
137 data, err := os.ReadFile(addrFile)
138 if err == nil {
139 addr = string(data)
140 break
141 }
142 if !os.IsNotExist(err) {
143 return nil, fmt.Errorf("cannot read file: %v", err)
144 }
145 if !it.Next(nil) {
146 return nil, fmt.Errorf("timed out waiting for server")
147 }
148 }
149 resp, err := http.Get("http://" + addr + "/v2/")
150 if err != nil {
151 return nil, fmt.Errorf("cannot ping server: %v", err)
152 }
153 resp.Body.Close()
154 if resp.StatusCode != http.StatusOK {
155 return nil, fmt.Errorf("unexpected ping status (%v)", resp.Status)
156 }
157 return ociclient.New(addr, &ociclient.Options{
158 Insecure: true,
159 })
160 }
161
162 func writeNetAddrForTest(l net.Listener) {
163 f := os.Getenv("ADDR_FILE")
164 if f == "" {
165 return
166 }
167 tmpf := f + ".tmp"
168 if err := os.WriteFile(tmpf, []byte(l.Addr().String()), 0o666); err != nil {
169 panic(err)
170 }
171 if err := os.Rename(tmpf, f); err != nil {
172 panic(err)
173 }
174 }