Beispiel #1
0
func TestPrepareInheriteFiles(t *testing.T) {
	var n Net

	port1, err := freeport.Get()
	ensure.Nil(t, err)
	addr1 := fmt.Sprintf(":%d", port1)
	l1, err := net.Listen("tcp", addr1)
	ensure.Nil(t, err)

	port2, err := freeport.Get()
	ensure.Nil(t, err)
	addr2 := fmt.Sprintf(":%d", port2)
	l2, err := net.Listen("tcp", addr2)
	ensure.Nil(t, err)

	files, err := n.prepareInheriteFiles([]net.Listener{l1, l2})
	ensure.Nil(t, err)
	ensure.DeepEqual(t, len(files), 2)

	// assign both to prevent GC from kicking in the finalizer
	fds := []int{dup(t, int(files[0].Fd())), dup(t, int(files[0].Fd()))}
	n.fdStart = fds[0]
	os.Setenv(envCountKey, "2")
	// Close these after to ensure we get coalaced file descriptors.
	ensure.Nil(t, l1.Close())
	ensure.Nil(t, l2.Close())

	ensure.Nil(t, n.inherit())
	ensure.DeepEqual(t, len(n.inherited), 2)
}
Beispiel #2
0
// Find 3 free ports and setup addresses.
func (h *harness) setupAddr() {
	port, err := freeport.Get()
	if err != nil {
		h.T.Fatalf("Failed to find a free port: %s", err)
	}
	h.httpAddr = fmt.Sprintf("127.0.0.1:%d", port)

	port, err = freeport.Get()
	if err != nil {
		h.T.Fatalf("Failed to find a free port: %s", err)
	}
	h.httpsAddr = fmt.Sprintf("127.0.0.1:%d", port)
	debug("Addresses %s & %s", h.httpAddr, h.httpsAddr)
}
Beispiel #3
0
func TestTwoTCP(t *testing.T) {
	var n Net

	port1, err := freeport.Get()
	ensure.Nil(t, err)
	addr1 := fmt.Sprintf(":%d", port1)
	l1, err := net.Listen("tcp", addr1)
	ensure.Nil(t, err)

	port2, err := freeport.Get()
	ensure.Nil(t, err)
	addr2 := fmt.Sprintf(":%d", port2)
	l2, err := net.Listen("tcp", addr2)
	ensure.Nil(t, err)

	file1, err := l1.(*net.TCPListener).File()
	ensure.Nil(t, err)
	file2, err := l2.(*net.TCPListener).File()
	ensure.Nil(t, err)

	// assign both to prevent GC from kicking in the finalizer
	fds := []int{dup(t, int(file1.Fd())), dup(t, int(file2.Fd()))}
	n.fdStart = fds[0]
	os.Setenv(envCountKey, "2")

	// Close these after to ensure we get coalaced file descriptors.
	ensure.Nil(t, l1.Close())
	ensure.Nil(t, l2.Close())

	ensure.Nil(t, n.inherit())
	ensure.DeepEqual(t, len(n.inherited), 2)

	l1, err = n.Listen("tcp", addr1)
	ensure.Nil(t, err)
	ensure.DeepEqual(t, len(n.active), 1)
	ensure.DeepEqual(t, n.inherited[0], nil)
	ensure.Nil(t, l1.Close())
	ensure.Nil(t, file1.Close())

	l2, err = n.Listen("tcp", addr2)
	ensure.Nil(t, err)
	ensure.DeepEqual(t, len(n.active), 2)
	ensure.DeepEqual(t, n.inherited[1], nil)
	ensure.Nil(t, l2.Close())
	ensure.Nil(t, file2.Close())
}
Beispiel #4
0
func TestTLS(t *testing.T) {
	t.Parallel()
	port, err := freeport.Get()
	ensure.Nil(t, err)

	cert, err := tls.X509KeyPair(localhostCert, localhostKey)
	if err != nil {
		t.Fatalf("error loading cert: %v", err)
	}
	const count = 10000
	hello := []byte("hello")
	finOkHandler := make(chan struct{})
	okHandler := func(w http.ResponseWriter, r *http.Request) {
		defer close(finOkHandler)
		w.WriteHeader(200)
		for i := 0; i < count; i++ {
			w.Write(hello)
		}
	}

	server := &http.Server{
		Addr:    fmt.Sprintf("0.0.0.0:%d", port),
		Handler: http.HandlerFunc(okHandler),
		TLSConfig: &tls.Config{
			NextProtos:   []string{"http/1.1"},
			Certificates: []tls.Certificate{cert},
		},
	}
	transport := &http.Transport{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
	}
	client := &http.Client{Transport: transport}
	down := &httpdown.HTTP{}
	s, err := down.ListenAndServe(server)
	ensure.Nil(t, err)
	res, err := client.Get(fmt.Sprintf("https://%s/", server.Addr))
	ensure.Nil(t, err)

	finStop := make(chan struct{})
	go func() {
		defer close(finStop)
		ensure.Nil(t, s.Stop())
	}()

	actualBody, err := ioutil.ReadAll(res.Body)
	ensure.Nil(t, err)
	ensure.DeepEqual(t, actualBody, bytes.Repeat(hello, count))
	ensure.Nil(t, res.Body.Close())
	<-finOkHandler
	<-finStop
}
func TestSafeRetry(t *testing.T) {
	t.Parallel()
	port, err := freeport.Get()
	if err != nil {
		t.Fatal(err)
	}
	addr := fmt.Sprintf("127.0.0.1:%d", port)
	server := httptest.NewUnstartedServer(sleepHandler(time.Millisecond))
	transport := &httpcontrol.Transport{
		MaxTries: 2,
	}
	first := false
	second := false
	transport.Stats = func(stats *httpcontrol.Stats) {
		if !first {
			first = true
			if stats.Error == nil {
				t.Fatal("was expecting error")
			}
			if !stats.Retry.Pending {
				t.Fatal("was expecting pending retry", stats.Error)
			}
			server.Listener, err = net.Listen("tcp", addr)
			if err != nil {
				t.Fatal(err)
			}
			server.Start()
			return
		}

		if !second {
			second = true
			if stats.Error != nil {
				t.Fatal(stats.Error, server.URL)
			}
			return
		}
	}
	defer call(transport.Close, t)
	client := &http.Client{Transport: transport}
	res, err := client.Get(fmt.Sprintf("http://%s/", addr))
	if err != nil {
		t.Fatal(err)
	}
	assertResponse(res, t)
	if !first {
		t.Fatal("did not see first request")
	}
	if !second {
		t.Fatal("did not see second request")
	}
}
Beispiel #6
0
func NewServer(t Fatalf) *Server {
	port, err := freeport.Get()
	if err != nil {
		t.Fatalf("failed to find freeport: %s", err)
	}
	s := &Server{
		Port: port,
		T:    t,
	}
	err = s.Start()
	if err != nil {
		t.Fatalf("failed to find freeport: %s", err)
	}
	return s
}
Beispiel #7
0
func TestOneUnixAndOneTcpInherit(t *testing.T) {
	var n Net

	tmpfile, err := ioutil.TempFile("", "TestOneUnixAndOneTcpInherit-")
	ensure.Nil(t, err)
	ensure.Nil(t, tmpfile.Close())
	ensure.Nil(t, os.Remove(tmpfile.Name()))
	defer os.Remove(tmpfile.Name())
	unixL, err := net.Listen("unix", tmpfile.Name())
	ensure.Nil(t, err)

	port, err := freeport.Get()
	ensure.Nil(t, err)
	addr := fmt.Sprintf(":%d", port)
	tcpL, err := net.Listen("tcp", addr)
	ensure.Nil(t, err)

	tcpF, err := tcpL.(*net.TCPListener).File()
	ensure.Nil(t, err)
	unixF, err := unixL.(*net.UnixListener).File()
	ensure.Nil(t, err)

	// assign both to prevent GC from kicking in the finalizer
	fds := []int{dup(t, int(tcpF.Fd())), dup(t, int(unixF.Fd()))}
	n.fdStart = fds[0]
	os.Setenv(envCountKey, "2")

	// Close these after to ensure we get coalaced file descriptors.
	ensure.Nil(t, tcpL.Close())
	ensure.Nil(t, unixL.Close())

	ensure.Nil(t, n.inherit())
	ensure.DeepEqual(t, len(n.inherited), 2)

	unixL, err = n.Listen("unix", tmpfile.Name())
	ensure.Nil(t, err)
	ensure.DeepEqual(t, len(n.active), 1)
	ensure.DeepEqual(t, n.inherited[1], nil)
	ensure.Nil(t, unixL.Close())
	ensure.Nil(t, unixF.Close())

	tcpL, err = n.Listen("tcp", addr)
	ensure.Nil(t, err)
	ensure.DeepEqual(t, len(n.active), 2)
	ensure.DeepEqual(t, n.inherited[0], nil)
	ensure.Nil(t, tcpL.Close())
	ensure.Nil(t, tcpF.Close())
}
Beispiel #8
0
// Start the server, this will return once the server has been started.
func (s *Server) Start() {
	port, err := freeport.Get()
	if err != nil {
		s.T.Fatalf(err.Error())
	}
	s.Port = port

	dir, err := ioutil.TempDir("", "mysql-DataDir-")
	if err != nil {
		s.T.Fatalf(err.Error())
	}
	s.DataDir = dir
	s.Socket = filepath.Join(dir, "socket")

	cf, err := os.Create(filepath.Join(dir, "my.cnf"))
	if err != nil {
		s.T.Fatalf(err.Error())
	}
	if err := configTemplate.Execute(cf, s); err != nil {
		s.T.Fatalf(err.Error())
	}
	if err := cf.Close(); err != nil {
		s.T.Fatalf(err.Error())
	}

	defaultsFile := fmt.Sprintf("--defaults-file=%s", cf.Name())
	s.cmd = exec.Command("mysql_install_db", defaultsFile, "--basedir", mysqlBaseDir)
	if os.Getenv("MYSQLTEST_VERBOSE") == "1" {
		s.cmd.Stdout = os.Stdout
		s.cmd.Stderr = os.Stderr
	}
	if err := s.cmd.Run(); err != nil {
		s.T.Fatalf(err.Error())
	}

	waiter := waitout.New(mysqlReadyForConnections)
	s.cmd = exec.Command("mysqld", defaultsFile, "--basedir", mysqlBaseDir)
	if os.Getenv("MYSQLTEST_VERBOSE") == "1" {
		s.cmd.Stdout = os.Stdout
		s.cmd.Stderr = io.MultiWriter(os.Stderr, waiter)
	} else {
		s.cmd.Stderr = waiter
	}
	if err := s.cmd.Start(); err != nil {
		s.T.Fatalf(err.Error())
	}
	waiter.Wait()
}
Beispiel #9
0
func (h *Harness) start() {
	var err error
	if h.port == 0 {
		if h.port, err = freeport.Get(); err != nil {
			h.t.Fatal(err)
		}
	}

	cf, err := ioutil.TempFile("", "gmetric_test_gmond_conf")
	if err != nil {
		h.t.Fatal(err)
	}
	h.configPath = cf.Name()

	td := struct{ Port int }{Port: h.port}
	if err := configTemplate.Execute(cf, td); err != nil {
		h.t.Fatal(err)
	}

	if err := cf.Close(); err != nil {
		h.t.Fatal(err)
	}

	waiter := waitout.New(gmondServerStarted)
	h.cmd = exec.Command("gmond", "--conf", h.configPath)
	if os.Getenv("GMONDTEST_VERBOSE") == "1" {
		h.cmd.Stderr = io.MultiWriter(os.Stderr, waiter)
		h.cmd.Stdout = os.Stdout
	} else {
		h.cmd.Stderr = waiter
	}
	if err := h.cmd.Start(); err != nil {
		h.t.Fatal(err)
	}
	waiter.Wait()

	h.Client = &gmetric.Client{
		Addr: []net.Addr{
			&net.UDPAddr{IP: net.ParseIP(localhostIP), Port: h.port},
		},
	}

	if err := h.Client.Open(); err != nil {
		h.t.Fatal(err)
	}
}
Beispiel #10
0
// Start the server, this will return once the server has been started.
func (s *Server) Start(args ...string) {
	if s.Port == 0 {
		port, err := freeport.Get()
		if err != nil {
			s.T.Fatalf(err.Error())
		}
		s.Port = port
	}
	if s.testName == "" {
		s.T.Fatalf("Cannot determine name for test")
	}
	dir, err := ioutil.TempDir("", "mgotest-dbpath-"+s.testName)
	if err != nil {
		s.T.Fatalf(err.Error())
	}
	s.DBPath = dir

	cf, err := ioutil.TempFile(s.DBPath, "config-")
	if err != nil {
		s.T.Fatalf(err.Error())
	}
	if err := configTemplate.Execute(cf, s); err != nil {
		s.T.Fatalf(err.Error())
	}
	if err := cf.Close(); err != nil {
		s.T.Fatalf(err.Error())
	}

	waiter := waitout.New(mgoWaitingForConnections)
	args = append(args, "--config", cf.Name(), "--setParameter", "enableTestCommands=1")
	s.cmd = exec.Command("mongod", args...)
	s.cmd.Env = envPlusLcAll()
	if os.Getenv("MGOTEST_VERBOSE") == "1" {
		s.cmd.Stdout = io.MultiWriter(os.Stdout, waiter)
		s.cmd.Stderr = os.Stderr
	} else {
		s.cmd.Stdout = waiter
	}
	if err := s.cmd.Start(); err != nil {
		s.T.Fatalf(err.Error())
	}
	waiter.Wait()
}
Beispiel #11
0
func TestWithTcp0000(t *testing.T) {
	var n Net
	port, err := freeport.Get()
	ensure.Nil(t, err)
	addr := fmt.Sprintf("0.0.0.0:%d", port)
	l, err := net.Listen("tcp", addr)
	ensure.Nil(t, err)
	file, err := l.(*net.TCPListener).File()
	ensure.Nil(t, err)
	ensure.Nil(t, l.Close())
	n.fdStart = dup(t, int(file.Fd()))
	ensure.Nil(t, file.Close())
	os.Setenv(envCountKey, "1")
	ensure.Nil(t, n.inherit())
	ensure.DeepEqual(t, len(n.inherited), 1)
	l, err = n.Listen("tcp", addr)
	ensure.Nil(t, err)
	ensure.DeepEqual(t, len(n.active), 1)
	ensure.DeepEqual(t, n.inherited[0], nil)
	ensure.Nil(t, l.Close())
}
func TestSafeRetryAfterTimeout(t *testing.T) {
	t.Parallel()
	port, err := freeport.Get()
	if err != nil {
		t.Fatal(err)
	}
	addr := fmt.Sprintf("127.0.0.1:%d", port)
	server := httptest.NewUnstartedServer(sleepHandler(5 * time.Second))
	transport := &httpcontrol.Transport{
		MaxTries:          3,
		RequestTimeout:    5 * time.Millisecond,
		RetryAfterTimeout: true,
	}
	first := false
	second := false
	third := false
	transport.Stats = func(stats *httpcontrol.Stats) {
		if !first {
			first = true
			if stats.Error == nil {
				t.Fatal("was expecting error")
			}
			if !stats.Retry.Pending {
				t.Fatal("was expecting pending retry", stats.Error)
			}
			server.Listener, err = net.Listen("tcp", addr)
			if err != nil {
				t.Fatal(err)
			}
			server.Start()
			return
		}

		if !second {
			second = true
			if stats.Error == nil {
				t.Fatal("was expecting error")
			}
			if !stats.Retry.Pending {
				t.Fatal("was expecting pending retry", stats.Error)
			}
			return
		}

		if !third {
			third = true
			if stats.Error == nil {
				t.Fatal("was expecting error")
			}
			if !stats.Retry.Pending {
				t.Fatal("was expecting pending retry", stats.Error)
			}
		}
	}
	client := &http.Client{Transport: transport}
	_, err = client.Get(fmt.Sprintf("http://%s/", addr))

	// Expect this to fail
	if err == nil {
		t.Fatal(err)
	}

	if !first {
		t.Fatal("did not see first request")
	}
	if !second {
		t.Fatal("did not see second request")
	}

	if !third {
		t.Fatal("did not see third request")
	}
}