Esempio n. 1
0
// apiGet wraps a GET request with a status code check, such that if the GET does
// not return 2xx, the error will be read and returned. The response body is
// not closed.
func apiGet(call string) (*http.Response, error) {
	if host, port, _ := net.SplitHostPort(addr); host == "" {
		addr = net.JoinHostPort("localhost", port)
	}
	resp, err := api.HttpGET("http://" + addr + call)
	if err != nil {
		return nil, errors.New("no response from daemon")
	}
	// check error code
	if resp.StatusCode == http.StatusUnauthorized {
		resp.Body.Close()
		// Prompt for password and retry request with authentication.
		password, err := speakeasy.Ask("API password: "******"http://"+addr+call, password)
		if err != nil {
			return nil, errors.New("no response from daemon - authentication failed")
		}
	}
	if resp.StatusCode == http.StatusNotFound {
		resp.Body.Close()
		return nil, errors.New("API call not recognized: " + call)
	}
	if non2xx(resp.StatusCode) {
		err := decodeError(resp)
		resp.Body.Close()
		return nil, err
	}
	return resp, nil
}
Esempio n. 2
0
// TestStartDaemon probes the startDaemon function.
func TestStartDaemon(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}

	testDir := build.TempDir("siad", "TestStartDaemon")
	config.Siad.NoBootstrap = false
	config.Siad.APIaddr = "localhost:45370"
	config.Siad.RPCaddr = ":0"
	config.Siad.SiaDir = testDir
	go func() {
		err := startDaemon()
		if err != nil {
			t.Error(err)
		}
	}()

	// Wait until the server has started, and then send a kill command to the
	// daemon.
	<-started
	time.Sleep(250 * time.Millisecond)
	resp, err := api.HttpGET("http://localhost:45370/daemon/stop")
	if err != nil {
		t.Fatal(err)
	}
	if resp.StatusCode != http.StatusOK {
		t.Fatal(resp.StatusCode)
	}
	resp.Body.Close()
}
Esempio n. 3
0
File: main.go Progetto: zzmjohn/Sia
// apiGet wraps a GET request with a status code check, such that if the GET does
// not return 200, the error will be read and returned. The response body is
// not closed.
func apiGet(call string) (*http.Response, error) {
	if host, port, _ := net.SplitHostPort(addr); host == "" {
		addr = net.JoinHostPort("localhost", port)
	}
	resp, err := api.HttpGET("http://" + addr + call)
	if err != nil {
		return nil, errors.New("no response from daemon")
	}
	// check error code
	if resp.StatusCode == http.StatusNotFound {
		resp.Body.Close()
		err = errors.New("API call not recognized: " + call)
	} else if resp.StatusCode != http.StatusOK {
		errResp, _ := ioutil.ReadAll(resp.Body)
		resp.Body.Close()
		err = errors.New(strings.TrimSpace(string(errResp)))
	}
	return resp, err
}
Esempio n. 4
0
// TestMain tries running the main executable using a few different commands.
func TestMain(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}

	testDir := build.TempDir("siad", "TestMain")

	// Try running and closing an instance of siad.
	os.Args = []string{
		"siad",
		"-n",
		"-a",
		"localhost:45350",
		"-r",
		":0",
		"-d",
		testDir,
	}
	go main()

	// Wait until the daemon has started and then send a kill signal to the
	// daemon.
	<-started
	time.Sleep(250 * time.Millisecond)
	resp, err := api.HttpGET("http://localhost:45350/daemon/stop")
	if err != nil {
		t.Fatal(err)
	}
	if resp.StatusCode != http.StatusOK {
		t.Fatal(resp.StatusCode)
	}
	resp.Body.Close()

	// Try to run the siad version command.
	os.Args = []string{
		"siad",
		"version",
	}
	main()
}