Skip to content

Commit 9b45901

Browse files
committed
plugin tests
1 parent 96fc535 commit 9b45901

File tree

14 files changed

+270
-15
lines changed

14 files changed

+270
-15
lines changed

api/ocm/cpi/dummy.go

+27
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,30 @@ func (d *DummyComponentVersionAccess) IsPersistent() bool {
191191
func (d *DummyComponentVersionAccess) UseDirectAccess() bool {
192192
return true
193193
}
194+
195+
////////////////////////////////////////////////////////////////////////////////
196+
197+
type DummyStorageContext struct {
198+
Context Context
199+
Component string
200+
Repository Repository
201+
RepoTypes ImplementationRepositoryType
202+
}
203+
204+
var _ StorageContext = (*DummyStorageContext)(nil)
205+
206+
func (d *DummyStorageContext) GetContext() Context {
207+
return d.Context
208+
}
209+
210+
func (d *DummyStorageContext) TargetComponentName() string {
211+
return d.Component
212+
}
213+
214+
func (d *DummyStorageContext) TargetComponentRepository() Repository {
215+
return d.Repository
216+
}
217+
218+
func (d *DummyStorageContext) GetImplementationRepositoryType() ImplementationRepositoryType {
219+
return d.RepoTypes
220+
}

api/ocm/plugin/ppi/cmds/accessmethod/validate/cmd.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/spf13/cobra"
88
"github.com/spf13/pflag"
99

10-
"ocm.software/ocm/api/credentials"
1110
"ocm.software/ocm/api/ocm/plugin/ppi"
1211
"ocm.software/ocm/api/utils/errkind"
1312
"ocm.software/ocm/api/utils/runtime"
@@ -74,12 +73,7 @@ func (o *Options) Complete(args []string) error {
7473
return nil
7574
}
7675

77-
type Result struct {
78-
MediaType string `json:"mediaType"`
79-
Short string `json:"description"`
80-
Hint string `json:"hint"`
81-
ConsumerId credentials.ConsumerIdentity `json:"consumerId"`
82-
}
76+
type Result = ppi.AccessSpecInfo
8377

8478
func Command(p ppi.Plugin, cmd *cobra.Command, opts *Options) error {
8579
spec, err := p.DecodeAccessSpecification(opts.Specification)
@@ -95,8 +89,7 @@ func Command(p ppi.Plugin, cmd *cobra.Command, opts *Options) error {
9589
if err != nil {
9690
return err
9791
}
98-
result := Result{MediaType: info.MediaType, ConsumerId: info.ConsumerId, Hint: info.Hint, Short: info.Short}
99-
data, err := json.Marshal(result)
92+
data, err := json.Marshal(info)
10093
if err != nil {
10194
return err
10295
}

api/ocm/plugin/ppi/cmds/upload/put/cmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
"github.com/mandelsoft/goutils/errors"
1010
"github.com/spf13/cobra"
1111
"github.com/spf13/pflag"
12-
"ocm.software/ocm/api/ocm/refhints"
1312

1413
"ocm.software/ocm/api/credentials"
1514
"ocm.software/ocm/api/ocm/plugin/descriptor"
1615
"ocm.software/ocm/api/ocm/plugin/ppi"
1716
"ocm.software/ocm/api/ocm/plugin/ppi/cmds/common"
17+
"ocm.software/ocm/api/ocm/refhints"
1818
"ocm.software/ocm/api/utils/cobrautils/flag"
1919
"ocm.software/ocm/api/utils/runtime"
2020
)

api/ocm/plugin/ppi/interface.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import (
55
"io"
66

77
"github.com/spf13/cobra"
8-
"ocm.software/ocm/api/ocm/refhints"
98

109
"ocm.software/ocm/api/config/cpi"
1110
"ocm.software/ocm/api/credentials"
1211
"ocm.software/ocm/api/datacontext/action"
1312
"ocm.software/ocm/api/ocm/extensions/accessmethods/options"
1413
"ocm.software/ocm/api/ocm/plugin/descriptor"
1514
"ocm.software/ocm/api/ocm/plugin/internal"
15+
"ocm.software/ocm/api/ocm/refhints"
1616
"ocm.software/ocm/api/utils/runtime"
1717
)
1818

cmds/demoplugin/accessmethods/demo.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88

99
"github.com/mandelsoft/filepath/pkg/filepath"
1010
"github.com/mandelsoft/goutils/errors"
11-
"ocm.software/ocm/api/ocm/refhints"
1211

1312
"ocm.software/ocm/api/credentials"
1413
"ocm.software/ocm/api/credentials/cpi"
1514
"ocm.software/ocm/api/ocm/extensions/accessmethods/options"
1615
"ocm.software/ocm/api/ocm/plugin/ppi"
16+
"ocm.software/ocm/api/ocm/refhints"
1717
"ocm.software/ocm/api/tech/oci/identity"
1818
"ocm.software/ocm/api/utils/cobrautils/flagsets"
1919
"ocm.software/ocm/api/utils/runtime"
@@ -26,7 +26,11 @@ const (
2626
VERSION = "v1"
2727
)
2828

29-
const ReferenceHintType = "demo"
29+
const ReferenceHintType = NAME
30+
31+
func ReferenceHint(p string, implicit ...bool) refhints.ReferenceHint {
32+
return refhints.New(ReferenceHintType, p, implicit...)
33+
}
3034

3135
type AccessSpec struct {
3236
runtime.ObjectVersionedType `json:",inline"`
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//go:build unix
2+
3+
package accessmethods_test
4+
5+
import (
6+
"fmt"
7+
"os"
8+
9+
. "github.com/mandelsoft/goutils/testutils"
10+
. "github.com/onsi/ginkgo/v2"
11+
. "github.com/onsi/gomega"
12+
. "ocm.software/ocm/api/helper/builder"
13+
. "ocm.software/ocm/api/helper/env"
14+
. "ocm.software/ocm/api/ocm/plugin/testutils"
15+
16+
"github.com/mandelsoft/vfs/pkg/utils"
17+
18+
"ocm.software/ocm/api/ocm/cpi"
19+
"ocm.software/ocm/api/ocm/extensions/attrs/plugincacheattr"
20+
"ocm.software/ocm/api/ocm/plugin/registration"
21+
"ocm.software/ocm/api/utils/mime"
22+
"ocm.software/ocm/api/utils/runtime"
23+
"ocm.software/ocm/cmds/demoplugin/accessmethods"
24+
)
25+
26+
const (
27+
CONTENT = "testdata"
28+
)
29+
30+
var _ = Describe("demoplugin", func() {
31+
Context("lib", func() {
32+
var env *Builder
33+
var plugins TempPluginDir
34+
var osf *os.File
35+
36+
BeforeEach(func() {
37+
env = NewBuilder(TestData())
38+
plugins = Must(ConfigureTestPlugins(env, "testdata"))
39+
40+
registry := plugincacheattr.Get(env)
41+
Expect(registration.RegisterExtensions(env)).To(Succeed())
42+
p := registry.Get("demo")
43+
Expect(p).NotTo(BeNil())
44+
Expect(p.Error()).To(Equal(""))
45+
46+
MustBeSuccessful(env.FileSystem().MkdirAll("data", 0o700))
47+
48+
f := Must(env.FileSystem().Create("/data/test"))
49+
Must(f.Write([]byte(CONTENT)))
50+
51+
osf = utils.OSFile(f)
52+
Expect(osf).NotTo(BeNil())
53+
})
54+
55+
AfterEach(func() {
56+
plugins.Cleanup()
57+
env.Cleanup()
58+
})
59+
60+
It("get templ file", func() {
61+
p := osf.Name()[len(os.TempDir())+1:]
62+
fmt.Printf("%s\n", p)
63+
64+
spec := accessmethods.AccessSpec{
65+
ObjectVersionedType: runtime.NewVersionedTypedObject(accessmethods.NAME),
66+
Path: p,
67+
MediaType: mime.MIME_TEXT,
68+
}
69+
a := Must(env.OCMContext().AccessSpecForSpec(spec))
70+
71+
cv := &cpi.DummyComponentVersionAccess{Context: env.OCMContext()}
72+
hints := cpi.ReferenceHint(a, cv)
73+
Expect(len(hints)).To(Equal(1))
74+
Expect(hints[0].GetReference()).To(Equal(p))
75+
Expect(a.Describe(env.OCMContext())).To(Equal("temp file " + p))
76+
77+
m := Must(a.AccessMethod(cv))
78+
data := Must(m.Get())
79+
80+
Expect(string(data)).To(Equal(CONTENT))
81+
})
82+
})
83+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package accessmethods_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestConfig(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Demo Plugin Acces Methods Test Suite")
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
go run ../main.go "$@"

cmds/demoplugin/uploaders/demo.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@ func init() {
3737
if err != nil {
3838
panic(err)
3939
}
40-
types = ppi.UploadFormats{NAME + runtime.VersionSeparator + VERSION: decoder}
40+
types = ppi.UploadFormats{
41+
NAME + runtime.VersionSeparator + VERSION: decoder,
42+
NAME: decoder,
43+
}
44+
}
45+
46+
func NewTarget(p string) *TargetSpec {
47+
return &TargetSpec{
48+
ObjectVersionedType: runtime.NewVersionedTypedObject(NAME),
49+
Path: p,
50+
}
4151
}
4252

4353
type Uploader struct {
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
})
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package uploaders_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestConfig(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Demo Plugin Uploaders Methods Test Suite")
13+
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
go run ../main.go "$@"

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ require (
4444
github.com/mandelsoft/goutils v0.0.0-20241005173814-114fa825bbdc
4545
github.com/mandelsoft/logging v0.0.0-20240618075559-fdca28a87b0a
4646
github.com/mandelsoft/spiff v1.7.0-beta-6
47-
github.com/mandelsoft/vfs v0.4.4
47+
github.com/mandelsoft/vfs v0.4.5-0.20241116195817-0f273ab32e08
4848
github.com/marstr/guid v1.1.0
4949
github.com/mikefarah/yq/v4 v4.44.6
5050
github.com/mitchellh/copystructure v1.2.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,8 @@ github.com/mandelsoft/spiff v1.7.0-beta-6 h1:0h2Si4u73Ys1TDK9MFzwzcsVZTviN3xHWvL
703703
github.com/mandelsoft/spiff v1.7.0-beta-6/go.mod h1:HXurS33cKPLXXAI3SYll+Z6QotB1yzFFDyOFZ4QODcA=
704704
github.com/mandelsoft/vfs v0.4.4 h1:hq+nI7NWzLLWR3Ii/w4agup4KpWjLpw6dAGtmvWr1Vw=
705705
github.com/mandelsoft/vfs v0.4.4/go.mod h1:3ODt1ze/dCdOJCbhHX8ARAw7l422fDZUhbt0wqplBRs=
706+
github.com/mandelsoft/vfs v0.4.5-0.20241116195817-0f273ab32e08 h1:L8gm2bTrd3YjjabU5AlsVUQCOcc62cyq0pOLYtlcKIk=
707+
github.com/mandelsoft/vfs v0.4.5-0.20241116195817-0f273ab32e08/go.mod h1:3ODt1ze/dCdOJCbhHX8ARAw7l422fDZUhbt0wqplBRs=
706708
github.com/marstr/guid v1.1.0 h1:/M4H/1G4avsieL6BbUwCOBzulmoeKVP5ux/3mQNnbyI=
707709
github.com/marstr/guid v1.1.0/go.mod h1:74gB1z2wpxxInTG6yaqA7KrtM0NZ+RbrcqDvYHefzho=
708710
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=

0 commit comments

Comments
 (0)