mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 00:07:50 +00:00
Merge pull request #120186 from chendave/testcase_exported
kubeadm: Add testcases for exported method `ValueFromFlagsOrConfig`
This commit is contained in:
commit
d4572d58db
@ -17,8 +17,10 @@ limitations under the License.
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
)
|
||||
|
||||
@ -103,3 +105,53 @@ func TestGetKubeConfigPath(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValueFromFlagsOrConfig(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
flag string
|
||||
cfg interface{}
|
||||
flagValue interface{}
|
||||
expected interface{}
|
||||
}{
|
||||
{
|
||||
name: "string: config is overridden by the flag",
|
||||
flag: "foo",
|
||||
cfg: "foo_cfg",
|
||||
flagValue: "foo_flag",
|
||||
expected: "foo_flag",
|
||||
},
|
||||
{
|
||||
name: "bool: config is overridden by the flag",
|
||||
flag: "bar",
|
||||
cfg: true,
|
||||
flagValue: false,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
type options struct {
|
||||
foo string
|
||||
bar bool
|
||||
}
|
||||
fakeOptions := &options{}
|
||||
fs := pflag.FlagSet{}
|
||||
fs.StringVar(&fakeOptions.foo, "foo", "", "")
|
||||
fs.BoolVar(&fakeOptions.bar, "bar", false, "")
|
||||
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := fs.Set(tt.flag, fmt.Sprintf("%v", tt.flagValue))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to set the value of the flag %v", tt.flagValue)
|
||||
}
|
||||
actualResult := ValueFromFlagsOrConfig(&fs, tt.flag, tt.cfg, tt.flagValue)
|
||||
if actualResult != tt.expected {
|
||||
t.Errorf(
|
||||
"failed ValueFromFlagsOrConfig:\n\texpected: %s\n\t actual: %s",
|
||||
tt.expected,
|
||||
actualResult,
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user