Skip to content

Commit

Permalink
eliminate lib to cli dependencies (#741)
Browse files Browse the repository at this point in the history
## Description

There are dependencies from the library (package pkg) to the cli (cmds).
This is because of
- using command executions in plugin tests
- using some parsing provided by cli in some app support
- using cli based test environment in lib tests

The cli based test environment can be replaced by the lib test
environment, it was just
accidently used.

The parsing support has been moved to a cli support package in the lib.

The plugin tests based on command executions have been moved to a plugin
test package in the
top-level cli package. Additionally, the lib now tests the pure lib
version of the plugin usage
without command executions.

## What type of PR is this? (check all applicable)

- [ ] 🍕 Feature
- [x] 🎇 Restructuring
- [ ] 🐛 Bug Fix
- [ ] 📝 Documentation Update
- [ ] 🎨 Style
- [ ] 🧑‍💻 Code Refactor
- [ ] 🔥 Performance Improvements
- [x] ✅ Test
- [x] 🤖 Build
- [ ] 🔁 CI
- [ ] 📦 Chore (Release)
- [ ] ⏩ Revert

## Related Tickets & Documents

<!-- 
Please use this format link issue numbers: Fixes #123

https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
-->
- Related Issue # (issue)
- Closes # (issue)
- Fixes # (issue)
> Remove if not applicable

## Screenshots

<!-- Visual changes require screenshots -->


## Added tests?

- [ ] 👍 yes
- [ ] 🙅 no, because they aren't needed
- [ ] 🙋 no, because I need help
- [ ] Separate ticket for tests # (issue/pr)

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration


## Added to documentation?

- [ ] 📜 README.md
- [ ] 🙅 no documentation needed

## Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules
  • Loading branch information
mandelsoft authored Apr 24, 2024
1 parent bcf0741 commit 600f585
Show file tree
Hide file tree
Showing 19 changed files with 474 additions and 113 deletions.
1 change: 1 addition & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Please include a summary of the changes and the related issue. Please also inclu
## What type of PR is this? (check all applicable)

- [ ] 🍕 Feature
- [ ] 🎇 Restructuring
- [ ] 🐛 Bug Fix
- [ ] 📝 Documentation Update
- [ ] 🎨 Style
Expand Down
2 changes: 1 addition & 1 deletion cmds/ocm/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"unicode"

common2 "github.com/open-component-model/ocm/pkg/clisupport"
_ "github.com/open-component-model/ocm/pkg/contexts/clictx/config"
_ "github.com/open-component-model/ocm/pkg/contexts/ocm/attrs"

Expand All @@ -24,7 +25,6 @@ import (
creds "github.com/open-component-model/ocm/cmds/ocm/commands/misccmds/credentials"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocicmds"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds"
common2 "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/componentarchive"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/components"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/plugins"
Expand Down
6 changes: 3 additions & 3 deletions cmds/ocm/commands/ocmcmds/componentarchive/create/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"strings"

"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/open-component-model/ocm/pkg/clisupport"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/open-component-model/ocm/cmds/ocm/commands/common/options/formatoption"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/options/fileoption"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/options/schemaoption"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/names"
Expand Down Expand Up @@ -91,7 +91,7 @@ func (o *Command) Complete(args []string) error {
o.Version = args[1]

for _, a := range args[2:] {
o.Labels, err = common.AddParsedLabel(o.FileSystem(), o.Labels, a)
o.Labels, err = clisupport.AddParsedLabel(o.FileSystem(), o.Labels, a)
if err != nil {
return err
}
Expand All @@ -104,7 +104,7 @@ func (o *Command) Complete(args []string) error {
o.Provider = a
continue
}
o.ProviderLabels, err = common.AddParsedLabel(o.FileSystem(), o.ProviderLabels, a)
o.ProviderLabels, err = clisupport.AddParsedLabel(o.FileSystem(), o.ProviderLabels, a)
if err != nil {
return err
}
Expand Down
79 changes: 79 additions & 0 deletions cmds/ocm/commands/ocmcmds/plugins/tests/accessmethods/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package accessmethods_test

import (
"encoding/json"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/open-component-model/ocm/cmds/ocm/testhelper"
. "github.com/open-component-model/ocm/pkg/testutils"

"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/plugincacheattr"
"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/plugindirattr"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
metav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/plugins"
"github.com/open-component-model/ocm/pkg/contexts/ocm/registration"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/comparch"
)

const CA = "/tmp/ca"
const VERSION = "v1"

var _ = Describe("Add with new access method", func() {
var env *TestEnv
var ctx ocm.Context
var registry plugins.Set

BeforeEach(func() {
env = NewTestEnv(TestData())
ctx = env.OCMContext()

plugindirattr.Set(ctx, "testdata")
registry = plugincacheattr.Get(ctx)
Expect(registration.RegisterExtensions(ctx)).To(Succeed())
p := registry.Get("test")
Expect(p).NotTo(BeNil())

Expect(env.Execute("create", "ca", "-ft", "directory", "test.de/x", VERSION, "--provider", "mandelsoft", "--file", CA)).To(Succeed())
})

AfterEach(func() {
env.Cleanup()
})

It("adds external resource by options", func() {
Expect(env.Execute("add", "resources", CA,
"--type", "testContent",
"--name", "text",
"--version", "v0.1.0",
"--accessType", "test",
"--accessPath", "textfile",
"--mediaType", "text/plain")).To(Succeed())
data := Must(env.ReadFile(env.Join(CA, comparch.ComponentDescriptorFileName)))
cd := Must(compdesc.Decode(data))
Expect(len(cd.Resources)).To(Equal(1))

r := Must(cd.GetResourceByIdentity(metav1.NewIdentity("text")))
Expect(r.Type).To(Equal("testContent"))
Expect(r.Version).To(Equal("v0.1.0"))
Expect(r.Relation).To(Equal(metav1.ResourceRelation("external")))

Expect(r.Access.GetType()).To(Equal("test"))
acc := Must(env.OCMContext().AccessSpecForSpec(r.Access))
var myacc AccessSpec

MustBeSuccessful(json.Unmarshal(Must(json.Marshal(acc)), &myacc))
Expect(myacc).To(Equal(AccessSpec{Type: "test", Path: "textfile", MediaType: "text/plain"}))

m := Must(acc.AccessMethod(&cpi.DummyComponentVersionAccess{env.OCMContext()}))
data = Must(m.Get())
Expect(string(data)).To(Equal("test content\n{\"mediaType\":\"text/plain\",\"path\":\"textfile\",\"type\":\"test\"}\n"))
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package accessmethods_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

type AccessSpec struct {
Type string `json:"type"`
Path string `json:"path"`
MediaType string `json:"mediaType"`
}

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Access Methods Plugin Test Suite")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash

# SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
#
# SPDX-License-Identifier: Apache-2.0

NAME="$(basename "$0")"

Error() {
echo '{ "error": "'$1'" }' >&2
exit 1
}

extract() {
v="$(echo "$2" | sed 's/.*"'"$1"'": *"\([^"]*\)".*/\1/')"
if [ "$v" != "$2" ]; then
echo "$v"
fi
}

setfield() {
local v
s="$(echo "$2" | sed 's/\//\\\//g')"
v="$(echo "$BASE" | sed 's/"'"$1"'": *"[^"]*"/"'"$1"'":"'"$s"'"/')"
if [ "$v" == "$BASE" ]; then
v="$(echo "$BASE" | sed 's/^{"/{"'"$1"'":"'"$s"'","/')"
fi
if [ "$v" == "$BASE" ]; then
v="$(echo "$BASE" | sed 's/^{/{"'"$1"'":"'"$s"'"/')"
fi
BASE="$v"
}

setopt() {
local v
v="$(extract "$1" "$OPTS")"
if [ -n "$v" ]; then
setfield "$2" "$v"
fi
}

Info() {
PATHOPT='{"name":"accessPath","type":"string","description":"file path"}'
MEDIAOPT='{"name":"mediaType"}'
OPTS='['$PATHOPT','$MEDIAOPT']'
echo '{"version":"v1","pluginName":"'$NAME'","pluginVersion":"v1","shortDescription":"a test plugin","description":"a test plugin with access method test","accessMethods":[{"name":"test","shortDescription":"test access","description":"","options":'$OPTS'},{"name":"test","version":"v1","shortDescription":"test access","description":""}]}
'
}

Get() {
echo "test content"
for arg; do
echo "$arg"
done
}

Validate() {
echo '{"short":"a test","mediaType":"plain/text","description":"","hint":"testfile","consumerId":{"hostname":"localhost","type":"test"}}'
}

Compose() {
BASE="$3"
OPTS="$2"

setopt accessPath path
setopt mediaType mediaType
echo "$BASE"
}

AccessMethod() {
case "$1" in
get) Get "${@:2}";;
validate) Validate "${@:2}";;
compose) Compose "${@:2}";;
*) Error "invalid accessmethod command $1";;
esac
}

case "$1" in
info) Info;;
accessmethod) AccessMethod "${@:2}";;
*) Error "invalid command $1";;
esac
69 changes: 69 additions & 0 deletions cmds/ocm/commands/ocmcmds/plugins/tests/routingslips/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package routingslips_test

import (
"bytes"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/open-component-model/ocm/cmds/ocm/testhelper"
. "github.com/open-component-model/ocm/pkg/testutils"

"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/common/accessobj"
"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/plugincacheattr"
"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/plugindirattr"
"github.com/open-component-model/ocm/pkg/contexts/ocm/labels/routingslip"
"github.com/open-component-model/ocm/pkg/contexts/ocm/registration"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/ctf"
)

const ARCH = "/tmp/ca"
const VERSION = "v1"
const COMP = "test.de/x"
const PROVIDER = "acme.org"

var _ = Describe("Test Environment", func() {
var env *TestEnv

BeforeEach(func() {
env = NewTestEnv()
env.OCMCommonTransport(ARCH, accessio.FormatDirectory, func() {
env.Component(COMP, func() {
env.Version(VERSION, func() {
env.Provider(PROVIDER)
})
})
})
env.RSAKeyPair(PROVIDER)

ctx := env.OCMContext()
plugindirattr.Set(ctx, "testdata")
registry := plugincacheattr.Get(ctx)
Expect(registration.RegisterExtensions(ctx)).To(Succeed())
p := registry.Get("test")
Expect(p).NotTo(BeNil())
})

AfterEach(func() {
env.Cleanup()
})

It("adds entry by plugin option", func() {
buf := bytes.NewBuffer(nil)
Expect(env.CatchOutput(buf).Execute("add", "routingslip", ARCH, PROVIDER, "test", "--accessPath", "some path", "--mediaType", "media type")).To(Succeed())
Expect(buf.String()).To(StringEqualTrimmedWithContext(
`
`))
repo := Must(ctf.Open(env, accessobj.ACC_READONLY, ARCH, 0, env))
defer Close(repo, "repo")
cv := Must(repo.LookupComponentVersion(COMP, VERSION))
defer Close(cv, "cv")
slip := Must(routingslip.GetSlip(cv, PROVIDER))
Expect(slip.Len()).To(Equal(1))
Expect(Must(slip.Get(0).Payload.Evaluate(env.OCMContext())).Describe(env.OCMContext())).To(Equal("a test"))
})
})
17 changes: 17 additions & 0 deletions cmds/ocm/commands/ocmcmds/plugins/tests/routingslips/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package routingslips_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Routing Slips Plugin Test Suite")
}
Loading

0 comments on commit 600f585

Please sign in to comment.