From 0372db2a3da92681694d42b7fe8514e3d738c7fd Mon Sep 17 00:00:00 2001 From: Stephanie Date: Tue, 13 Apr 2021 19:38:14 -0400 Subject: [PATCH 1/7] add source attribute function Signed-off-by: Stephanie --- pkg/devfile/parser/sourceAttribute.go | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 pkg/devfile/parser/sourceAttribute.go diff --git a/pkg/devfile/parser/sourceAttribute.go b/pkg/devfile/parser/sourceAttribute.go new file mode 100644 index 00000000..4132c14a --- /dev/null +++ b/pkg/devfile/parser/sourceAttribute.go @@ -0,0 +1,37 @@ +package parser + +import ( + v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" + "github.com/devfile/api/v2/pkg/attributes" +) + +const ImportSourceAttribute = "library.devfile.io/imported-by" + +// AddSourceAttributesForTemplateSpec adds an attribute 'library.devfile.io/imported-by=' to all elements of +// that support attributes. +func AddSourceAttributesForTemplateSpec(sourceID string, template *v1.DevWorkspaceTemplateSpec) { + for idx, component := range template.Components { + if component.Attributes == nil { + template.Components[idx].Attributes = attributes.Attributes{} + } + template.Components[idx].Attributes.PutString(ImportSourceAttribute, sourceID) + } + for idx, command := range template.Commands { + if command.Attributes == nil { + template.Commands[idx].Attributes = attributes.Attributes{} + } + template.Commands[idx].Attributes.PutString(ImportSourceAttribute, sourceID) + } + for idx, project := range template.Projects { + if project.Attributes == nil { + template.Projects[idx].Attributes = attributes.Attributes{} + } + template.Projects[idx].Attributes.PutString(ImportSourceAttribute, sourceID) + } + for idx, project := range template.StarterProjects { + if project.Attributes == nil { + template.StarterProjects[idx].Attributes = attributes.Attributes{} + } + template.StarterProjects[idx].Attributes.PutString(ImportSourceAttribute, sourceID) + } +} \ No newline at end of file From a33f7e3db6d5d1792c4bbf440521088a9b950374 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Wed, 14 Apr 2021 18:51:33 -0400 Subject: [PATCH 2/7] added functions for parentoverrides and pluginoverrides Signed-off-by: Stephanie --- pkg/devfile/parser/parse.go | 10 ++++ pkg/devfile/parser/resolutionContext.go | 2 +- pkg/devfile/parser/sourceAttribute.go | 67 ++++++++++++++++++++++--- 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/pkg/devfile/parser/parse.go b/pkg/devfile/parser/parse.go index ad3c70c0..071d7b6e 100644 --- a/pkg/devfile/parser/parse.go +++ b/pkg/devfile/parser/parse.go @@ -211,7 +211,12 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool } parentWorkspaceContent := parentDevfileObj.Data.GetDevfileWorkspaceSpecContent() + // add attribute to parent elements + AddSourceAttributesForTemplateSpecContent(parent.ImportReference, parentWorkspaceContent) if !reflect.DeepEqual(parent.ParentOverrides, v1.ParentOverrides{}) { + // add attribute to parentOverrides elements + curNodeImportReference := resolveCtx.importReference + AddSourceAttributesForParentOverride(curNodeImportReference, &parent.ParentOverrides) flattenedParent, err = apiOverride.OverrideDevWorkspaceTemplateSpec(parentWorkspaceContent, parent.ParentOverrides) if err != nil { return err @@ -250,8 +255,13 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool return fmt.Errorf("plugin %s does not define any resources", component.Name) } pluginWorkspaceContent := pluginDevfileObj.Data.GetDevfileWorkspaceSpecContent() + // add attribute to plugin elements + AddSourceAttributesForTemplateSpecContent(parent.ImportReference, pluginWorkspaceContent) flattenedPlugin := pluginWorkspaceContent if !reflect.DeepEqual(plugin.PluginOverrides, v1.PluginOverrides{}) { + // add attribute to parentOverrides elements + curNodeImportReference := resolveCtx.importReference + AddSourceAttributesForPluginOverride(curNodeImportReference, plugin.Id, &plugin.PluginOverrides) flattenedPlugin, err = apiOverride.OverrideDevWorkspaceTemplateSpec(pluginWorkspaceContent, plugin.PluginOverrides) if err != nil { return err diff --git a/pkg/devfile/parser/resolutionContext.go b/pkg/devfile/parser/resolutionContext.go index 5bb0d4bf..8eb9d2f3 100644 --- a/pkg/devfile/parser/resolutionContext.go +++ b/pkg/devfile/parser/resolutionContext.go @@ -61,4 +61,4 @@ func resolveImportReference(importReference v1.ImportReference) string { } // the first node return "main devfile" -} +} \ No newline at end of file diff --git a/pkg/devfile/parser/sourceAttribute.go b/pkg/devfile/parser/sourceAttribute.go index 4132c14a..d5625445 100644 --- a/pkg/devfile/parser/sourceAttribute.go +++ b/pkg/devfile/parser/sourceAttribute.go @@ -1,37 +1,88 @@ package parser import ( + "fmt" v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" "github.com/devfile/api/v2/pkg/attributes" ) -const ImportSourceAttribute = "library.devfile.io/imported-by" +const ImportSourceAttribute = "library.devfile.io/imported-from" -// AddSourceAttributesForTemplateSpec adds an attribute 'library.devfile.io/imported-by=' to all elements of -// that support attributes. -func AddSourceAttributesForTemplateSpec(sourceID string, template *v1.DevWorkspaceTemplateSpec) { +// AddSourceAttributesForTemplateSpecContent adds an attribute 'library.devfile.io/imported-from=' +// to all elements of template spec content that support attributes. +func AddSourceAttributesForTemplateSpecContent(sourceImportReference v1.ImportReference, template *v1.DevWorkspaceTemplateSpecContent) { for idx, component := range template.Components { if component.Attributes == nil { template.Components[idx].Attributes = attributes.Attributes{} } - template.Components[idx].Attributes.PutString(ImportSourceAttribute, sourceID) + template.Components[idx].Attributes.PutString(ImportSourceAttribute, resolveImportReference(sourceImportReference)) } for idx, command := range template.Commands { if command.Attributes == nil { template.Commands[idx].Attributes = attributes.Attributes{} } - template.Commands[idx].Attributes.PutString(ImportSourceAttribute, sourceID) + template.Commands[idx].Attributes.PutString(ImportSourceAttribute, resolveImportReference(sourceImportReference)) } for idx, project := range template.Projects { if project.Attributes == nil { template.Projects[idx].Attributes = attributes.Attributes{} } - template.Projects[idx].Attributes.PutString(ImportSourceAttribute, sourceID) + template.Projects[idx].Attributes.PutString(ImportSourceAttribute, resolveImportReference(sourceImportReference)) } for idx, project := range template.StarterProjects { if project.Attributes == nil { template.StarterProjects[idx].Attributes = attributes.Attributes{} } - template.StarterProjects[idx].Attributes.PutString(ImportSourceAttribute, sourceID) + template.StarterProjects[idx].Attributes.PutString(ImportSourceAttribute, resolveImportReference(sourceImportReference)) } +} + + +// AddSourceAttributesForParentOverride adds an attribute 'library.devfile.io/imported-from=' +// to all elements of parent override that support attributes. +func AddSourceAttributesForParentOverride(sourceImportReference v1.ImportReference, parentoverride *v1.ParentOverrides) { + for idx, component := range parentoverride.Components { + if component.Attributes == nil { + parentoverride.Components[idx].Attributes = attributes.Attributes{} + } + parentoverride.Components[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("parentOverrides from: %s", resolveImportReference(sourceImportReference))) + } + for idx, command := range parentoverride.Commands { + if command.Attributes == nil { + parentoverride.Commands[idx].Attributes = attributes.Attributes{} + } + parentoverride.Commands[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("parentOverrides from: %s", resolveImportReference(sourceImportReference))) + } + for idx, project := range parentoverride.Projects { + if project.Attributes == nil { + parentoverride.Projects[idx].Attributes = attributes.Attributes{} + } + parentoverride.Projects[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("parentOverrides from: %s", resolveImportReference(sourceImportReference))) + } + for idx, project := range parentoverride.StarterProjects { + if project.Attributes == nil { + parentoverride.StarterProjects[idx].Attributes = attributes.Attributes{} + } + parentoverride.StarterProjects[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("parentOverrides from: %s", resolveImportReference(sourceImportReference))) + } + +} + + +// AddSourceAttributesForPluginOverride adds an attribute 'library.devfile.io/imported-from=' +// to all elements of plugin override that support attributes. +func AddSourceAttributesForPluginOverride(sourceImportReference v1.ImportReference, pluginId string, pluginoverride *v1.PluginOverrides) { + for idx, component := range pluginoverride.Components { + if component.Attributes == nil { + pluginoverride.Components[idx].Attributes = attributes.Attributes{} + } + pluginoverride.Components[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("pluginOverrides from: %s, plugin : %s", resolveImportReference(sourceImportReference), pluginId)) + } + for idx, command := range pluginoverride.Commands { + if command.Attributes == nil { + pluginoverride.Commands[idx].Attributes = attributes.Attributes{} + } + pluginoverride.Commands[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("pluginOverrides from: %s, plugin : %s", resolveImportReference(sourceImportReference), pluginId)) + } + } \ No newline at end of file From 34e258c35e131e58a706ea938b31558d9d5fd7c3 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Fri, 16 Apr 2021 17:58:25 -0400 Subject: [PATCH 3/7] update unit tests Signed-off-by: Stephanie --- pkg/devfile/parser/parse.go | 4 +- pkg/devfile/parser/parse_test.go | 298 ++++++++++++------------ pkg/devfile/parser/resolutionContext.go | 2 +- pkg/devfile/parser/sourceAttribute.go | 10 +- 4 files changed, 153 insertions(+), 161 deletions(-) diff --git a/pkg/devfile/parser/parse.go b/pkg/devfile/parser/parse.go index 071d7b6e..d492d7d4 100644 --- a/pkg/devfile/parser/parse.go +++ b/pkg/devfile/parser/parse.go @@ -256,12 +256,12 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool } pluginWorkspaceContent := pluginDevfileObj.Data.GetDevfileWorkspaceSpecContent() // add attribute to plugin elements - AddSourceAttributesForTemplateSpecContent(parent.ImportReference, pluginWorkspaceContent) + AddSourceAttributesForTemplateSpecContent(plugin.ImportReference, pluginWorkspaceContent) flattenedPlugin := pluginWorkspaceContent if !reflect.DeepEqual(plugin.PluginOverrides, v1.PluginOverrides{}) { // add attribute to parentOverrides elements curNodeImportReference := resolveCtx.importReference - AddSourceAttributesForPluginOverride(curNodeImportReference, plugin.Id, &plugin.PluginOverrides) + AddSourceAttributesForPluginOverride(curNodeImportReference, component.Name, &plugin.PluginOverrides) flattenedPlugin, err = apiOverride.OverrideDevWorkspaceTemplateSpec(pluginWorkspaceContent, plugin.PluginOverrides) if err != nil { return err diff --git a/pkg/devfile/parser/parse_test.go b/pkg/devfile/parser/parse_test.go index b0df6c4a..21c5062e 100644 --- a/pkg/devfile/parser/parse_test.go +++ b/pkg/devfile/parser/parse_test.go @@ -14,6 +14,7 @@ import ( "testing" v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" + "github.com/devfile/api/v2/pkg/attributes" devfilepkg "github.com/devfile/api/v2/pkg/devfile" devfileCtx "github.com/devfile/library/pkg/devfile/parser/context" v2 "github.com/devfile/library/pkg/devfile/parser/data/v2" @@ -26,6 +27,74 @@ import ( const schemaV200 = "2.0.0" func Test_parseParentAndPluginFromURI(t *testing.T) { + const uri1 = "127.0.0.1:8080" + const uri2 = "127.0.0.1:9090" + const pluginName = "plugincomp" + importFromUri1 := attributes.Attributes{} + importFromUri1.PutString(ImportSourceAttribute, fmt.Sprintf("uri: http://%s", uri1)) + importFromUri2 := attributes.Attributes{} + importFromUri2.PutString(ImportSourceAttribute, fmt.Sprintf("uri: http://%s", uri2)) + parentOverridesFromMainDevfile := attributes.Attributes{} + parentOverridesFromMainDevfile.PutString(ImportSourceAttribute, "parentOverrides from: main devfile") + pluginOverridesFromMainDevfile := attributes.Attributes{} + pluginOverridesFromMainDevfile.PutString(ImportSourceAttribute, fmt.Sprintf("pluginOverrides from: main devfile, plugin : %s", pluginName)) + + parentDevfile := DevfileObj{ + Data: &v2.DevfileV2{ + Devfile: v1.Devfile{ + DevfileHeader: devfilepkg.DevfileHeader{ + SchemaVersion: schemaV200, + }, + DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ + DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ + Commands: []v1.Command{ + { + Id: "devrun", + CommandUnion: v1.CommandUnion{ + Exec: &v1.ExecCommand{ + WorkingDir: "/projects", + CommandLine: "npm run", + }, + }, + }, + }, + Components: []v1.Component{ + { + Name: "nodejs", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Container: v1.Container{ + Image: "quay.io/nodejs-10", + }, + }, + }, + }, + }, + Events: &v1.Events{ + DevWorkspaceEvents: v1.DevWorkspaceEvents{ + PostStart: []string{"post-start-0"}, + }, + }, + Projects: []v1.Project{ + { + ClonePath: "/data", + ProjectSource: v1.ProjectSource{ + Github: &v1.GithubProjectSource{ + GitLikeProjectSource: v1.GitLikeProjectSource{ + Remotes: map[string]string{ + "master": "https://githube.com/somerepo/someproject.git", + }, + }, + }, + }, + Name: "nodejs-starter", + }, + }, + }, + }, + }, + }, + } type args struct { devFileObj DevfileObj @@ -120,62 +189,7 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { }, }, }, - parentDevfile: DevfileObj{ - Data: &v2.DevfileV2{ - Devfile: v1.Devfile{ - DevfileHeader: devfilepkg.DevfileHeader{ - SchemaVersion: schemaV200, - }, - DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ - DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ - Commands: []v1.Command{ - { - Id: "devrun", - CommandUnion: v1.CommandUnion{ - Exec: &v1.ExecCommand{ - WorkingDir: "/projects", - CommandLine: "npm run", - }, - }, - }, - }, - Components: []v1.Component{ - { - Name: "nodejs", - ComponentUnion: v1.ComponentUnion{ - Container: &v1.ContainerComponent{ - Container: v1.Container{ - Image: "quay.io/nodejs-10", - }, - }, - }, - }, - }, - Events: &v1.Events{ - DevWorkspaceEvents: v1.DevWorkspaceEvents{ - PostStart: []string{"post-start-0"}, - }, - }, - Projects: []v1.Project{ - { - ClonePath: "/data", - ProjectSource: v1.ProjectSource{ - Github: &v1.GithubProjectSource{ - GitLikeProjectSource: v1.GitLikeProjectSource{ - Remotes: map[string]string{ - "master": "https://githube.com/somerepo/someproject.git", - }, - }, - }, - }, - Name: "nodejs-starter", - }, - }, - }, - }, - }, - }, - }, + parentDevfile: parentDevfile, wantDevFile: DevfileObj{ Data: &v2.DevfileV2{ Devfile: v1.Devfile{ @@ -183,7 +197,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ Commands: []v1.Command{ { - Id: "devrun", + Attributes: parentOverridesFromMainDevfile, + Id: "devrun", CommandUnion: v1.CommandUnion{ Exec: &v1.ExecCommand{ CommandLine: "npm run", @@ -202,7 +217,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { }, Components: []v1.Component{ { - Name: "nodejs", + Attributes: parentOverridesFromMainDevfile, + Name: "nodejs", ComponentUnion: v1.ComponentUnion{ Container: &v1.ContainerComponent{ Container: v1.Container{ @@ -232,7 +248,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { }, Projects: []v1.Project{ { - ClonePath: "/projects", + Attributes: parentOverridesFromMainDevfile, + ClonePath: "/projects", ProjectSource: v1.ProjectSource{ Github: &v1.GithubProjectSource{ GitLikeProjectSource: v1.GitLikeProjectSource{ @@ -303,62 +320,7 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { }, }, }, - parentDevfile: DevfileObj{ - Data: &v2.DevfileV2{ - Devfile: v1.Devfile{ - DevfileHeader: devfilepkg.DevfileHeader{ - SchemaVersion: schemaV200, - }, - DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ - DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ - Commands: []v1.Command{ - { - Id: "devrun", - CommandUnion: v1.CommandUnion{ - Exec: &v1.ExecCommand{ - WorkingDir: "/projects", - CommandLine: "npm run", - }, - }, - }, - }, - Components: []v1.Component{ - { - Name: "nodejs", - ComponentUnion: v1.ComponentUnion{ - Container: &v1.ContainerComponent{ - Container: v1.Container{ - Image: "quay.io/nodejs-10", - }, - }, - }, - }, - }, - Events: &v1.Events{ - DevWorkspaceEvents: v1.DevWorkspaceEvents{ - PostStart: []string{"post-start-0"}, - }, - }, - Projects: []v1.Project{ - { - ClonePath: "/data", - ProjectSource: v1.ProjectSource{ - Github: &v1.GithubProjectSource{ - GitLikeProjectSource: v1.GitLikeProjectSource{ - Remotes: map[string]string{ - "master": "https://githube.com/somerepo/someproject.git", - }, - }, - }, - }, - Name: "nodejs-starter", - }, - }, - }, - }, - }, - }, - }, + parentDevfile: parentDevfile, wantDevFile: DevfileObj{ Data: &v2.DevfileV2{ Devfile: v1.Devfile{ @@ -366,7 +328,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ Commands: []v1.Command{ { - Id: "devrun", + Attributes: importFromUri1, + Id: "devrun", CommandUnion: v1.CommandUnion{ Exec: &v1.ExecCommand{ CommandLine: "npm run", @@ -385,7 +348,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { }, Components: []v1.Component{ { - Name: "nodejs", + Attributes: importFromUri1, + Name: "nodejs", ComponentUnion: v1.ComponentUnion{ Container: &v1.ContainerComponent{ Container: v1.Container{ @@ -415,7 +379,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { }, Projects: []v1.Project{ { - ClonePath: "/data", + Attributes: importFromUri1, + ClonePath: "/data", ProjectSource: v1.ProjectSource{ Github: &v1.GithubProjectSource{ GitLikeProjectSource: v1.GitLikeProjectSource{ @@ -827,7 +792,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ Commands: []v1.Command{ { - Id: "devrun", + Attributes: importFromUri2, + Id: "devrun", CommandUnion: v1.CommandUnion{ Exec: &v1.ExecCommand{ CommandLine: "npm run", @@ -846,7 +812,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { }, Components: []v1.Component{ { - Name: "nodejs", + Attributes: importFromUri2, + Name: "nodejs", ComponentUnion: v1.ComponentUnion{ Container: &v1.ContainerComponent{ Container: v1.Container{ @@ -876,7 +843,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { }, Projects: []v1.Project{ { - ClonePath: "/data", + Attributes: importFromUri2, + ClonePath: "/data", ProjectSource: v1.ProjectSource{ Github: &v1.GithubProjectSource{ GitLikeProjectSource: v1.GitLikeProjectSource{ @@ -1037,7 +1005,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ Commands: []v1.Command{ { - Id: "devrun", + Attributes: pluginOverridesFromMainDevfile, + Id: "devrun", CommandUnion: v1.CommandUnion{ Exec: &v1.ExecCommand{ CommandLine: "npm build", @@ -1056,7 +1025,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { }, Components: []v1.Component{ { - Name: "nodejs", + Attributes: pluginOverridesFromMainDevfile, + Name: "nodejs", ComponentUnion: v1.ComponentUnion{ Container: &v1.ContainerComponent{ Container: v1.Container{ @@ -1086,7 +1056,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { }, Projects: []v1.Project{ { - ClonePath: "/data", + Attributes: importFromUri2, + ClonePath: "/data", ProjectSource: v1.ProjectSource{ Github: &v1.GithubProjectSource{ GitLikeProjectSource: v1.GitLikeProjectSource{ @@ -1705,7 +1676,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ Commands: []v1.Command{ { - Id: "devrun", + Attributes: parentOverridesFromMainDevfile, + Id: "devrun", CommandUnion: v1.CommandUnion{ Exec: &v1.ExecCommand{ CommandLine: "npm run", @@ -1714,7 +1686,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { }, }, { - Id: "devdebug", + Attributes: importFromUri2, + Id: "devdebug", CommandUnion: v1.CommandUnion{ Exec: &v1.ExecCommand{ WorkingDir: "/projects", @@ -1733,7 +1706,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { }, Components: []v1.Component{ { - Name: "nodejs", + Attributes: pluginOverridesFromMainDevfile, + Name: "nodejs", ComponentUnion: v1.ComponentUnion{ Container: &v1.ContainerComponent{ Container: v1.Container{ @@ -1763,7 +1737,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { }, Projects: []v1.Project{ { - ClonePath: "/projects", + Attributes: parentOverridesFromMainDevfile, + ClonePath: "/projects", ProjectSource: v1.ProjectSource{ Github: &v1.GithubProjectSource{ GitLikeProjectSource: v1.GitLikeProjectSource{ @@ -1899,7 +1874,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ Components: []v1.Component{ { - Name: "runtime", + Attributes: pluginOverridesFromMainDevfile, + Name: "runtime", ComponentUnion: v1.ComponentUnion{ Container: &v1.ContainerComponent{ Container: v1.Container{ @@ -2034,7 +2010,8 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ Components: []v1.Component{ { - Name: "runtime", + Attributes: parentOverridesFromMainDevfile, + Name: "runtime", ComponentUnion: v1.ComponentUnion{ Container: &v1.ContainerComponent{ Container: v1.Container{ @@ -2071,7 +2048,7 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { Parent: &v1.Parent{ ImportReference: v1.ImportReference{ ImportReferenceUnion: v1.ImportReferenceUnion{ - Uri: "http://127.0.0.1:8080", + Uri: "http://" + uri2, }, }, }, @@ -2102,8 +2079,10 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + var parentTestServer *httptest.Server + var pluginTestServer *httptest.Server if !reflect.DeepEqual(tt.parentDevfile, DevfileObj{}) { - testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + parentTestServer = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { data, err := yaml.Marshal(tt.parentDevfile.Data) if err != nil { t.Errorf("unexpected error: %v", err) @@ -2113,18 +2092,31 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { t.Errorf("unexpected error: %v", err) } })) - defer testServer.Close() + // create a listener with the desired port. + l1, err := net.Listen("tcp", uri1) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + + // NewUnstartedServer creates a listener. Close that listener and replace + // with the one we created. + parentTestServer.Listener.Close() + parentTestServer.Listener = l1 + + parentTestServer.Start() + defer parentTestServer.Close() + parent := tt.args.devFileObj.Data.GetParent() if parent == nil { parent = &v1.Parent{} } - parent.Uri = testServer.URL + parent.Uri = parentTestServer.URL tt.args.devFileObj.Data.SetParent(parent) } if !reflect.DeepEqual(tt.pluginDevfile, DevfileObj{}) { - testServer := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + pluginTestServer = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { data, err := yaml.Marshal(tt.pluginDevfile.Data) if err != nil { t.Errorf("unexpected error: %v", err) @@ -2134,29 +2126,27 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { t.Errorf("unexpected error: %v", err) } })) - if tt.testRecursiveReference { - // create a listener with the desired port. - l, err := net.Listen("tcp", "127.0.0.1:8080") - if err != nil { - t.Errorf("unexpected error: %v", err) - } - - // NewUnstartedServer creates a listener. Close that listener and replace - // with the one we created. - testServer.Listener.Close() - testServer.Listener = l + l, err := net.Listen("tcp", uri2) + if err != nil { + t.Errorf("unexpected error: %v", err) } - testServer.Start() - defer testServer.Close() + + // NewUnstartedServer creates a listener. Close that listener and replace + // with the one we created. + pluginTestServer.Listener.Close() + pluginTestServer.Listener = l + + pluginTestServer.Start() + defer pluginTestServer.Close() plugincomp := []v1.Component{ { - Name: "plugincomp", + Name: pluginName, ComponentUnion: v1.ComponentUnion{ Plugin: &v1.PluginComponent{ ImportReference: v1.ImportReference{ ImportReferenceUnion: v1.ImportReferenceUnion{ - Uri: testServer.URL, + Uri: pluginTestServer.URL, }, }, PluginOverrides: tt.pluginOverride, @@ -2183,7 +2173,6 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { if !reflect.DeepEqual(tt.args.devFileObj.Data, tt.wantDevFile.Data) { t.Errorf("wanted: %v, got: %v, difference at %v", tt.wantDevFile.Data, tt.args.devFileObj.Data, pretty.Compare(tt.args.devFileObj.Data, tt.wantDevFile.Data)) } - }) } } @@ -2534,6 +2523,10 @@ func Test_parseParentFromRegistry(t *testing.T) { }, }, } + + parentOverridesFromMainDevfile := attributes.Attributes{} + parentOverridesFromMainDevfile.PutString(ImportSourceAttribute, "parentOverrides from: main devfile") + wantDevfileContent := v1.Devfile{ DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ @@ -2549,7 +2542,8 @@ func Test_parseParentFromRegistry(t *testing.T) { }, Components: []v1.Component{ { - Name: "parent-runtime", + Attributes: parentOverridesFromMainDevfile, + Name: "parent-runtime", ComponentUnion: v1.ComponentUnion{ Container: &v1.ContainerComponent{ Container: v1.Container{ diff --git a/pkg/devfile/parser/resolutionContext.go b/pkg/devfile/parser/resolutionContext.go index 8eb9d2f3..5bb0d4bf 100644 --- a/pkg/devfile/parser/resolutionContext.go +++ b/pkg/devfile/parser/resolutionContext.go @@ -61,4 +61,4 @@ func resolveImportReference(importReference v1.ImportReference) string { } // the first node return "main devfile" -} \ No newline at end of file +} diff --git a/pkg/devfile/parser/sourceAttribute.go b/pkg/devfile/parser/sourceAttribute.go index d5625445..635f623b 100644 --- a/pkg/devfile/parser/sourceAttribute.go +++ b/pkg/devfile/parser/sourceAttribute.go @@ -37,7 +37,6 @@ func AddSourceAttributesForTemplateSpecContent(sourceImportReference v1.ImportRe } } - // AddSourceAttributesForParentOverride adds an attribute 'library.devfile.io/imported-from=' // to all elements of parent override that support attributes. func AddSourceAttributesForParentOverride(sourceImportReference v1.ImportReference, parentoverride *v1.ParentOverrides) { @@ -68,21 +67,20 @@ func AddSourceAttributesForParentOverride(sourceImportReference v1.ImportReferen } - // AddSourceAttributesForPluginOverride adds an attribute 'library.devfile.io/imported-from=' // to all elements of plugin override that support attributes. -func AddSourceAttributesForPluginOverride(sourceImportReference v1.ImportReference, pluginId string, pluginoverride *v1.PluginOverrides) { +func AddSourceAttributesForPluginOverride(sourceImportReference v1.ImportReference, pluginName string, pluginoverride *v1.PluginOverrides) { for idx, component := range pluginoverride.Components { if component.Attributes == nil { pluginoverride.Components[idx].Attributes = attributes.Attributes{} } - pluginoverride.Components[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("pluginOverrides from: %s, plugin : %s", resolveImportReference(sourceImportReference), pluginId)) + pluginoverride.Components[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("pluginOverrides from: %s, plugin : %s", resolveImportReference(sourceImportReference), pluginName)) } for idx, command := range pluginoverride.Commands { if command.Attributes == nil { pluginoverride.Commands[idx].Attributes = attributes.Attributes{} } - pluginoverride.Commands[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("pluginOverrides from: %s, plugin : %s", resolveImportReference(sourceImportReference), pluginId)) + pluginoverride.Commands[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("pluginOverrides from: %s, plugin : %s", resolveImportReference(sourceImportReference), pluginName)) } -} \ No newline at end of file +} From 7e3eadc8341d10851683017a318e6b3acb0efd57 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Mon, 19 Apr 2021 15:23:38 -0400 Subject: [PATCH 4/7] add more unit tests Signed-off-by: Stephanie --- pkg/devfile/parser/parse_test.go | 390 +++++++++++++++++++++++++++++++ 1 file changed, 390 insertions(+) diff --git a/pkg/devfile/parser/parse_test.go b/pkg/devfile/parser/parse_test.go index 21c5062e..3d200333 100644 --- a/pkg/devfile/parser/parse_test.go +++ b/pkg/devfile/parser/parse_test.go @@ -2403,6 +2403,7 @@ func Test_parseParentFromRegistry(t *testing.T) { tool := resolverTools{ registryURLs: []string{"http://" + validRegistry}, } + parentDevfile := DevfileObj{ Data: &v2.DevfileV2{ Devfile: v1.Devfile{ @@ -2526,6 +2527,8 @@ func Test_parseParentFromRegistry(t *testing.T) { parentOverridesFromMainDevfile := attributes.Attributes{} parentOverridesFromMainDevfile.PutString(ImportSourceAttribute, "parentOverrides from: main devfile") + importFromRegistry := attributes.Attributes{} + importFromRegistry.PutString(ImportSourceAttribute, resolveImportReference(mainDevfileContent.Parent.ImportReference)) wantDevfileContent := v1.Devfile{ DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ @@ -2616,6 +2619,92 @@ func Test_parseParentFromRegistry(t *testing.T) { }, }, }, + { + name: "it should merge the requested parent's data from provided registryURL if no override is set", + mainDevfile: DevfileObj{ + Data: &v2.DevfileV2{ + Devfile: v1.Devfile{ + DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ + Parent: &v1.Parent{ + ImportReference: v1.ImportReference{ + RegistryUrl: "http://" + validRegistry, + ImportReferenceUnion: v1.ImportReferenceUnion{ + Id: "nodejs", + }, + }, + }, + DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ + Commands: []v1.Command{ + { + Id: "devbuild", + CommandUnion: v1.CommandUnion{ + Exec: &v1.ExecCommand{ + WorkingDir: "/projects/nodejs-starter", + }, + }, + }, + }, + Components: []v1.Component{ + { + Name: "runtime2", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Container: v1.Container{ + Image: "quay.io/nodejs-12", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + wantDevFile: DevfileObj{ + Data: &v2.DevfileV2{ + Devfile: v1.Devfile{ + DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ + DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ + Commands: []v1.Command{ + { + Id: "devbuild", + CommandUnion: v1.CommandUnion{ + Exec: &v1.ExecCommand{ + WorkingDir: "/projects/nodejs-starter", + }, + }, + }, + }, + Components: []v1.Component{ + { + Attributes: importFromRegistry, + Name: "parent-runtime", + ComponentUnion: v1.ComponentUnion{ + Volume: &v1.VolumeComponent{ + Volume: v1.Volume{ + Size: "500Mi", + }, + }, + }, + }, + { + Name: "runtime2", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Container: v1.Container{ + Image: "quay.io/nodejs-12", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, { name: "it should error out with invalid registry provided", mainDevfile: DevfileObj{ @@ -2684,6 +2773,307 @@ func Test_parseParentFromRegistry(t *testing.T) { } } +func Test_parseParentFromKubeCRD(t *testing.T) { + + const ( + namespace = "default" + name = "test-parent-k8s" + apiVersion = "testgroup/v1alpha2" + ) + + kubeCRDReference := v1.ImportReference{ + ImportReferenceUnion: v1.ImportReferenceUnion{ + Kubernetes: &v1.KubernetesCustomResourceImportReference{ + Name: name, + Namespace: namespace, + }, + }, + } + + parentOverridesFromMainDevfile := attributes.Attributes{} + parentOverridesFromMainDevfile.PutString(ImportSourceAttribute, "parentOverrides from: main devfile") + importFromKubeCRD := attributes.Attributes{} + importFromKubeCRD.PutString(ImportSourceAttribute, resolveImportReference(kubeCRDReference)) + + parentSpec := v1.DevWorkspaceTemplateSpec{ + DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ + Components: []v1.Component{ + { + Name: "parent-runtime", + ComponentUnion: v1.ComponentUnion{ + Volume: &v1.VolumeComponent{ + Volume: v1.Volume{ + Size: "500Mi", + }, + }, + }, + }, + }, + }, + } + + tests := []struct { + name string + devWorkspaceResources map[string]v1.DevWorkspaceTemplate + errors map[string]string + mainDevfile DevfileObj + wantDevFile DevfileObj + wantErr bool + }{ + { + name: "should successfully override the parent data", + mainDevfile: DevfileObj{ + Ctx: devfileCtx.NewDevfileCtx(OutputDevfileYamlPath), + Data: &v2.DevfileV2{ + Devfile: v1.Devfile{ + DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ + Parent: &v1.Parent{ + ImportReference: kubeCRDReference, + ParentOverrides: v1.ParentOverrides{ + Components: []v1.ComponentParentOverride{ + { + Name: "parent-runtime", + ComponentUnionParentOverride: v1.ComponentUnionParentOverride{ + Container: &v1.ContainerComponentParentOverride{ + ContainerParentOverride: v1.ContainerParentOverride{ + Image: "quay.io/nodejs-12", + }, + }, + }, + }, + }, + }, + }, + DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ + Commands: []v1.Command{ + { + Id: "devbuild", + CommandUnion: v1.CommandUnion{ + Exec: &v1.ExecCommand{ + WorkingDir: "/projects/nodejs-starter", + }, + }, + }, + }, + Components: []v1.Component{ + { + Name: "runtime", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Container: v1.Container{ + Image: "quay.io/nodejs-12", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + wantDevFile: DevfileObj{ + Ctx: devfileCtx.NewDevfileCtx(OutputDevfileYamlPath), + Data: &v2.DevfileV2{ + Devfile: v1.Devfile{ + DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ + DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ + Commands: []v1.Command{ + { + Id: "devbuild", + CommandUnion: v1.CommandUnion{ + Exec: &v1.ExecCommand{ + WorkingDir: "/projects/nodejs-starter", + }, + }, + }, + }, + Components: []v1.Component{ + { + Attributes: parentOverridesFromMainDevfile, + Name: "parent-runtime", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Container: v1.Container{ + Image: "quay.io/nodejs-12", + }, + }, + }, + }, + { + Name: "runtime", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Container: v1.Container{ + Image: "quay.io/nodejs-12", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + devWorkspaceResources: map[string]v1.DevWorkspaceTemplate{ + name: { + TypeMeta: kubev1.TypeMeta{ + Kind: "DevWorkspaceTemplate", + APIVersion: apiVersion, + }, + Spec: parentSpec, + }, + }, + wantErr: false, + }, + { + name: "should successfully merge the parent data without override defined", + mainDevfile: DevfileObj{ + Ctx: devfileCtx.NewDevfileCtx(OutputDevfileYamlPath), + Data: &v2.DevfileV2{ + Devfile: v1.Devfile{ + DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ + Parent: &v1.Parent{ + ImportReference: kubeCRDReference, + }, + DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ + Commands: []v1.Command{ + { + Id: "devbuild", + CommandUnion: v1.CommandUnion{ + Exec: &v1.ExecCommand{ + WorkingDir: "/projects/nodejs-starter", + }, + }, + }, + }, + Components: []v1.Component{ + { + Name: "runtime", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Container: v1.Container{ + Image: "quay.io/nodejs-12", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + wantDevFile: DevfileObj{ + Ctx: devfileCtx.NewDevfileCtx(OutputDevfileYamlPath), + Data: &v2.DevfileV2{ + Devfile: v1.Devfile{ + DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ + DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ + Commands: []v1.Command{ + { + Id: "devbuild", + CommandUnion: v1.CommandUnion{ + Exec: &v1.ExecCommand{ + WorkingDir: "/projects/nodejs-starter", + }, + }, + }, + }, + Components: []v1.Component{ + { + Attributes: importFromKubeCRD, + Name: "parent-runtime", + ComponentUnion: v1.ComponentUnion{ + Volume: &v1.VolumeComponent{ + Volume: v1.Volume{ + Size: "500Mi", + }, + }, + }, + }, + { + Name: "runtime", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Container: v1.Container{ + Image: "quay.io/nodejs-12", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + devWorkspaceResources: map[string]v1.DevWorkspaceTemplate{ + name: { + TypeMeta: kubev1.TypeMeta{ + Kind: "DevWorkspaceTemplate", + APIVersion: apiVersion, + }, + Spec: parentSpec, + }, + }, + wantErr: false, + }, + { + name: "should fail if kclient get returns error", + mainDevfile: DevfileObj{ + Ctx: devfileCtx.NewDevfileCtx(OutputDevfileYamlPath), + Data: &v2.DevfileV2{ + Devfile: v1.Devfile{ + DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ + Parent: &v1.Parent{ + ImportReference: kubeCRDReference, + }, + DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{}, + }, + }, + }, + }, + devWorkspaceResources: map[string]v1.DevWorkspaceTemplate{}, + errors: map[string]string{ + name: "not found", + }, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + testK8sClient := &testingutil.FakeK8sClient{ + DevWorkspaceResources: tt.devWorkspaceResources, + Errors: tt.errors, + } + tool := resolverTools{ + k8sClient: testK8sClient, + context: context.Background(), + } + err := parseParentAndPlugin(tt.mainDevfile, &resolutionContextTree{}, tool) + + // Unexpected error + if (err != nil) != tt.wantErr { + t.Errorf("parseParentAndPlugin() error = %v, wantErr %v", err, tt.wantErr) + return + } + + // Expected error and got an err + if tt.wantErr && err != nil { + return + } + + if !reflect.DeepEqual(tt.mainDevfile.Data, tt.wantDevFile.Data) { + t.Errorf("wanted: %v, got: %v, difference at %v", tt.wantDevFile.Data, tt.mainDevfile.Data, pretty.Compare(tt.mainDevfile.Data, tt.wantDevFile.Data)) + } + + }) + } +} + func Test_parseFromURI(t *testing.T) { const uri1 = "127.0.0.1:8080" const httpPrefix = "http://" From dbf9043c25557e34e63f521065eed5ad6f9f9c45 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Tue, 20 Apr 2021 15:27:37 -0400 Subject: [PATCH 5/7] combine 3 addAttribute functions into one Signed-off-by: Stephanie --- pkg/devfile/parser/parse.go | 10 ++-- pkg/devfile/parser/parse_test.go | 31 +++++------ pkg/devfile/parser/sourceAttribute.go | 74 ++++++++++++++++++--------- 3 files changed, 68 insertions(+), 47 deletions(-) diff --git a/pkg/devfile/parser/parse.go b/pkg/devfile/parser/parse.go index d492d7d4..2f23cb65 100644 --- a/pkg/devfile/parser/parse.go +++ b/pkg/devfile/parser/parse.go @@ -212,11 +212,11 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool parentWorkspaceContent := parentDevfileObj.Data.GetDevfileWorkspaceSpecContent() // add attribute to parent elements - AddSourceAttributesForTemplateSpecContent(parent.ImportReference, parentWorkspaceContent) + AddSourceAttributesForOverrideAndMerge(parent.ImportReference, parentWorkspaceContent) if !reflect.DeepEqual(parent.ParentOverrides, v1.ParentOverrides{}) { // add attribute to parentOverrides elements curNodeImportReference := resolveCtx.importReference - AddSourceAttributesForParentOverride(curNodeImportReference, &parent.ParentOverrides) + AddSourceAttributesForOverrideAndMerge(curNodeImportReference, &parent.ParentOverrides) flattenedParent, err = apiOverride.OverrideDevWorkspaceTemplateSpec(parentWorkspaceContent, parent.ParentOverrides) if err != nil { return err @@ -256,12 +256,12 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool } pluginWorkspaceContent := pluginDevfileObj.Data.GetDevfileWorkspaceSpecContent() // add attribute to plugin elements - AddSourceAttributesForTemplateSpecContent(plugin.ImportReference, pluginWorkspaceContent) + AddSourceAttributesForOverrideAndMerge(plugin.ImportReference, pluginWorkspaceContent) flattenedPlugin := pluginWorkspaceContent if !reflect.DeepEqual(plugin.PluginOverrides, v1.PluginOverrides{}) { - // add attribute to parentOverrides elements + // add attribute to pluginOverrides elements curNodeImportReference := resolveCtx.importReference - AddSourceAttributesForPluginOverride(curNodeImportReference, component.Name, &plugin.PluginOverrides) + AddSourceAttributesForOverrideAndMerge(curNodeImportReference, &plugin.PluginOverrides) flattenedPlugin, err = apiOverride.OverrideDevWorkspaceTemplateSpec(pluginWorkspaceContent, plugin.PluginOverrides) if err != nil { return err diff --git a/pkg/devfile/parser/parse_test.go b/pkg/devfile/parser/parse_test.go index 3d200333..fb940c1c 100644 --- a/pkg/devfile/parser/parse_test.go +++ b/pkg/devfile/parser/parse_test.go @@ -29,15 +29,12 @@ const schemaV200 = "2.0.0" func Test_parseParentAndPluginFromURI(t *testing.T) { const uri1 = "127.0.0.1:8080" const uri2 = "127.0.0.1:9090" - const pluginName = "plugincomp" - importFromUri1 := attributes.Attributes{} - importFromUri1.PutString(ImportSourceAttribute, fmt.Sprintf("uri: http://%s", uri1)) - importFromUri2 := attributes.Attributes{} - importFromUri2.PutString(ImportSourceAttribute, fmt.Sprintf("uri: http://%s", uri2)) - parentOverridesFromMainDevfile := attributes.Attributes{} - parentOverridesFromMainDevfile.PutString(ImportSourceAttribute, "parentOverrides from: main devfile") - pluginOverridesFromMainDevfile := attributes.Attributes{} - pluginOverridesFromMainDevfile.PutString(ImportSourceAttribute, fmt.Sprintf("pluginOverrides from: main devfile, plugin : %s", pluginName)) + importFromUri1 := attributes.Attributes{}.PutString(ImportSourceAttribute, fmt.Sprintf("uri: http://%s", uri1)) + importFromUri2 := attributes.Attributes{}.PutString(ImportSourceAttribute, fmt.Sprintf("uri: http://%s", uri2)) + parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute, + fmt.Sprintf("uri: http://%s", uri1)).PutString(ParentOverrideAttribute, "main devfile") + pluginOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute, + fmt.Sprintf("uri: http://%s", uri2)).PutString(PluginOverrideAttribute, "main devfile") parentDevfile := DevfileObj{ Data: &v2.DevfileV2{ @@ -2141,7 +2138,7 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { plugincomp := []v1.Component{ { - Name: pluginName, + Name: "plugincomp", ComponentUnion: v1.ComponentUnion{ Plugin: &v1.PluginComponent{ ImportReference: v1.ImportReference{ @@ -2525,10 +2522,9 @@ func Test_parseParentFromRegistry(t *testing.T) { }, } - parentOverridesFromMainDevfile := attributes.Attributes{} - parentOverridesFromMainDevfile.PutString(ImportSourceAttribute, "parentOverrides from: main devfile") - importFromRegistry := attributes.Attributes{} - importFromRegistry.PutString(ImportSourceAttribute, resolveImportReference(mainDevfileContent.Parent.ImportReference)) + importFromRegistry := attributes.Attributes{}.PutString(ImportSourceAttribute, resolveImportReference(mainDevfileContent.Parent.ImportReference)) + parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute, + resolveImportReference(mainDevfileContent.Parent.ImportReference)).PutString(ParentOverrideAttribute, "main devfile") wantDevfileContent := v1.Devfile{ DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ @@ -2790,10 +2786,9 @@ func Test_parseParentFromKubeCRD(t *testing.T) { }, } - parentOverridesFromMainDevfile := attributes.Attributes{} - parentOverridesFromMainDevfile.PutString(ImportSourceAttribute, "parentOverrides from: main devfile") - importFromKubeCRD := attributes.Attributes{} - importFromKubeCRD.PutString(ImportSourceAttribute, resolveImportReference(kubeCRDReference)) + importFromKubeCRD := attributes.Attributes{}.PutString(ImportSourceAttribute, resolveImportReference(kubeCRDReference)) + parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute, + resolveImportReference(kubeCRDReference)).PutString(ParentOverrideAttribute, "main devfile") parentSpec := v1.DevWorkspaceTemplateSpec{ DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ diff --git a/pkg/devfile/parser/sourceAttribute.go b/pkg/devfile/parser/sourceAttribute.go index 635f623b..8e8c53b7 100644 --- a/pkg/devfile/parser/sourceAttribute.go +++ b/pkg/devfile/parser/sourceAttribute.go @@ -7,10 +7,12 @@ import ( ) const ImportSourceAttribute = "library.devfile.io/imported-from" +const PluginOverrideAttribute = "library.devfile.io/plugin-override-from" +const ParentOverrideAttribute = "library.devfile.io/parent-override-from" -// AddSourceAttributesForTemplateSpecContent adds an attribute 'library.devfile.io/imported-from=' +// addSourceAttributesForTemplateSpecContent adds an attribute 'library.devfile.io/imported-from=' // to all elements of template spec content that support attributes. -func AddSourceAttributesForTemplateSpecContent(sourceImportReference v1.ImportReference, template *v1.DevWorkspaceTemplateSpecContent) { +func addSourceAttributesForTemplateSpecContent(sourceImportReference v1.ImportReference, template *v1.DevWorkspaceTemplateSpecContent) { for idx, component := range template.Components { if component.Attributes == nil { template.Components[idx].Attributes = attributes.Attributes{} @@ -37,50 +39,74 @@ func AddSourceAttributesForTemplateSpecContent(sourceImportReference v1.ImportRe } } -// AddSourceAttributesForParentOverride adds an attribute 'library.devfile.io/imported-from=' +// addSourceAttributesForParentOverride adds an attribute 'library.devfile.io/imported-from=' // to all elements of parent override that support attributes. -func AddSourceAttributesForParentOverride(sourceImportReference v1.ImportReference, parentoverride *v1.ParentOverrides) { - for idx, component := range parentoverride.Components { +func addSourceAttributesForParentOverride(sourceImportReference v1.ImportReference, parentOverrides *v1.ParentOverrides) { + for idx, component := range parentOverrides.Components { if component.Attributes == nil { - parentoverride.Components[idx].Attributes = attributes.Attributes{} + parentOverrides.Components[idx].Attributes = attributes.Attributes{} } - parentoverride.Components[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("parentOverrides from: %s", resolveImportReference(sourceImportReference))) + parentOverrides.Components[idx].Attributes.PutString(ParentOverrideAttribute, resolveImportReference(sourceImportReference)) } - for idx, command := range parentoverride.Commands { + for idx, command := range parentOverrides.Commands { if command.Attributes == nil { - parentoverride.Commands[idx].Attributes = attributes.Attributes{} + parentOverrides.Commands[idx].Attributes = attributes.Attributes{} } - parentoverride.Commands[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("parentOverrides from: %s", resolveImportReference(sourceImportReference))) + parentOverrides.Commands[idx].Attributes.PutString(ParentOverrideAttribute, resolveImportReference(sourceImportReference)) } - for idx, project := range parentoverride.Projects { + for idx, project := range parentOverrides.Projects { if project.Attributes == nil { - parentoverride.Projects[idx].Attributes = attributes.Attributes{} + parentOverrides.Projects[idx].Attributes = attributes.Attributes{} } - parentoverride.Projects[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("parentOverrides from: %s", resolveImportReference(sourceImportReference))) + parentOverrides.Projects[idx].Attributes.PutString(ParentOverrideAttribute, resolveImportReference(sourceImportReference)) } - for idx, project := range parentoverride.StarterProjects { + for idx, project := range parentOverrides.StarterProjects { if project.Attributes == nil { - parentoverride.StarterProjects[idx].Attributes = attributes.Attributes{} + parentOverrides.StarterProjects[idx].Attributes = attributes.Attributes{} } - parentoverride.StarterProjects[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("parentOverrides from: %s", resolveImportReference(sourceImportReference))) + parentOverrides.StarterProjects[idx].Attributes.PutString(ParentOverrideAttribute, resolveImportReference(sourceImportReference)) } } -// AddSourceAttributesForPluginOverride adds an attribute 'library.devfile.io/imported-from=' +// addSourceAttributesForPluginOverride adds an attribute 'library.devfile.io/imported-from=' // to all elements of plugin override that support attributes. -func AddSourceAttributesForPluginOverride(sourceImportReference v1.ImportReference, pluginName string, pluginoverride *v1.PluginOverrides) { - for idx, component := range pluginoverride.Components { +func addSourceAttributesForPluginOverride(sourceImportReference v1.ImportReference, pluginOverrides *v1.PluginOverrides) { + for idx, component := range pluginOverrides.Components { if component.Attributes == nil { - pluginoverride.Components[idx].Attributes = attributes.Attributes{} + pluginOverrides.Components[idx].Attributes = attributes.Attributes{} } - pluginoverride.Components[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("pluginOverrides from: %s, plugin : %s", resolveImportReference(sourceImportReference), pluginName)) + pluginOverrides.Components[idx].Attributes.PutString(PluginOverrideAttribute, resolveImportReference(sourceImportReference)) } - for idx, command := range pluginoverride.Commands { + for idx, command := range pluginOverrides.Commands { if command.Attributes == nil { - pluginoverride.Commands[idx].Attributes = attributes.Attributes{} + pluginOverrides.Commands[idx].Attributes = attributes.Attributes{} } - pluginoverride.Commands[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("pluginOverrides from: %s, plugin : %s", resolveImportReference(sourceImportReference), pluginName)) + pluginOverrides.Commands[idx].Attributes.PutString(PluginOverrideAttribute, resolveImportReference(sourceImportReference)) } } + +// AddSourceAttributesForOverrideAndMerge adds an attribute record the import reference to all elements of template that support attributes. +func AddSourceAttributesForOverrideAndMerge(sourceImportReference v1.ImportReference, template interface{}) error { + if template == nil { + fmt.Errorf("cannot add source attributes to nil") + } + + mainContent, isMainContent := template.(*v1.DevWorkspaceTemplateSpecContent) + parentOverride, isParentOverride := template.(*v1.ParentOverrides) + pluginOverride, isPluginOverride := template.(*v1.PluginOverrides) + + switch { + case isMainContent: + addSourceAttributesForTemplateSpecContent(sourceImportReference, mainContent) + case isParentOverride: + addSourceAttributesForParentOverride(sourceImportReference, parentOverride) + case isPluginOverride: + addSourceAttributesForPluginOverride(sourceImportReference, pluginOverride) + default: + return fmt.Errorf("unknown template type") + } + + return nil +} From 86d3c1cdaf4b624aac8547a28d475b7208a9bcf9 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Tue, 20 Apr 2021 17:06:53 -0400 Subject: [PATCH 6/7] add unit test for AddSourceAttributesForOverrideAndMerge Signed-off-by: Stephanie --- pkg/devfile/parser/parse.go | 20 ++- pkg/devfile/parser/parse_test.go | 24 +-- pkg/devfile/parser/sourceAttribute.go | 38 ++--- pkg/devfile/parser/sourceAttribute_test.go | 162 +++++++++++++++++++++ 4 files changed, 210 insertions(+), 34 deletions(-) create mode 100644 pkg/devfile/parser/sourceAttribute_test.go diff --git a/pkg/devfile/parser/parse.go b/pkg/devfile/parser/parse.go index 2f23cb65..efc42bab 100644 --- a/pkg/devfile/parser/parse.go +++ b/pkg/devfile/parser/parse.go @@ -212,11 +212,17 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool parentWorkspaceContent := parentDevfileObj.Data.GetDevfileWorkspaceSpecContent() // add attribute to parent elements - AddSourceAttributesForOverrideAndMerge(parent.ImportReference, parentWorkspaceContent) + err = addSourceAttributesForOverrideAndMerge(parent.ImportReference, parentWorkspaceContent) + if err != nil { + return err + } if !reflect.DeepEqual(parent.ParentOverrides, v1.ParentOverrides{}) { // add attribute to parentOverrides elements curNodeImportReference := resolveCtx.importReference - AddSourceAttributesForOverrideAndMerge(curNodeImportReference, &parent.ParentOverrides) + err = addSourceAttributesForOverrideAndMerge(curNodeImportReference, &parent.ParentOverrides) + if err != nil { + return err + } flattenedParent, err = apiOverride.OverrideDevWorkspaceTemplateSpec(parentWorkspaceContent, parent.ParentOverrides) if err != nil { return err @@ -256,12 +262,18 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool } pluginWorkspaceContent := pluginDevfileObj.Data.GetDevfileWorkspaceSpecContent() // add attribute to plugin elements - AddSourceAttributesForOverrideAndMerge(plugin.ImportReference, pluginWorkspaceContent) + err = addSourceAttributesForOverrideAndMerge(plugin.ImportReference, pluginWorkspaceContent) + if err != nil { + return err + } flattenedPlugin := pluginWorkspaceContent if !reflect.DeepEqual(plugin.PluginOverrides, v1.PluginOverrides{}) { // add attribute to pluginOverrides elements curNodeImportReference := resolveCtx.importReference - AddSourceAttributesForOverrideAndMerge(curNodeImportReference, &plugin.PluginOverrides) + err = addSourceAttributesForOverrideAndMerge(curNodeImportReference, &plugin.PluginOverrides) + if err != nil { + return err + } flattenedPlugin, err = apiOverride.OverrideDevWorkspaceTemplateSpec(pluginWorkspaceContent, plugin.PluginOverrides) if err != nil { return err diff --git a/pkg/devfile/parser/parse_test.go b/pkg/devfile/parser/parse_test.go index fb940c1c..9a11b8c1 100644 --- a/pkg/devfile/parser/parse_test.go +++ b/pkg/devfile/parser/parse_test.go @@ -29,12 +29,12 @@ const schemaV200 = "2.0.0" func Test_parseParentAndPluginFromURI(t *testing.T) { const uri1 = "127.0.0.1:8080" const uri2 = "127.0.0.1:9090" - importFromUri1 := attributes.Attributes{}.PutString(ImportSourceAttribute, fmt.Sprintf("uri: http://%s", uri1)) - importFromUri2 := attributes.Attributes{}.PutString(ImportSourceAttribute, fmt.Sprintf("uri: http://%s", uri2)) - parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute, - fmt.Sprintf("uri: http://%s", uri1)).PutString(ParentOverrideAttribute, "main devfile") - pluginOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute, - fmt.Sprintf("uri: http://%s", uri2)).PutString(PluginOverrideAttribute, "main devfile") + importFromUri1 := attributes.Attributes{}.PutString(importSourceAttribute, fmt.Sprintf("uri: http://%s", uri1)) + importFromUri2 := attributes.Attributes{}.PutString(importSourceAttribute, fmt.Sprintf("uri: http://%s", uri2)) + parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(importSourceAttribute, + fmt.Sprintf("uri: http://%s", uri1)).PutString(parentOverrideAttribute, "main devfile") + pluginOverridesFromMainDevfile := attributes.Attributes{}.PutString(importSourceAttribute, + fmt.Sprintf("uri: http://%s", uri2)).PutString(pluginOverrideAttribute, "main devfile") parentDevfile := DevfileObj{ Data: &v2.DevfileV2{ @@ -2522,9 +2522,9 @@ func Test_parseParentFromRegistry(t *testing.T) { }, } - importFromRegistry := attributes.Attributes{}.PutString(ImportSourceAttribute, resolveImportReference(mainDevfileContent.Parent.ImportReference)) - parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute, - resolveImportReference(mainDevfileContent.Parent.ImportReference)).PutString(ParentOverrideAttribute, "main devfile") + importFromRegistry := attributes.Attributes{}.PutString(importSourceAttribute, resolveImportReference(mainDevfileContent.Parent.ImportReference)) + parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(importSourceAttribute, + resolveImportReference(mainDevfileContent.Parent.ImportReference)).PutString(parentOverrideAttribute, "main devfile") wantDevfileContent := v1.Devfile{ DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ @@ -2786,9 +2786,9 @@ func Test_parseParentFromKubeCRD(t *testing.T) { }, } - importFromKubeCRD := attributes.Attributes{}.PutString(ImportSourceAttribute, resolveImportReference(kubeCRDReference)) - parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute, - resolveImportReference(kubeCRDReference)).PutString(ParentOverrideAttribute, "main devfile") + importFromKubeCRD := attributes.Attributes{}.PutString(importSourceAttribute, resolveImportReference(kubeCRDReference)) + parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(importSourceAttribute, + resolveImportReference(kubeCRDReference)).PutString(parentOverrideAttribute, "main devfile") parentSpec := v1.DevWorkspaceTemplateSpec{ DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ diff --git a/pkg/devfile/parser/sourceAttribute.go b/pkg/devfile/parser/sourceAttribute.go index 8e8c53b7..bad1eec0 100644 --- a/pkg/devfile/parser/sourceAttribute.go +++ b/pkg/devfile/parser/sourceAttribute.go @@ -6,36 +6,38 @@ import ( "github.com/devfile/api/v2/pkg/attributes" ) -const ImportSourceAttribute = "library.devfile.io/imported-from" -const PluginOverrideAttribute = "library.devfile.io/plugin-override-from" -const ParentOverrideAttribute = "library.devfile.io/parent-override-from" +const ( + importSourceAttribute = "library.devfile.io/imported-from" + pluginOverrideAttribute = "library.devfile.io/plugin-override-from" + parentOverrideAttribute = "library.devfile.io/parent-override-from" +) -// addSourceAttributesForTemplateSpecContent adds an attribute 'library.devfile.io/imported-from=' +// addSourceAttributesForTemplateSpecContent adds an attribute 'library.devfile.io/parent-override-from=' // to all elements of template spec content that support attributes. func addSourceAttributesForTemplateSpecContent(sourceImportReference v1.ImportReference, template *v1.DevWorkspaceTemplateSpecContent) { for idx, component := range template.Components { if component.Attributes == nil { template.Components[idx].Attributes = attributes.Attributes{} } - template.Components[idx].Attributes.PutString(ImportSourceAttribute, resolveImportReference(sourceImportReference)) + template.Components[idx].Attributes.PutString(importSourceAttribute, resolveImportReference(sourceImportReference)) } for idx, command := range template.Commands { if command.Attributes == nil { template.Commands[idx].Attributes = attributes.Attributes{} } - template.Commands[idx].Attributes.PutString(ImportSourceAttribute, resolveImportReference(sourceImportReference)) + template.Commands[idx].Attributes.PutString(importSourceAttribute, resolveImportReference(sourceImportReference)) } for idx, project := range template.Projects { if project.Attributes == nil { template.Projects[idx].Attributes = attributes.Attributes{} } - template.Projects[idx].Attributes.PutString(ImportSourceAttribute, resolveImportReference(sourceImportReference)) + template.Projects[idx].Attributes.PutString(importSourceAttribute, resolveImportReference(sourceImportReference)) } for idx, project := range template.StarterProjects { if project.Attributes == nil { template.StarterProjects[idx].Attributes = attributes.Attributes{} } - template.StarterProjects[idx].Attributes.PutString(ImportSourceAttribute, resolveImportReference(sourceImportReference)) + template.StarterProjects[idx].Attributes.PutString(importSourceAttribute, resolveImportReference(sourceImportReference)) } } @@ -46,51 +48,51 @@ func addSourceAttributesForParentOverride(sourceImportReference v1.ImportReferen if component.Attributes == nil { parentOverrides.Components[idx].Attributes = attributes.Attributes{} } - parentOverrides.Components[idx].Attributes.PutString(ParentOverrideAttribute, resolveImportReference(sourceImportReference)) + parentOverrides.Components[idx].Attributes.PutString(parentOverrideAttribute, resolveImportReference(sourceImportReference)) } for idx, command := range parentOverrides.Commands { if command.Attributes == nil { parentOverrides.Commands[idx].Attributes = attributes.Attributes{} } - parentOverrides.Commands[idx].Attributes.PutString(ParentOverrideAttribute, resolveImportReference(sourceImportReference)) + parentOverrides.Commands[idx].Attributes.PutString(parentOverrideAttribute, resolveImportReference(sourceImportReference)) } for idx, project := range parentOverrides.Projects { if project.Attributes == nil { parentOverrides.Projects[idx].Attributes = attributes.Attributes{} } - parentOverrides.Projects[idx].Attributes.PutString(ParentOverrideAttribute, resolveImportReference(sourceImportReference)) + parentOverrides.Projects[idx].Attributes.PutString(parentOverrideAttribute, resolveImportReference(sourceImportReference)) } for idx, project := range parentOverrides.StarterProjects { if project.Attributes == nil { parentOverrides.StarterProjects[idx].Attributes = attributes.Attributes{} } - parentOverrides.StarterProjects[idx].Attributes.PutString(ParentOverrideAttribute, resolveImportReference(sourceImportReference)) + parentOverrides.StarterProjects[idx].Attributes.PutString(parentOverrideAttribute, resolveImportReference(sourceImportReference)) } } -// addSourceAttributesForPluginOverride adds an attribute 'library.devfile.io/imported-from=' +// addSourceAttributesForPluginOverride adds an attribute 'library.devfile.io/plugin-override-from=' // to all elements of plugin override that support attributes. func addSourceAttributesForPluginOverride(sourceImportReference v1.ImportReference, pluginOverrides *v1.PluginOverrides) { for idx, component := range pluginOverrides.Components { if component.Attributes == nil { pluginOverrides.Components[idx].Attributes = attributes.Attributes{} } - pluginOverrides.Components[idx].Attributes.PutString(PluginOverrideAttribute, resolveImportReference(sourceImportReference)) + pluginOverrides.Components[idx].Attributes.PutString(pluginOverrideAttribute, resolveImportReference(sourceImportReference)) } for idx, command := range pluginOverrides.Commands { if command.Attributes == nil { pluginOverrides.Commands[idx].Attributes = attributes.Attributes{} } - pluginOverrides.Commands[idx].Attributes.PutString(PluginOverrideAttribute, resolveImportReference(sourceImportReference)) + pluginOverrides.Commands[idx].Attributes.PutString(pluginOverrideAttribute, resolveImportReference(sourceImportReference)) } } -// AddSourceAttributesForOverrideAndMerge adds an attribute record the import reference to all elements of template that support attributes. -func AddSourceAttributesForOverrideAndMerge(sourceImportReference v1.ImportReference, template interface{}) error { +// addSourceAttributesForOverrideAndMerge adds an attribute record the import reference to all elements of template that support attributes. +func addSourceAttributesForOverrideAndMerge(sourceImportReference v1.ImportReference, template interface{}) error { if template == nil { - fmt.Errorf("cannot add source attributes to nil") + return fmt.Errorf("cannot add source attributes to nil") } mainContent, isMainContent := template.(*v1.DevWorkspaceTemplateSpecContent) diff --git a/pkg/devfile/parser/sourceAttribute_test.go b/pkg/devfile/parser/sourceAttribute_test.go new file mode 100644 index 00000000..f85fd55d --- /dev/null +++ b/pkg/devfile/parser/sourceAttribute_test.go @@ -0,0 +1,162 @@ +package parser + +import ( + v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" + "github.com/devfile/api/v2/pkg/attributes" + "github.com/kylelemons/godebug/pretty" + "reflect" + "testing" +) + +func TestAddSourceAttributesForOverrideAndMerge(t *testing.T) { + importReference := v1.ImportReference{ + ImportReferenceUnion: v1.ImportReferenceUnion{ + Uri: "127.0.0.1:8080", + }, + } + uriImportAttribute := attributes.Attributes{}.PutString(importSourceAttribute, resolveImportReference(importReference)) + pluginOverrideImportAttribute := attributes.Attributes{}.PutString(pluginOverrideAttribute, "main devfile") + parentOverrideImportAttribute := attributes.Attributes{}.PutString(parentOverrideAttribute, "main devfile") + + tests := []struct { + name string + wantErr bool + importReference v1.ImportReference + template interface{} + wantResult interface{} + }{ + { + name: "should fail if template is nil", + template: nil, + wantErr: true, + }, + { + name: "should fail if template is a not support type", + template: "invalid template", + wantErr: true, + }, + { + name: "template is with type *DevWorkspaceTemplateSpecContent", + importReference: importReference, + template: &v1.DevWorkspaceTemplateSpecContent{ + Components: []v1.Component{ + { + Name: "nodejs", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Container: v1.Container{ + Image: "quay.io/nodejs-10", + }, + }, + }, + }, + }, + }, + wantResult: &v1.DevWorkspaceTemplateSpecContent{ + Components: []v1.Component{ + { + Attributes: uriImportAttribute, + Name: "nodejs", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Container: v1.Container{ + Image: "quay.io/nodejs-10", + }, + }, + }, + }, + }, + }, + wantErr: false, + }, + { + name: "template is with type *PluginOverrides", + importReference: v1.ImportReference{}, + template: &v1.PluginOverrides{ + Components: []v1.ComponentPluginOverride{ + { + Name: "nodejs", + ComponentUnionPluginOverride: v1.ComponentUnionPluginOverride{ + Container: &v1.ContainerComponentPluginOverride{ + ContainerPluginOverride: v1.ContainerPluginOverride{ + Image: "quay.io/nodejs-10", + }, + }, + }, + }, + }, + }, + wantResult: &v1.PluginOverrides{ + Components: []v1.ComponentPluginOverride{ + { + Name: "nodejs", + Attributes: pluginOverrideImportAttribute, + ComponentUnionPluginOverride: v1.ComponentUnionPluginOverride{ + Container: &v1.ContainerComponentPluginOverride{ + ContainerPluginOverride: v1.ContainerPluginOverride{ + Image: "quay.io/nodejs-10", + }, + }, + }, + }, + }, + }, + wantErr: false, + }, + { + name: "template is with type *ParentOverrides", + importReference: v1.ImportReference{}, + template: &v1.ParentOverrides{ + Components: []v1.ComponentParentOverride{ + { + Name: "nodejs", + ComponentUnionParentOverride: v1.ComponentUnionParentOverride{ + Container: &v1.ContainerComponentParentOverride{ + ContainerParentOverride: v1.ContainerParentOverride{ + Image: "quay.io/nodejs-10", + }, + }, + }, + }, + }, + }, + wantResult: &v1.ParentOverrides{ + Components: []v1.ComponentParentOverride{ + { + Name: "nodejs", + Attributes: parentOverrideImportAttribute, + ComponentUnionParentOverride: v1.ComponentUnionParentOverride{ + Container: &v1.ContainerComponentParentOverride{ + ContainerParentOverride: v1.ContainerParentOverride{ + Image: "quay.io/nodejs-10", + }, + }, + }, + }, + }, + }, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := addSourceAttributesForOverrideAndMerge(tt.importReference, tt.template) + + if tt.wantErr == (err == nil) { + t.Errorf("Test_AddSourceAttributesForOverrideAndMerge() error = %v, wantErr %v", err, tt.wantErr) + return + } + + if err != nil { + return + } + + if !reflect.DeepEqual(tt.template, tt.wantResult) { + t.Errorf("wanted: %v, got: %v, difference at %v", tt.wantResult, tt.template, pretty.Compare(tt.template, tt.wantResult)) + } + + }) + } + +} From e3d1c45f283aef24ae46fa6c73575d74b9397ec5 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Wed, 21 Apr 2021 15:17:38 -0400 Subject: [PATCH 7/7] fix error check and function desc Signed-off-by: Stephanie --- pkg/devfile/parser/parse.go | 21 ++++++--------------- pkg/devfile/parser/sourceAttribute.go | 6 +++--- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/pkg/devfile/parser/parse.go b/pkg/devfile/parser/parse.go index efc42bab..8c948159 100644 --- a/pkg/devfile/parser/parse.go +++ b/pkg/devfile/parser/parse.go @@ -193,22 +193,16 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool switch { case parent.Uri != "": parentDevfileObj, err = parseFromURI(parent.ImportReference, d.Ctx, resolveCtx, tool) - if err != nil { - return err - } case parent.Id != "": parentDevfileObj, err = parseFromRegistry(parent.ImportReference, resolveCtx, tool) - if err != nil { - return err - } case parent.Kubernetes != nil: parentDevfileObj, err = parseFromKubeCRD(parent.ImportReference, resolveCtx, tool) - if err != nil { - return err - } default: return fmt.Errorf("devfile parent does not define any resources") } + if err != nil { + return err + } parentWorkspaceContent := parentDevfileObj.Data.GetDevfileWorkspaceSpecContent() // add attribute to parent elements @@ -247,19 +241,16 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool switch { case plugin.Uri != "": pluginDevfileObj, err = parseFromURI(plugin.ImportReference, d.Ctx, resolveCtx, tool) - if err != nil { - return err - } case plugin.Id != "": pluginDevfileObj, err = parseFromRegistry(plugin.ImportReference, resolveCtx, tool) - if err != nil { - return err - } case plugin.Kubernetes != nil: pluginDevfileObj, err = parseFromKubeCRD(plugin.ImportReference, resolveCtx, tool) default: return fmt.Errorf("plugin %s does not define any resources", component.Name) } + if err != nil { + return err + } pluginWorkspaceContent := pluginDevfileObj.Data.GetDevfileWorkspaceSpecContent() // add attribute to plugin elements err = addSourceAttributesForOverrideAndMerge(plugin.ImportReference, pluginWorkspaceContent) diff --git a/pkg/devfile/parser/sourceAttribute.go b/pkg/devfile/parser/sourceAttribute.go index bad1eec0..a92d3093 100644 --- a/pkg/devfile/parser/sourceAttribute.go +++ b/pkg/devfile/parser/sourceAttribute.go @@ -8,11 +8,11 @@ import ( const ( importSourceAttribute = "library.devfile.io/imported-from" - pluginOverrideAttribute = "library.devfile.io/plugin-override-from" parentOverrideAttribute = "library.devfile.io/parent-override-from" + pluginOverrideAttribute = "library.devfile.io/plugin-override-from" ) -// addSourceAttributesForTemplateSpecContent adds an attribute 'library.devfile.io/parent-override-from=' +// addSourceAttributesForParentOverride adds an attribute 'library.devfile.io/imported-from=' // to all elements of template spec content that support attributes. func addSourceAttributesForTemplateSpecContent(sourceImportReference v1.ImportReference, template *v1.DevWorkspaceTemplateSpecContent) { for idx, component := range template.Components { @@ -41,7 +41,7 @@ func addSourceAttributesForTemplateSpecContent(sourceImportReference v1.ImportRe } } -// addSourceAttributesForParentOverride adds an attribute 'library.devfile.io/imported-from=' +// addSourceAttributesForParentOverride adds an attribute 'library.devfile.io/parent-override-from=' // to all elements of parent override that support attributes. func addSourceAttributesForParentOverride(sourceImportReference v1.ImportReference, parentOverrides *v1.ParentOverrides) { for idx, component := range parentOverrides.Components {