-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eliminate lib to cli dependencies (#741)
## 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
1 parent
bcf0741
commit 600f585
Showing
19 changed files
with
474 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
cmds/ocm/commands/ocmcmds/plugins/tests/accessmethods/cmd_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
}) |
23 changes: 23 additions & 0 deletions
23
cmds/ocm/commands/ocmcmds/plugins/tests/accessmethods/suite_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
83 changes: 83 additions & 0 deletions
83
cmds/ocm/commands/ocmcmds/plugins/tests/accessmethods/testdata/test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
69
cmds/ocm/commands/ocmcmds/plugins/tests/routingslips/cmd_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
cmds/ocm/commands/ocmcmds/plugins/tests/routingslips/suite_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Oops, something went wrong.