11package ocm_cli_test
22
33import (
4+ "fmt"
45 "testing"
56
67 "github.com/stretchr/testify/assert"
@@ -11,23 +12,93 @@ import (
1112
1213func 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}
0 commit comments