Skip to content

Commit

Permalink
fix(storage/sql/rollout): validate rule is supplied on create
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Jun 30, 2023
1 parent 6e41f35 commit df2ba34
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 19 deletions.
7 changes: 5 additions & 2 deletions build/internal/flipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Base(ctx context.Context, client *dagger.Client, req FliptRequest) (*dagger
golang := client.Container(dagger.ContainerOpts{
Platform: dagger.Platform(platforms.Format(req.BuildTarget)),
}).
From("golang:1.20-alpine3.16").
From("golang:1.20-alpine3.18").
WithEnvVariable("GOCACHE", goBuildCachePath).
WithEnvVariable("GOMODCACHE", goModCachePath).
WithExec([]string{"apk", "add", "bash", "gcc", "binutils-gold", "build-base", "git"})
Expand Down Expand Up @@ -168,6 +168,9 @@ func Base(ctx context.Context, client *dagger.Client, req FliptRequest) (*dagger
return golang.
WithMountedDirectory("./ui", embed.Directory("./ui")).
WithMountedDirectory("./ui/dist", req.UI.Directory("./dist")).
// see: https://github.com/golang/go/issues/60825
// should be fixed in go 1.20.6
WithEnvVariable("GOEXPERIMENT", "nocoverageredesign").
WithExec([]string{"mkdir", "-p", req.binary()}).
WithExec([]string{"sh", "-c", goBuildCmd}), nil
}
Expand All @@ -176,7 +179,7 @@ func Base(ctx context.Context, client *dagger.Client, req FliptRequest) (*dagger
// into a thinner alpine distribution.
func Package(ctx context.Context, client *dagger.Client, flipt *dagger.Container, req FliptRequest) (*dagger.Container, error) {
// build container with just Flipt + config
return client.Container().From("alpine:3.16").
return client.Container().From("alpine:3.18").
WithExec([]string{"apk", "add", "--no-cache", "postgresql-client", "openssl", "ca-certificates"}).
WithExec([]string{"mkdir", "-p", "/var/opt/flipt"}).
WithExec([]string{"mkdir", "-p", "/var/log/flipt"}).
Expand Down
4 changes: 3 additions & 1 deletion internal/storage/sql/common/rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,10 @@ func (s *Store) CreateRollout(ctx context.Context, r *flipt.CreateRolloutRequest
rollout.Type = flipt.RolloutType_SEGMENT_ROLLOUT_TYPE
case *flipt.CreateRolloutRequest_Percentage:
rollout.Type = flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE
case nil:
return nil, errs.ErrInvalid("rollout rule is missing")
default:
return nil, fmt.Errorf("invalid rollout rule type %T", r.GetRule())
return nil, errs.ErrInvalidf("invalid rollout rule type %T", r.GetRule())
}

tx, err := s.db.Begin()
Expand Down
117 changes: 101 additions & 16 deletions internal/storage/sql/rollout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
errs "go.flipt.io/flipt/errors"
"go.flipt.io/flipt/internal/storage"
"go.flipt.io/flipt/internal/storage/sql/common"
flipt "go.flipt.io/flipt/rpc/flipt"
Expand All @@ -29,7 +30,6 @@ func (s *DBTestSuite) TestGetRollout() {
rollout, err := s.store.CreateRollout(context.TODO(), &flipt.CreateRolloutRequest{
FlagKey: flag.Key,
Rank: 1,
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand Down Expand Up @@ -75,7 +75,6 @@ func (s *DBTestSuite) TestGetRolloutNamespace() {
FlagKey: flag.Key,
NamespaceKey: s.namespace,
Rank: 1,
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand Down Expand Up @@ -153,7 +152,6 @@ func (s *DBTestSuite) TestListRollouts() {
{
FlagKey: flag.Key,
Rank: 1,
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand All @@ -164,7 +162,6 @@ func (s *DBTestSuite) TestListRollouts() {
{
FlagKey: flag.Key,
Rank: 2,
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand Down Expand Up @@ -212,7 +209,6 @@ func (s *DBTestSuite) TestListRolloutsNamespace() {
NamespaceKey: s.namespace,
FlagKey: flag.Key,
Rank: 1,
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand All @@ -224,7 +220,6 @@ func (s *DBTestSuite) TestListRolloutsNamespace() {
NamespaceKey: s.namespace,
FlagKey: flag.Key,
Rank: 2,
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand Down Expand Up @@ -269,7 +264,6 @@ func (s *DBTestSuite) TestListRolloutsPagination_LimitOffset() {
{
FlagKey: flag.Key,
Rank: 1,
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand All @@ -280,7 +274,6 @@ func (s *DBTestSuite) TestListRolloutsPagination_LimitOffset() {
{
FlagKey: flag.Key,
Rank: 2,
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand Down Expand Up @@ -320,7 +313,6 @@ func (s *DBTestSuite) TestListRolloutsPagination_LimitWithNextPage() {
{
FlagKey: flag.Key,
Rank: 1,
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand All @@ -331,7 +323,6 @@ func (s *DBTestSuite) TestListRolloutsPagination_LimitWithNextPage() {
{
FlagKey: flag.Key,
Rank: 2,
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand Down Expand Up @@ -373,24 +364,59 @@ func (s *DBTestSuite) TestListRolloutsPagination_LimitWithNextPage() {
assert.Equal(t, reqs[0].Rank, got[0].Rank)
}

func (s *DBTestSuite) TestCreateRollout_InvalidRolloutType() {
t := s.T()

_, err := s.store.CreateRollout(context.TODO(), &flipt.CreateRolloutRequest{
FlagKey: "foo",
Rank: 1,
})

assert.Equal(t, err, errs.ErrInvalid("rollout rule is missing"))
}

func (s *DBTestSuite) TestCreateRollout_FlagNotFound() {
t := s.T()

_, err := s.store.CreateRollout(context.TODO(), &flipt.CreateRolloutRequest{
FlagKey: "foo",
Rank: 1,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Percentage: 50.0,
Value: true,
},
},
})

assert.EqualError(t, err, "flag \"default/foo\" not found")
}

func (s *DBTestSuite) TestCreateRolloutNamespace_InvalidRolloutType() {
t := s.T()

_, err := s.store.CreateRollout(context.TODO(), &flipt.CreateRolloutRequest{
NamespaceKey: s.namespace,
FlagKey: "foo",
Rank: 1,
})

assert.Equal(t, err, errs.ErrInvalid("rollout rule is missing"))
}

func (s *DBTestSuite) TestCreateRolloutNamespace_FlagNotFound() {
t := s.T()

_, err := s.store.CreateRollout(context.TODO(), &flipt.CreateRolloutRequest{
NamespaceKey: s.namespace,
FlagKey: "foo",
Rank: 1,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Percentage: 50.0,
Value: true,
},
},
})

assert.EqualError(t, err, fmt.Sprintf("flag \"%s/foo\" not found", s.namespace))
Expand All @@ -412,7 +438,6 @@ func (s *DBTestSuite) TestUpdateRollout() {
rollout, err := s.store.CreateRollout(context.TODO(), &flipt.CreateRolloutRequest{
FlagKey: flag.Key,
Rank: 1,
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand All @@ -438,7 +463,6 @@ func (s *DBTestSuite) TestUpdateRollout() {
Id: rollout.Id,
FlagKey: rollout.FlagKey,
Description: "foobar",
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.UpdateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: false,
Expand Down Expand Up @@ -479,7 +503,6 @@ func (s *DBTestSuite) TestUpdateRolloutNamespace() {
FlagKey: flag.Key,
NamespaceKey: s.namespace,
Rank: 1,
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand All @@ -506,7 +529,6 @@ func (s *DBTestSuite) TestUpdateRolloutNamespace() {
FlagKey: rollout.FlagKey,
NamespaceKey: s.namespace,
Description: "foobar",
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.UpdateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: false,
Expand All @@ -529,6 +551,71 @@ func (s *DBTestSuite) TestUpdateRolloutNamespace() {
assert.NotZero(t, updated.UpdatedAt)
}

func (s *DBTestSuite) TestUpdateRollout_InvalidType() {
t := s.T()

ctx := context.TODO()
flag, err := s.store.CreateFlag(ctx, &flipt.CreateFlagRequest{
Key: t.Name(),
NamespaceKey: s.namespace,
Name: "foo",
Description: "bar",
Enabled: true,
})

require.NoError(t, err)
assert.NotNil(t, flag)

_, err = s.store.CreateSegment(ctx, &flipt.CreateSegmentRequest{
NamespaceKey: s.namespace,
Key: "segment_one",
Name: "Segment One",
MatchType: flipt.MatchType_ANY_MATCH_TYPE,
})

require.NoError(t, err)

rollout, err := s.store.CreateRollout(context.TODO(), &flipt.CreateRolloutRequest{
FlagKey: flag.Key,
NamespaceKey: s.namespace,
Rank: 1,
Rule: &flipt.CreateRolloutRequest_Segment{
Segment: &flipt.RolloutSegment{
SegmentKey: "segment_one",
Value: true,
},
},
})

require.NoError(t, err)
assert.NotNil(t, rollout)

assert.NotZero(t, rollout.Id)
assert.Equal(t, s.namespace, rollout.NamespaceKey)
assert.Equal(t, flag.Key, rollout.FlagKey)
assert.Equal(t, int32(1), rollout.Rank)
assert.Equal(t, flipt.RolloutType_SEGMENT_ROLLOUT_TYPE, rollout.Type)
assert.Equal(t, "segment_one", rollout.GetSegment().SegmentKey)
assert.Equal(t, true, rollout.GetSegment().Value)
assert.NotZero(t, rollout.CreatedAt)
assert.Equal(t, rollout.CreatedAt.Seconds, rollout.UpdatedAt.Seconds)

_, err = s.store.UpdateRollout(context.TODO(), &flipt.UpdateRolloutRequest{
Id: rollout.Id,
FlagKey: rollout.FlagKey,
NamespaceKey: s.namespace,
Description: "foobar",
Rule: &flipt.UpdateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: false,
Percentage: 80,
},
},
})

require.EqualError(t, err, "cannot change type of rollout: have \"SEGMENT_ROLLOUT_TYPE\" attempted \"PERCENTAGE_ROLLOUT_TYPE\"")
}

func (s *DBTestSuite) TestUpdateRollout_NotFound() {
t := s.T()

Expand Down Expand Up @@ -596,7 +683,6 @@ func (s *DBTestSuite) TestDeleteRollout() {
rollout, err := s.store.CreateRollout(context.TODO(), &flipt.CreateRolloutRequest{
FlagKey: flag.Key,
Rank: int32(i + 1),
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand Down Expand Up @@ -656,7 +742,6 @@ func (s *DBTestSuite) TestDeleteRolloutNamespace() {
NamespaceKey: s.namespace,
FlagKey: flag.Key,
Rank: int32(i + 1),
Type: flipt.RolloutType_PERCENTAGE_ROLLOUT_TYPE,
Rule: &flipt.CreateRolloutRequest_Percentage{
Percentage: &flipt.RolloutPercentage{
Value: true,
Expand Down

0 comments on commit df2ba34

Please sign in to comment.