Skip to content

Commit

Permalink
chore: add additional test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Dec 2, 2024
1 parent 6692c8c commit 2934704
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 13 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/stage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ type Stage struct {
// one or more downstream Stages.
func (s *Stage) IsControlFlow() bool {
switch {
case s.Spec.PromotionTemplateRef != nil && len(s.Spec.PromotionTemplate.Name) > 0:
case s.Spec.PromotionTemplateRef != nil && len(s.Spec.PromotionTemplateRef.Name) > 0:
return false

Check warning on line 159 in api/v1alpha1/stage_types.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/stage_types.go#L158-L159

Added lines #L158 - L159 were not covered by tests
case s.Spec.PromotionTemplate != nil && len(s.Spec.PromotionTemplate.Spec.Steps) > 0:
return false
Expand Down
2 changes: 1 addition & 1 deletion internal/api/promote_downstream_v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (s *server) PromoteDownstream(
var ok bool
if template, ok = templates[downstream.Spec.PromotionTemplateRef.Name]; !ok {
template = &kargoapi.PromotionTemplate{}
if err = s.client.Get(ctx, types.NamespacedName{
if err = s.getPromotionTemplateFn(ctx, types.NamespacedName{
Namespace: downstream.Namespace,
Name: downstream.Spec.PromotionTemplateRef.Name,
}, template); err != nil {
Expand Down
87 changes: 87 additions & 0 deletions internal/api/promote_downstream_v1alpha1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,93 @@ func TestPromoteDownstream(t *testing.T) {
require.Equal(t, "not authorized", err.Error())
},
},
{
name: "PromotionTemplate reference not found",
req: &svcv1alpha1.PromoteDownstreamRequest{
Project: "fake-project",
Stage: "fake-stage",
Freight: "fake-freight",
},
server: &server{
validateProjectExistsFn: func(context.Context, string) error {
return nil
},
getStageFn: func(
context.Context,
client.Client,
types.NamespacedName,
) (*kargoapi.Stage, error) {
return &kargoapi.Stage{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-project",
Name: "fake-stage",
},
Spec: testStageSpec,
}, nil
},
getFreightByNameOrAliasFn: func(
context.Context,
client.Client,
string, string, string,
) (*kargoapi.Freight, error) {
return &kargoapi.Freight{
Status: kargoapi.FreightStatus{
VerifiedIn: map[string]kargoapi.VerifiedStage{
"fake-stage": {},
},
},
}, nil
},
getPromotionTemplateFn: func(
context.Context,
client.ObjectKey,
client.Object,
...client.GetOption,
) error {
return errors.New("not found")
},
findDownstreamStagesFn: func(
context.Context,
*kargoapi.Stage,
kargoapi.FreightOrigin,
) ([]kargoapi.Stage, error) {
return []kargoapi.Stage{
{
Spec: kargoapi.StageSpec{
PromotionTemplateRef: &kargoapi.PromotionTemplateReference{
Name: "fake-promotion-template",
},
},
},
}, nil
},
authorizeFn: func(
context.Context,
string,
schema.GroupVersionResource,
string,
client.ObjectKey,
) error {
return nil
},
createPromotionFn: func(
context.Context,
client.Object,
...client.CreateOption,
) error {
return nil
},
},
assertions: func(
t *testing.T,
_ *fakeevent.EventRecorder,
_ *connect.Response[svcv1alpha1.PromoteDownstreamResponse],
err error,
) {
require.ErrorContains(t, err, "get PromotionTemplate")
require.ErrorContains(t, err, "not found")
},
},
{
name: "error creating Promotion",
req: &svcv1alpha1.PromoteDownstreamRequest{
Expand Down
2 changes: 1 addition & 1 deletion internal/api/promote_to_stage_v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (s *server) PromoteToStage(
}

Check warning on line 125 in internal/api/promote_to_stage_v1alpha1.go

View check run for this annotation

Codecov / codecov/patch

internal/api/promote_to_stage_v1alpha1.go#L121-L125

Added lines #L121 - L125 were not covered by tests

template = &kargoapi.PromotionTemplate{}
if err = s.client.Get(ctx, types.NamespacedName{
if err = s.getPromotionTemplateFn(ctx, types.NamespacedName{
Namespace: project,
Name: stage.Spec.PromotionTemplateRef.Name,
}, template); err != nil {
Expand Down
85 changes: 75 additions & 10 deletions internal/api/promote_to_stage_v1alpha1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func TestPromoteToStage(t *testing.T) {
_ *connect.Response[svcv1alpha1.PromoteToStageResponse],
err error,
) {
require.Error(t, err)
require.Equal(t, "something went wrong", err.Error())
require.ErrorContains(t, err, "something went wrong")
},
},
{
Expand Down Expand Up @@ -110,8 +109,7 @@ func TestPromoteToStage(t *testing.T) {
_ *connect.Response[svcv1alpha1.PromoteToStageResponse],
err error,
) {
require.Error(t, err)
require.Equal(t, "get stage: something went wrong", err.Error())
require.ErrorContains(t, err, "get stage: something went wrong")
},
},
{
Expand Down Expand Up @@ -181,8 +179,7 @@ func TestPromoteToStage(t *testing.T) {
_ *connect.Response[svcv1alpha1.PromoteToStageResponse],
err error,
) {
require.Error(t, err)
require.Equal(t, "get freight: something went wrong", err.Error())
require.ErrorContains(t, err, "get freight: something went wrong")
},
},
{
Expand Down Expand Up @@ -320,8 +317,77 @@ func TestPromoteToStage(t *testing.T) {
_ *connect.Response[svcv1alpha1.PromoteToStageResponse],
err error,
) {
require.Error(t, err)
require.Equal(t, "not authorized", err.Error())
require.ErrorContains(t, err, "not authorized")
},
},
{
name: "PromotionTemplate not found",
req: &svcv1alpha1.PromoteToStageRequest{
Project: "fake-project",
Stage: "fake-stage",
Freight: "fake-freight",
},
server: &server{
validateProjectExistsFn: func(context.Context, string) error {
return nil
},
getStageFn: func(
context.Context,
client.Client,
types.NamespacedName,
) (*kargoapi.Stage, error) {
return &kargoapi.Stage{
Spec: kargoapi.StageSpec{
PromotionTemplateRef: &kargoapi.PromotionTemplateReference{
Name: "fake-promotion-template",
},
RequestedFreight: testStageSpec.RequestedFreight,
},
}, nil
},
getPromotionTemplateFn: func(
context.Context,
client.ObjectKey,
client.Object,
...client.GetOption,
) error {
return errors.New("not found")
},
getFreightByNameOrAliasFn: func(
context.Context,
client.Client,
string, string, string,
) (*kargoapi.Freight, error) {
return &kargoapi.Freight{}, nil
},
isFreightAvailableFn: func(*kargoapi.Stage, *kargoapi.Freight) bool {
return true
},
authorizeFn: func(
context.Context,
string,
schema.GroupVersionResource,
string,
client.ObjectKey,
) error {
return nil
},
createPromotionFn: func(
context.Context,
client.Object,
...client.CreateOption,
) error {
return nil
},
},
assertions: func(
t *testing.T,
_ *fakeevent.EventRecorder,
_ *connect.Response[svcv1alpha1.PromoteToStageResponse],
err error,
) {
require.ErrorContains(t, err, "get PromotionTemplate")
require.ErrorContains(t, err, "not found")
},
},
{
Expand Down Expand Up @@ -377,8 +443,7 @@ func TestPromoteToStage(t *testing.T) {
_ *connect.Response[svcv1alpha1.PromoteToStageResponse],
err error,
) {
require.Error(t, err)
require.Equal(t, "create promotion: something went wrong", err.Error())
require.ErrorContains(t, err, "create promotion: something went wrong")
},
},
{
Expand Down
7 changes: 7 additions & 0 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ type server struct {
client.Client,
types.NamespacedName,
) (*kargoapi.Stage, error)
getPromotionTemplateFn func(
context.Context,
client.ObjectKey,
client.Object,
...client.GetOption,
) error
getFreightByNameOrAliasFn func(
ctx context.Context,
c client.Client,
Expand Down Expand Up @@ -175,6 +181,7 @@ func NewServer(
s.getFreightByNameOrAliasFn = kargoapi.GetFreightByNameOrAlias
s.isFreightAvailableFn = kargoapi.IsFreightAvailable
s.createPromotionFn = kubeClient.Create
s.getPromotionTemplateFn = kubeClient.Get
s.findDownstreamStagesFn = s.findDownstreamStages
s.listFreightFn = kubeClient.List
s.getAvailableFreightForStageFn = s.getAvailableFreightForStage
Expand Down

0 comments on commit 2934704

Please sign in to comment.