cuelang.org/go@v0.10.1/internal/registrytest/registry_test.go (about) 1 package registrytest 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "path" 8 "path/filepath" 9 "strings" 10 "testing" 11 12 "cuelabs.dev/go/oci/ociregistry" 13 "cuelabs.dev/go/oci/ociregistry/ociclient" 14 "cuelabs.dev/go/oci/ociregistry/ocifilter" 15 "github.com/go-quicktest/qt" 16 "golang.org/x/tools/txtar" 17 18 "cuelang.org/go/mod/modregistry" 19 "cuelang.org/go/mod/module" 20 ) 21 22 func TestRegistry(t *testing.T) { 23 const testDir = "testdata" 24 files, err := os.ReadDir(testDir) 25 qt.Assert(t, qt.IsNil(err)) 26 27 for _, fi := range files { 28 name := fi.Name() 29 if fi.IsDir() || filepath.Ext(name) != ".txtar" { 30 continue 31 } 32 ar, err := txtar.ParseFile(filepath.Join(testDir, fi.Name())) 33 qt.Assert(t, qt.IsNil(err)) 34 t.Run(strings.TrimSuffix(name, ".txtar"), func(t *testing.T) { 35 tfs, err := txtar.FS(ar) 36 qt.Assert(t, qt.IsNil(err)) 37 r, err := New(tfs, "someprefix/other") 38 qt.Assert(t, qt.IsNil(err)) 39 defer r.Close() 40 client, err := ociclient.New(r.Host(), &ociclient.Options{ 41 Insecure: true, 42 }) 43 qt.Assert(t, qt.IsNil(err)) 44 runTest(t, ocifilter.Sub(client, "someprefix/other"), string(ar.Comment), ar) 45 }) 46 } 47 } 48 49 func runTest(t *testing.T, registry ociregistry.Interface, script string, ar *txtar.Archive) { 50 ctx := context.Background() 51 client := modregistry.NewClient(registry) 52 for _, line := range strings.Split(script, "\n") { 53 if line == "" || line[0] == '#' { 54 continue 55 } 56 args := strings.Fields(line) 57 if len(args) == 0 || args[0] == "" { 58 t.Fatalf("invalid line %q", line) 59 } 60 switch args[0] { 61 case "modfile": 62 if len(args) != 3 { 63 t.Fatalf("usage: getmod $version $wantFile") 64 } 65 mv, err := module.ParseVersion(args[1]) 66 if err != nil { 67 t.Fatalf("invalid version %q in getmod", args[1]) 68 } 69 m, err := client.GetModule(ctx, mv) 70 if err != nil { 71 t.Fatal(err) 72 } 73 gotData, err := m.ModuleFile(ctx) 74 if err != nil { 75 t.Fatal(err) 76 } 77 wantData, err := getFile(ar, args[2]) 78 if err != nil { 79 t.Fatalf("cannot open file for body comparison: %v", err) 80 } 81 if string(gotData) != string(wantData) { 82 t.Errorf("unexpected GET response\ngot %q\nwant %q", gotData, wantData) 83 } 84 default: 85 t.Fatalf("unknown command %q", line) 86 } 87 } 88 } 89 90 func getFile(ar *txtar.Archive, name string) ([]byte, error) { 91 name = path.Clean(name) 92 for _, f := range ar.Files { 93 if path.Clean(f.Name) == name { 94 return f.Data, nil 95 } 96 } 97 return nil, fmt.Errorf("file %q not found in txtar archive", name) 98 }