Skip to content

Commit 02aaf33

Browse files
committed
add tests
1 parent 751b588 commit 02aaf33

File tree

8 files changed

+163
-7
lines changed

8 files changed

+163
-7
lines changed

cmd/ocmTransfer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var ocmTransferCmd = &cobra.Command{
3838
}
3939

4040
func init() {
41-
rootCmd.AddCommand(ocmTransferCmd)
41+
RootCmd.AddCommand(ocmTransferCmd)
4242

4343
ocmTransferCmd.PersistentFlags().StringP("config", "c", "", "ocm configuration file")
4444
}

cmd/ocmTransfer_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cmd_test
2+
3+
import (
4+
"errors"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
10+
"github.com/openmcp-project/bootstrapper/cmd"
11+
testutil "github.com/openmcp-project/bootstrapper/test/utils"
12+
)
13+
14+
func TestOcmTransfer(t *testing.T) {
15+
expectError := errors.New("expected error")
16+
17+
testutil.DownloadOCMAndAddToPath(t)
18+
19+
ctfIn := testutil.BuildComponent("./testdata/component-constructor.yaml", t)
20+
ctfOut := filepath.Join(t.TempDir(), "ctfOut")
21+
22+
testCases := []struct {
23+
desc string
24+
arguments []string
25+
expectedError error
26+
}{
27+
{
28+
desc: "No arguments specified",
29+
arguments: []string{},
30+
expectedError: expectError,
31+
},
32+
{
33+
desc: "One argument specified",
34+
arguments: []string{"source"},
35+
expectedError: expectError,
36+
},
37+
{
38+
desc: "Two arguments specified",
39+
arguments: []string{ctfIn, ctfOut},
40+
expectedError: nil,
41+
},
42+
}
43+
44+
for _, tc := range testCases {
45+
t.Run(tc.desc, func(t *testing.T) {
46+
root := cmd.RootCmd
47+
args := []string{"ocmTransfer"}
48+
if len(tc.arguments) > 0 {
49+
args = append(args, tc.arguments...)
50+
}
51+
root.SetArgs(args)
52+
53+
err := root.Execute()
54+
if tc.expectedError != nil {
55+
assert.Error(t, err)
56+
} else {
57+
assert.NoError(t, err)
58+
}
59+
})
60+
}
61+
}

