Skip to content

Commit bad8a25

Browse files
committed
feat: component getter tests
1 parent 58b1b1e commit bad8a25

File tree

3 files changed

+108
-18
lines changed

3 files changed

+108
-18
lines changed

internal/ocm-cli/component_getter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func (g *ComponentGetter) TemplatesComponentVersion() *ComponentVersion {
6969
return g.templatesComponentVersion
7070
}
7171

72+
func (g *ComponentGetter) TemplatesResourceName() string {
73+
return g.templatesResourceName
74+
}
75+
7276
func getReferencedComponentVersion(ctx context.Context, repo string, parentCV *ComponentVersion, refName string, ocmConfig string) (*ComponentVersion, error) {
7377
ref, err := parentCV.GetComponentReference(refName)
7478
if err != nil {
Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ocm_cli_test
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -11,23 +12,93 @@ import (
1112

1213
func TestComponentGetter(t *testing.T) {
1314
const (
14-
rootComponentName = "github.com/openmcp-project/openmcp"
15+
componentA = "github.com/openmcp-project/bootstrapper/test-component-getter-a"
16+
componentB = "github.com/openmcp-project/bootstrapper/test-component-getter-b"
17+
componentC = "github.com/openmcp-project/bootstrapper/test-component-getter-c"
18+
version001 = "v0.0.1"
1519
templatesComponentName = "github.com/openmcp-project/gitops-templates"
1620
)
1721

1822
testutil.DownloadOCMAndAddToPath(t)
1923
ctf := testutil.BuildComponent("./testdata/02/component-constructor.yaml", t)
20-
rootLocation := ctf + "//github.com/openmcp-project/openmcp:v0.0.11"
21-
g := ocmcli.NewComponentGetter(rootLocation, "gitops-templates/test-resource", ocmcli.NoOcmConfig)
2224

23-
err := g.InitializeComponents(t.Context())
24-
assert.NoError(t, err, "Error initializing components")
25+
testCases := []struct {
26+
desc string
27+
rootLocation string
28+
deployTemplates string
29+
expectInitializationError bool
30+
expectedTemplatesComponentName string
31+
expectResourceError bool
32+
expectedResourceName string
33+
}{
34+
{
35+
desc: "should get a resource of the root component",
36+
rootLocation: fmt.Sprintf("%s//%s:%s", ctf, componentA, version001),
37+
deployTemplates: "test-resource-a",
38+
expectedTemplatesComponentName: componentA,
39+
expectedResourceName: "test-resource-a",
40+
},
41+
{
42+
desc: "should get a resource of a referenced component",
43+
rootLocation: fmt.Sprintf("%s//%s:%s", ctf, componentA, version001),
44+
deployTemplates: "reference-b/test-resource-b",
45+
expectedTemplatesComponentName: componentB,
46+
expectedResourceName: "test-resource-b",
47+
},
48+
{
49+
desc: "should get a resource of a nested referenced component",
50+
rootLocation: fmt.Sprintf("%s//%s:%s", ctf, componentA, version001),
51+
deployTemplates: "reference-b/reference-c/test-resource-c",
52+
expectedTemplatesComponentName: componentC,
53+
expectedResourceName: "test-resource-c",
54+
},
55+
{
56+
desc: "should fail for an unknown root component",
57+
rootLocation: fmt.Sprintf("%s//%s:%s", ctf, "unknown-component", version001),
58+
deployTemplates: "reference-b/test-resource-b",
59+
expectInitializationError: true,
60+
},
61+
{
62+
desc: "should fail for an unknown component reference",
63+
rootLocation: fmt.Sprintf("%s//%s:%s", ctf, componentA, version001),
64+
deployTemplates: "unknown-reference/test-resource-b",
65+
expectInitializationError: true,
66+
},
67+
{
68+
desc: "should fail for an unknown resource",
69+
rootLocation: fmt.Sprintf("%s//%s:%s", ctf, componentA, version001),
70+
deployTemplates: "reference-b/unknown-resource",
71+
expectedTemplatesComponentName: componentB,
72+
expectResourceError: true,
73+
},
74+
}
2575

26-
rootComponentVersion := g.RootComponentVersion()
27-
assert.NotNil(t, rootComponentVersion, "Root component version should not be nil")
28-
assert.Equal(t, rootComponentName, rootComponentVersion.Component.Name, "Root component name should match")
76+
for _, tc := range testCases {
77+
t.Run(tc.desc, func(t *testing.T) {
78+
g := ocmcli.NewComponentGetter(tc.rootLocation, tc.deployTemplates, ocmcli.NoOcmConfig)
2979

30-
templatesComponentVersion := g.TemplatesComponentVersion()
31-
assert.NotNil(t, templatesComponentVersion, "Templates component version should not be nil")
32-
assert.Equal(t, templatesComponentName, templatesComponentVersion.Component.Name, "Templates component name should match")
80+
err := g.InitializeComponents(t.Context())
81+
if tc.expectInitializationError {
82+
assert.Error(t, err, "Expected an error initializing components")
83+
return
84+
}
85+
assert.NoError(t, err, "Error initializing components")
86+
87+
rootComponentVersion := g.RootComponentVersion()
88+
assert.NotNil(t, rootComponentVersion, "Root component version should not be nil")
89+
assert.Equal(t, componentA, rootComponentVersion.Component.Name, "Root component name should match")
90+
91+
templatesComponentVersion := g.TemplatesComponentVersion()
92+
assert.NotNil(t, templatesComponentVersion, "Templates component version should not be nil")
93+
assert.Equal(t, tc.expectedTemplatesComponentName, templatesComponentVersion.Component.Name, "Templates component name should match")
94+
95+
resource, err := templatesComponentVersion.GetResource(g.TemplatesResourceName())
96+
if tc.expectResourceError {
97+
assert.Error(t, err, "Expected an error getting resource")
98+
return
99+
}
100+
assert.NoError(t, err, "Error getting resource")
101+
assert.Equal(t, tc.expectedResourceName, resource.Name, "Resource name should match")
102+
})
103+
}
33104
}

internal/ocm-cli/testdata/02/component-constructor.yaml

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
11
components:
22

3-
- name: github.com/openmcp-project/openmcp
4-
version: v0.0.11
3+
- name: github.com/openmcp-project/bootstrapper/test-component-getter-a
4+
version: v0.0.1
5+
provider:
6+
name: openmcp-project
7+
componentReferences:
8+
- componentName: github.com/openmcp-project/bootstrapper/test-component-getter-b
9+
name: reference-b
10+
version: v0.0.1
11+
resources:
12+
- name: test-resource-a
13+
type: blob
14+
input:
15+
type: file
16+
path: ./test-resource.yaml
17+
18+
- name: github.com/openmcp-project/bootstrapper/test-component-getter-b
19+
version: v0.0.1
520
provider:
621
name: openmcp-project
722
componentReferences:
8-
- componentName: github.com/openmcp-project/gitops-templates
9-
name: gitops-templates
23+
- componentName: github.com/openmcp-project/bootstrapper/test-component-getter-c
24+
name: reference-c
1025
version: v0.0.1
1126
resources:
12-
- name: test-resource
27+
- name: test-resource-b
1328
type: blob
1429
input:
1530
type: file
1631
path: ./test-resource.yaml
1732

18-
- name: github.com/openmcp-project/gitops-templates
33+
- name: github.com/openmcp-project/bootstrapper/test-component-getter-c
1934
version: v0.0.1
2035
provider:
2136
name: openmcp-project
2237
resources:
23-
- name: test-resource
38+
- name: test-resource-c
2439
type: blob
2540
input:
2641
type: file

0 commit comments

Comments
 (0)