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

init CEL validation #2059

Merged
merged 10 commits into from
Oct 26, 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
2 changes: 1 addition & 1 deletion api/v1alpha1/validation/envoyproxy_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
_ "github.com/envoyproxy/gateway/internal/xds/extensions" // register the generated types to support protojson unmarshalling
)

// Validate validates the provided EnvoyProxy.
// ValidateEnvoyProxy validates the provided EnvoyProxy.
func ValidateEnvoyProxy(proxy *egv1a1.EnvoyProxy) error {
var errs []error
if proxy == nil {
Expand Down
89 changes: 89 additions & 0 deletions test/cel-validation/envoyproxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.

//go:build validation
// +build validation

package cel_validation

import (
"context"
"fmt"
"strings"
"testing"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
)

func TestEnvoyProxyProvider(t *testing.T) {
ctx := context.Background()
baseEnvoyProxy := egv1a1.EnvoyProxy{
ObjectMeta: metav1.ObjectMeta{
Name: "proxy",
Namespace: metav1.NamespaceDefault,
},
Spec: egv1a1.EnvoyProxySpec{},
}

cases := []struct {
desc string
mutate func(envoy *egv1a1.EnvoyProxy)
mutateStatus func(envoy *egv1a1.EnvoyProxy)
wantErrors []string
}{
{
desc: "nil provider",
mutate: func(envoy *egv1a1.EnvoyProxy) {

},
wantErrors: []string{},
},
{
desc: "unsupported provider",
mutate: func(envoy *egv1a1.EnvoyProxy) {
envoy.Spec = egv1a1.EnvoyProxySpec{
Provider: &egv1a1.EnvoyProxyProvider{
Type: "foo",
},
}
},
wantErrors: []string{"Unsupported value: \"foo\": supported values: \"Kubernetes\""},
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
proxy := baseEnvoyProxy.DeepCopy()
proxy.Name = fmt.Sprintf("proxy-%v", time.Now().UnixNano())

if tc.mutate != nil {
tc.mutate(proxy)
}
err := c.Create(ctx, proxy)

if tc.mutateStatus != nil {
tc.mutateStatus(proxy)
err = c.Status().Update(ctx, proxy)
}

if (len(tc.wantErrors) != 0) != (err != nil) {
t.Fatalf("Unexpected response while creating EnvoyProxy; got err=\n%v\n;want error=%v", err, tc.wantErrors != nil)
}

var missingErrorStrings []string
for _, wantError := range tc.wantErrors {
if !strings.Contains(strings.ToLower(err.Error()), strings.ToLower(wantError)) {
missingErrorStrings = append(missingErrorStrings, wantError)
}
}
if len(missingErrorStrings) != 0 {
t.Errorf("Unexpected response while creating EnvoyProxy; got err=\n%v\n;missing strings within error=%q", err, missingErrorStrings)
}
})
}
}
66 changes: 66 additions & 0 deletions test/cel-validation/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.

//go:build celvalidation
// +build celvalidation

package celvalidation

import (
"context"
"fmt"
"os"
"path/filepath"
"testing"

"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
)

var c client.Client

func TestMain(m *testing.M) {
// Setup the test environment.
testEnv, restCfg, err := startEnv()
if err != nil {
panic(fmt.Sprintf("Failed to start testenv: %v", err))
}

_, cancel := context.WithCancel(ctrl.SetupSignalHandler())
defer func() {
cancel()
if err := testEnv.Stop(); err != nil {
panic(fmt.Sprintf("Failed to stop testenv: %v", err))
}
}()

c, err = client.New(restCfg, client.Options{})
if err != nil {
panic(fmt.Sprintf("Error initializing client: %v", err))
}
_ = egv1a1.AddToScheme(c.Scheme())

os.Exit(m.Run())
}

func startEnv() (*envtest.Environment, *rest.Config, error) {
log.SetLogger(zap.New(zap.WriteTo(os.Stderr), zap.UseDevMode(true)))
egAPIs := filepath.Join("..", "..", "charts", "gateway-helm", "crds", "generated")

env := &envtest.Environment{
CRDDirectoryPaths: []string{egAPIs},
}
cfg, err := env.Start()
if err != nil {
return env, nil, err
}
return env, cfg, nil
}
2 changes: 1 addition & 1 deletion tools/make/golang.mk
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ go.testdata.complete: ## Override test ouputdata
go test -timeout 30s github.com/envoyproxy/gateway/internal/gatewayapi --override-testdata=true

.PHONY: go.test.coverage
go.test.coverage: $(tools/setup-envtest) ## Run go unit and integration tests in GitHub Actions
go.test.coverage: kube-test $(tools/setup-envtest) ## Run go unit and integration tests in GitHub Actions
@$(LOG_TARGET)
KUBEBUILDER_ASSETS="$(shell $(tools/setup-envtest) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... --tags=integration -race -coverprofile=coverage.xml -covermode=atomic

Expand Down
1 change: 1 addition & 0 deletions tools/make/kube.mk
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ kube-generate: $(tools/controller-gen) ## Generate code containing DeepCopy, Dee
kube-test: manifests generate $(tools/setup-envtest) ## Run Kubernetes provider tests.
@$(LOG_TARGET)
KUBEBUILDER_ASSETS="$(shell $(tools/setup-envtest) use $(ENVTEST_K8S_VERSION) -p path)" go test --tags=integration ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(tools/setup-envtest) use $(ENVTEST_K8S_VERSION) -p path)" go test --tags=celvalidation ./... -coverprofile cover.out

##@ Kubernetes Deployment

Expand Down