// Copyright (c) 2026 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Gateway API conformance runner. Wraps the upstream
// sigs.k8s.io/gateway-api/conformance suite, runs it against the
// cluster at $KUBECONFIG, and writes the ConformanceReport YAML for
// upstream submission.
//
// All flags come from sigs.k8s.io/gateway-api/conformance/utils/flags;
// see that package for the full list. One additional flag is defined
// here, -curated, which selects a pre-baked feature/test/profile set
// that mirrors a known upstream implementation's curated configuration
// (currently: envoy-gateway).

package gateway_test

import (
	"flag"
	"fmt"
	"io/fs"
	"os"
	"testing"

	apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/util/sets"
	"k8s.io/client-go/kubernetes"
	_ "k8s.io/client-go/plugin/pkg/client/auth"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/client/config"
	ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
	"sigs.k8s.io/controller-runtime/pkg/log/zap"
	gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
	gwapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
	gwapiv1alpha3 "sigs.k8s.io/gateway-api/apis/v1alpha3"
	gwapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
	"sigs.k8s.io/gateway-api/conformance"
	"sigs.k8s.io/gateway-api/conformance/tests"
	"sigs.k8s.io/gateway-api/conformance/utils/flags"
	"sigs.k8s.io/gateway-api/conformance/utils/suite"
	"sigs.k8s.io/gateway-api/pkg/features"
	"sigs.k8s.io/yaml"
)

// curatedFlag selects a pre-baked feature/test/profile set. Empty means
// "honour the individual upstream flags". Currently supports:
//
//	envoy-gateway   - mirrors envoyproxy/gateway v1.9.1's
//	                  EnvoyGatewaySuite for GatewayNamespace mode (the
//	                  mode the tigera-operator deploys): full upstream
//	                  features minus mesh, plus UDP, with the features
//	                  and tests EG and Calico's deployment don't support
//	                  skipped. See test/conformance/suite.go in the
//	                  envoyproxy/gateway v1.9.1 tag, and
//	                  envoyGatewayCuratedSet for the exact set.
var curatedFlag = flag.String("curated", "", "Pre-baked feature/test/profile set: \"envoy-gateway\" or empty.")

func TestGatewayAPIConformance(t *testing.T) {
	// controller-runtime emits a stack-trace warning the first time any of
	// its loggers is consulted without SetLogger having been called. Match
	// the upstream conformance.DefaultOptions and install a zap logger.
	ctrllog.SetLogger(zap.New(zap.WriteTo(os.Stderr), zap.UseDevMode(true)))

	cfg, err := config.GetConfig()
	if err != nil {
		t.Fatalf("loading kube config: %v", err)
	}

	c, err := client.New(cfg, client.Options{})
	if err != nil {
		t.Fatalf("controller-runtime client: %v", err)
	}

	cs, err := kubernetes.NewForConfig(cfg)
	if err != nil {
		t.Fatalf("clientset: %v", err)
	}

	// Install every gateway-api API version that conformance tests touch.
	// HTTPRoute reference-grant tests delete v1beta1.ReferenceGrant; the
	// TLSRoute / UDPRoute tests work in v1alpha2; BackendTLSPolicy is
	// v1alpha3. Mirrors sigs.k8s.io/gateway-api/conformance.DefaultOptions.
	for _, install := range []func(*runtime.Scheme) error{
		gwapiv1alpha3.Install,
		gwapiv1alpha2.Install,
		gwapiv1beta1.Install,
		gwapiv1.Install,
	} {
		if err := install(c.Scheme()); err != nil {
			t.Fatalf("install gateway-api scheme: %v", err)
		}
	}
	if err := apiextv1.AddToScheme(c.Scheme()); err != nil {
		t.Fatalf("install apiextensions scheme: %v", err)
	}

	// gateway-api v1.6 replaced the individual exported flag pointers with a
	// registry plus flags.ApplyAll, which applies only the flags the caller
	// actually set. Defaults therefore have to be seeded here.
	opts := suite.ConfigurableOptions{
		GatewayClassName:     flags.DefaultGatewayClassName,
		Mode:                 flags.DefaultMode,
		CleanupBaseResources: flags.DefaultCleanupBaseResources,
		CleanupTestResources: flags.DefaultCleanupTestResources,
	}
	flags.ApplyAll(&opts)

	if *curatedFlag != "" {
		curated, err := curatedConfig(*curatedFlag)
		if err != nil {
			t.Fatalf("curated config: %v", err)
		}
		// Curated overrides individual feature/test/profile flags so the
		// caller can't accidentally mix and match an inconsistent set.
		opts.SupportedFeatures = curated.SupportedFeatures.UnsortedList()
		opts.ExemptFeatures = curated.ExemptFeatures.UnsortedList()
		opts.SkipTests = curated.SkipTests
		opts.ConformanceProfiles = curated.Profiles.UnsortedList()
		opts.EnableAllSupportedFeatures = false
	}

	if len(opts.ConformanceProfiles) == 0 {
		// Default to GATEWAY-HTTP for Calico's Envoy-Gateway-based impl.
		opts.ConformanceProfiles = []suite.ConformanceProfileName{suite.GatewayHTTPConformanceProfileName}
	}

	t.Logf("running gateway-api conformance: gatewayClass=%s mode=%s version=%q curated=%q profiles=%v allFeatures=%t",
		opts.GatewayClassName, opts.Mode, opts.Implementation.Version, *curatedFlag, opts.ConformanceProfiles, opts.EnableAllSupportedFeatures)

	cSuite, err := suite.NewConformanceTestSuite(suite.ConformanceOptions{
		ConfigurableOptions: opts,
		Client:              c,
		Clientset:           cs,
		RestConfig:          cfg,
		ManifestFS:          []fs.FS{&conformance.Manifests},
	})
	if err != nil {
		t.Fatalf("constructing conformance suite: %v", err)
	}

	cSuite.Setup(t, tests.ConformanceTests)
	if err := cSuite.Run(t, tests.ConformanceTests); err != nil {
		t.Fatalf("running conformance: %v", err)
	}

	report, err := cSuite.Report()
	if err != nil {
		t.Fatalf("generating report: %v", err)
	}

	out, err := yaml.Marshal(report)
	if err != nil {
		t.Fatalf("marshalling report: %v", err)
	}

	if opts.ReportOutputPath == "" {
		t.Logf("\n--- ConformanceReport ---\n%s\n", out)
		return
	}
	// Per upstream README: report MUST be uploaded exactly as produced.
	if err := os.WriteFile(opts.ReportOutputPath, out, 0o600); err != nil {
		t.Fatalf("writing report: %v", err)
	}
	t.Logf("wrote ConformanceReport to %s", opts.ReportOutputPath)
}

