Skip to content

Commit

Permalink
Add Delete Comp, Command, Proj, StarterProj
Browse files Browse the repository at this point in the history
Signed-off-by: Maysun J Faisal <maysunaneek@gmail.com>
  • Loading branch information
maysunfaisal committed Mar 1, 2021
1 parent c175e96 commit b17b135
Show file tree
Hide file tree
Showing 11 changed files with 571 additions and 45 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/openshift/api v0.0.0-20200930075302-db52bc4ef99f
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.2.2
github.com/stretchr/testify v1.6.1 // indirect
github.com/stretchr/testify v1.6.1
github.com/xeipuuv/gojsonschema v1.2.0
k8s.io/api v0.19.0
k8s.io/apimachinery v0.19.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
4 changes: 4 additions & 0 deletions pkg/devfile/parser/data/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ type DevfileData interface {
GetComponents(common.DevfileOptions) ([]v1.Component, error)
AddComponents(components []v1.Component) error
UpdateComponent(component v1.Component)
DeleteComponent(name string) error

// project related methods
GetProjects(common.DevfileOptions) ([]v1.Project, error)
AddProjects(projects []v1.Project) error
UpdateProject(project v1.Project)
DeleteProject(name string) error

// starter projects related commands
GetStarterProjects(common.DevfileOptions) ([]v1.StarterProject, error)
AddStarterProjects(projects []v1.StarterProject) error
UpdateStarterProject(project v1.StarterProject)
DeleteStarterProject(name string) error

// command related methods
GetCommands(common.DevfileOptions) ([]v1.Command, error)
AddCommands(commands ...v1.Command) error
UpdateCommand(command v1.Command)
DeleteCommand(id string) error

// volume related methods
AddVolume(volume v1.Component, path string) error
Expand Down
29 changes: 29 additions & 0 deletions pkg/devfile/parser/data/v2/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,32 @@ func (d *DevfileV2) UpdateCommand(command v1.Command) {
}
}
}

// DeleteCommand removes the specified command
func (d *DevfileV2) DeleteCommand(id string) error {

found := false
for i := len(d.Commands) - 1; i >= 0; i-- {
if d.Commands[i].Composite != nil && d.Commands[i].Id != id {
var subCmd []string
for _, command := range d.Commands[i].Composite.Commands {
if command != id {
subCmd = append(subCmd, command)
}
}
d.Commands[i].Composite.Commands = subCmd
} else if d.Commands[i].Id == id {
found = true
d.Commands = append(d.Commands[:i], d.Commands[i+1:]...)
}
}

if !found {
return &common.FieldNotFoundError{
Field: "command",
Name: id,
}
}

return nil
}
145 changes: 145 additions & 0 deletions pkg/devfile/parser/data/v2/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/api/v2/pkg/attributes"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
"github.com/stretchr/testify/assert"
)

func TestDevfile200_GetCommands(t *testing.T) {
Expand Down Expand Up @@ -300,3 +301,147 @@ func TestDevfile200_UpdateCommands(t *testing.T) {
})
}
}

func TestDeleteCommands(t *testing.T) {

tests := []struct {
name string
commandToDelete string
commands []v1.Command
wantCommands []v1.Command
wantErr bool
}{
{
name: "Commands that belong to Composite Command",
commandToDelete: "command1",
commands: []v1.Command{
{
Id: "command1",
CommandUnion: v1.CommandUnion{
Exec: &v1.ExecCommand{},
},
},
{
Id: "command2",
CommandUnion: v1.CommandUnion{
Exec: &v1.ExecCommand{},
},
},
{
Id: "command3",
CommandUnion: v1.CommandUnion{
Composite: &v1.CompositeCommand{
Commands: []string{"command1", "command2", "command1"},
},
},
},
},
wantCommands: []v1.Command{
{
Id: "command2",
CommandUnion: v1.CommandUnion{
Exec: &v1.ExecCommand{},
},
},
{
Id: "command3",
CommandUnion: v1.CommandUnion{
Composite: &v1.CompositeCommand{
Commands: []string{"command2"},
},
},
},
},
wantErr: false,
},
{
name: "Commands that do not belong to Composite Command",
commandToDelete: "command3",
commands: []v1.Command{
{
Id: "command1",
CommandUnion: v1.CommandUnion{
Exec: &v1.ExecCommand{},
},
},
{
Id: "command2",
CommandUnion: v1.CommandUnion{
Composite: &v1.CompositeCommand{
Commands: []string{"command1"},
},
},
},
{
Id: "command3",
CommandUnion: v1.CommandUnion{
Composite: &v1.CompositeCommand{
Commands: []string{"command1"},
},
},
},
},
wantCommands: []v1.Command{
{
Id: "command1",
CommandUnion: v1.CommandUnion{
Exec: &v1.ExecCommand{},
},
},
{
Id: "command2",
CommandUnion: v1.CommandUnion{
Composite: &v1.CompositeCommand{
Commands: []string{"command1"},
},
},
},
},
wantErr: false,
},
{
name: "Missing Command",
commandToDelete: "command34",
commands: []v1.Command{
{
Id: "command1",
CommandUnion: v1.CommandUnion{
Exec: &v1.ExecCommand{},
},
},
},
wantCommands: []v1.Command{
{
Id: "command1",
CommandUnion: v1.CommandUnion{
Exec: &v1.ExecCommand{},
},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &DevfileV2{
v1.Devfile{
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
Commands: tt.commands,
},
},
},
}

err := d.DeleteCommand(tt.commandToDelete)
if tt.wantErr && err == nil {
t.Errorf("Expected error from test but got nil")
} else if !tt.wantErr && err != nil {
t.Errorf("Got unexpected error: %s", err)
} else if err == nil {
assert.Equal(t, tt.wantCommands, d.Commands, "The two values should be the same.")
}
})
}

}
29 changes: 29 additions & 0 deletions pkg/devfile/parser/data/v2/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,32 @@ func (d *DevfileV2) UpdateComponent(component v1.Component) {
d.Components[index] = component
}
}

// DeleteComponent removes the specified component
func (d *DevfileV2) DeleteComponent(name string) error {

found := false
for i := len(d.Components) - 1; i >= 0; i-- {
if d.Components[i].Container != nil && d.Components[i].Name != name {
var tmp []v1.VolumeMount
for _, volumeMount := range d.Components[i].Container.VolumeMounts {
if volumeMount.Name != name {
tmp = append(tmp, volumeMount)
}
}
d.Components[i].Container.VolumeMounts = tmp
} else if d.Components[i].Name == name {
found = true
d.Components = append(d.Components[:i], d.Components[i+1:]...)
}
}

if !found {
return &common.FieldNotFoundError{
Field: "component",
Name: name,
}
}

return nil
}
Loading

0 comments on commit b17b135

Please sign in to comment.