Merge pull request #120186 from chendave/testcase_exported

kubeadm: Add testcases for exported method `ValueFromFlagsOrConfig`
This commit is contained in:
Kubernetes Prow Robot 2023-08-27 00:47:22 -07:00 committed by GitHub
commit d4572d58db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,
)
}
})
}
}