// curatedSet bundles the four conformance suite levers a curated profile
// drives: which features are claimed, which are exempt, which tests are
// skipped, and which profiles are exercised.
type curatedSet struct {
	SupportedFeatures sets.Set[features.FeatureName]
	ExemptFeatures    sets.Set[features.FeatureName]
	SkipTests         []string
	Profiles          sets.Set[suite.ConformanceProfileName]
}

func curatedConfig(name string) (curatedSet, error) {
	switch name {
	case "envoy-gateway":
		return envoyGatewayCuratedSet(), nil
	default:
		return curatedSet{}, fmt.Errorf("unknown curated set %q (supported: envoy-gateway)", name)
	}
}

// envoyGatewayCuratedSet mirrors the curated configuration in
// envoyproxy/gateway v1.9.1's test/conformance/suite.go for the
// GatewayNamespace operating mode (the mode the tigera-operator deploys).
// Calico's Gateway API implementation is a Calico-deployed Envoy Gateway,
// so the supported feature surface is, by construction, the same.
//
// Source ref: https://github.com/envoyproxy/gateway/blob/v1.9.1/test/conformance/suite.go
func envoyGatewayCuratedSet() curatedSet {
	skipFeatures := sets.New(
		features.GatewayStaticAddressesFeature.Name,
		features.GatewayInfrastructurePropagationFeature.Name,
		// EG does not implement misdirected-request detection on HTTPS
		// listeners. Still skipped upstream at v1.9.1.
		features.GatewayHTTPSListenerDetectMisdirectedRequestsFeature.Name,
	)

	supported := sets.New[features.FeatureName]()
	for _, f := range features.AllFeatures.UnsortedList() {
		if !skipFeatures.Has(f.Name) {
			supported.Insert(f.Name)
		}
	}
	for _, f := range features.UDPRouteFeatures.UnsortedList() {
		supported.Insert(f.Name)
	}

	exempt := sets.New[features.FeatureName]()
	for _, f := range features.MeshCoreFeatures.UnsortedList() {
		exempt.Insert(f.Name)
	}
	for _, f := range features.MeshExtendedFeatures.UnsortedList() {
		exempt.Insert(f.Name)
	}

	return curatedSet{
		SupportedFeatures: supported,
		ExemptFeatures:    exempt,
		SkipTests: []string{
			tests.GatewayStaticAddresses.ShortName,
			tests.GatewayInfrastructure.ShortName,
			// EG fails only the uri-type SAN case (dns and multi SAN pass).
			// Skip until EG fixes URI SAN verification, likely an Envoy-side fix.
			tests.BackendTLSPolicySANValidation.ShortName,
		},
		Profiles: sets.New(
			suite.GatewayHTTPConformanceProfileName,
			suite.GatewayTLSConformanceProfileName,
			suite.GatewayGRPCConformanceProfileName,
		),
	}
}
