Esempio n. 1
0
func TestStaleErrorFormatting(t *testing.T) {
	s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusUnauthorized)
	}))
	defer s.Close()

	rpcsvc := revrpc.MustService(s.URL + "/test")
	a := newAuth(10 * time.Second)
	wg := sync.WaitGroup{}
	wg.Add(1)
	go func() {
		oldDef := revrpc.DefaultBabysitErrorPolicy.(revrpc.DefaultErrorPolicy)
		defer func() {
			revrpc.DefaultBabysitErrorPolicy = oldDef
		}()
		tmpDef := oldDef
		tmpDef.RestartsToExit = 1
		revrpc.DefaultBabysitErrorPolicy = tmpDef
		runRPCForSvc(rpcsvc, a.svc)
		wg.Done()
	}()

	_, err := a.Auth("", "")
	se, ok := err.(*DBStaleError)
	if !ok {
		t.Fatalf("Expected stale error. Got: %s", err)
	}
	errString := se.Error()
	t.Log("error string: ", errString)
	expectedString := "CBAuth database is stale: last reason: Need 200 status!. Got "
	if errString[:len(expectedString)] != expectedString {
		t.Fatalf("Expecting specific prefix of stale error. Got %s", errString)
	}
	wg.Wait()
}
Esempio n. 2
0
// InternalRetryDefaultInit can be used by golang services that are
// willing to perform manual initialization of cbauth (i.e. for easier
// testing). This API is subject to change and should be used only if
// really needed. Returns false if Default Authenticator was already
// initialized.
func InternalRetryDefaultInit(mgmtHostPort, user, password string) (bool, error) {
	if Default != nil {
		return false, nil
	}
	serviceName := filepath.Base(os.Args[0]) + "-cbauth"
	host, port, err := SplitHostPort(mgmtHostPort)
	if err != nil {
		return false, nil
	}
	baseurl := fmt.Sprintf("http://%s:%d/%s", host, port, serviceName)
	u, err := url.Parse(baseurl)
	if err != nil {
		return false, fmt.Errorf("Failed to parse constructed url `%s': %s", baseurl, err)
	}
	u.User = url.UserPassword(user, password)

	startDefault(revrpc.MustService(u.String()))

	return true, nil
}