Skip to content

Commit

Permalink
tests: add mockery for Konnect SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Aug 20, 2024
1 parent a4fc06f commit 3498087
Show file tree
Hide file tree
Showing 27 changed files with 642 additions and 179 deletions.
5 changes: 5 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ linters-settings:

- pkg: github.com/kong/gateway-operator/internal/types
alias: gwtypes

- pkg: github.com/Kong/sdk-konnect-go/models/components
alias: sdkkonnectgocomp
- pkg: github.com/Kong/sdk-konnect-go/models/operations
alias: sdkkonnectgoops
revive:
rules:
- name: errorf
Expand Down
14 changes: 14 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
quiet: False
inpackage: True
disable-version-string: True
with-expecter: True

filename: "{{.InterfaceName | lower}}_mock_test.go"
dir: "{{.InterfaceDir}}"
mockname: "Mock{{.InterfaceName}}"
outpkg: "{{.PackageName}}"

packages:
github.com/kong/gateway-operator/controller/konnect/ops:
interfaces:
ControlPlaneSDK:
2 changes: 2 additions & 0 deletions .tools_versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ dlv: "1.23.0"
gotestsum: "1.12.0"
# renovate: datasource=github-releases depName=elastic/crd-ref-docs
crd-ref-docs: "0.1.0"
# renovate: datasource=github-releases depName=vektra/mockery
mockery: "2.44.2"
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ skaffold: mise yq ## Download skaffold locally if necessary.
@$(MISE) plugin install --yes -q skaffold
@$(MISE) install -q skaffold@$(SKAFFOLD_VERSION)

MOCKERY_VERSION = $(shell $(YQ) -r '.mockery' < $(TOOLS_VERSIONS_FILE))
MOCKERY = $(PROJECT_DIR)/bin/installs/mockery/$(MOCKERY_VERSION)/bin/mockery
.PHONY: mockery
mockery: mise yq ## Download mockery locally if necessary.
@$(MISE) plugin install --yes -q mockery https://github.com/cabify/asdf-mockery.git
@$(MISE) install -q mockery@$(MOCKERY_VERSION)

# ------------------------------------------------------------------------------
# Build
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -388,6 +395,11 @@ test.conformance:
test.samples: kustomize
find ./config/samples -not -name "kustomization.*" -type f | sort | xargs -I{} bash -c "kubectl apply -f {}; kubectl delete -f {}"

# https://github.com/vektra/mockery/issues/803#issuecomment-2287198024
.PHONY: generate.mocks
generate.mocks: mockery
GODEBUG=gotypesalias=0 $(MOCKERY)

# ------------------------------------------------------------------------------
# Gateway API
# ------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package konnect
package conditions

// TODO(pmalek): move this to Konnect API directory so that it's part of the API contract.
// https://github.com/Kong/kubernetes-configuration/issues/14
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package konnect
package constraints

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -10,7 +10,7 @@ import (
konnectv1alpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1"
)

// SupportedKonnectEntityType is an interface that all Konnect entity types
// constraints.SupportedKonnectEntityType is an interface that all Konnect entity types
// must implement.
type SupportedKonnectEntityType interface {
konnectv1alpha1.KonnectControlPlane |
Expand All @@ -23,10 +23,10 @@ type SupportedKonnectEntityType interface {
GetTypeName() string
}

// EntityType is an interface that all Konnect entity types must implement.
// Separating this from SupportedKonnectEntityType allows us to use EntityType
// constraints.EntityType is an interface that all Konnect entity types must implement.
// Separating this from constraints.SupportedKonnectEntityType allows us to use constraints.EntityType
// where client.Object is required, since it embeds client.Object and uses pointer
// to refer to the SupportedKonnectEntityType.
// to refer to the constraints.SupportedKonnectEntityType.
type EntityType[T SupportedKonnectEntityType] interface {
*T
// Kubernetes Object methods
Expand Down
6 changes: 6 additions & 0 deletions controller/konnect/constraints/entitytypename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package constraints

func EntityTypeName[T SupportedKonnectEntityType]() string {
var e T
return e.GetTypeName()
}
6 changes: 0 additions & 6 deletions controller/konnect/entitytypename.go

This file was deleted.

19 changes: 0 additions & 19 deletions controller/konnect/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@ import (
"k8s.io/apimachinery/pkg/types"
)

// FailedKonnectOpError is an error type that is returned when an operation against
// Konnect API fails.
type FailedKonnectOpError[T SupportedKonnectEntityType] struct {
Op Op
Err error
}

// Error implements the error interface.
func (e FailedKonnectOpError[T]) Error() string {
return fmt.Sprintf("failed to %s %s on Konnect: %v",
e.Op, entityTypeName[T](), e.Err,
)
}

// Unwrap returns the underlying error.
func (e FailedKonnectOpError[T]) Unwrap() error {
return e.Err
}

// ReferencedControlPlaneDoesNotExistError is an error type that is returned when
// a Konnect entity references a KonnectControlPlane that does not exist.
type ReferencedControlPlaneDoesNotExistError struct {
Expand Down
15 changes: 15 additions & 0 deletions controller/konnect/ops/controlplane.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ops

import (
"context"

sdkkonnectgocomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectgoops "github.com/Kong/sdk-konnect-go/models/operations"
)

// ControlPlaneSDK is the interface for the Konnect ControlPlaneSDK SDK.
type ControlPlaneSDK interface {
CreateControlPlane(ctx context.Context, req sdkkonnectgocomp.CreateControlPlaneRequest, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.CreateControlPlaneResponse, error)
DeleteControlPlane(ctx context.Context, id string, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.DeleteControlPlaneResponse, error)
UpdateControlPlane(ctx context.Context, id string, req sdkkonnectgocomp.UpdateControlPlaneRequest, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.UpdateControlPlaneResponse, error)
}
Loading

0 comments on commit 3498087

Please sign in to comment.