Merge pull request #2067 from yichengq/284

add tests in pkg/transport package
release-2.0
Yicheng Qin 2015-01-10 13:13:23 -08:00
commit aec2eef498
4 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,39 @@
/*
Copyright 2015 CoreOS, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package transport
import (
"net/http"
"testing"
)
// TestNewKeepAliveListener tests NewKeepAliveListener returns a listener
// that accepts connections.
// TODO: verify the keepalive option is set correctly
func TestNewKeepAliveListener(t *testing.T) {
ln, err := NewKeepAliveListener(":0", "http", TLSInfo{})
if err != nil {
t.Fatalf("unexpected NewKeepAliveListener error: %v", err)
}
go http.Get("http://" + ln.Addr().String())
conn, err := ln.Accept()
if err != nil {
t.Fatalf("unexpected Accept error: %v", err)
}
conn.Close()
}

View File

@ -20,6 +20,7 @@ import (
"crypto/tls"
"errors"
"io/ioutil"
"net/http"
"os"
"testing"
)
@ -44,6 +45,46 @@ func fakeCertificateParserFunc(cert tls.Certificate, err error) func(certPEMBloc
}
}
// TestNewListenerTLSInfo tests that NewListener with valid TLSInfo returns
// a TLS listerner that accepts TLS connections.
func TestNewListenerTLSInfo(t *testing.T) {
tmp, err := createTempFile([]byte("XXX"))
if err != nil {
t.Fatalf("unable to create tmpfile: %v", err)
}
defer os.Remove(tmp)
tlsInfo := TLSInfo{CertFile: tmp, KeyFile: tmp}
tlsInfo.parseFunc = fakeCertificateParserFunc(tls.Certificate{}, nil)
ln, err := NewListener(":0", "https", tlsInfo)
if err != nil {
t.Fatalf("unexpected NewListener error: %v", err)
}
defer ln.Close()
go http.Get("https://" + ln.Addr().String())
conn, err := ln.Accept()
if err != nil {
t.Fatalf("unexpected Accept error: %v", err)
}
defer conn.Close()
if _, ok := conn.(*tls.Conn); !ok {
t.Errorf("failed to accept *tls.Conn")
}
}
func TestNewListenerTLSInfoNonexist(t *testing.T) {
tlsInfo := TLSInfo{CertFile: "@badname", KeyFile: "@badname"}
_, err := NewListener(":0", "https", tlsInfo)
werr := &os.PathError{
Op: "open",
Path: "@badname",
Err: errors.New("no such file or directory"),
}
if err.Error() != werr.Error() {
t.Errorf("err = %v, want %v", err, werr)
}
}
func TestNewTransportTLSInfo(t *testing.T) {
tmp, err := createTempFile([]byte("XXX"))
if err != nil {

View File

@ -22,6 +22,23 @@ import (
"time"
)
// TestNewTimeoutListener tests that NewTimeoutListener returns a
// rwTimeoutListener struct with timeouts set.
func TestNewTimeoutListener(t *testing.T) {
l, err := NewTimeoutListener(":0", "http", TLSInfo{}, time.Hour, time.Hour)
if err != nil {
t.Fatalf("unexpected NewTimeoutListener error: %v", err)
}
defer l.Close()
tln := l.(*rwTimeoutListener)
if tln.rdtimeoutd != time.Hour {
t.Errorf("read timeout = %s, want %s", tln.rdtimeoutd, time.Hour)
}
if tln.wtimeoutd != time.Hour {
t.Errorf("write timeout = %s, want %s", tln.wtimeoutd, time.Hour)
}
}
func TestWriteReadTimeoutListener(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {

View File

@ -0,0 +1,51 @@
/*
Copyright 2014 CoreOS, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package transport
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
// TestNewTimeoutTransport tests that NewTimeoutTransport returns a transport
// that can dial out timeout connections.
func TestNewTimeoutTransport(t *testing.T) {
tr, err := NewTimeoutTransport(TLSInfo{}, time.Hour, time.Hour)
if err != nil {
t.Fatalf("unexpected NewTimeoutTransport error: %v", err)
}
srv := httptest.NewServer(http.NotFoundHandler())
defer srv.Close()
conn, err := tr.Dial("tcp", srv.Listener.Addr().String())
if err != nil {
t.Fatalf("unexpected dial error: %v", err)
}
defer conn.Close()
tconn, ok := conn.(*timeoutConn)
if !ok {
t.Fatalf("failed to dial out *timeoutConn")
}
if tconn.rdtimeoutd != time.Hour {
t.Errorf("read timeout = %s, want %s", tconn.rdtimeoutd, time.Hour)
}
if tconn.wtimeoutd != time.Hour {
t.Errorf("write timeout = %s, want %s", tconn.wtimeoutd, time.Hour)
}
}