diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 00000000..5fbf267e --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,65 @@ +package config + +import ( + "encoding/json" + "errors" + "fmt" + "io/ioutil" + "log" + "os" + "path" + + "sigs.k8s.io/yaml" +) + +var globalConfig map[string][]byte + +// ReadConfigNamed is like ReadConfig, but you can specify a file name +func ReadConfigNamed(name string) { + // I expect the config in the main + dir, err := os.Getwd() + if err != nil { + panic(err) + } + + configPath := path.Join(dir, name) + + log.Println("Reading config from ", configPath) + + out, err := ioutil.ReadFile(configPath) + if err != nil { + panic(err) + } + + m := make(map[string]interface{}) + err = yaml.Unmarshal(out, &m) + if err != nil { + panic(err) + } + + globalConfig = make(map[string][]byte) + for k, v := range m { + b, err := json.Marshal(v) + if err != nil { + panic(err) + } + globalConfig[k] = b + } +} + +// ReadConfig reads the config +func ReadConfig() { + ReadConfigNamed("config.yaml") +} + +// UnmarshalConfig reads the global config and unmarshal it +func UnmarshalConfig(testName string, out interface{}) error { + if globalConfig == nil { + return errors.New("there's no global config!!! Make sure you invoke ReadConfig() or ReadConfigNamed(name) in your TestMain") + } + in := globalConfig[testName] + if in == nil { + return fmt.Errorf("there's no config for test name %s", testName) + } + return json.Unmarshal(in, out) +} diff --git a/pkg/feature/config.go b/pkg/feature/config.go new file mode 100644 index 00000000..8b7cea31 --- /dev/null +++ b/pkg/feature/config.go @@ -0,0 +1,16 @@ +package feature + +import ( + "fmt" + + "knative.dev/reconciler-test/pkg/config" +) + +func (f *Feature) Config(out interface{}) interface{} { + err := config.UnmarshalConfig(f.Name, out) + if err != nil { + panic(fmt.Sprintf("cannot read config: %v", err)) + } + + return out +} diff --git a/pkg/feature/feature.go b/pkg/feature/feature.go index 3a3209ec..0ce11897 100644 --- a/pkg/feature/feature.go +++ b/pkg/feature/feature.go @@ -19,6 +19,8 @@ package feature import ( "context" "fmt" + "runtime" + "strings" "testing" ) @@ -28,6 +30,25 @@ type Feature struct { Steps []Step } +func NewFeatureNamed(name string) *Feature { + f := new(Feature) + f.Name = name + return f +} + +func NewFeature() *Feature { + f := new(Feature) + + pc, _, _, _ := runtime.Caller(1) + caller := runtime.FuncForPC(pc) + if caller != nil { + splitted := strings.Split(caller.Name(), ".") + f.Name = splitted[len(splitted)-1] + } + + return f +} + // StepFn is the function signature for steps. type StepFn func(ctx context.Context, t *testing.T) diff --git a/test/example/config.yaml b/test/example/config.yaml new file mode 100644 index 00000000..64650dfd --- /dev/null +++ b/test/example/config.yaml @@ -0,0 +1,3 @@ +RecorderFeature: + Sink: my-sink + Source: my-sender diff --git a/test/example/examples_test.go b/test/example/examples_test.go index e780d2fc..b053b425 100644 --- a/test/example/examples_test.go +++ b/test/example/examples_test.go @@ -41,7 +41,7 @@ func TestRecorder(t *testing.T) { // environment settings. Additional options can be passed to Environment() // if customization is required. ctx, env := global.Environment( - knative.WithKnativeNamespace("knative-reconciler-test"), + knative.WithKnativeNamespace("knative-eventing"), knative.WithLoggingConfig, knative.WithTracingConfig, k8s.WithEventListener, diff --git a/test/example/main_test.go b/test/example/main_test.go index 110caee2..cbf8d78c 100644 --- a/test/example/main_test.go +++ b/test/example/main_test.go @@ -29,6 +29,7 @@ import ( "knative.dev/pkg/injection" _ "knative.dev/pkg/system/testing" + "knative.dev/reconciler-test/pkg/config" "knative.dev/reconciler-test/pkg/environment" ) @@ -48,6 +49,9 @@ func TestMain(m *testing.M) { // framework as well as any additional flags included in the integration. flag.Parse() + // Read config file for the tests + config.ReadConfig() + // EnableInjectionOrDie will enable client injection, this is used by the // testing framework for namespace management, and could be leveraged by // features to pull Kubernetes clients or the test environment out of the diff --git a/test/example/recorder_feature.go b/test/example/recorder_feature.go index 4e1ed297..e85d5636 100644 --- a/test/example/recorder_feature.go +++ b/test/example/recorder_feature.go @@ -28,13 +28,19 @@ import ( "knative.dev/reconciler-test/pkg/k8s" ) +type recorderFeatureConfig struct { + Sink string + Source string +} + func RecorderFeature() *feature.Feature { - svc := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "services"} + f := feature.NewFeature() + conf := f.Config(&recorderFeatureConfig{}).(*recorderFeatureConfig) - from := feature.MakeRandomK8sName("sender") - to := feature.MakeRandomK8sName("recorder") + svc := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "services"} - f := new(feature.Feature) + from := feature.MakeRandomK8sName(conf.Source) + to := feature.MakeRandomK8sName(conf.Sink) event := FullEvent()