Beispiel #1
0
func (s *ServerSuite) TestNewServer() {
	configInvalid := coordinator.NewConfig(nil, nil)

	server, err := coordinator.NewServer(configInvalid)
	s.Nil(server, "should not create server with invalid config")
	s.Error(err, "should error with invalid config")
}
Beispiel #2
0
func main() {
	log.SetFormatter(&logx.MistifyFormatter{})

	config := coordinator.NewConfig(nil, nil)
	flag.Parse()

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

	server, err := coordinator.NewServer(config)
	dieOnError(err)

	dieOnError(server.Start())
	server.StopOnSignal()
}
Beispiel #3
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(coordinator.NewConfig(test.flagSet, test.viper), msg("failed to return a config"))
	}
}
Beispiel #4
0
func newConfig(setFlags, writeConfig bool, configData *coordinator.ConfigData) (*coordinator.Config, *flag.FlagSet, *viper.Viper, *os.File, error) {
	fs := flag.NewFlagSet(uuid.New(), flag.ExitOnError)
	v := viper.New()
	v.SetConfigType("json")
	config := coordinator.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("", "coordinatorTest-")
		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("external_port", strconv.FormatUint(uint64(configData.ExternalPort), 10)); err != nil {
			return nil, nil, nil, configFile, err
		}
	}

	return config, fs, v, configFile, nil
}