|
| 1 | +//go:build unix |
| 2 | + |
| 3 | +package uploaders_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + |
| 10 | + . "github.com/mandelsoft/goutils/testutils" |
| 11 | + . "github.com/onsi/ginkgo/v2" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | + . "ocm.software/ocm/api/helper/builder" |
| 14 | + . "ocm.software/ocm/api/helper/env" |
| 15 | + . "ocm.software/ocm/api/ocm/plugin/testutils" |
| 16 | + |
| 17 | + "github.com/mandelsoft/filepath/pkg/filepath" |
| 18 | + "github.com/mandelsoft/vfs/pkg/utils" |
| 19 | + "github.com/mandelsoft/vfs/pkg/vfs" |
| 20 | + |
| 21 | + "ocm.software/ocm/api/ocm/cpi" |
| 22 | + resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes" |
| 23 | + "ocm.software/ocm/api/ocm/extensions/attrs/plugincacheattr" |
| 24 | + "ocm.software/ocm/api/ocm/extensions/blobhandler/handlers/generic/plugin" |
| 25 | + "ocm.software/ocm/api/ocm/plugin/registration" |
| 26 | + "ocm.software/ocm/api/ocm/refhints" |
| 27 | + "ocm.software/ocm/api/utils/blobaccess/blobaccess" |
| 28 | + "ocm.software/ocm/api/utils/mime" |
| 29 | + "ocm.software/ocm/cmds/demoplugin/accessmethods" |
| 30 | + "ocm.software/ocm/cmds/demoplugin/uploaders" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + CONTENT = "testdata" |
| 35 | +) |
| 36 | + |
| 37 | +var _ = Describe("demoplugin", func() { |
| 38 | + Context("lib", func() { |
| 39 | + var env *Builder |
| 40 | + var plugins TempPluginDir |
| 41 | + var osf *os.File |
| 42 | + |
| 43 | + BeforeEach(func() { |
| 44 | + env = NewBuilder(TestData()) |
| 45 | + plugins = Must(ConfigureTestPlugins(env, "testdata")) |
| 46 | + |
| 47 | + registry := plugincacheattr.Get(env) |
| 48 | + Expect(registration.RegisterExtensions(env)).To(Succeed()) |
| 49 | + p := registry.Get("demo") |
| 50 | + Expect(p).NotTo(BeNil()) |
| 51 | + Expect(p.Error()).To(Equal("")) |
| 52 | + |
| 53 | + MustBeSuccessful(env.FileSystem().MkdirAll("data", 0o700)) |
| 54 | + |
| 55 | + f := Must(env.FileSystem().Create("/data/test")) |
| 56 | + Must(f.Write([]byte(CONTENT))) |
| 57 | + |
| 58 | + osf = utils.OSFile(f) |
| 59 | + Expect(osf).NotTo(BeNil()) |
| 60 | + }) |
| 61 | + |
| 62 | + AfterEach(func() { |
| 63 | + plugins.Cleanup() |
| 64 | + env.Cleanup() |
| 65 | + }) |
| 66 | + |
| 67 | + It("get templ file", func() { |
| 68 | + p := osf.Name()[len(os.TempDir())+1:] |
| 69 | + fmt.Printf("%s\n", p) |
| 70 | + |
| 71 | + registry := plugincacheattr.Get(env) |
| 72 | + |
| 73 | + _, comps, _ := vfs.SplitPath(env.FileSystem(), p) |
| 74 | + |
| 75 | + tgt := vfs.Join(env.FileSystem(), comps[0], "upload") |
| 76 | + cfg := uploaders.NewTarget(tgt) |
| 77 | + raw := Must(json.Marshal(cfg)) |
| 78 | + |
| 79 | + uploader := Must(plugin.New(registry.Get("demo"), "demo", raw)) |
| 80 | + Expect(uploader).NotTo(BeNil()) |
| 81 | + |
| 82 | + sctx := &cpi.DummyStorageContext{ |
| 83 | + Context: env.OCMContext(), |
| 84 | + } |
| 85 | + cv := &cpi.DummyComponentVersionAccess{ |
| 86 | + Context: env.OCMContext(), |
| 87 | + } |
| 88 | + |
| 89 | + hint := "uploaded" |
| 90 | + blob := blobaccess.ForString(mime.MIME_TEXT, CONTENT) |
| 91 | + spec := Must(uploader.StoreBlob(blob, resourcetypes.PLAIN_TEXT, refhints.NewHints(accessmethods.ReferenceHint, hint), nil, sctx)) |
| 92 | + |
| 93 | + Expect(spec).NotTo(BeNil()) |
| 94 | + |
| 95 | + hints := cpi.ReferenceHint(spec, cv) |
| 96 | + Expect(len(hints)).To(Equal(1)) |
| 97 | + name := vfs.Join(env.FileSystem(), tgt, hint) |
| 98 | + Expect(hints[0].GetReference()).To(Equal(name)) |
| 99 | + |
| 100 | + f := filepath.Join(os.TempDir(), tgt, hint) |
| 101 | + data := Must(os.ReadFile(f)) |
| 102 | + fmt.Printf("written %q: %s\n", f, string(data)) |
| 103 | + Expect(string(data)).To(Equal(CONTENT)) |
| 104 | + }) |
| 105 | + }) |
| 106 | +}) |
0 commit comments