* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
335 lines
6.9 KiB
Go
335 lines
6.9 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package errors
|
|
|
|
import (
|
|
"context"
|
|
stderrors "errors"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestT(t *testing.T) {
|
|
t.Parallel()
|
|
stdErr := stderrors.New("test error")
|
|
tests := []struct {
|
|
name string
|
|
args []any
|
|
want *Template
|
|
}{
|
|
{
|
|
name: "all fields",
|
|
args: []any{
|
|
"test error msg",
|
|
Op("alice.Bob"),
|
|
InvalidParameter,
|
|
stdErr,
|
|
Integrity,
|
|
},
|
|
want: &Template{
|
|
Err: Err{
|
|
Code: InvalidParameter,
|
|
Msg: "test error msg",
|
|
Op: "alice.Bob",
|
|
Wrapped: stdErr,
|
|
},
|
|
Kind: Integrity,
|
|
},
|
|
},
|
|
{
|
|
name: "Kind only",
|
|
args: []any{
|
|
Integrity,
|
|
},
|
|
want: &Template{
|
|
Kind: Integrity,
|
|
},
|
|
},
|
|
{
|
|
name: "multiple Kinds",
|
|
args: []any{
|
|
Search,
|
|
Integrity,
|
|
},
|
|
want: &Template{
|
|
Kind: Integrity,
|
|
},
|
|
},
|
|
{
|
|
name: "ignore",
|
|
args: []any{
|
|
32,
|
|
},
|
|
want: &Template{},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
got := T(tt.args...)
|
|
assert.Equal(tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTemplate_Info(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []struct {
|
|
name string
|
|
template *Template
|
|
want Info
|
|
}{
|
|
{
|
|
name: "nil",
|
|
template: nil,
|
|
want: errorCodeInfo[Unknown],
|
|
},
|
|
{
|
|
name: "Code",
|
|
template: T(InvalidParameter),
|
|
want: errorCodeInfo[InvalidParameter],
|
|
},
|
|
{
|
|
name: "Code and Kind",
|
|
template: T(InvalidParameter, Integrity),
|
|
want: errorCodeInfo[InvalidParameter],
|
|
},
|
|
{
|
|
name: "Kind without Code",
|
|
template: T(Integrity),
|
|
want: Info{Kind: Integrity, Message: "Unknown"},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
assert.Equal(tt.want, tt.template.Info())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTemplate_Error(t *testing.T) {
|
|
t.Parallel()
|
|
stdErr := stderrors.New("test error")
|
|
tests := []struct {
|
|
name string
|
|
template *Template
|
|
}{
|
|
{
|
|
name: "Kind only",
|
|
template: T(Integrity),
|
|
},
|
|
{
|
|
name: "all params",
|
|
template: T(
|
|
"test error msg",
|
|
Op("alice.Bob"),
|
|
InvalidParameter,
|
|
stdErr,
|
|
Integrity,
|
|
),
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
got := tt.template.Error()
|
|
assert.Equal("Template error", got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMatch(t *testing.T) {
|
|
t.Parallel()
|
|
stdErr := stderrors.New("test error")
|
|
errInvalidFieldMask := E(context.TODO(), WithCode(InvalidFieldMask), WithMsg("test invalid field mask error"))
|
|
errNotUnique := E(context.TODO(), WithCode(NotUnique), WithMsg("test not unique error"))
|
|
|
|
tests := []struct {
|
|
name string
|
|
template *Template
|
|
err error
|
|
want bool
|
|
}{
|
|
{
|
|
name: "nil template",
|
|
template: nil,
|
|
err: E(context.TODO(), WithCode(NotUnique), WithMsg("this thing was must be unique")),
|
|
want: false,
|
|
},
|
|
{
|
|
name: "nil err",
|
|
template: T(Integrity),
|
|
err: nil,
|
|
want: false,
|
|
},
|
|
{
|
|
name: "match on Kind only",
|
|
template: T(Integrity),
|
|
err: E(context.TODO(),
|
|
WithCode(NotUnique),
|
|
WithMsg("this thing must be unique"),
|
|
WithOp("alice.Bob"),
|
|
WithWrap(errInvalidFieldMask),
|
|
),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "no match on Kind only",
|
|
template: T(Integrity),
|
|
err: E(context.TODO(),
|
|
WithCode(RecordNotFound),
|
|
WithMsg("this thing is missing"),
|
|
WithOp("alice.Bob"),
|
|
WithWrap(errInvalidFieldMask),
|
|
),
|
|
want: false,
|
|
},
|
|
{
|
|
name: "match on Code only",
|
|
template: T(NotUnique),
|
|
err: E(context.TODO(),
|
|
WithCode(NotUnique),
|
|
WithMsg("this thing must be unique"),
|
|
WithOp("alice.Bob"),
|
|
WithWrap(errInvalidFieldMask),
|
|
),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "no match on Code only",
|
|
template: T(NotUnique),
|
|
err: E(context.TODO(),
|
|
WithCode(RecordNotFound),
|
|
WithMsg("this thing is missing"),
|
|
WithOp("alice.Bob"),
|
|
WithWrap(errInvalidFieldMask),
|
|
),
|
|
want: false,
|
|
},
|
|
{
|
|
name: "match on Op only",
|
|
template: T(Op("alice.Bob")),
|
|
err: E(context.TODO(),
|
|
WithCode(NotUnique),
|
|
WithMsg("this thing must be unique"),
|
|
WithOp("alice.Bob"),
|
|
WithWrap(errInvalidFieldMask),
|
|
),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "no match on Op only",
|
|
template: T(Op("alice.Alice")),
|
|
err: E(context.TODO(),
|
|
WithCode(RecordNotFound),
|
|
WithMsg("this thing is missing"),
|
|
WithOp("alice.Bob"),
|
|
WithWrap(errInvalidFieldMask),
|
|
),
|
|
want: false,
|
|
},
|
|
{
|
|
name: "match on everything",
|
|
template: T(
|
|
"this thing must be unique",
|
|
Integrity,
|
|
InvalidParameter,
|
|
errInvalidFieldMask,
|
|
Op("alice.Bob"),
|
|
),
|
|
err: E(context.TODO(),
|
|
WithCode(InvalidParameter),
|
|
WithMsg("this thing must be unique"),
|
|
WithOp("alice.Bob"),
|
|
WithWrap(errInvalidFieldMask),
|
|
),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "match on Wrapped only",
|
|
template: T(errInvalidFieldMask),
|
|
err: E(context.TODO(),
|
|
WithCode(NotUnique),
|
|
WithMsg("this thing must be unique"),
|
|
WithOp("alice.Bob"),
|
|
WithWrap(errInvalidFieldMask),
|
|
),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "no match on Wrapped only",
|
|
template: T(errNotUnique),
|
|
err: E(context.TODO(),
|
|
WithCode(RecordNotFound),
|
|
WithMsg("this thing is missing"),
|
|
WithOp("alice.Bob"),
|
|
WithWrap(errInvalidFieldMask),
|
|
),
|
|
want: false,
|
|
},
|
|
{
|
|
name: "match on Wrapped only stderror",
|
|
template: T(stdErr),
|
|
err: E(context.TODO(),
|
|
WithCode(NotUnique),
|
|
WithMsg("this thing must be unique"),
|
|
WithOp("alice.Bob"),
|
|
WithWrap(stdErr),
|
|
),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "match on go multi error",
|
|
template: T(errInvalidFieldMask),
|
|
err: stderrors.Join(stdErr, errInvalidFieldMask),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "match on go multi error for specific code",
|
|
template: T(InvalidFieldMask),
|
|
err: stderrors.Join(stdErr, errInvalidFieldMask),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "match on go multi error both boundary errors",
|
|
template: T(errInvalidFieldMask),
|
|
err: stderrors.Join(errNotUnique, errInvalidFieldMask),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "match on hashicorp multi error",
|
|
template: T(errInvalidFieldMask),
|
|
err: multierror.Append(stdErr, errInvalidFieldMask),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "match on hashicorp multi error for specific code",
|
|
template: T(InvalidFieldMask),
|
|
err: multierror.Append(stdErr, errInvalidFieldMask),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "no match on Wrapped only stderror",
|
|
template: T(stderrors.New("no match")),
|
|
err: E(context.TODO(),
|
|
WithCode(RecordNotFound),
|
|
WithMsg("this thing is missing"),
|
|
WithOp("alice.Bob"),
|
|
WithWrap(stdErr),
|
|
),
|
|
want: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
got := Match(tt.template, tt.err)
|
|
assert.Equal(tt.want, got)
|
|
})
|
|
}
|
|
}
|