Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
fix(cli): Print error message if service does not exist with `trigger…
Browse files Browse the repository at this point in the history
… delivery` (#6351)

* fix(cli): trigger delivery with non-existing service

Signed-off-by: odubajDT <[email protected]>

* refactoring

Signed-off-by: odubajDT <[email protected]>

* minor changes regarding review

Signed-off-by: odubajDT <[email protected]>

* rewrite unit test

Signed-off-by: odubajDT <[email protected]>

* update regarding go-utils update

Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT authored Dec 15, 2021
1 parent 826c05b commit 1d994a4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 15 deletions.
21 changes: 16 additions & 5 deletions cli/cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"context"
"encoding/json"
"fmt"
models "github.com/keptn/go-utils/pkg/api/models"
apiutils "github.com/keptn/go-utils/pkg/api/utils"
"github.com/keptn/keptn/cli/pkg/logging"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"io"
"math"
"net/http"
"strings"
"time"

apimodels "github.com/keptn/go-utils/pkg/api/models"
models "github.com/keptn/go-utils/pkg/api/models"
apiutils "github.com/keptn/go-utils/pkg/api/utils"
"github.com/keptn/keptn/cli/pkg/logging"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

// AddWatchFlag ads the --watch flag to the command
Expand Down Expand Up @@ -127,3 +129,12 @@ func CheckEndpointStatus(endPoint string) error {

return nil
}

func ServiceInSlice(service string, serviceList []*apimodels.Service) bool {
for _, s := range serviceList {
if s.ServiceName == service {
return true
}
}
return false
}
18 changes: 14 additions & 4 deletions cli/cmd/trigger_delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"strings"
"time"

cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/google/uuid"
apimodels "github.com/keptn/go-utils/pkg/api/models"
Expand All @@ -14,10 +19,6 @@ import (
"github.com/keptn/keptn/cli/pkg/docker"
"github.com/keptn/keptn/cli/pkg/logging"
"github.com/spf13/cobra"
"net/url"
"os"
"strings"
"time"
)

type deliveryStruct struct {
Expand Down Expand Up @@ -100,6 +101,15 @@ func doTriggerDelivery(deliveryInputData deliveryStruct) error {
} else {
return fmt.Errorf("Could not start sequence because no stage has been found in project %s", *deliveryInputData.Project)
}

servicesHandler := apiutils.NewAuthenticatedServiceHandler(endPoint.String(), apiToken, "x-token", nil, endPoint.Scheme)
projectServices, err := servicesHandler.GetAllServices(*deliveryInputData.Project, *deliveryInputData.Stage)
if err != nil {
return fmt.Errorf("Error while retrieving information for service %s: %s", *deliveryInputData.Service, err.Error())
}
if !ServiceInSlice(*deliveryInputData.Service, projectServices) {
return fmt.Errorf("Could not start sequence because service %s has not been found in project %s", *deliveryInputData.Service, *deliveryInputData.Project)
}
}

jsonStr, err := internal.JSONPathToJSONObj(*deliveryInputData.Values)
Expand Down
44 changes: 44 additions & 0 deletions cli/cmd/trigger_delivery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,47 @@ func TestCheckImageNonAvailabilityD(t *testing.T) {
}
}
}

// TestTriggerDeliveryNonExistingService tests the trigger delivery
// with non-existing service.
func TestTriggerDeliveryNonExistingService(t *testing.T) {

credentialmanager.MockAuthCreds = true
checkEndPointStatusMock = true
projectName := "sockshop"

shipyardFilePath := "./shipyard.yaml"
defer testShipyard(t, shipyardFilePath, "")()

cmd := fmt.Sprintf("create project %s --shipyard=%s --mock", projectName, shipyardFilePath)
_, err := executeActionCommandC(cmd)
if err != nil {
t.Errorf(unexpectedErrMsg, err)
}

tests := []struct {
service string
wantErr bool
}{
{
service: "some-service",
wantErr: true,
},
{
service: "",
wantErr: true,
},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
cmd := fmt.Sprintf("trigger delivery --project=%s --service=%s --sequence=%s "+
"--image=%s --tag=%s --values=a.b.c=d --mock --values=c.d=e --mock", projectName, tt.service, "artifact-delivery", "docker.io/keptnexamples/carts", "0.9.1")
_, err := executeActionCommandC(cmd)

if (err != nil) != tt.wantErr {
t.Errorf("wanted error: %t, got: %v", tt.wantErr, err)
}
})
}
}
5 changes: 1 addition & 4 deletions cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN
github.com/aws/aws-sdk-go v1.34.9/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -736,8 +736,6 @@ github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8
github.com/karrick/godirwalk v1.15.8 h1:7+rWAZPn9zuRxaIqqT8Ohs2Q2Ac0msBqwRdxNCr2VVs=
github.com/karrick/godirwalk v1.15.8/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/keptn/go-utils v0.10.0/go.mod h1:ub4G0WZUckc3TizUoe5jKqfCOOLiH5pnf4M1SDCOT0M=
github.com/keptn/go-utils v0.11.1-0.20211206141641-bf58a65bf8c3 h1:vcOvalthsObVZCn+VWYIqjPRYD4MFD+JYkF+bna4PA8=
github.com/keptn/go-utils v0.11.1-0.20211206141641-bf58a65bf8c3/go.mod h1:gjrWX2loNCKhray5NGVtNk/t2/L1XleveU3UlhzY184=
github.com/keptn/go-utils v0.11.1-0.20211215075500-dcfdacb80594 h1:0sAg3bA8zl+5VJfz3fhlBAVqTuf5Zm3b6zPcdODu9kI=
github.com/keptn/go-utils v0.11.1-0.20211215075500-dcfdacb80594/go.mod h1:yJM7pnCUj23VHKa2az9eWUTAmLDv94f6DVHON9qV1kU=
github.com/keptn/kubernetes-utils v0.10.1-0.20211102080304-e59377afdc8b h1:L+1m9DuYPfQCEv82/u05jj7EgeeZ/8tDkwf4JoRzMbw=
Expand Down Expand Up @@ -844,7 +842,6 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.4.2 h1:6h7AQ0yhTcIsmFmnAwQls75jp2Gzs4iB8W7pjMO+rqo=
github.com/mitchellh/mapstructure v1.4.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
Expand Down
8 changes: 6 additions & 2 deletions cli/internal/cespec/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ var getActionTriggeredEventData = keptnv2.GetActionTriggeredEventData{
ProblemTitle: "Response time degradation",
RootCause: "Problem root cause",
},
ActionIndex: 0,
GetAction: keptnv2.GetActionData{
ActionIndex: 0,
},
}

var getActionStartedEventData = keptnv2.GetActionStartedEventData{
Expand All @@ -264,7 +266,9 @@ var getActionFinishedEventData = keptnv2.GetActionFinishedEventData{
Description: "Toggles a feature flag",
Value: map[string]string{"EnableItemCache": "on"},
},
ActionIndex: 1,
GetAction: keptnv2.GetActionData{
ActionIndex: 1,
},
}

var actionTriggeredEventData = keptnv2.ActionTriggeredEventData{
Expand Down

0 comments on commit 1d994a4

Please sign in to comment.