Skip to content
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ local-stovepipe-stop: ## Stop the Stovepipe service

mocks: ## Generate mock files using mockgen
@echo "Generating mocks..."
@$(BAZEL) run @rules_go//go -- generate ./submitqueue/extension/storage/... ./submitqueue/extension/buildrunner/... ./submitqueue/extension/changeprovider/... ./platform/extension/counter/... ./platform/extension/messagequeue/... ./submitqueue/extension/queueconfig/... ./submitqueue/extension/mergechecker/... ./submitqueue/extension/pusher/... ./submitqueue/extension/scorer/... ./submitqueue/extension/conflict/... ./platform/consumer/... ./stovepipe/extension/storage/... ./stovepipe/extension/sourcecontrol/...
@$(BAZEL) run @rules_go//go -- generate ./submitqueue/extension/storage/... ./submitqueue/extension/buildrunner/... ./submitqueue/extension/changeprovider/... ./platform/extension/counter/... ./platform/extension/messagequeue/... ./submitqueue/extension/queueconfig/... ./submitqueue/extension/mergechecker/... ./submitqueue/extension/pusher/... ./submitqueue/extension/scorer/... ./submitqueue/extension/conflict/... ./submitqueue/extension/validator/... ./platform/consumer/... ./stovepipe/extension/storage/... ./stovepipe/extension/sourcecontrol/...
@echo "Mocks generated successfully!"

