diff --git a/README.md b/README.md index b7804ab2..144157c1 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,16 @@ The function documentation can be accessed via [pkg.go.dev](https://pkg.go.dev/g // To get all the components from the devfile components, err := devfile.Data.GetComponents(DevfileOptions{}) + + // To get all the components from the devfile with attributes tagged - tool: console-import & import: {strategy: Dockerfile} + components, err := devfile.Data.GetComponents(DevfileOptions{ + Filter: map[string]interface{}{ + "tool": "console-import", + "import": map[string]interface{}{ + "strategy": "Dockerfile", + }, + }, + }) ``` 2. To get the Kubernetes objects from the devfile, visit pkg/devfile/generator/generators.go ``` diff --git a/go.mod b/go.mod index 2124eedd..7e886bf3 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/devfile/library go 1.13 require ( - github.com/devfile/api/v2 v2.0.0-20210211160219-33a78aec06af + github.com/devfile/api/v2 v2.0.0-20210219185433-585f5fe35835 github.com/fatih/color v1.7.0 github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 github.com/gobwas/glob v0.2.3 diff --git a/go.sum b/go.sum index 4a1b660c..0fae7b99 100644 --- a/go.sum +++ b/go.sum @@ -44,8 +44,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devfile/api/v2 v2.0.0-20210211160219-33a78aec06af h1:egbFAAS/CWJMAqa4zIm8Cik3+iCqTAfLPfCj6PLEG5Y= -github.com/devfile/api/v2 v2.0.0-20210211160219-33a78aec06af/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg= +github.com/devfile/api/v2 v2.0.0-20210219185433-585f5fe35835 h1:PalHtpqhvX/yu5DKFqwTWa1h7UJBd0H7+veJStur/wg= +github.com/devfile/api/v2 v2.0.0-20210219185433-585f5fe35835/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index cf5a5a78..5158c7d7 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -4,9 +4,11 @@ import ( "reflect" "testing" + "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" "github.com/devfile/api/v2/pkg/attributes" "github.com/devfile/library/pkg/devfile/parser" + v2 "github.com/devfile/library/pkg/devfile/parser/data/v2" "github.com/devfile/library/pkg/devfile/parser/data/v2/common" "github.com/devfile/library/pkg/testingutil" @@ -25,6 +27,21 @@ func TestGetContainers(t *testing.T) { containerImages := []string{"image1", "image2"} trueMountSources := true falseMountSources := false + + project := v1alpha2.Project{ + ClonePath: "test-project/", + Name: "project0", + ProjectSource: v1.ProjectSource{ + Git: &v1.GitProjectSource{ + GitLikeProjectSource: v1.GitLikeProjectSource{ + Remotes: map[string]string{ + "origin": "repo", + }, + }, + }, + }, + } + tests := []struct { name string containerComponents []v1.Component @@ -167,8 +184,17 @@ func TestGetContainers(t *testing.T) { t.Run(tt.name, func(t *testing.T) { devObj := parser.DevfileObj{ - Data: &testingutil.TestDevfileData{ - Components: tt.containerComponents, + Data: &v2.DevfileV2{ + Devfile: v1.Devfile{ + DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ + DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ + Components: tt.containerComponents, + Projects: []v1alpha2.Project{ + project, + }, + }, + }, + }, }, } diff --git a/pkg/devfile/generator/utils_test.go b/pkg/devfile/generator/utils_test.go index 68c07ba1..eab2821c 100644 --- a/pkg/devfile/generator/utils_test.go +++ b/pkg/devfile/generator/utils_test.go @@ -8,6 +8,7 @@ import ( "github.com/devfile/api/v2/pkg/attributes" "github.com/devfile/library/pkg/devfile/parser" + v2 "github.com/devfile/library/pkg/devfile/parser/data/v2" "github.com/devfile/library/pkg/devfile/parser/data/v2/common" "github.com/devfile/library/pkg/testingutil" buildv1 "github.com/openshift/api/build/v1" @@ -747,8 +748,14 @@ func TestGetServiceSpec(t *testing.T) { t.Run(tt.name, func(t *testing.T) { devObj := parser.DevfileObj{ - Data: &testingutil.TestDevfileData{ - Components: tt.containerComponents, + Data: &v2.DevfileV2{ + Devfile: v1.Devfile{ + DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ + DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ + Components: tt.containerComponents, + }, + }, + }, }, } @@ -1076,10 +1083,17 @@ func TestGetPortExposure(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { devObj := parser.DevfileObj{ - Data: &testingutil.TestDevfileData{ - Components: tt.containerComponents, + Data: &v2.DevfileV2{ + Devfile: v1.Devfile{ + DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ + DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ + Components: tt.containerComponents, + }, + }, + }, }, } + mapCreated, err := getPortExposure(devObj, tt.filterOptions) if !tt.wantErr && err != nil { t.Errorf("TestGetPortExposure unexpected error: %v", err) diff --git a/pkg/devfile/parser/configurables.go b/pkg/devfile/parser/configurables.go index 8bef2197..f25437dd 100644 --- a/pkg/devfile/parser/configurables.go +++ b/pkg/devfile/parser/configurables.go @@ -22,7 +22,8 @@ const ( // SetMetadataName set metadata name in a devfile func (d DevfileObj) SetMetadataName(name string) error { metadata := d.Data.GetMetadata() - d.Data.SetMetadata(name, metadata.Version) + metadata.Name = name + d.Data.SetMetadata(metadata) return d.WriteYamlDevfile() } diff --git a/pkg/devfile/parser/data/interface.go b/pkg/devfile/parser/data/interface.go index 732a15cf..6e465b9f 100644 --- a/pkg/devfile/parser/data/interface.go +++ b/pkg/devfile/parser/data/interface.go @@ -11,7 +11,7 @@ type DevfileData interface { GetSchemaVersion() string SetSchemaVersion(version string) GetMetadata() devfilepkg.DevfileMetadata - SetMetadata(name, version string) + SetMetadata(metadata devfilepkg.DevfileMetadata) // parent related methods GetParent() *v1.Parent diff --git a/pkg/devfile/parser/data/v2/2.1.0/devfileJsonSchema210.go b/pkg/devfile/parser/data/v2/2.1.0/devfileJsonSchema210.go index 5bf3fac4..2ae44959 100644 --- a/pkg/devfile/parser/data/v2/2.1.0/devfileJsonSchema210.go +++ b/pkg/devfile/parser/data/v2/2.1.0/devfileJsonSchema210.go @@ -28,16 +28,6 @@ const JsonSchema210 = `{ "apply" ] }, - { - "required": [ - "vscodeTask" - ] - }, - { - "required": [ - "vscodeLaunch" - ] - }, { "required": [ "composite" @@ -218,108 +208,6 @@ const JsonSchema210 = `{ "type": "string", "maxLength": 63, "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" - }, - "vscodeLaunch": { - "description": "Command providing the definition of a VsCode launch action", - "type": "object", - "oneOf": [ - { - "required": [ - "uri" - ] - }, - { - "required": [ - "inlined" - ] - } - ], - "properties": { - "group": { - "description": "Defines the group this command is part of", - "type": "object", - "required": [ - "kind" - ], - "properties": { - "isDefault": { - "description": "Identifies the default command for a given group kind", - "type": "boolean" - }, - "kind": { - "description": "Kind of group the command is part of", - "type": "string", - "enum": [ - "build", - "run", - "test", - "debug" - ] - } - }, - "additionalProperties": false - }, - "inlined": { - "description": "Inlined content of the VsCode configuration", - "type": "string" - }, - "uri": { - "description": "Location as an absolute of relative URI the VsCode configuration will be fetched from", - "type": "string" - } - }, - "additionalProperties": false - }, - "vscodeTask": { - "description": "Command providing the definition of a VsCode Task", - "type": "object", - "oneOf": [ - { - "required": [ - "uri" - ] - }, - { - "required": [ - "inlined" - ] - } - ], - "properties": { - "group": { - "description": "Defines the group this command is part of", - "type": "object", - "required": [ - "kind" - ], - "properties": { - "isDefault": { - "description": "Identifies the default command for a given group kind", - "type": "boolean" - }, - "kind": { - "description": "Kind of group the command is part of", - "type": "string", - "enum": [ - "build", - "run", - "test", - "debug" - ] - } - }, - "additionalProperties": false - }, - "inlined": { - "description": "Inlined content of the VsCode configuration", - "type": "string" - }, - "uri": { - "description": "Location as an absolute of relative URI the VsCode configuration will be fetched from", - "type": "string" - } - }, - "additionalProperties": false } }, "additionalProperties": false @@ -733,16 +621,6 @@ const JsonSchema210 = `{ "apply" ] }, - { - "required": [ - "vscodeTask" - ] - }, - { - "required": [ - "vscodeLaunch" - ] - }, { "required": [ "composite" @@ -906,102 +784,6 @@ const JsonSchema210 = `{ "type": "string", "maxLength": 63, "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" - }, - "vscodeLaunch": { - "description": "Command providing the definition of a VsCode launch action", - "type": "object", - "oneOf": [ - { - "required": [ - "uri" - ] - }, - { - "required": [ - "inlined" - ] - } - ], - "properties": { - "group": { - "description": "Defines the group this command is part of", - "type": "object", - "properties": { - "isDefault": { - "description": "Identifies the default command for a given group kind", - "type": "boolean" - }, - "kind": { - "description": "Kind of group the command is part of", - "type": "string", - "enum": [ - "build", - "run", - "test", - "debug" - ] - } - }, - "additionalProperties": false - }, - "inlined": { - "description": "Inlined content of the VsCode configuration", - "type": "string" - }, - "uri": { - "description": "Location as an absolute of relative URI the VsCode configuration will be fetched from", - "type": "string" - } - }, - "additionalProperties": false - }, - "vscodeTask": { - "description": "Command providing the definition of a VsCode Task", - "type": "object", - "oneOf": [ - { - "required": [ - "uri" - ] - }, - { - "required": [ - "inlined" - ] - } - ], - "properties": { - "group": { - "description": "Defines the group this command is part of", - "type": "object", - "properties": { - "isDefault": { - "description": "Identifies the default command for a given group kind", - "type": "boolean" - }, - "kind": { - "description": "Kind of group the command is part of", - "type": "string", - "enum": [ - "build", - "run", - "test", - "debug" - ] - } - }, - "additionalProperties": false - }, - "inlined": { - "description": "Inlined content of the VsCode configuration", - "type": "string" - }, - "uri": { - "description": "Location as an absolute of relative URI the VsCode configuration will be fetched from", - "type": "string" - } - }, - "additionalProperties": false } }, "additionalProperties": false @@ -1360,6 +1142,10 @@ const JsonSchema210 = `{ "description": "Allows specifying the definition of a volume shared by several other components", "type": "object", "properties": { + "ephemeral": { + "description": "Ephemeral volumes are not stored persistently across restarts. Defaults to false", + "type": "boolean" + }, "size": { "description": "Size of the volume", "type": "string" @@ -1405,6 +1191,10 @@ const JsonSchema210 = `{ "description": "Allows specifying the definition of a volume shared by several other components", "type": "object", "properties": { + "ephemeral": { + "description": "Ephemeral volumes are not stored persistently across restarts. Defaults to false", + "type": "boolean" + }, "size": { "description": "Size of the volume", "type": "string" @@ -1476,10 +1266,18 @@ const JsonSchema210 = `{ "description": "Optional devfile icon", "type": "string" }, + "language": { + "description": "Optional devfile language", + "type": "string" + }, "name": { "description": "Optional devfile name", "type": "string" }, + "projectType": { + "description": "Optional devfile project type", + "type": "string" + }, "tags": { "description": "Optional devfile tags", "type": "array", @@ -1491,6 +1289,10 @@ const JsonSchema210 = `{ "description": "Optional semver-compatible version", "type": "string", "pattern": "^([0-9]+)\\.([0-9]+)\\.([0-9]+)(\\-[0-9a-z-]+(\\.[0-9a-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$" + }, + "website": { + "description": "Optional devfile website", + "type": "string" } }, "additionalProperties": true @@ -1535,16 +1337,6 @@ const JsonSchema210 = `{ "apply" ] }, - { - "required": [ - "vscodeTask" - ] - }, - { - "required": [ - "vscodeLaunch" - ] - }, { "required": [ "composite" @@ -1708,102 +1500,6 @@ const JsonSchema210 = `{ "type": "string", "maxLength": 63, "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" - }, - "vscodeLaunch": { - "description": "Command providing the definition of a VsCode launch action", - "type": "object", - "oneOf": [ - { - "required": [ - "uri" - ] - }, - { - "required": [ - "inlined" - ] - } - ], - "properties": { - "group": { - "description": "Defines the group this command is part of", - "type": "object", - "properties": { - "isDefault": { - "description": "Identifies the default command for a given group kind", - "type": "boolean" - }, - "kind": { - "description": "Kind of group the command is part of", - "type": "string", - "enum": [ - "build", - "run", - "test", - "debug" - ] - } - }, - "additionalProperties": false - }, - "inlined": { - "description": "Inlined content of the VsCode configuration", - "type": "string" - }, - "uri": { - "description": "Location as an absolute of relative URI the VsCode configuration will be fetched from", - "type": "string" - } - }, - "additionalProperties": false - }, - "vscodeTask": { - "description": "Command providing the definition of a VsCode Task", - "type": "object", - "oneOf": [ - { - "required": [ - "uri" - ] - }, - { - "required": [ - "inlined" - ] - } - ], - "properties": { - "group": { - "description": "Defines the group this command is part of", - "type": "object", - "properties": { - "isDefault": { - "description": "Identifies the default command for a given group kind", - "type": "boolean" - }, - "kind": { - "description": "Kind of group the command is part of", - "type": "string", - "enum": [ - "build", - "run", - "test", - "debug" - ] - } - }, - "additionalProperties": false - }, - "inlined": { - "description": "Inlined content of the VsCode configuration", - "type": "string" - }, - "uri": { - "description": "Location as an absolute of relative URI the VsCode configuration will be fetched from", - "type": "string" - } - }, - "additionalProperties": false } }, "additionalProperties": false @@ -2203,16 +1899,6 @@ const JsonSchema210 = `{ "apply" ] }, - { - "required": [ - "vscodeTask" - ] - }, - { - "required": [ - "vscodeLaunch" - ] - }, { "required": [ "composite" @@ -2376,102 +2062,6 @@ const JsonSchema210 = `{ "type": "string", "maxLength": 63, "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" - }, - "vscodeLaunch": { - "description": "Command providing the definition of a VsCode launch action", - "type": "object", - "oneOf": [ - { - "required": [ - "uri" - ] - }, - { - "required": [ - "inlined" - ] - } - ], - "properties": { - "group": { - "description": "Defines the group this command is part of", - "type": "object", - "properties": { - "isDefault": { - "description": "Identifies the default command for a given group kind", - "type": "boolean" - }, - "kind": { - "description": "Kind of group the command is part of", - "type": "string", - "enum": [ - "build", - "run", - "test", - "debug" - ] - } - }, - "additionalProperties": false - }, - "inlined": { - "description": "Inlined content of the VsCode configuration", - "type": "string" - }, - "uri": { - "description": "Location as an absolute of relative URI the VsCode configuration will be fetched from", - "type": "string" - } - }, - "additionalProperties": false - }, - "vscodeTask": { - "description": "Command providing the definition of a VsCode Task", - "type": "object", - "oneOf": [ - { - "required": [ - "uri" - ] - }, - { - "required": [ - "inlined" - ] - } - ], - "properties": { - "group": { - "description": "Defines the group this command is part of", - "type": "object", - "properties": { - "isDefault": { - "description": "Identifies the default command for a given group kind", - "type": "boolean" - }, - "kind": { - "description": "Kind of group the command is part of", - "type": "string", - "enum": [ - "build", - "run", - "test", - "debug" - ] - } - }, - "additionalProperties": false - }, - "inlined": { - "description": "Inlined content of the VsCode configuration", - "type": "string" - }, - "uri": { - "description": "Location as an absolute of relative URI the VsCode configuration will be fetched from", - "type": "string" - } - }, - "additionalProperties": false } }, "additionalProperties": false @@ -2830,6 +2420,10 @@ const JsonSchema210 = `{ "description": "Allows specifying the definition of a volume shared by several other components", "type": "object", "properties": { + "ephemeral": { + "description": "Ephemeral volumes are not stored persistently across restarts. Defaults to false", + "type": "boolean" + }, "size": { "description": "Size of the volume", "type": "string" @@ -2872,6 +2466,10 @@ const JsonSchema210 = `{ "description": "Allows specifying the definition of a volume shared by several other components", "type": "object", "properties": { + "ephemeral": { + "description": "Ephemeral volumes are not stored persistently across restarts. Defaults to false", + "type": "boolean" + }, "size": { "description": "Size of the volume", "type": "string" @@ -2968,7 +2566,7 @@ const JsonSchema210 = `{ "additionalProperties": false }, "github": { - "description": "Project's GitHub source", + "description": "Project's GitHub source. Deprecated, use 'Git' instead", "type": "object", "properties": { "checkoutFrom": { @@ -3092,7 +2690,7 @@ const JsonSchema210 = `{ "additionalProperties": false }, "github": { - "description": "Project's GitHub source", + "description": "Project's GitHub source. Deprecated, use 'Git' instead", "type": "object", "properties": { "checkoutFrom": { @@ -3220,7 +2818,7 @@ const JsonSchema210 = `{ "additionalProperties": false }, "github": { - "description": "Project's GitHub source", + "description": "Project's GitHub source. Deprecated, use 'Git' instead", "type": "object", "required": [ "remotes" @@ -3352,7 +2950,7 @@ const JsonSchema210 = `{ "additionalProperties": false }, "github": { - "description": "Project's GitHub source", + "description": "Project's GitHub source. Deprecated, use 'Git' instead", "type": "object", "required": [ "remotes" diff --git a/pkg/devfile/parser/data/v2/common/command_helper.go b/pkg/devfile/parser/data/v2/common/command_helper.go index 728ce29d..d3e44b49 100644 --- a/pkg/devfile/parser/data/v2/common/command_helper.go +++ b/pkg/devfile/parser/data/v2/common/command_helper.go @@ -13,10 +13,6 @@ func GetGroup(dc v1.Command) *v1.CommandGroup { return dc.Exec.Group case dc.Apply != nil: return dc.Apply.Group - case dc.VscodeLaunch != nil: - return dc.VscodeLaunch.Group - case dc.VscodeTask != nil: - return dc.VscodeTask.Group case dc.Custom != nil: return dc.Custom.Group diff --git a/pkg/devfile/parser/data/v2/common/command_helper_test.go b/pkg/devfile/parser/data/v2/common/command_helper_test.go index b13e2a11..e3391db3 100644 --- a/pkg/devfile/parser/data/v2/common/command_helper_test.go +++ b/pkg/devfile/parser/data/v2/common/command_helper_test.go @@ -15,7 +15,7 @@ func TestGetGroup(t *testing.T) { want *v1.CommandGroup }{ { - name: "Case 1: Exec command group", + name: "Exec command group", command: v1.Command{ Id: "exec1", CommandUnion: v1.CommandUnion{ @@ -37,7 +37,7 @@ func TestGetGroup(t *testing.T) { }, }, { - name: "Case 2: Composite command group", + name: "Composite command group", command: v1.Command{ Id: "composite1", CommandUnion: v1.CommandUnion{ @@ -59,12 +59,12 @@ func TestGetGroup(t *testing.T) { }, }, { - name: "Case 3: Empty command", + name: "Empty command", command: v1.Command{}, want: nil, }, { - name: "Case 4: Apply command group", + name: "Apply command group", command: v1.Command{ Id: "apply1", CommandUnion: v1.CommandUnion{ @@ -86,47 +86,7 @@ func TestGetGroup(t *testing.T) { }, }, { - name: "Case 5: VscodeTaskcommand group", - command: v1.Command{ - Id: "vscodetask1", - CommandUnion: v1.CommandUnion{ - VscodeTask: &v1.VscodeConfigurationCommand{ - BaseCommand: v1.BaseCommand{ - Group: &v1.CommandGroup{ - IsDefault: true, - Kind: v1.TestCommandGroupKind, - }, - }, - }, - }, - }, - want: &v1.CommandGroup{ - IsDefault: true, - Kind: v1.TestCommandGroupKind, - }, - }, - { - name: "Case 6: VscodeLaunchCommand group", - command: v1.Command{ - Id: "vscodetask1", - CommandUnion: v1.CommandUnion{ - VscodeLaunch: &v1.VscodeConfigurationCommand{ - BaseCommand: v1.BaseCommand{ - Group: &v1.CommandGroup{ - IsDefault: true, - Kind: v1.DebugCommandGroupKind, - }, - }, - }, - }, - }, - want: &v1.CommandGroup{ - IsDefault: true, - Kind: v1.DebugCommandGroupKind, - }, - }, - { - name: "Case 7: Custom command group", + name: "Custom command group", command: v1.Command{ Id: "custom1", CommandUnion: v1.CommandUnion{ diff --git a/pkg/devfile/parser/data/v2/header.go b/pkg/devfile/parser/data/v2/header.go index 6798ec95..b005fd44 100644 --- a/pkg/devfile/parser/data/v2/header.go +++ b/pkg/devfile/parser/data/v2/header.go @@ -20,9 +20,6 @@ func (d *DevfileV2) GetMetadata() devfilepkg.DevfileMetadata { } // SetMetadata sets the metadata for devfile -func (d *DevfileV2) SetMetadata(name, version string) { - d.Metadata = devfilepkg.DevfileMetadata{ - Name: name, - Version: version, - } +func (d *DevfileV2) SetMetadata(metadata devfilepkg.DevfileMetadata) { + d.Metadata = metadata } diff --git a/pkg/devfile/parser/data/v2/header_test.go b/pkg/devfile/parser/data/v2/header_test.go index 81cc250c..f7e2835f 100644 --- a/pkg/devfile/parser/data/v2/header_test.go +++ b/pkg/devfile/parser/data/v2/header_test.go @@ -153,8 +153,7 @@ func TestDevfile200_SetSetMetadata(t *testing.T) { } tests := []struct { name string - metadataName string - metadataVersion string + metadata devfilepkg.DevfileMetadata devfilev2 *DevfileV2 expectedDevfilev2 *DevfileV2 }{ @@ -165,8 +164,10 @@ func TestDevfile200_SetSetMetadata(t *testing.T) { DevfileHeader: devfilepkg.DevfileHeader{}, }, }, - metadataName: "nodejs", - metadataVersion: "2.0.0", + metadata: devfilepkg.DevfileMetadata{ + Name: "nodejs", + Version: "2.0.0", + }, expectedDevfilev2: &DevfileV2{ v1.Devfile{ DevfileHeader: devfilepkg.DevfileHeader{ @@ -187,15 +188,39 @@ func TestDevfile200_SetSetMetadata(t *testing.T) { }, }, }, - metadataName: "nodejs", - metadataVersion: "2.0.0", + metadata: devfilepkg.DevfileMetadata{ + Name: "nodejs", + Version: "2.1.0", + Attributes: attributes.Attributes{}.FromMap(map[string]interface{}{ + "xyz": "xyz", + }, nil), + DisplayName: "display", + Description: "decription", + Tags: []string{"tag1"}, + Icon: "icon", + GlobalMemoryLimit: "globalmemorylimit", + ProjectType: "projectype", + Language: "language", + Website: "website", + }, expectedDevfilev2: &DevfileV2{ v1.Devfile{ DevfileHeader: devfilepkg.DevfileHeader{ SchemaVersion: "2.0.0", Metadata: devfilepkg.DevfileMetadata{ Name: "nodejs", - Version: "2.0.0", + Version: "2.1.0", + Attributes: attributes.Attributes{}.FromMap(map[string]interface{}{ + "xyz": "xyz", + }, nil), + DisplayName: "display", + Description: "decription", + Tags: []string{"tag1"}, + Icon: "icon", + GlobalMemoryLimit: "globalmemorylimit", + ProjectType: "projectype", + Language: "language", + Website: "website", }, }, }, @@ -204,7 +229,7 @@ func TestDevfile200_SetSetMetadata(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.devfilev2.SetMetadata(tt.metadataName, tt.metadataVersion) + tt.devfilev2.SetMetadata(tt.metadata) if !reflect.DeepEqual(tt.devfilev2, tt.expectedDevfilev2) { t.Errorf("TestDevfile200_SetSchemaVersion() expected %v, got %v", tt.expectedDevfilev2, tt.devfilev2) } diff --git a/pkg/devfile/parser/parse.go b/pkg/devfile/parser/parse.go index a86459b4..087b3f3f 100644 --- a/pkg/devfile/parser/parse.go +++ b/pkg/devfile/parser/parse.go @@ -113,12 +113,12 @@ func ParseFromData(data []byte) (d DevfileObj, err error) { func parseParentAndPlugin(d DevfileObj) (err error) { flattenedParent := &v1.DevWorkspaceTemplateSpecContent{} - if d.Data.GetParent() != nil { - if !reflect.DeepEqual(d.Data.GetParent(), &v1.Parent{}) { + parent := d.Data.GetParent() + if parent != nil { + if !reflect.DeepEqual(parent, &v1.Parent{}) { - parent := d.Data.GetParent() var parentDevfileObj DevfileObj - if d.Data.GetParent().Uri != "" { + if parent.Uri != "" { parentDevfileObj, err = parseFromURI(parent.Uri, d.Ctx) if err != nil { return err diff --git a/pkg/testingutil/devfile.go b/pkg/testingutil/devfile.go index 3386d9df..87ce3c80 100644 --- a/pkg/testingutil/devfile.go +++ b/pkg/testingutil/devfile.go @@ -1,233 +1,9 @@ package testingutil import ( - "fmt" - "strings" - v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" - devfilepkg "github.com/devfile/api/v2/pkg/devfile" - "github.com/devfile/library/pkg/devfile/parser/data/v2/common" ) -// TestDevfileData is a convenience data type used to mock up a devfile configuration -type TestDevfileData struct { - Components []v1.Component - ExecCommands []v1.ExecCommand - CompositeCommands []v1.CompositeCommand - Commands []v1.Command - Events v1.Events -} - -// GetMetadata is a mock function to get metadata from devfile -func (d TestDevfileData) GetMetadata() devfilepkg.DevfileMetadata { - return devfilepkg.DevfileMetadata{} -} - -// SetMetadata sets metadata for the test devfile -func (d TestDevfileData) SetMetadata(name, version string) {} - -// GetSchemaVersion gets the schema version for the test devfile -func (d TestDevfileData) GetSchemaVersion() string { return "testSchema" } - -// SetSchemaVersion sets the schema version for the test devfile -func (d TestDevfileData) SetSchemaVersion(version string) {} - -// GetParent is a mock function to get parent from devfile -func (d TestDevfileData) GetParent() *v1.Parent { - return &v1.Parent{} -} - -// SetParent is a mock function to set parent of the test devfile -func (d TestDevfileData) SetParent(parent *v1.Parent) {} - -// GetEvents is a mock function to get events from devfile -func (d TestDevfileData) GetEvents() v1.Events { - return d.Events -} - -// AddEvents is a mock function to add events to the test devfile -func (d TestDevfileData) AddEvents(events v1.Events) error { return nil } - -// UpdateEvents is a mock function to update the events of the test devfile -func (d TestDevfileData) UpdateEvents(postStart, postStop, preStart, preStop []string) {} - -// GetComponents is a mock function to get the components from a devfile -func (d TestDevfileData) GetComponents(options common.DevfileOptions) ([]v1.Component, error) { - if len(options.Filter) == 0 { - return d.Components, nil - } - - var components []v1.Component - for _, comp := range d.Components { - filterIn, err := common.FilterDevfileObject(comp.Attributes, options) - if err != nil { - return nil, err - } - - if filterIn { - components = append(components, comp) - } - } - - return components, nil -} - -// AddComponents is a mock function to add components to the test devfile -func (d TestDevfileData) AddComponents(components []v1.Component) error { return nil } - -// UpdateComponent is a mock function to update the component of the test devfile -func (d TestDevfileData) UpdateComponent(component v1.Component) {} - -// GetProjects is a mock function to get the projects from a test devfile -func (d TestDevfileData) GetProjects(options common.DevfileOptions) ([]v1.Project, error) { - projectName := [...]string{"test-project", "anotherproject"} - clonePath := [...]string{"test-project/", "anotherproject/"} - sourceLocation := [...]string{"https://github.com/someproject/test-project.git", "https://github.com/another/project.git"} - - project1 := v1.Project{ - ClonePath: clonePath[0], - Name: projectName[0], - ProjectSource: v1.ProjectSource{ - Git: &v1.GitProjectSource{ - GitLikeProjectSource: v1.GitLikeProjectSource{ - Remotes: map[string]string{ - "origin": sourceLocation[0], - }, - }, - }, - }, - } - - project2 := v1.Project{ - ClonePath: clonePath[1], - Name: projectName[1], - ProjectSource: v1.ProjectSource{ - Git: &v1.GitProjectSource{ - GitLikeProjectSource: v1.GitLikeProjectSource{ - Remotes: map[string]string{ - "origin": sourceLocation[1], - }, - }, - }, - }, - } - return []v1.Project{project1, project2}, nil - -} - -// AddProjects is a mock function to add projects to the test devfile -func (d TestDevfileData) AddProjects(projects []v1.Project) error { return nil } - -// UpdateProject is a mock function to update a project for the test devfile -func (d TestDevfileData) UpdateProject(project v1.Project) {} - -// GetStarterProjects is a mock function to get the starter projects from a test devfile -func (d TestDevfileData) GetStarterProjects(options common.DevfileOptions) ([]v1.StarterProject, error) { - return []v1.StarterProject{}, nil -} - -// AddStarterProjects is a mock func to add the starter projects to the test devfile -func (d TestDevfileData) AddStarterProjects(projects []v1.StarterProject) error { - return nil -} - -// UpdateStarterProject is a mock func to update the starter project for a test devfile -func (d TestDevfileData) UpdateStarterProject(project v1.StarterProject) {} - -// GetCommands is a mock function to get the commands from a devfile -func (d TestDevfileData) GetCommands(options common.DevfileOptions) ([]v1.Command, error) { - - var commands []v1.Command - - for _, command := range d.Commands { - // we convert devfile command id to lowercase so that we can handle - // cases efficiently without being error prone - command.Id = strings.ToLower(command.Id) - commands = append(commands, command) - } - - return commands, nil -} - -// AddCommands is a mock func that adds commands to the test devfile -func (d *TestDevfileData) AddCommands(commands ...v1.Command) error { - devfileCommands, err := d.GetCommands(common.DevfileOptions{}) - if err != nil { - return err - } - - for _, command := range commands { - id := command.Id - for _, devfileCommand := range devfileCommands { - if id == devfileCommand.Id { - return fmt.Errorf("command %s already exist in the devfile", id) - } - } - - d.Commands = append(d.Commands, command) - } - return nil -} - -// UpdateCommand is a mock func to update the command in a test devfile -func (d TestDevfileData) UpdateCommand(command v1.Command) {} - -// AddVolume is a mock func that adds volume to the test devfile -func (d TestDevfileData) AddVolume(volumeComponent v1.Component, path string) error { - return nil -} - -// DeleteVolume is a mock func that deletes volume from the test devfile -func (d TestDevfileData) DeleteVolume(name string) error { return nil } - -// GetVolumeMountPath is a mock func that gets the volume mount path of a container -func (d TestDevfileData) GetVolumeMountPath(name string) (string, error) { - return "", nil -} - -// GetDevfileContainerComponents gets the container components from the test devfile -func (d TestDevfileData) GetDevfileContainerComponents(options common.DevfileOptions) ([]v1.Component, error) { - var components []v1.Component - devfileComponents, err := d.GetComponents(options) - if err != nil { - return nil, err - } - for _, comp := range devfileComponents { - if comp.Container != nil { - components = append(components, comp) - } - } - return components, nil -} - -// GetDevfileVolumeComponents gets the volume components from the test devfile -func (d TestDevfileData) GetDevfileVolumeComponents(options common.DevfileOptions) ([]v1.Component, error) { - var components []v1.Component - devfileComponents, err := d.GetComponents(options) - if err != nil { - return nil, err - } - for _, comp := range devfileComponents { - if comp.Volume != nil { - components = append(components, comp) - } - } - return components, nil -} - -// GetDevfileWorkspace is a mock func to get the DevfileWorkspace in a test devfile -func (d TestDevfileData) GetDevfileWorkspace() *v1.DevWorkspaceTemplateSpecContent { - return &v1.DevWorkspaceTemplateSpecContent{} -} - -// SetDevfileWorkspace is a mock func to set the DevfileWorkspace in a test devfile -func (d TestDevfileData) SetDevfileWorkspace(content v1.DevWorkspaceTemplateSpecContent) {} - -// Validate is a mock validation that always validates without error -func (d TestDevfileData) Validate() error { - return nil -} - // GetFakeContainerComponent returns a fake container component for testing. func GetFakeContainerComponent(name string) v1.Component { image := "docker.io/maven:latest"