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

Go113errors #160

Merged
merged 15 commits into from
Dec 3, 2021
21 changes: 10 additions & 11 deletions constraint/pkg/apis/templates/v1/constrainttemplate_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
)

func TestStorageConstraintTemplate(t *testing.T) {
Expand Down Expand Up @@ -144,8 +145,6 @@ func TestValidationVersionConversionAndTransformation(t *testing.T) {
t.Fatal(err)
}

trueBool := true
falseBool := false
testCases := []struct {
name string
v *Validation
Expand All @@ -154,22 +153,22 @@ func TestValidationVersionConversionAndTransformation(t *testing.T) {
{
name: "Two deep properties, LegacySchema=true",
v: &Validation{
LegacySchema: &trueBool,
LegacySchema: pointer.Bool(true),
OpenAPIV3Schema: schema.VersionedIncompleteSchema(),
},
exp: &templates.Validation{
LegacySchema: &trueBool,
LegacySchema: pointer.Bool(true),
OpenAPIV3Schema: schema.VersionlessSchemaWithXPreserve(),
},
},
{
name: "Two deep properties, LegacySchema=false",
v: &Validation{
LegacySchema: &falseBool,
LegacySchema: pointer.Bool(false),
OpenAPIV3Schema: schema.VersionedIncompleteSchema(),
},
exp: &templates.Validation{
LegacySchema: &falseBool,
LegacySchema: pointer.Bool(false),
OpenAPIV3Schema: schema.VersionlessSchema(),
},
},
Expand All @@ -185,24 +184,24 @@ func TestValidationVersionConversionAndTransformation(t *testing.T) {
{
name: "Nil properties, LegacySchema=true",
v: &Validation{
LegacySchema: &trueBool,
LegacySchema: pointer.Bool(true),
OpenAPIV3Schema: nil,
},
exp: &templates.Validation{
LegacySchema: &trueBool,
LegacySchema: pointer.Bool(true),
OpenAPIV3Schema: &apiextensions.JSONSchemaProps{
XPreserveUnknownFields: &trueBool,
XPreserveUnknownFields: pointer.Bool(true),
},
},
},
{
name: "Nil properties, LegacySchema=false",
v: &Validation{
LegacySchema: &falseBool,
LegacySchema: pointer.Bool(false),
OpenAPIV3Schema: nil,
},
exp: &templates.Validation{
LegacySchema: &falseBool,
LegacySchema: pointer.Bool(false),
OpenAPIV3Schema: nil,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
)

func TestStorageConstraintTemplate(t *testing.T) {
Expand Down Expand Up @@ -139,8 +140,6 @@ func TestValidationVersionConversionAndTransformation(t *testing.T) {
t.Fatal(err)
}

trueBool := true
falseBool := false
testCases := []struct {
name string
v *Validation
Expand All @@ -149,22 +148,22 @@ func TestValidationVersionConversionAndTransformation(t *testing.T) {
{
name: "Two deep properties, LegacySchema=true",
v: &Validation{
LegacySchema: &trueBool,
LegacySchema: pointer.Bool(true),
OpenAPIV3Schema: schema.VersionedIncompleteSchema(),
},
exp: &templates.Validation{
LegacySchema: &trueBool,
LegacySchema: pointer.Bool(true),
OpenAPIV3Schema: schema.VersionlessSchemaWithXPreserve(),
},
},
{
name: "Two deep properties, LegacySchema=false",
v: &Validation{
LegacySchema: &falseBool,
LegacySchema: pointer.Bool(false),
OpenAPIV3Schema: schema.VersionedIncompleteSchema(),
},
exp: &templates.Validation{
LegacySchema: &falseBool,
LegacySchema: pointer.Bool(false),
OpenAPIV3Schema: schema.VersionlessSchema(),
},
},
Expand All @@ -180,24 +179,24 @@ func TestValidationVersionConversionAndTransformation(t *testing.T) {
{
name: "Nil properties, LegacySchema=true",
v: &Validation{
LegacySchema: &trueBool,
LegacySchema: pointer.Bool(true),
OpenAPIV3Schema: nil,
},
exp: &templates.Validation{
LegacySchema: &trueBool,
LegacySchema: pointer.Bool(true),
OpenAPIV3Schema: &apiextensions.JSONSchemaProps{
XPreserveUnknownFields: &trueBool,
XPreserveUnknownFields: pointer.Bool(true),
},
},
},
{
name: "Nil properties, LegacySchema=false",
v: &Validation{
LegacySchema: &falseBool,
LegacySchema: pointer.Bool(false),
OpenAPIV3Schema: nil,
},
exp: &templates.Validation{
LegacySchema: &falseBool,
LegacySchema: pointer.Bool(false),
OpenAPIV3Schema: nil,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
)

func TestStorageConstraintTemplate(t *testing.T) {
Expand Down Expand Up @@ -138,8 +139,6 @@ func TestValidationVersionConversionAndTransformation(t *testing.T) {
t.Fatal(err)
}

trueBool := true
falseBool := false
testCases := []struct {
name string
v *Validation
Expand All @@ -148,22 +147,22 @@ func TestValidationVersionConversionAndTransformation(t *testing.T) {
{
name: "Two deep properties, LegacySchema=true",
v: &Validation{
LegacySchema: &trueBool,
LegacySchema: pointer.Bool(true),
OpenAPIV3Schema: schema.VersionedIncompleteSchema(),
},
exp: &templates.Validation{
LegacySchema: &trueBool,
LegacySchema: pointer.Bool(true),
OpenAPIV3Schema: schema.VersionlessSchemaWithXPreserve(),
},
},
{
name: "Two deep properties, LegacySchema=false",
v: &Validation{
LegacySchema: &falseBool,
LegacySchema: pointer.Bool(false),
OpenAPIV3Schema: schema.VersionedIncompleteSchema(),
},
exp: &templates.Validation{
LegacySchema: &falseBool,
LegacySchema: pointer.Bool(false),
OpenAPIV3Schema: schema.VersionlessSchema(),
},
},
Expand All @@ -179,24 +178,24 @@ func TestValidationVersionConversionAndTransformation(t *testing.T) {
{
name: "Nil properties, LegacySchema=true",
v: &Validation{
LegacySchema: &trueBool,
LegacySchema: pointer.Bool(true),
OpenAPIV3Schema: nil,
},
exp: &templates.Validation{
LegacySchema: &trueBool,
LegacySchema: pointer.Bool(true),
OpenAPIV3Schema: &apiextensions.JSONSchemaProps{
XPreserveUnknownFields: &trueBool,
XPreserveUnknownFields: pointer.Bool(true),
},
},
},
{
name: "Nil properties, LegacySchema=false",
v: &Validation{
LegacySchema: &falseBool,
LegacySchema: pointer.Bool(false),
OpenAPIV3Schema: nil,
},
exp: &templates.Validation{
LegacySchema: &falseBool,
LegacySchema: pointer.Bool(false),
OpenAPIV3Schema: nil,
},
},
Expand Down
26 changes: 14 additions & 12 deletions constraint/pkg/client/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client

import (
"context"
"errors"
"fmt"

"github.com/open-policy-agent/frameworks/constraint/pkg/client/drivers"
Expand All @@ -24,8 +23,10 @@ func Driver(d drivers.Driver) BackendOpt {
}
}

// NewBackend creates a new backend. A backend could be a connection to a remote server or
// a new local OPA instance.
// NewBackend creates a new backend. A backend could be a connection to a remote
// server or a new local OPA instance.
//
// A BackendOpt setting driver, such as Driver() must be passed.
func NewBackend(opts ...BackendOpt) (*Backend, error) {
helper, err := newCRDHelper()
if err != nil {
Expand All @@ -37,7 +38,7 @@ func NewBackend(opts ...BackendOpt) (*Backend, error) {
}

if b.driver == nil {
return nil, errors.New("no driver supplied to the backend")
return nil, fmt.Errorf("%w: no driver supplied", ErrCreatingBackend)
}

return b, nil
Expand All @@ -46,7 +47,8 @@ func NewBackend(opts ...BackendOpt) (*Backend, error) {
// NewClient creates a new client for the supplied backend.
func (b *Backend) NewClient(opts ...Opt) (*Client, error) {
if b.hasClient {
return nil, errors.New("currently only one client per backend is supported")
return nil, fmt.Errorf("%w: only one client per backend is allowed",
ErrCreatingClient)
}

var fields []string
Expand All @@ -61,24 +63,22 @@ func (b *Backend) NewClient(opts ...Opt) (*Client, error) {
allowedDataFields: fields,
}

var errs Errors
for _, opt := range opts {
if err := opt(c); err != nil {
errs = append(errs, err)
return nil, err
}
}
if len(errs) > 0 {
return nil, errs
}

for _, field := range c.allowedDataFields {
if !validDataFields[field] {
return nil, fmt.Errorf("invalid data field %s", field)
return nil, fmt.Errorf("%w: invalid data field %q; allowed fields are: %v",
ErrCreatingClient, field, validDataFields)
}
}

if len(c.targets) == 0 {
return nil, errors.New("no targets registered: please register a target via client.Targets()")
return nil, fmt.Errorf("%w: must specify at least one target with client.Targets",
ErrCreatingClient)
}

if err := b.driver.Init(context.Background()); err != nil {
Expand All @@ -88,5 +88,7 @@ func (b *Backend) NewClient(opts ...Opt) (*Client, error) {
if err := c.init(); err != nil {
return nil, err
}

b.hasClient = true
return c, nil
}
96 changes: 96 additions & 0 deletions constraint/pkg/client/backend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package client

import (
"errors"
"testing"

"github.com/open-policy-agent/frameworks/constraint/pkg/client/drivers/local"
)

func TestNewBackend(t *testing.T) {
testCases := []struct {
name string
opts []BackendOpt
wantError error
}{
{
name: "no args",
opts: nil,
wantError: ErrCreatingBackend,
},
{
name: "good",
opts: []BackendOpt{Driver(local.New())},
wantError: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, gotErr := NewBackend(tc.opts...)

if !errors.Is(gotErr, tc.wantError) {
t.Fatalf("got NewBackent() error = %v, want %v",
gotErr, tc.wantError)
}
})
}
}

func TestBackend_NewClient(t *testing.T) {
testCases := []struct {
name string
backendOpts []BackendOpt
clientOpts []Opt
wantError error
}{
{
name: "no opts",
backendOpts: nil,
clientOpts: nil,
wantError: ErrCreatingClient,
},
{
name: "with handler",
backendOpts: nil,
clientOpts: []Opt{Targets(&handler{})},
wantError: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
opts := []BackendOpt{Driver(local.New())}
opts = append(opts, tc.backendOpts...)

backend, err := NewBackend(opts...)
if err != nil {
t.Fatal(err)
}

_, err = backend.NewClient(tc.clientOpts...)
if !errors.Is(err, tc.wantError) {
t.Fatalf("got NewClient() eror = %v, want %v",
err, tc.wantError)
}
})
}
}

func TestBackend_NewClient2(t *testing.T) {
backend, err := NewBackend(Driver(local.New()))
if err != nil {
t.Fatal(err)
}

_, err = backend.NewClient(Targets(&handler{}))
if err != nil {
t.Fatal(err)
}

_, err = backend.NewClient(Targets(&handler{}))
if !errors.Is(err, ErrCreatingClient) {
t.Fatalf("got NewClient() err = %v, want %v",
err, ErrCreatingClient)
}
}
Loading