Skip to content

test: Extract common variable testing funcs #131

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

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module github.com/d2iq-labs/capi-runtime-extensions/common
go 1.21

require (
github.com/onsi/gomega v1.27.10
github.com/spf13/pflag v1.0.5
k8s.io/apiextensions-apiserver v0.28.1
k8s.io/apimachinery v0.28.1
Expand Down
71 changes: 1 addition & 70 deletions common/pkg/handlers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,6 @@

package handlers

import (
"context"

"github.com/spf13/pflag"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
)

type NamedHandler interface {
type Named interface {
Name() string
}

type FlagConfigurableHandler interface {
AddFlags(prefix string, fs *pflag.FlagSet)
}

type BeforeClusterCreateLifecycleHandler interface {
BeforeClusterCreate(
context.Context,
*runtimehooksv1.BeforeClusterCreateRequest,
*runtimehooksv1.BeforeClusterCreateResponse,
)
}
type AfterControlPlaneInitializedLifecycleHandler interface {
AfterControlPlaneInitialized(
context.Context,
*runtimehooksv1.AfterControlPlaneInitializedRequest,
*runtimehooksv1.AfterControlPlaneInitializedResponse,
)
}
type BeforeClusterUpgradeLifecycleHandler interface {
BeforeClusterUpgrade(
context.Context,
*runtimehooksv1.BeforeClusterUpgradeRequest,
*runtimehooksv1.BeforeClusterUpgradeResponse,
)
}
type AfterControlPlaneUpgradeLifecycleHandler interface {
AfterControlPlaneUpgrade(
context.Context,
*runtimehooksv1.AfterControlPlaneUpgradeRequest,
*runtimehooksv1.AfterControlPlaneUpgradeResponse,
)
}
type BeforeClusterDeleteLifecycleHandler interface {
BeforeClusterDelete(
context.Context,
*runtimehooksv1.BeforeClusterDeleteRequest,
*runtimehooksv1.BeforeClusterDeleteResponse,
)
}

type DiscoverVariablesMutationHandler interface {
DiscoverVariables(
context.Context,
*runtimehooksv1.DiscoverVariablesRequest,
*runtimehooksv1.DiscoverVariablesResponse,
)
}
type GeneratePatchesMutationHandler interface {
GeneratePatches(
context.Context,
*runtimehooksv1.GeneratePatchesRequest,
*runtimehooksv1.GeneratePatchesResponse,
)
}
type ValidateTopologyMutationHandler interface {
ValidateTopology(
context.Context,
*runtimehooksv1.ValidateTopologyRequest,
*runtimehooksv1.ValidateTopologyResponse,
)
}
46 changes: 46 additions & 0 deletions common/pkg/handlers/lifecycle/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package lifecycle

import (
"context"

runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
)

type BeforeClusterCreate interface {
BeforeClusterCreate(
context.Context,
*runtimehooksv1.BeforeClusterCreateRequest,
*runtimehooksv1.BeforeClusterCreateResponse,
)
}
type AfterControlPlaneInitialized interface {
AfterControlPlaneInitialized(
context.Context,
*runtimehooksv1.AfterControlPlaneInitializedRequest,
*runtimehooksv1.AfterControlPlaneInitializedResponse,
)
}
type BeforeClusterUpgrade interface {
BeforeClusterUpgrade(
context.Context,
*runtimehooksv1.BeforeClusterUpgradeRequest,
*runtimehooksv1.BeforeClusterUpgradeResponse,
)
}
type AfterControlPlaneUpgrade interface {
AfterControlPlaneUpgrade(
context.Context,
*runtimehooksv1.AfterControlPlaneUpgradeRequest,
*runtimehooksv1.AfterControlPlaneUpgradeResponse,
)
}
type BeforeClusterDelete interface {
BeforeClusterDelete(
context.Context,
*runtimehooksv1.BeforeClusterDeleteRequest,
*runtimehooksv1.BeforeClusterDeleteResponse,
)
}
32 changes: 32 additions & 0 deletions common/pkg/handlers/mutation/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package mutation

import (
"context"

runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
)

type DiscoverVariables interface {
DiscoverVariables(
context.Context,
*runtimehooksv1.DiscoverVariablesRequest,
*runtimehooksv1.DiscoverVariablesResponse,
)
}
type GeneratePatches interface {
GeneratePatches(
context.Context,
*runtimehooksv1.GeneratePatchesRequest,
*runtimehooksv1.GeneratePatchesResponse,
)
}
type ValidateTopology interface {
ValidateTopology(
context.Context,
*runtimehooksv1.ValidateTopologyRequest,
*runtimehooksv1.ValidateTopologyResponse,
)
}
15 changes: 15 additions & 0 deletions common/pkg/openapi/patterns/distribution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package patterns

const (
// See: https://github.com/distribution/reference/blob/v0.5.0/regexp.go#L53
NameSeparator = `(?:[._]|__|[-]+)`

// See: https://github.com/distribution/reference/blob/v0.5.0/regexp.go#L123C18-L123C65
PathComponent = Alphanumeric + `(` + NameSeparator + Alphanumeric + `)*`

// See: https://github.com/distribution/reference/blob/v0.5.0/regexp.go#L130
ImageRegistry = `(` + DNS1123Subdomain + `|` + IPv6 + `)` + OptionalPort + `(/` + PathComponent + `)*`
)
9 changes: 9 additions & 0 deletions common/pkg/openapi/patterns/generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package patterns

const (
// See: https://github.com/distribution/reference/blob/v0.5.0/regexp.go#L44C2-L44C28
Alphanumeric = `[a-z0-9]+`
)
13 changes: 13 additions & 0 deletions common/pkg/openapi/patterns/net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package patterns

const (
// See: https://github.com/distribution/reference/blob/v0.5.0/regexp.go#L91
IPv6 = `\[(?:[a-fA-F0-9:]+)\]`

Port = `:[0-9]+`

OptionalPort = `(` + Port + `)?`
)
22 changes: 12 additions & 10 deletions common/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import (
ctrl "sigs.k8s.io/controller-runtime"

"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/handlers"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/handlers/lifecycle"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/handlers/mutation"
)

type Server struct {
allExtensionHandlers []handlers.NamedHandler
allExtensionHandlers []handlers.Named

webhookPort int
webhookCertDir string
Expand All @@ -28,7 +30,7 @@ type Server struct {
enabledHandlers []string
}

func NewServer(extensionHandlers ...handlers.NamedHandler) *Server {
func NewServer(extensionHandlers ...handlers.Named) *Server {
// catalog contains all information about RuntimeHooks.
catalog := runtimecatalog.New()

Expand Down Expand Up @@ -87,7 +89,7 @@ func (s *Server) Start(ctx context.Context) error {
continue
}

if t, ok := h.(handlers.BeforeClusterCreateLifecycleHandler); ok {
if t, ok := h.(lifecycle.BeforeClusterCreate); ok {
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
Hook: runtimehooksv1.BeforeClusterCreate,
Name: strings.ToLower(h.Name()),
Expand All @@ -98,7 +100,7 @@ func (s *Server) Start(ctx context.Context) error {
}
}

if t, ok := h.(handlers.AfterControlPlaneInitializedLifecycleHandler); ok {
if t, ok := h.(lifecycle.AfterControlPlaneInitialized); ok {
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
Hook: runtimehooksv1.AfterControlPlaneInitialized,
Name: strings.ToLower(h.Name()),
Expand All @@ -109,7 +111,7 @@ func (s *Server) Start(ctx context.Context) error {
}
}

if t, ok := h.(handlers.BeforeClusterUpgradeLifecycleHandler); ok {
if t, ok := h.(lifecycle.BeforeClusterUpgrade); ok {
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
Hook: runtimehooksv1.BeforeClusterUpgrade,
Name: strings.ToLower(h.Name()),
Expand All @@ -120,7 +122,7 @@ func (s *Server) Start(ctx context.Context) error {
}
}

if t, ok := h.(handlers.AfterControlPlaneUpgradeLifecycleHandler); ok {
if t, ok := h.(lifecycle.AfterControlPlaneUpgrade); ok {
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
Hook: runtimehooksv1.AfterControlPlaneUpgrade,
Name: h.Name(),
Expand All @@ -131,7 +133,7 @@ func (s *Server) Start(ctx context.Context) error {
}
}

if t, ok := h.(handlers.BeforeClusterDeleteLifecycleHandler); ok {
if t, ok := h.(lifecycle.BeforeClusterDelete); ok {
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
Hook: runtimehooksv1.BeforeClusterDelete,
Name: strings.ToLower(h.Name()),
Expand All @@ -142,7 +144,7 @@ func (s *Server) Start(ctx context.Context) error {
}
}

if t, ok := h.(handlers.DiscoverVariablesMutationHandler); ok {
if t, ok := h.(mutation.DiscoverVariables); ok {
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
Hook: runtimehooksv1.DiscoverVariables,
Name: strings.ToLower(h.Name()),
Expand All @@ -153,7 +155,7 @@ func (s *Server) Start(ctx context.Context) error {
}
}

if t, ok := h.(handlers.GeneratePatchesMutationHandler); ok {
if t, ok := h.(mutation.GeneratePatches); ok {
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
Hook: runtimehooksv1.GeneratePatches,
Name: strings.ToLower(h.Name()),
Expand All @@ -164,7 +166,7 @@ func (s *Server) Start(ctx context.Context) error {
}
}

if t, ok := h.(handlers.ValidateTopologyMutationHandler); ok {
if t, ok := h.(mutation.ValidateTopology); ok {
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
Hook: runtimehooksv1.ValidateTopology,
Name: strings.ToLower(h.Name()),
Expand Down
83 changes: 83 additions & 0 deletions common/pkg/testutils/capitest/variables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package capitest

import (
"context"
"encoding/json"
"testing"

"github.com/onsi/gomega"
"github.com/onsi/gomega/gstruct"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"

"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/handlers/mutation"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/openapi"
)

type VariableTestDef struct {
Name string
Vals any
ExpectError bool
}

func ValidateVariable[T mutation.DiscoverVariables](
t *testing.T,
variableName string,
variableSchema *clusterv1.VariableSchema,
handlerCreator func() T,
variableTestDefs ...VariableTestDef,
) {
t.Helper()

t.Parallel()

for idx := range variableTestDefs {
tt := variableTestDefs[idx]

t.Run(tt.Name, func(t *testing.T) {
t.Parallel()

g := gomega.NewWithT(t)
h := handlerCreator()
resp := &runtimehooksv1.DiscoverVariablesResponse{}
h.DiscoverVariables(
context.Background(),
&runtimehooksv1.DiscoverVariablesRequest{},
resp,
)

g.Expect(resp.Status).To(gomega.Equal(runtimehooksv1.ResponseStatusSuccess))
g.Expect(resp.Variables).To(gomega.HaveLen(1))

variable := resp.Variables[0]
g.Expect(variable).To(gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{
"Name": gomega.Equal(variableName),
"Required": gomega.BeFalse(),
"Schema": gomega.Equal(*variableSchema),
}))

encodedVals, err := json.Marshal(tt.Vals)
g.Expect(err).NotTo(gomega.HaveOccurred())

validateErr := openapi.ValidateClusterVariable(
&clusterv1.ClusterVariable{
Name: variableName,
Value: apiextensionsv1.JSON{Raw: encodedVals},
},
&variable,
field.NewPath(variableName),
).ToAggregate()

if tt.ExpectError {
g.Expect(validateErr).To(gomega.HaveOccurred())
} else {
g.Expect(validateErr).NotTo(gomega.HaveOccurred())
}
})
}
}
6 changes: 3 additions & 3 deletions docs/content/http-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ spec:
- name: proxy
value:
http: http://example.com
https: http://example.com
https: https://example.com
no:
- http://no-proxy-1.example.com
- http://no-proxy-2.example.com
- no-proxy-1.example.com
- no-proxy-2.example.com
```

Applying this configuration will result in new bootstrap files on the `KubeadmControlPlaneTemplate`
Expand Down
Loading