Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.
Open
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 lib/managedservicesplatform/runtime/contract/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ go_test(
embed = [":contract"],
deps = [
"//lib/errors",
"//lib/managedservicesplatform/runtime/contract/contractest:mock",
"//lib/pointers",
"@com_github_hexops_autogold_v2//:autogold",
"@com_github_sourcegraph_log//logtest",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
"@io_opentelemetry_go_otel_sdk//trace",
],
)
24 changes: 9 additions & 15 deletions lib/managedservicesplatform/runtime/contract/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,24 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/sourcegraph/log/logtest"

"github.com/sourcegraph/sourcegraph/lib/managedservicesplatform/runtime/contract"
"github.com/sourcegraph/sourcegraph/lib/errors"
mock "github.com/sourcegraph/sourcegraph/lib/managedservicesplatform/runtime/contract/mock"
)

type mockServiceMetadata struct{}

func (m mockServiceMetadata) Name() string { return "mock-name" }
func (m mockServiceMetadata) Version() string { return "mock-version" }

func TestNewContract(t *testing.T) {
t.Run("sanity check", func(t *testing.T) {
e, err := contract.ParseEnv([]string{"MSP=true"})
require.NoError(t, err)

c := contract.New(logtest.Scoped(t), mockServiceMetadata{}, e)
c, err := mock.NewContract(t, mock.ServiceMetadata{}, "MSP=true")
if errors.Is(err, mock.MockError{}) {
t.Fatal(err)
}
assert.NotZero(t, c)
assert.True(t, c.MSP)

// If our error is not a Mockerror, then it could be an environment validation error
// Expected to error, as there are missing required env vars.
err = e.Validate()
assert.Error(t, err)
envErr := err
assert.Error(t, envErr)
})

// TODO: Add more validation tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "mock",
srcs = ["contract_mock.go"],
importpath = "github.com/sourcegraph/sourcegraph/lib/managedservicesplatform/runtime/contract/mock",
visibility = ["//visibility:public"],
deps = [
"//lib/managedservicesplatform/runtime/contract",
"@com_github_sourcegraph_log//logtest",
],
)

go_library(
name = "contractest",
srcs = ["contract_mock.go"],
importpath = "github.com/sourcegraph/sourcegraph/lib/managedservicesplatform/runtime/contract/contractest",
visibility = ["//visibility:public"],
deps = [
"//lib/managedservicesplatform/runtime/contract",
"@com_github_sourcegraph_log//logtest",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package contracttest

import (
"cmp"
"testing"

"github.com/sourcegraph/log/logtest"

"github.com/sourcegraph/sourcegraph/lib/managedservicesplatform/runtime/contract"
)

type MockServiceMetadata struct {
MockName string
MockVersion string
}

// MockError is the error that is returned when there is an error parsing the given environment
type MockError struct {
error
}

func (s MockServiceMetadata) Name() string { return cmp.Or(s.MockName, "mock-name") }
func (s MockServiceMetadata) Version() string { return cmp.Or(s.MockVersion, "mock-version") }

// NewMockContract returns a new contract instance from the given env. If there is an error parsing the given environment
// a MockError is returned that contains the error.
//
// Otherwise a new contract instance is returned as well as the environment validation result from `env.Validate()`
func NewContract(t *testing.T, mockServiceMeta contract.ServiceMetadataProvider, env ...string) (*contract.Contract, error) {
t.Helper()
e, err := contract.ParseEnv(env)
if err != nil {
return nil, MockError{err}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A naked error/simply wrapped error is sufficient IMO:

Suggested change
return nil, MockError{err}
return nil, errors.New(err, "contract.ParseEnv")

}

c := contract.New(logtest.Scoped(t), mockServiceMeta, e)
return &c, e.Validate()
}