cmd/root.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9-
// rootCmd represents the base command when called without any subcommands
10-
var rootCmd = &cobra.Command{
9+
// RootCmd represents the base command when called without any subcommands
10+
var RootCmd = &cobra.Command{
1111
Use: "bootstrapper",
1212
Short: "A brief description of your application",
1313
Long: `A longer description that spans multiple lines and likely contains
@@ -24,7 +24,7 @@ to quickly create a Cobra application.`,
2424
// Execute adds all child commands to the root command and sets flags appropriately.
2525
// This is called by main.main(). It only needs to happen once to the rootCmd.
2626
func Execute() {
27-
err := rootCmd.Execute()
27+
err := RootCmd.Execute()
2828
if err != nil {
2929
os.Exit(1)
3030
}
@@ -39,5 +39,5 @@ func init() {
3939

4040
// Cobra also supports local flags, which will only run
4141
// when this action is called directly.
42-
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
42+
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
4343
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
components:
2+
- name: github.com/openmcp-project/bootstrapper/test
3+
version: v0.0.1
4+
provider:
5+
name: openmcp-project

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ go 1.24.5
44

55
require (
66
github.com/spf13/cobra v1.9.1
7+
github.com/stretchr/testify v1.10.0
78
gopkg.in/yaml.v3 v3.0.1
89
)
910

1011
require (
12+
github.com/davecgh/go-spew v1.1.1 // indirect
1113
github.com/inconshreveable/mousetrap v1.1.0 // indirect
14+
github.com/pmezard/go-difflib v1.0.0 // indirect
1215
github.com/spf13/pflag v1.0.7 // indirect
1316
)

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
35
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
6+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
7+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
48
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
59
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
610
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
711
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
812
github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
913
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
14+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
15+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1017
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1118
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1219
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

renovate.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@
3939
],
4040
"customManagers": [
4141
{
42-
"description": "All component dependencies and their versions used in the Dockerfile.",
42+
"description": "All component dependencies in other locations.",
4343
"customType": "regex",
4444
"managerFilePatterns": [
45-
"/Dockerfile/"
45+
"/Dockerfile/",
46+
"/test/util/ocm.go"
4647
],
4748
"matchStrings": [
4849
"# renovate: datasource=(?<datasource>[a-z-.]+?) depName=(?<depName>[^\\s]+?)(?: (lookupName|packageName)=(?<packageName>[^\\s]+?))?(?: versioning=(?<versioning>[^\\s]+?))?(?: extractVersion=(?<extractVersion>[^\\s]+?))?(?: registryUrl=(?<registryUrl>[^\\s]+?))?\\s.+?(_version|_VERSION)=\"?(?<currentValue>.+?)\"?\\s"

test/utils/ocm.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package utils
2+
3+
import (
4+
"io"
5+
"net/http"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"runtime"
10+
"testing"
11+
)
12+
13+
const (
14+
// renovate: datasource=github-releases depName=ocm packageName=open-component-model/ocm
15+
OCM_VERSION = "0.27.0"
16+
)
17+
18+
// DownloadOCMAndAddToPath downloads the OCM cli for the current platform and puts it to the PATH of the test
19+
func DownloadOCMAndAddToPath(t *testing.T) {
20+
t.Helper()
21+
22+
downloadURL := "https://github.com/open-component-model/ocm/releases/download/v" +
23+
OCM_VERSION + "/ocm-" + OCM_VERSION + "-" + runtime.GOOS + "-" + runtime.GOARCH + ".tar.gz"
24+
25+
tempDir := t.TempDir()
26+
archivePath := filepath.Join(tempDir, "ocm.tar.gz")
27+
out, err := os.Create(archivePath)
28+
if err != nil {
29+
t.Fatalf("failed to create file: %v", err)
30+
}
31+
defer out.Close()
32+
33+
resp, err := http.Get(downloadURL)
34+
if err != nil {
35+
t.Fatalf("failed to download ocm: %v", err)
36+
}
37+
defer resp.Body.Close()
38+
39+
_, err = io.Copy(out, resp.Body)
40+
if err != nil {
41+
t.Fatalf("failed to save ocm: %v", err)
42+
}
43+
44+
// Extract the tar.gz
45+
cmd := exec.Command("tar", "-xzf", archivePath, "-C", tempDir)
46+
if err := cmd.Run(); err != nil {
47+
t.Fatalf("failed to extract ocm: %v", err)
48+
}
49+
50+
// Find the ocm binary
51+
ocmPath := filepath.Join(tempDir, "ocm")
52+
if _, err := os.Stat(ocmPath); err != nil {
53+
t.Fatalf("ocm binary not found after extraction: %v", err)
54+
}
55+
56+
t.Setenv("PATH", tempDir+":"+os.Getenv("PATH"))
57+
}
58+
59+
// BuildComponent builds the component for the specified componentConstructorLocation and returns the ctf out directory.
60+
func BuildComponent(componentConstructorLocation string, t *testing.T) string {
61+
tempDir := t.TempDir()
62+
ctfDir := filepath.Join(tempDir, "ctf")
63+
64+
cmd := exec.Command("ocm", []string{
65+
"add",
66+
"componentversions",
67+
"--create",
68+
"--file",
69+
ctfDir,
70+
componentConstructorLocation}...)
71+
72+
out, err := cmd.CombinedOutput()
73+
t.Log("OCM Output:", string(out))
74+
if err != nil {
75+
t.Fatalf("failed to build component: %v", err)
76+
}
77+
78+
return ctfDir
79+
}

0 commit comments

Comments
 (0)