kubeadm: Add testcases for exported method ValueFromFlagsOrConfig

Signed-off-by: Dave Chen <dave.chen@arm.com>
This commit is contained in:
Dave Chen 2023-07-18 15:09:08 +08:00
parent f563910656
commit dcbe7755bf

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