Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix more unit tests #3941

Merged
merged 5 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions config/flipt.schema.cue
Original file line number Diff line number Diff line change
Expand Up @@ -190,28 +190,6 @@ import "list"
ca_cert_bytes?: string
insecure_skip_tls?: bool | *false
credentials?: string
publishers?: {
object?: {
type: "s3" | "azblob" | "googlecloud" | *""
s3?: {
region: string
bucket: string
prefix?: string
endpoint?: string
poll_interval?: =~#duration | *"1m"
}
azblob?: {
container: string
endpoint?: string
poll_interval?: =~#duration | *"1m"
}
googlecloud?: {
bucket: string
prefix?: string
poll_interval?: =~#duration | *"1m"
}
}
}
}

#credentials: [string]: {
Expand Down
104 changes: 0 additions & 104 deletions config/flipt.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -765,110 +765,6 @@
},
"credentials": {
"type": "string"
},
"publishers": {
"type": [
"object",
"null"
],
"additionalProperties": false,
"properties": {
"object": {
"type": [
"object",
"null"
],
"additionalProperties": false,
"properties": {
"type": {
"type": "string",
"enum": [
"",
"s3",
"azblob",
"googlecloud"
],
"default": ""
},
"s3": {
"type": [
"object",
"null"
],
"additionalProperties": false,
"required": [
"region",
"bucket"
],
"properties": {
"region": {
"type": "string"
},
"bucket": {
"type": "string"
},
"prefix": {
"type": "string"
},
"endpoint": {
"type": "string"
},
"poll_interval": {
"type": "string",
"pattern": "^([0-9]+(ns|us|µs|ms|s|m|h))+$",
"default": "1m"
}
}
},
"azblob": {
"type": [
"object",
"null"
],
"additionalProperties": false,
"required": [
"container"
],
"properties": {
"container": {
"type": "string"
},
"endpoint": {
"type": "string"
},
"poll_interval": {
"type": "string",
"pattern": "^([0-9]+(ns|us|µs|ms|s|m|h))+$",
"default": "1m"
}
}
},
"googlecloud": {
"type": [
"object",
"null"
],
"additionalProperties": false,
"required": [
"bucket"
],
"properties": {
"bucket": {
"type": "string"
},
"prefix": {
"type": "string"
},
"poll_interval": {
"type": "string",
"pattern": "^([0-9]+(ns|us|µs|ms|s|m|h))+$",
"default": "1m"
}
}
}
}
}
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion config/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ func defaultConfig(t *testing.T) (conf map[string]any) {
DecodeHook: mapstructure.ComposeDecodeHookFunc(config.DecodeHooks...),
Result: &conf,
})
config := config.Default()
// hack to get around validation not being able to handle types that map[string]*struct
config.Storage["default"].PollInterval = 0
require.NoError(t, err)
require.NoError(t, dec.Decode(config.Default()))
require.NoError(t, dec.Decode(config))

// adapt converts instances of time.Duration to their
// string representation, which CUE is going to validate
Expand Down
8 changes: 0 additions & 8 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,11 @@ import (
"github.com/iancoleman/strcase"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xeipuuv/gojsonschema"
"gocloud.dev/blob"
"gocloud.dev/blob/memblob"
"gopkg.in/yaml.v2"
)

func TestJSONSchema(t *testing.T) {
schemaBytes, err := os.ReadFile("../../config/flipt.schema.json")
require.NoError(t, err)
_, err = gojsonschema.NewSchema(gojsonschema.NewBytesLoader(schemaBytes))
require.NoError(t, err)
}

func TestScheme(t *testing.T) {
tests := []struct {
name string
Expand Down
4 changes: 1 addition & 3 deletions internal/config/testdata/marshal/yaml/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ server:

environments:
default:
default: true
name: default
storage: default
second:
name: second
storage: default

storage:
default:
Expand Down
2 changes: 1 addition & 1 deletion internal/info/flipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ func TestHttpHandler(t *testing.T) {
w := httptest.NewRecorder()
f.ServeHTTP(w, r)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, `{"updateAvailable":false,"isRelease":false,"authentication":{"required":false},"storage":{"type":"database"}}`, w.Body.String())
assert.JSONEq(t, `{"updateAvailable":false,"isRelease":false,"authentication":{"required":false}}`, w.Body.String())
}
17 changes: 6 additions & 11 deletions internal/server/authz/middleware/grpc/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.flipt.io/flipt/rpc/v2/environments"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/anypb"
)

type mockPolicyVerifier struct {
Expand Down Expand Up @@ -73,15 +74,18 @@ func TestAuthorizationRequiredInterceptor(t *testing.T) {
Environment: "default",
Namespace: "default",
Key: "some_flag",
Payload: &anypb.Any{
TypeUrl: "flipt.core.Flag",
Value: []byte(`{"key":"some_flag","name":"Some Flag","description":"Some description","enabled":true}`),
},
},
validatorAllowed: true,
wantAllowed: true,
authzInput: map[string]any{
"request": flipt.Request{
Namespace: "default",
Resource: flipt.ResourceFlag,
Subject: flipt.SubjectFlag,
Action: flipt.ActionCreate,
Action: flipt.ActionUpdate,
Status: flipt.StatusSuccess,
},
"authentication": adminAuth,
Expand All @@ -97,15 +101,6 @@ func TestAuthorizationRequiredInterceptor(t *testing.T) {
},
validatorAllowed: false,
wantAllowed: false,
authzInput: map[string]any{
"request": flipt.Request{
Namespace: "default",
Resource: flipt.ResourceFlag,
Subject: flipt.SubjectFlag,
Action: flipt.ActionCreate,
},
"authentication": adminAuth,
},
},
{
name: "skips authz",
Expand Down
12 changes: 6 additions & 6 deletions internal/server/ofrep/evaluation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/stretchr/testify/assert"
"go.flipt.io/flipt/internal/common"
"go.flipt.io/flipt/internal/storage"
"go.flipt.io/flipt/rpc/flipt"
"go.flipt.io/flipt/rpc/flipt/core"
rpcevaluation "go.flipt.io/flipt/rpc/flipt/evaluation"
"go.flipt.io/flipt/rpc/flipt/ofrep"
"go.uber.org/zap/zaptest"
Expand Down Expand Up @@ -224,10 +224,10 @@ func TestEvaluateBulkSuccess(t *testing.T) {
}, nil)

store.On("ListFlags", mock.Anything, storage.ListWithOptions(storage.NewNamespace("default"))).Return(
storage.ResultSet[*flipt.Flag]{
Results: []*flipt.Flag{
{Key: flagKey, Type: flipt.FlagType_VARIANT_FLAG_TYPE, Enabled: true},
{Key: "disabled", Type: flipt.FlagType_VARIANT_FLAG_TYPE},
storage.ResultSet[*core.Flag]{
Results: []*core.Flag{
{Key: flagKey, Type: core.FlagType_VARIANT_FLAG_TYPE, Enabled: true},
{Key: "disabled", Type: core.FlagType_VARIANT_FLAG_TYPE},
},
NextPageToken: "YmFy",
}, nil).Once()
Expand All @@ -246,7 +246,7 @@ func TestEvaluateBulkSuccess(t *testing.T) {

t.Run("without flags in the evaluation request failed fetch the flags", func(t *testing.T) {
store.On("ListFlags", mock.Anything, storage.ListWithOptions(storage.NewNamespace("default"))).Return(
storage.ResultSet[*flipt.Flag]{
storage.ResultSet[*core.Flag]{
Results: nil,
NextPageToken: "",
}, errors.New("failed to fetch flags")).Once()
Expand Down
9 changes: 5 additions & 4 deletions internal/storage/analytics/migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ func TestMigratorRun_NoChange(t *testing.T) {

func TestMigratorExpectedVersions(t *testing.T) {
for _, driver := range stringToDriver {
migrations, err := os.ReadDir(filepath.Join("../../../config/migrations", driver.Migrations()))
migrations, err := os.ReadDir(filepath.Join("../../migrations", driver.Migrations()))
require.NoError(t, err)

count := len(migrations)
require.Positive(t, count, "no migrations found for %s", driver)

// migrations start at 0
actual := uint(count - 1)
assert.Equal(t, actual, expectedVersions[driver], "expectedVersions for %s should be set to %d. you need to increment expectedVersions after adding a new migration", driver, actual)
require.Equal(t, 0, count%2, "migrations for %s should be even", driver)
// we only care about the up migrations not the down migrations
actual := count / 2
assert.Equal(t, expectedVersions[driver], uint(actual), "expectedVersions for %s should be set to %d. you need to increment expectedVersions after adding a new migration", driver, actual)
}
}
6 changes: 3 additions & 3 deletions internal/storage/environments/fs/namespaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (
)

var (
defaultContents = `version: "1.4"
defaultContents = `version: "1.5"
namespace:
key: default
name: Default
description: The default namespace
`

teamAContents = `version: "1.4"
teamAContents = `version: "1.5"
namespace:
key: team_a
name: Team A
Expand Down Expand Up @@ -130,7 +130,7 @@ func Test_NamespaceStorage_PutNamespace(t *testing.T) {
data, err := io.ReadAll(fi)
require.NoError(t, err)

assert.Equal(t, `version: "1.4"
assert.Equal(t, `version: "1.5"
namespace:
key: team_b
name: Team B
Expand Down
22 changes: 3 additions & 19 deletions internal/storage/fs/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,22 +651,6 @@ func TestSnapshot_GetEvaluationRollouts(t *testing.T) {
}
}

// evaluationDistTransformer allows us to ensure the contents of the attachment
// is preserved as JSON. We cannot rely on protojson output to be stable
// as it is unstable by design.
func evaluationDistTransformer() cmp.Option {
return cmp.FilterPath(func(p cmp.Path) bool {
return p.String() == "flipt.evaluation.EvaluationDistribution.VariantAttachment"
}, cmp.Transformer("flipt.evaluation.EvaluationDistribution.VariantAttachment", func(a string) map[string]any {
attachment := make(map[string]any)
if err := json.Unmarshal([]byte(a), &attachment); err != nil {
panic(err)
}

return attachment
}))
}

func TestSnapshot_EvaluationNamespaceSnapshot(t *testing.T) {
snap, err := SnapshotFromFS(zaptest.NewLogger(t), DefaultFliptConfig(), testdata)
require.NoError(t, err)
Expand Down Expand Up @@ -826,11 +810,10 @@ func TestSnapshot_EvaluationNamespaceSnapshot(t *testing.T) {
}
}

// Ignore variant IDs and attachments in comparison since they're generated
// Ignore variant IDs in comparison since they're generated
for _, flag := range snapshot.Flags {
if flag.DefaultVariant != nil {
flag.DefaultVariant.Id = ""
flag.DefaultVariant.Attachment = ""
}
}

Expand All @@ -839,7 +822,8 @@ func TestSnapshot_EvaluationNamespaceSnapshot(t *testing.T) {
protocmp.SortRepeated(func(a, b *evaluation.EvaluationFlag) bool {
return a.Key < b.Key
}),
evaluationDistTransformer(),
protocmp.IgnoreFields(&evaluation.EvaluationVariant{}, "attachment"),
protocmp.IgnoreFields(&evaluation.EvaluationDistribution{}, "variant_attachment"),
}

if !cmp.Equal(tt.wantSnap, snapshot, opts...) {
Expand Down
2 changes: 1 addition & 1 deletion rpc/flipt/auth/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func (req *CreateTokenRequest) Request() []flipt.Request {
return []flipt.Request{flipt.NewRequest(flipt.ResourceAuthentication, flipt.ActionCreate, flipt.WithSubject(flipt.SubjectToken))}
return []flipt.Request{flipt.NewRequest(flipt.ResourceAuthentication, flipt.ActionCreate)}
}

func (req *ListAuthenticationsRequest) Request() []flipt.Request {
Expand Down
Loading
Loading