コード例 #1
0
ファイル: ping_server.go プロジェクト: xtracdev/xavi
//Run executes the PingServer command using the supplied args
func (ps *PingServer) Run(args []string) int {
	//Assume we get os.Args[1:] as the input via the CLI
	if len(args) != 1 {
		ps.UI.Error(ps.Help())
		return 1
	}

	log.Info("pinging ", args[0])

	//Read the definition from the key store
	key := "servers/" + args[0]
	log.Info("Read key " + key)
	bv, err := ps.KVStore.Get(key)
	if err != nil {
		ps.UI.Error("Error reading key: " + err.Error())
		return 1
	}

	if bv == nil {
		ps.UI.Error("No server definition exists named " + key)
		return 1
	}

	serverDef := config.JSONToServer(bv)
	if serverDef.PingURI == "" {
		ps.UI.Error("No ping uri specified for server")
		return 1
	}

	//Form the url
	url := fmt.Sprintf("http://%s:%d%s",
		serverDef.Address,
		serverDef.Port,
		serverDef.PingURI)

	//Ping the server
	resp, err := http.Get(url)
	defer resp.Body.Close()
	if err != nil {
		ps.UI.Error(err.Error())
		return 1
	}

	ps.UI.Info("server ping ok")
	return 0

}
コード例 #2
0
ファイル: add_server_test.go プロジェクト: xtracdev/xavi
func TestAddServer(t *testing.T) {

	_, addServer := testMakeAddServer(false)

	args := []string{"-address", "an-address", "-port", "42", "-name", "test-name", "-ping-uri", "/dev/null",
		"-health-check", "http-get", "-health-check-interval", "42", "-health-check-timeout", "10"}
	addServer.Run(args)
	storedBytes, err := addServer.KVStore.Get("servers/test-name")
	assert.Nil(t, err)

	s := config.JSONToServer(storedBytes)
	assert.Equal(t, "test-name", s.Name)
	assert.Equal(t, "an-address", s.Address)
	assert.Equal(t, 42, s.Port)
	assert.Equal(t, "/dev/null", s.PingURI)
	assert.Equal(t, "http-get", s.HealthCheck)
	assert.Equal(t, 42, s.HealthCheckInterval)
	assert.Equal(t, 10, s.HealthCheckTimeout)
}