mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-05 16:27:05 +00:00
source/fake: make the fake source configurable
Enables more flexible testing.
This commit is contained in:
parent
d761bc4fcd
commit
278ccdb997
2 changed files with 38 additions and 9 deletions
|
@ -213,6 +213,7 @@ func TestCreateFeatureLabels(t *testing.T) {
|
||||||
Convey("When fake feature source is configured", func() {
|
Convey("When fake feature source is configured", func() {
|
||||||
emptyLabelWL := regex{*regexp.MustCompile("")}
|
emptyLabelWL := regex{*regexp.MustCompile("")}
|
||||||
fakeFeatureSource := source.FeatureSource(new(fake.Source))
|
fakeFeatureSource := source.FeatureSource(new(fake.Source))
|
||||||
|
fakeFeatureSource.SetConfig(fakeFeatureSource.NewConfig())
|
||||||
sources := []source.FeatureSource{}
|
sources := []source.FeatureSource{}
|
||||||
sources = append(sources, fakeFeatureSource)
|
sources = append(sources, fakeFeatureSource)
|
||||||
labels := createFeatureLabels(sources, emptyLabelWL)
|
labels := createFeatureLabels(sources, emptyLabelWL)
|
||||||
|
|
|
@ -16,22 +16,51 @@ limitations under the License.
|
||||||
|
|
||||||
package fake
|
package fake
|
||||||
|
|
||||||
import "sigs.k8s.io/node-feature-discovery/source"
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"sigs.k8s.io/node-feature-discovery/source"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Configuration file options
|
||||||
|
type Config struct {
|
||||||
|
Labels map[string]string `json:"labels"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// newDefaultConfig returns a new config with defaults values
|
||||||
|
func newDefaultConfig() *Config {
|
||||||
|
return &Config{
|
||||||
|
Labels: map[string]string{
|
||||||
|
"fakefeature1": "true",
|
||||||
|
"fakefeature2": "true",
|
||||||
|
"fakefeature3": "true",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Source implements FeatureSource.
|
// Source implements FeatureSource.
|
||||||
type Source struct{}
|
type Source struct {
|
||||||
|
config *Config
|
||||||
|
}
|
||||||
|
|
||||||
// Name returns an identifier string for this feature source.
|
// Name returns an identifier string for this feature source.
|
||||||
func (s Source) Name() string { return "fake" }
|
func (s Source) Name() string { return "fake" }
|
||||||
|
|
||||||
// NewConfig method of the FeatureSource interface
|
// NewConfig method of the FeatureSource interface
|
||||||
func (s *Source) NewConfig() source.Config { return nil }
|
func (s *Source) NewConfig() source.Config { return newDefaultConfig() }
|
||||||
|
|
||||||
// GetConfig method of the FeatureSource interface
|
// GetConfig method of the FeatureSource interface
|
||||||
func (s *Source) GetConfig() source.Config { return nil }
|
func (s *Source) GetConfig() source.Config { return s.config }
|
||||||
|
|
||||||
// SetConfig method of the FeatureSource interface
|
// SetConfig method of the FeatureSource interface
|
||||||
func (s *Source) SetConfig(source.Config) {}
|
func (s *Source) SetConfig(conf source.Config) {
|
||||||
|
switch v := conf.(type) {
|
||||||
|
case *Config:
|
||||||
|
s.config = v
|
||||||
|
default:
|
||||||
|
panic(fmt.Sprintf("invalid config type: %T", conf))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Configure method of the FeatureSource interface
|
// Configure method of the FeatureSource interface
|
||||||
func (s Source) Configure([]byte) error { return nil }
|
func (s Source) Configure([]byte) error { return nil }
|
||||||
|
@ -39,10 +68,9 @@ func (s Source) Configure([]byte) error { return nil }
|
||||||
// Discover returns feature names for some fake features.
|
// Discover returns feature names for some fake features.
|
||||||
func (s Source) Discover() (source.Features, error) {
|
func (s Source) Discover() (source.Features, error) {
|
||||||
// Adding three fake features.
|
// Adding three fake features.
|
||||||
features := source.Features{
|
features := make(source.Features, len(s.config.Labels))
|
||||||
"fakefeature1": true,
|
for k, v := range s.config.Labels {
|
||||||
"fakefeature2": true,
|
features[k] = v
|
||||||
"fakefeature3": true,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return features, nil
|
return features, nil
|
||||||
|
|
Loading…
Add table
Reference in a new issue