/*
Copyright 2019 The Kubernetes Authors.
Copyright 2025 The Volcano Authors.

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.
*/

package options

import (
	"testing"
	"time"

	"github.com/spf13/pflag"
	"github.com/stretchr/testify/assert"
	"k8s.io/apimachinery/pkg/api/equality"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	utilfeature "k8s.io/apiserver/pkg/util/feature"
	"k8s.io/client-go/tools/leaderelection/resourcelock"
	"k8s.io/component-base/config"
	componentbaseoptions "k8s.io/component-base/config/options"
	"k8s.io/component-base/featuregate"

	voptions "volcano.sh/volcano/cmd/scheduler/app/options"
	"volcano.sh/volcano/pkg/features"
	"volcano.sh/volcano/pkg/kube"
	commonutil "volcano.sh/volcano/pkg/util"
)

func TestAddFlags(t *testing.T) {
	fs := pflag.NewFlagSet("addflagstest", pflag.ExitOnError)
	s := NewServerOption()
	commonutil.LeaderElectionDefault(&s.LeaderElection)
	componentbaseoptions.BindLeaderElectionFlags(&s.LeaderElection, fs)
	s.AddFlags(fs)
	utilfeature.DefaultMutableFeatureGate.AddFlag(fs)

	args := []string{
		"--resync-period=0",
		"--cache-dumper=false",
		"--leader-elect-lease-duration=60s",
		"--leader-elect-renew-deadline=20s",
		"--leader-elect-retry-period=10s",
		"--feature-gates=PodDisruptionBudgetsSupport=false,VolcanoJobSupport=true",
	}
	fs.Parse(args)

	// This is a snapshot of expected options parsed by args.
	serverOptions := &voptions.ServerOption{
		ResyncPeriod: 0,
		LeaderElection: config.LeaderElectionConfiguration{
			LeaderElect:       true,
			LeaseDuration:     metav1.Duration{Duration: 60 * time.Second},
			RenewDeadline:     metav1.Duration{Duration: 20 * time.Second},
			RetryPeriod:       metav1.Duration{Duration: 10 * time.Second},
			ResourceLock:      resourcelock.LeasesResourceLock,
			ResourceNamespace: defaultLockObjectNamespace,
		},
		ListenAddress:    defaultListenAddress,
		EnableCSIStorage: true,
		KubeClientOptions: kube.ClientOptions{
			Master:     "",
			KubeConfig: "",
			QPS:        defaultQPS,
			Burst:      defaultBurst,
		},
		HealthzBindAddress:            ":11251",
		MinNodesToFind:                defaultMinNodesToFind,
		MinPercentageOfNodesToFind:    defaultMinPercentageOfNodesToFind,
		PercentageOfNodesToFind:       defaultPercentageOfNodesToFind,
		NodeWorkerThreads:             defaultNodeWorkers,
		CacheDumpFileDir:              "/tmp",
		DisableDefaultSchedulerConfig: false,
		ResourceSyncTimeout:           60 * time.Second,
	}
	expected := &ServerOption{
		ServerOption:        serverOptions,
		SchedulerName:       defaultSchedulerName,
		ScheduleWorkerCount: defaultScheduleWorkerCount,
		ShardingMode:        commonutil.NoneShardingMode,
		ShardName:           defaultSchedulerName,
	}
	expectedFeatureGates := map[featuregate.Feature]bool{
		features.PodDisruptionBudgetsSupport: false,
		features.VolcanoJobSupport:           true,
	}

	if !equality.Semantic.DeepEqual(expected, s) {
		t.Errorf("Got different run options than expected.\nGot: %+v\nExpected: %+v\n", s, expected)
	}
	for k, v := range expectedFeatureGates {
		assert.Equal(t, v, utilfeature.DefaultFeatureGate.Enabled(k))
	}
}

func TestCheckOptionOrDieSyncsAgentShardingOptions(t *testing.T) {
	fs := pflag.NewFlagSet("shardingtest", pflag.ExitOnError)
	s := NewServerOption()
	commonutil.LeaderElectionDefault(&s.LeaderElection)
	componentbaseoptions.BindLeaderElectionFlags(&s.LeaderElection, fs)
	s.AddFlags(fs)
	s.LeaderElection.ResourceName = commonutil.GenerateComponentName([]string{s.SchedulerName})

	err := fs.Parse([]string{
		"--scheduler-sharding-mode=hard",
		"--scheduler-sharding-name=agent-scheduler",
	})
	assert.NoError(t, err)

	err = s.CheckOptionOrDie()
	assert.NoError(t, err)
	assert.Equal(t, commonutil.HardShardingMode, s.ShardingMode)
	assert.Equal(t, defaultShardName, s.ShardName)
	assert.Equal(t, commonutil.HardShardingMode, s.ServerOption.ShardingMode)
	assert.Equal(t, defaultShardName, s.ServerOption.ShardName)
}

func TestCheckOptionOrDieRejectsNonPositiveScheduleWorkerCount(t *testing.T) {
	fs := pflag.NewFlagSet("worker-count-test", pflag.ExitOnError)
	s := NewServerOption()
	commonutil.LeaderElectionDefault(&s.LeaderElection)
	componentbaseoptions.BindLeaderElectionFlags(&s.LeaderElection, fs)
	s.AddFlags(fs)
	s.LeaderElection.ResourceName = commonutil.GenerateComponentName([]string{s.SchedulerName})

	err := fs.Parse([]string{"--scheduler-worker-count=0"})
	assert.NoError(t, err)

	err = s.CheckOptionOrDie()
	assert.EqualError(t, err, "scheduler-worker-count must be greater than 0")
}
