|
| 1 | +/* |
| 2 | +Copyright 2024 The Karmada Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package options |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/spf13/pflag" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func TestNewOptions(t *testing.T) { |
| 28 | + opts := NewOptions() |
| 29 | + |
| 30 | + assert.True(t, opts.LeaderElection.LeaderElect, "Expected default LeaderElect to be true") |
| 31 | + assert.Equal(t, "karmada-system", opts.LeaderElection.ResourceNamespace, "Unexpected default ResourceNamespace") |
| 32 | + assert.Equal(t, 15*time.Second, opts.LeaderElection.LeaseDuration.Duration, "Unexpected default LeaseDuration") |
| 33 | + assert.Equal(t, "karmada-scheduler", opts.LeaderElection.ResourceName, "Unexpected default ResourceName") |
| 34 | +} |
| 35 | + |
| 36 | +func TestAddFlags(t *testing.T) { |
| 37 | + opts := NewOptions() |
| 38 | + fs := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 39 | + opts.AddFlags(fs) |
| 40 | + |
| 41 | + testCases := []struct { |
| 42 | + name string |
| 43 | + expectedType string |
| 44 | + expectedDefault string |
| 45 | + }{ |
| 46 | + {"kubeconfig", "string", ""}, |
| 47 | + {"leader-elect", "bool", "true"}, |
| 48 | + {"enable-scheduler-estimator", "bool", "false"}, |
| 49 | + {"scheduler-estimator-port", "int", "10352"}, |
| 50 | + {"plugins", "stringSlice", "[*]"}, |
| 51 | + {"scheduler-name", "string", "default-scheduler"}, |
| 52 | + } |
| 53 | + |
| 54 | + for _, tc := range testCases { |
| 55 | + flag := fs.Lookup(tc.name) |
| 56 | + assert.NotNil(t, flag, "Flag %s not found", tc.name) |
| 57 | + assert.Equal(t, tc.expectedType, flag.Value.Type(), "Unexpected type for flag %s", tc.name) |
| 58 | + assert.Equal(t, tc.expectedDefault, flag.DefValue, "Unexpected default value for flag %s", tc.name) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestOptionsComplete(t *testing.T) { |
| 63 | + testCases := []struct { |
| 64 | + name string |
| 65 | + bindAddress string |
| 66 | + securePort int |
| 67 | + expectedMetrics string |
| 68 | + expectedHealth string |
| 69 | + }{ |
| 70 | + { |
| 71 | + name: "Default values", |
| 72 | + bindAddress: defaultBindAddress, |
| 73 | + securePort: defaultPort, |
| 74 | + expectedMetrics: "0.0.0.0:10351", |
| 75 | + expectedHealth: "0.0.0.0:10351", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "Custom values", |
| 79 | + bindAddress: "127.0.0.1", |
| 80 | + securePort: 8080, |
| 81 | + expectedMetrics: "127.0.0.1:8080", |
| 82 | + expectedHealth: "127.0.0.1:8080", |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for _, tc := range testCases { |
| 87 | + t.Run(tc.name, func(t *testing.T) { |
| 88 | + opts := &Options{ |
| 89 | + BindAddress: tc.bindAddress, |
| 90 | + SecurePort: tc.securePort, |
| 91 | + } |
| 92 | + err := opts.Complete() |
| 93 | + assert.NoError(t, err) |
| 94 | + assert.Equal(t, tc.expectedMetrics, opts.MetricsBindAddress) |
| 95 | + assert.Equal(t, tc.expectedHealth, opts.HealthProbeBindAddress) |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestOptionsFlagParsing(t *testing.T) { |
| 101 | + opts := NewOptions() |
| 102 | + fs := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 103 | + opts.AddFlags(fs) |
| 104 | + |
| 105 | + testArgs := []string{ |
| 106 | + "--leader-elect=false", |
| 107 | + "--enable-scheduler-estimator=true", |
| 108 | + "--plugins=*,-foo,bar", |
| 109 | + "--scheduler-name=custom-scheduler", |
| 110 | + } |
| 111 | + |
| 112 | + err := fs.Parse(testArgs) |
| 113 | + assert.NoError(t, err) |
| 114 | + |
| 115 | + assert.False(t, opts.LeaderElection.LeaderElect) |
| 116 | + assert.True(t, opts.EnableSchedulerEstimator) |
| 117 | + assert.Equal(t, []string{"*", "-foo", "bar"}, opts.Plugins) |
| 118 | + assert.Equal(t, "custom-scheduler", opts.SchedulerName) |
| 119 | +} |
0 commit comments