Skip to content

Commit

Permalink
chore: fix broken tests
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Dec 13, 2024
1 parent b226153 commit 3557742
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 22 deletions.
112 changes: 102 additions & 10 deletions internal/api/promote_downstream_v1alpha1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestPromoteDownstream(t *testing.T) {
},
}},
}

testCases := []struct {
name string
req *svcv1alpha1.PromoteDownstreamRequest
Expand Down Expand Up @@ -74,8 +75,7 @@ func TestPromoteDownstream(t *testing.T) {
_ *connect.Response[svcv1alpha1.PromoteDownstreamResponse],
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 @@ -103,8 +103,7 @@ func TestPromoteDownstream(t *testing.T) {
_ *connect.Response[svcv1alpha1.PromoteDownstreamResponse],
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 @@ -174,8 +173,7 @@ func TestPromoteDownstream(t *testing.T) {
_ *connect.Response[svcv1alpha1.PromoteDownstreamResponse],
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 @@ -313,8 +311,7 @@ func TestPromoteDownstream(t *testing.T) {
_ *connect.Response[svcv1alpha1.PromoteDownstreamResponse],
err error,
) {
require.Error(t, err)
require.Equal(t, "find downstream stages: something went wrong", err.Error())
require.ErrorContains(t, err, "find downstream stages: something went wrong")
},
},
{
Expand Down Expand Up @@ -438,8 +435,87 @@ func TestPromoteDownstream(t *testing.T) {
_ *connect.Response[svcv1alpha1.PromoteDownstreamResponse],
err error,
) {
require.Error(t, err)
require.Equal(t, "not authorized", err.Error())
require.ErrorContains(t, err, "not authorized")
},
},
{
name: "error building Promotion",
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
},
findDownstreamStagesFn: func(
context.Context,
*kargoapi.Stage,
kargoapi.FreightOrigin,
) ([]kargoapi.Stage, error) {
return []kargoapi.Stage{
{
Spec: kargoapi.StageSpec{
PromotionTemplate: &kargoapi.PromotionTemplate{
Spec: kargoapi.PromotionTemplateSpec{
Steps: []kargoapi.PromotionStep{{}},
},
},
},
},
}, 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, "stage is required")
},
},
{
Expand Down Expand Up @@ -472,6 +548,10 @@ func TestPromoteDownstream(t *testing.T) {
string, string, string,
) (*kargoapi.Freight, error) {
return &kargoapi.Freight{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-project",
Name: "fake-freight",
},
Status: kargoapi.FreightStatus{
VerifiedIn: map[string]kargoapi.VerifiedStage{
"fake-stage": {},
Expand All @@ -486,6 +566,10 @@ func TestPromoteDownstream(t *testing.T) {
) ([]kargoapi.Stage, error) {
return []kargoapi.Stage{
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-project",
Name: "fake-downstream-stage",
},
Spec: kargoapi.StageSpec{
PromotionTemplate: &kargoapi.PromotionTemplate{
Spec: kargoapi.PromotionTemplateSpec{
Expand Down Expand Up @@ -556,6 +640,10 @@ func TestPromoteDownstream(t *testing.T) {
string, string, string,
) (*kargoapi.Freight, error) {
return &kargoapi.Freight{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-project",
Name: "fake-freight",
},
Status: kargoapi.FreightStatus{
VerifiedIn: map[string]kargoapi.VerifiedStage{
"fake-stage": {},
Expand All @@ -570,6 +658,10 @@ func TestPromoteDownstream(t *testing.T) {
) ([]kargoapi.Stage, error) {
return []kargoapi.Stage{
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-project",
Name: "fake-downstream-stage",
},
Spec: kargoapi.StageSpec{
PromotionTemplate: &kargoapi.PromotionTemplate{
Spec: kargoapi.PromotionTemplateSpec{
Expand Down
96 changes: 84 additions & 12 deletions internal/api/promote_to_stage_v1alpha1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"connectrpc.com/connect"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -28,6 +29,11 @@ func TestPromoteToStage(t *testing.T) {
Stages: []string{"fake-upstream-stage"},
},
}},
PromotionTemplate: &kargoapi.PromotionTemplate{
Spec: kargoapi.PromotionTemplateSpec{
Steps: []kargoapi.PromotionStep{{}},
},
},
}
testCases := []struct {
name string
Expand Down Expand Up @@ -74,8 +80,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 @@ -103,8 +108,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 @@ -174,8 +178,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 @@ -313,12 +316,11 @@ func TestPromoteToStage(t *testing.T) {
_ *connect.Response[svcv1alpha1.PromoteToStageResponse],
err error,
) {
require.Error(t, err)
require.Equal(t, "not authorized", err.Error())
require.Error(t, err, "not authorized")
},
},
{
name: "error creating Promotion",
name: "error building Promotion",
req: &svcv1alpha1.PromoteToStageRequest{
Project: "fake-project",
Stage: "fake-stage",
Expand All @@ -334,6 +336,10 @@ func TestPromoteToStage(t *testing.T) {
types.NamespacedName,
) (*kargoapi.Stage, error) {
return &kargoapi.Stage{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-project",
Name: "fake-stage",
},
Spec: testStageSpec,
}, nil
},
Expand All @@ -356,6 +362,64 @@ func TestPromoteToStage(t *testing.T) {
) error {
return nil
},
},
assertions: func(
t *testing.T,
_ *fakeevent.EventRecorder,
_ *connect.Response[svcv1alpha1.PromoteToStageResponse],
err error,
) {
require.ErrorContains(t, err, "build promotion")
},
},
{
name: "error creating Promotion",
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{
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{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-project",
Name: "fake-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,
Expand All @@ -370,8 +434,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.Error(t, err, "create promotion: something went wrong")
},
},
{
Expand All @@ -391,6 +454,10 @@ func TestPromoteToStage(t *testing.T) {
types.NamespacedName,
) (*kargoapi.Stage, error) {
return &kargoapi.Stage{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-project",
Name: "fake-stage",
},
Spec: testStageSpec,
}, nil
},
Expand All @@ -399,7 +466,12 @@ func TestPromoteToStage(t *testing.T) {
client.Client,
string, string, string,
) (*kargoapi.Freight, error) {
return &kargoapi.Freight{}, nil
return &kargoapi.Freight{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-project",
Name: "fake-freight",
},
}, nil
},
isFreightAvailableFn: func(*kargoapi.Stage, *kargoapi.Freight) bool {
return true
Expand Down
Loading

0 comments on commit 3557742

Please sign in to comment.