Exemplo n.º 1
0
func (s *ServerSuite) TestNewServer() {
	configInvalid := provider.NewConfig(nil, nil)

	server, err := provider.NewServer(configInvalid)
	s.Nil(server, "should not create server with invalid config")
	s.Error(err, "should error with invalid config")
}
Exemplo n.º 2
0
func (s *ConfigSuite) TestNewConfig() {
	tests := []struct {
		description string
		flagSet     *flag.FlagSet
		viper       *viper.Viper
	}{
		{"defaults", nil, nil},
		{"specified flagset", flag.NewFlagSet(uuid.New(), flag.ExitOnError), nil},
		{"specified viper", nil, viper.New()},
		{"specified both", flag.NewFlagSet(uuid.New(), flag.ExitOnError), viper.New()},
	}

	for _, test := range tests {
		msg := testMsgFunc(test.description)
		// Reset the default CommandLine flags between runs
		flag.CommandLine = flag.NewFlagSet(uuid.New(), flag.ExitOnError)
		s.NotNil(provider.NewConfig(test.flagSet, test.viper), msg("failed to return a config"))
	}
}
Exemplo n.º 3
0
func newConfig(setFlags, writeConfig bool, configData *provider.ConfigData) (*provider.Config, *flag.FlagSet, *viper.Viper, *os.File, error) {
	fs := flag.NewFlagSet(uuid.New(), flag.ExitOnError)
	v := viper.New()
	v.SetConfigType("json")
	config := provider.NewConfig(fs, v)
	if config == nil {
		return nil, nil, nil, nil, errors.New("failed to return a config")
	}

	var configFile *os.File
	if writeConfig {
		var err error
		configFile, err = ioutil.TempFile("", "providerTest-")
		if err != nil {
			return nil, nil, nil, nil, err
		}
		defer func() { _ = configFile.Close() }()

		configJSON, _ := json.Marshal(configData)
		if _, err := configFile.Write(configJSON); err != nil {
			return nil, nil, nil, configFile, err
		}

		if err := fs.Set("config_file", configFile.Name()); err != nil {
			return nil, nil, nil, configFile, err
		}
	}

	if setFlags {
		if err := fs.Set("socket_dir", configData.SocketDir); err != nil {
			return nil, nil, nil, configFile, err
		}
		if err := fs.Set("service_name", configData.ServiceName); err != nil {
			return nil, nil, nil, configFile, err
		}
		if err := fs.Set("coordinator_url", configData.CoordinatorURL); err != nil {
			return nil, nil, nil, configFile, err
		}
	}

	return config, fs, v, configFile, nil
}
Exemplo n.º 4
0
func main() {
	log.SetFormatter(&logx.MistifyFormatter{})

	config := provider.NewConfig(nil, nil)

	flag.StringP("foobar", "f", "", "path to config file")

	flag.Parse()

	dieOnError(config.LoadConfig())
	dieOnError(config.SetupLogging())

	server, err := provider.NewServer(config)
	dieOnError(err)
	s := simple.NewSimple(config, server.Tracker())
	s.RegisterTasks(server)

	if len(server.RegisteredTasks()) != 0 {
		dieOnError(server.Start())
		server.StopOnSignal()
	} else {
		log.Warn("no registered tasks, exiting")
	}
}