func registryFromList(name string, registrants []registry.Registrant) *registry.Registry { registry := registry.Create(name) for _, registrant := range registrants { registry.Register(registrant) } return registry }
func TestGet(t *testing.T) { type in struct { providers []ProviderCreator name string } type out struct { creator ProviderCreator } a := mockProviderCreator{name: "a"} b := mockProviderCreator{name: "b"} c := mockProviderCreator{name: "c"} tests := []struct { in in out out }{ { in: in{providers: []ProviderCreator{}, name: "a"}, out: out{creator: nil}, }, { in: in{providers: []ProviderCreator{a, b, c}, name: "a"}, out: out{creator: a}, }, { in: in{providers: []ProviderCreator{a, b, c}, name: "c"}, out: out{creator: c}, }, } for i, test := range tests { providers = registry.Create(fmt.Sprintf("test %d", i)) for _, p := range test.in.providers { providers.Register(p) } p := Get(test.in.name) if !reflect.DeepEqual(test.out.creator, p) { t.Errorf("#%d: bad creator: want %#v, got %#v", i, test.out.creator, p) } } }
func TestNames(t *testing.T) { type in struct { providers []ProviderCreator } type out struct { names []string } a := mockProviderCreator{name: "a"} b := mockProviderCreator{name: "b"} c := mockProviderCreator{name: "c"} tests := []struct { in in out out }{ { in: in{providers: nil}, out: out{names: []string{}}, }, { in: in{providers: []ProviderCreator{a, b, c}}, out: out{names: []string{"a", "b", "c"}}, }, } for i, test := range tests { providers = registry.Create(fmt.Sprintf("test %d", i)) for _, p := range test.in.providers { providers.Register(p) } names := providers.Names() if !reflect.DeepEqual(test.out.names, names) { t.Errorf("#%d: bad names: want %#v, got %#v", i, test.out.names, names) } } }
// Config represents a set of command line flags that map to a particular OEM. type Config struct { name string flags map[string]string } func (c Config) Name() string { return c.name } func (c Config) Flags() map[string]string { return c.flags } var configs = registry.Create("oem configs") func init() { configs.Register(Config{ name: "azure", flags: map[string]string{ "provider": "noop", }, }) configs.Register(Config{ name: "cloudsigma", flags: map[string]string{ "provider": "noop", }, }) configs.Register(Config{
// or not the source is online, if the caller should try again when the source // is offline, and how long the caller should wait before retries. type Provider interface { Name() string FetchConfig() (config.Config, error) IsOnline() bool ShouldRetry() bool BackoffDuration() time.Duration } type ProviderCreator interface { Name() string Create(logger log.Logger) Provider } var providers = registry.Create("providers") func Register(provider ProviderCreator) { providers.Register(provider) } func Get(name string) ProviderCreator { if p, ok := providers.Get(name).(ProviderCreator); ok { return p } return nil } func Names() []string { return providers.Names() }
func (e Engine) Init() Engine { e.providers = registry.Create("engine.providers") return e }
) // Stage is responsible for actually executing a stage of the configuration. type Stage interface { Run(config config.Config) bool Name() string } // StageCreator is responsible for instantiating a particular stage given a // logger and root path under the root partition. type StageCreator interface { Create(logger *log.Logger, root string) Stage Name() string } var stages = registry.Create("stages") func Register(stage StageCreator) { stages.Register(stage) } func Get(name string) StageCreator { if s, ok := stages.Get(name).(StageCreator); ok { return s } return nil } func Names() (names []string) { return stages.Names() }