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 22, 2024
1 parent 05a1c72 commit ec51bfc
Show file tree
Hide file tree
Showing 30 changed files with 1,035 additions and 214 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/zz_generated*.go linguist-generated=true
pkg/clientset/** linguist-generated=true
controller/konnect/ops/*_mock_test.go linguist-generated=true
7 changes: 7 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ 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
- pkg: github.com/Kong/sdk-konnect-go/models/sdkerrors
alias: sdkkonnectgoerrs
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"
14 changes: 13 additions & 1 deletion 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 @@ -191,7 +198,7 @@ verify.generators: verify.repo generate verify.diff
API_DIR ?= api

.PHONY: generate
generate: generate.api generate.clientsets generate.rbacs generate.gateway-api-urls generate.docs generate.k8sio-gomod-replace generate.testcases-registration generate.kic-webhook-config
generate: generate.api generate.clientsets generate.rbacs generate.gateway-api-urls generate.docs generate.k8sio-gomod-replace generate.testcases-registration generate.kic-webhook-config generate.mocks

.PHONY: generate.api
generate.api: controller-gen
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 @@ -24,9 +24,9 @@ type SupportedKonnectEntityType interface {
}

// EntityType is an interface that all Konnect entity types must implement.
// Separating this from SupportedKonnectEntityType allows us to use EntityType
// Separating this from constraints.SupportedKonnectEntityType allows us to use 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
7 changes: 7 additions & 0 deletions controller/konnect/constraints/entitytypename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package constraints

// EntityTypeName returns the name of the entity type.
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 ec51bfc

Please sign in to comment.