e2e: move funs of framework/viperconfig to e2e
Signed-off-by: clarklee92 <clarklee1992@hotmail.com>
This commit is contained in:
@@ -149,7 +149,6 @@ filegroup(
|
||||
"//test/e2e/framework/statefulset:all-srcs",
|
||||
"//test/e2e/framework/testfiles:all-srcs",
|
||||
"//test/e2e/framework/timer:all-srcs",
|
||||
"//test/e2e/framework/viperconfig:all-srcs",
|
||||
"//test/e2e/framework/volume:all-srcs",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["viperconfig.go"],
|
||||
importpath = "k8s.io/kubernetes/test/e2e/framework/viperconfig",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//vendor/github.com/pkg/errors:go_default_library",
|
||||
"//vendor/github.com/spf13/viper:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["viperconfig_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//test/e2e/framework/config:go_default_library",
|
||||
"//vendor/github.com/stretchr/testify/require:go_default_library",
|
||||
],
|
||||
)
|
||||
@@ -1,145 +0,0 @@
|
||||
/*
|
||||
Copyright 2018 The Kubernetes 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 viperconfig
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// ViperizeFlags checks whether a configuration file was specified,
|
||||
// reads it, and updates the configuration variables in the specified
|
||||
// flag set accordingly. Must be called after framework.HandleFlags()
|
||||
// and before framework.AfterReadingAllFlags().
|
||||
//
|
||||
// The logic is so that a required configuration file must be present. If empty,
|
||||
// the optional configuration file is used instead, unless also empty.
|
||||
//
|
||||
// Files can be specified with just a base name ("e2e", matches "e2e.json/yaml/..." in
|
||||
// the current directory) or with path and suffix.
|
||||
func ViperizeFlags(requiredConfig, optionalConfig string, flags *flag.FlagSet) error {
|
||||
viperConfig := optionalConfig
|
||||
required := false
|
||||
if requiredConfig != "" {
|
||||
viperConfig = requiredConfig
|
||||
required = true
|
||||
}
|
||||
if viperConfig == "" {
|
||||
return nil
|
||||
}
|
||||
viper.SetConfigName(filepath.Base(viperConfig))
|
||||
viper.AddConfigPath(filepath.Dir(viperConfig))
|
||||
wrapError := func(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
errorPrefix := fmt.Sprintf("viper config %q", viperConfig)
|
||||
actualFile := viper.ConfigFileUsed()
|
||||
if actualFile != "" && actualFile != viperConfig {
|
||||
errorPrefix = fmt.Sprintf("%s = %q", errorPrefix, actualFile)
|
||||
}
|
||||
return errors.Wrap(err, errorPrefix)
|
||||
}
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
// If the user specified a file suffix, the Viper won't
|
||||
// find the file because it always appends its known set
|
||||
// of file suffices. Therefore try once more without
|
||||
// suffix.
|
||||
ext := filepath.Ext(viperConfig)
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok && ext != "" {
|
||||
viper.SetConfigName(filepath.Base(viperConfig[0 : len(viperConfig)-len(ext)]))
|
||||
err = viper.ReadInConfig()
|
||||
}
|
||||
if err != nil {
|
||||
// If a config was required, then parsing must
|
||||
// succeed. This catches syntax errors and
|
||||
// "file not found". Unfortunately error
|
||||
// messages are sometimes hard to understand,
|
||||
// so try to help the user a bit.
|
||||
switch err.(type) {
|
||||
case viper.ConfigFileNotFoundError:
|
||||
if required {
|
||||
return wrapError(errors.New("not found"))
|
||||
}
|
||||
// Proceed without config.
|
||||
return nil
|
||||
case viper.UnsupportedConfigError:
|
||||
if required {
|
||||
return wrapError(errors.New("not using a supported file format"))
|
||||
}
|
||||
// Proceed without config.
|
||||
return nil
|
||||
default:
|
||||
// Something isn't right in the file.
|
||||
return wrapError(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update all flag values not already set with values found
|
||||
// via Viper. We do this ourselves instead of calling
|
||||
// something like viper.Unmarshal(&TestContext) because we
|
||||
// want to support all values, regardless where they are
|
||||
// stored.
|
||||
return wrapError(viperUnmarshal(flags))
|
||||
}
|
||||
|
||||
// viperUnmarshall updates all flags with the corresponding values found
|
||||
// via Viper, regardless whether the flag value is stored in TestContext, some other
|
||||
// context or a local variable.
|
||||
func viperUnmarshal(flags *flag.FlagSet) error {
|
||||
var result error
|
||||
set := make(map[string]bool)
|
||||
|
||||
// Determine which values were already set explicitly via
|
||||
// flags. Those we don't overwrite because command line
|
||||
// flags have a higher priority.
|
||||
flags.Visit(func(f *flag.Flag) {
|
||||
set[f.Name] = true
|
||||
})
|
||||
|
||||
flags.VisitAll(func(f *flag.Flag) {
|
||||
if result != nil ||
|
||||
set[f.Name] ||
|
||||
!viper.IsSet(f.Name) {
|
||||
return
|
||||
}
|
||||
|
||||
// In contrast to viper.Unmarshal(), values
|
||||
// that have the wrong type (for example, a
|
||||
// list instead of a plain string) will not
|
||||
// trigger an error here. This could be fixed
|
||||
// by checking the type ourselves, but
|
||||
// probably isn't worth the effort.
|
||||
//
|
||||
// "%v" correctly turns bool, int, strings into
|
||||
// the representation expected by flag, so those
|
||||
// can be used in config files. Plain strings
|
||||
// always work there, just as on the command line.
|
||||
str := fmt.Sprintf("%v", viper.Get(f.Name))
|
||||
if err := f.Value.Set(str); err != nil {
|
||||
result = fmt.Errorf("setting option %q from config file value: %s", f.Name, err)
|
||||
}
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes 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 viperconfig
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/kubernetes/test/e2e/framework/config"
|
||||
)
|
||||
|
||||
func TestViperConfig(t *testing.T) {
|
||||
flags := flag.NewFlagSet("test", 0)
|
||||
type Context struct {
|
||||
Bool bool `default:"true"`
|
||||
Duration time.Duration `default:"1ms"`
|
||||
Float64 float64 `default:"1.23456789"`
|
||||
String string `default:"hello world"`
|
||||
Int int `default:"-1" usage:"some number"`
|
||||
Int64 int64 `default:"-1234567890123456789"`
|
||||
Uint uint `default:"1"`
|
||||
Uint64 uint64 `default:"1234567890123456789"`
|
||||
}
|
||||
var context Context
|
||||
require.NotPanics(t, func() {
|
||||
config.AddOptionsToSet(flags, &context, "")
|
||||
})
|
||||
|
||||
viperConfig := `
|
||||
bool: false
|
||||
duration: 1s
|
||||
float64: -1.23456789
|
||||
string: pong
|
||||
int: -2
|
||||
int64: -9123456789012345678
|
||||
uint: 2
|
||||
uint64: 9123456789012345678
|
||||
`
|
||||
tmpfile, err := ioutil.TempFile("", "viperconfig-*.yaml")
|
||||
require.NoError(t, err, "temp file")
|
||||
defer os.Remove(tmpfile.Name())
|
||||
if _, err := tmpfile.Write([]byte(viperConfig)); err != nil {
|
||||
require.NoError(t, err, "write config")
|
||||
}
|
||||
require.NoError(t, tmpfile.Close(), "close temp file")
|
||||
|
||||
require.NoError(t, ViperizeFlags(tmpfile.Name(), "", flags), "read config file")
|
||||
require.Equal(t,
|
||||
Context{false, time.Second, -1.23456789, "pong",
|
||||
-2, -9123456789012345678, 2, 9123456789012345678,
|
||||
},
|
||||
context,
|
||||
"values from viper must match")
|
||||
}
|
||||
Reference in New Issue
Block a user