consul/internal/resource/resourcetest/require.go

92 lines
2.8 KiB
Go
Raw Permalink Normal View History

[COMPLIANCE] License changes (#18443) * 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 <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package resourcetest
import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/proto-public/pbresource"
"github.com/hashicorp/consul/proto/private/prototest"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/testing/protocmp"
)
// CompareErrorString is a helper to generate a custom go-cmp comparer method
// that will perform an equality check on the error message. This is mainly
// useful to get around not being able to see unexported data within errors.
func CompareErrorString[T error]() cmp.Option {
return cmp.Comparer(func(e1, e2 T) bool {
return e1.Error() == e2.Error()
})
}
// default comparers for known types that don't play well with go-cmp
var comparers = []cmp.Option{
CompareErrorString[resource.ConstError](),
}
// RequireError is useful for asserting that some chained multierror contains a specific error.
func RequireError[E error](t T, err error, expected E, opts ...cmp.Option) {
t.Helper()
var actual E
require.ErrorAs(t, err, &actual)
opts = append(opts, comparers...)
prototest.AssertDeepEqual(t, expected, actual, opts...)
}
func RequireVersionUnchanged(t T, res *pbresource.Resource, version string) {
t.Helper()
require.Equal(t, version, res.Version)
}
func RequireVersionChanged(t T, res *pbresource.Resource, version string) {
t.Helper()
require.NotEqual(t, version, res.Version)
}
func RequireStatusCondition(t T, res *pbresource.Resource, statusKey string, condition *pbresource.Condition) {
t.Helper()
require.NotNil(t, res.Status)
status, found := res.Status[statusKey]
require.True(t, found)
prototest.AssertContainsElement(t, status.Conditions, condition)
}
func RequireStatusConditionForCurrentGen(t T, res *pbresource.Resource, statusKey string, condition *pbresource.Condition) {
t.Helper()
require.NotNil(t, res.Status)
status, found := res.Status[statusKey]
require.True(t, found)
require.Equal(t, res.Generation, status.ObservedGeneration)
prototest.AssertContainsElement(t, status.Conditions, condition)
}
func RequireResourceMeta(t T, res *pbresource.Resource, key string, value string) {
t.Helper()
require.NotNil(t, res.Metadata)
require.Contains(t, res.Metadata, key)
require.Equal(t, value, res.Metadata[key])
}
func RequireReconciledCurrentGen(t T, res *pbresource.Resource, statusKey string) {
t.Helper()
require.NotNil(t, res.Status)
status, found := res.Status[statusKey]
require.True(t, found)
require.Equal(t, res.Generation, status.ObservedGeneration)
}
func RequireOwner(t T, res *pbresource.Resource, owner *pbresource.ID, ignoreUid bool) {
t.Helper()
var opts []cmp.Option
if ignoreUid {
opts = append(opts, protocmp.IgnoreFields(owner, "uid"))
}
prototest.AssertDeepEqual(t, res.Owner, owner, opts...)
}