proto: ## Generate protobuf files from .proto definitions
Expand Down
1 change: 1 addition & 0 deletions service/submitqueue/orchestrator/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ go_library(
"//submitqueue/extension/scorer/heuristic:go_default_library",
"//submitqueue/extension/storage:go_default_library",
"//submitqueue/extension/storage/mysql:go_default_library",
"//submitqueue/extension/validator/fake:go_default_library",
"//submitqueue/orchestrator/controller:go_default_library",
"//submitqueue/orchestrator/controller/batch:go_default_library",
"//submitqueue/orchestrator/controller/build:go_default_library",
Expand Down
2 changes: 2 additions & 0 deletions service/submitqueue/orchestrator/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import (
"github.com/uber/submitqueue/submitqueue/extension/scorer/heuristic"
"github.com/uber/submitqueue/submitqueue/extension/storage"
mysqlstorage "github.com/uber/submitqueue/submitqueue/extension/storage/mysql"
validatorfake "github.com/uber/submitqueue/submitqueue/extension/validator/fake"
"github.com/uber/submitqueue/submitqueue/orchestrator/controller"
"github.com/uber/submitqueue/submitqueue/orchestrator/controller/batch"
"github.com/uber/submitqueue/submitqueue/orchestrator/controller/build"
Expand Down Expand Up @@ -571,6 +572,7 @@ func registerPrimaryControllers(c consumer.Consumer, logger *zap.SugaredLogger,
store,
registry,
cpf,
validatorfake.NewFactory(),
runwaymq.TopicKeyMergeConflictCheck,
topickey.TopicKeyValidate,
"orchestrator-validate",
Expand Down
9 changes: 9 additions & 0 deletions submitqueue/extension/validator/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["validator.go"],
importpath = "github.com/uber/submitqueue/submitqueue/extension/validator",
visibility = ["//visibility:public"],
deps = ["//submitqueue/entity:go_default_library"],
)
26 changes: 26 additions & 0 deletions submitqueue/extension/validator/composite/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("@rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["validator.go"],
importpath = "github.com/uber/submitqueue/submitqueue/extension/validator/composite",
visibility = ["//visibility:public"],
deps = [
"//submitqueue/entity:go_default_library",
"//submitqueue/extension/validator:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["validator_test.go"],
deps = [
":go_default_library",
"//submitqueue/entity:go_default_library",
"//submitqueue/extension/validator:go_default_library",
"//submitqueue/extension/validator/mock:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
"@com_github_stretchr_testify//require:go_default_library",
"@org_uber_go_mock//gomock:go_default_library",
],
)
44 changes: 44 additions & 0 deletions submitqueue/extension/validator/composite/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2025 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package composite

import (
"context"
"errors"

"github.com/uber/submitqueue/submitqueue/entity"
"github.com/uber/submitqueue/submitqueue/extension/validator"
)

// compositeValidator runs all validators and joins their errors.
type compositeValidator struct {
// validators is the set of validators to run.
validators []validator.Validator
}

// New creates a Validator that evaluates all child validators and joins their errors.
func New(validators []validator.Validator) validator.Validator {
return &compositeValidator{validators: validators}
}

func (c *compositeValidator) Validate(ctx context.Context, request entity.Request) error {
var errs []error
for _, v := range c.validators {
if err := v.Validate(ctx, request); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
107 changes: 107 additions & 0 deletions submitqueue/extension/validator/composite/validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) 2025 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package composite_test

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber/submitqueue/submitqueue/entity"
"github.com/uber/submitqueue/submitqueue/extension/validator"
"github.com/uber/submitqueue/submitqueue/extension/validator/composite"
validatormock "github.com/uber/submitqueue/submitqueue/extension/validator/mock"
"go.uber.org/mock/gomock"
)

func TestNew(t *testing.T) {
errA := errors.New("validation A failed")
errB := errors.New("validation B failed")

tests := []struct {
name string
setup func(v1, v2 *validatormock.MockValidator) []validator.Validator
wantErrs []error
}{
{
name: "no validators returns no error",
setup: func(v1, v2 *validatormock.MockValidator) []validator.Validator { return nil },
},
{
name: "single passing validator",
setup: func(v1, v2 *validatormock.MockValidator) []validator.Validator {
v1.EXPECT().Validate(gomock.Any(), gomock.Any()).Return(nil)
return []validator.Validator{v1}
},
},
{
name: "single failing validator",
setup: func(v1, v2 *validatormock.MockValidator) []validator.Validator {
v1.EXPECT().Validate(gomock.Any(), gomock.Any()).Return(errA)
return []validator.Validator{v1}
},
wantErrs: []error{errA},
},
{
name: "all pass",
setup: func(v1, v2 *validatormock.MockValidator) []validator.Validator {
v1.EXPECT().Validate(gomock.Any(), gomock.Any()).Return(nil)
v2.EXPECT().Validate(gomock.Any(), gomock.Any()).Return(nil)
return []validator.Validator{v1, v2}
},
},
{
name: "some fail",
setup: func(v1, v2 *validatormock.MockValidator) []validator.Validator {
v1.EXPECT().Validate(gomock.Any(), gomock.Any()).Return(nil)
v2.EXPECT().Validate(gomock.Any(), gomock.Any()).Return(errA)
return []validator.Validator{v1, v2}
},
wantErrs: []error{errA},
},
{
name: "all fail",
setup: func(v1, v2 *validatormock.MockValidator) []validator.Validator {
v1.EXPECT().Validate(gomock.Any(), gomock.Any()).Return(errA)
v2.EXPECT().Validate(gomock.Any(), gomock.Any()).Return(errB)
return []validator.Validator{v1, v2}
},
wantErrs: []error{errA, errB},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
v1 := validatormock.NewMockValidator(ctrl)
v2 := validatormock.NewMockValidator(ctrl)
validators := tt.setup(v1, v2)

v := composite.New(validators)

err := v.Validate(context.Background(), entity.Request{})
if len(tt.wantErrs) == 0 {
assert.NoError(t, err)
return
}
require.Error(t, err)
for _, want := range tt.wantErrs {
assert.ErrorIs(t, err, want)
}
})
}
}
23 changes: 23 additions & 0 deletions submitqueue/extension/validator/fake/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["fake.go"],
importpath = "github.com/uber/submitqueue/submitqueue/extension/validator/fake",
visibility = ["//visibility:public"],
deps = [
"//submitqueue/entity:go_default_library",
"//submitqueue/extension/validator:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["fake_test.go"],
embed = [":go_default_library"],
deps = [
"//submitqueue/entity:go_default_library",
"//submitqueue/extension/validator:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
],
)
47 changes: 47 additions & 0 deletions submitqueue/extension/validator/fake/fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2025 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package fake provides a validator.Validator that always passes and a
// validator.Factory that returns it. Intended for examples, tests, and default
// wiring when no custom validation is needed.
package fake

import (
"context"

"github.com/uber/submitqueue/submitqueue/entity"
"github.com/uber/submitqueue/submitqueue/extension/validator"
)

type fakeValidator struct{}

// New returns a validator.Validator that always passes (returns nil).
func New() validator.Validator {
return fakeValidator{}
}

func (fakeValidator) Validate(context.Context, entity.Request) error {
return nil
}

type fakeFactory struct{}

// NewFactory returns a validator.Factory that always returns a passing validator.
func NewFactory() validator.Factory {
return fakeFactory{}
}

func (fakeFactory) For(validator.Config) (validator.Validator, error) {
return fakeValidator{}, nil
}
44 changes: 44 additions & 0 deletions submitqueue/extension/validator/fake/fake_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2025 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fake

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/uber/submitqueue/submitqueue/entity"
"github.com/uber/submitqueue/submitqueue/extension/validator"
)

func TestNew_ImplementsInterface(t *testing.T) {
var _ validator.Validator = New()
}

func TestNew_AlwaysPasses(t *testing.T) {
v := New()
assert.NoError(t, v.Validate(context.Background(), entity.Request{}))
}

func TestNewFactory_ImplementsInterface(t *testing.T) {
var _ validator.Factory = NewFactory()
}

func TestNewFactory_ReturnsPassingValidator(t *testing.T) {
f := NewFactory()
v, err := f.For(validator.Config{QueueName: "test-queue"})
assert.NoError(t, err)
assert.NoError(t, v.Validate(context.Background(), entity.Request{}))
}
13 changes: 13 additions & 0 deletions submitqueue/extension/validator/mock/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["validator_mock.go"],
importpath = "github.com/uber/submitqueue/submitqueue/extension/validator/mock",
visibility = ["//visibility:public"],
deps = [
"//submitqueue/entity:go_default_library",
"//submitqueue/extension/validator:go_default_library",
"@org_uber_go_mock//gomock:go_default_library",
],
)
Loading
Loading