mirror of https://github.com/k3s-io/k3s
followup to add http server close method
parent
43b644ea6f
commit
5923fd352e
|
@ -141,8 +141,8 @@ func TestInstallAPIGroups(t *testing.T) {
|
||||||
}
|
}
|
||||||
s.InstallAPIGroups(apiGroupsInfo)
|
s.InstallAPIGroups(apiGroupsInfo)
|
||||||
|
|
||||||
// TODO: Close() this server when fix #19254
|
|
||||||
server := httptest.NewServer(s.HandlerContainer.ServeMux)
|
server := httptest.NewServer(s.HandlerContainer.ServeMux)
|
||||||
|
defer server.Close()
|
||||||
validPaths := []string{
|
validPaths := []string{
|
||||||
// "/api"
|
// "/api"
|
||||||
config.APIPrefix,
|
config.APIPrefix,
|
||||||
|
|
|
@ -214,7 +214,6 @@ func newServerTest() *serverTestFramework {
|
||||||
true,
|
true,
|
||||||
&kubecontainertesting.Mock{})
|
&kubecontainertesting.Mock{})
|
||||||
fw.serverUnderTest = &server
|
fw.serverUnderTest = &server
|
||||||
// TODO: Close() this when fix #19254
|
|
||||||
fw.testHTTPServer = httptest.NewServer(fw.serverUnderTest)
|
fw.testHTTPServer = httptest.NewServer(fw.serverUnderTest)
|
||||||
return fw
|
return fw
|
||||||
}
|
}
|
||||||
|
@ -244,6 +243,7 @@ func getPodName(name, namespace string) string {
|
||||||
|
|
||||||
func TestContainerInfo(t *testing.T) {
|
func TestContainerInfo(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
expectedInfo := &cadvisorapi.ContainerInfo{}
|
expectedInfo := &cadvisorapi.ContainerInfo{}
|
||||||
podID := "somepod"
|
podID := "somepod"
|
||||||
expectedPodID := getPodName(podID, "")
|
expectedPodID := getPodName(podID, "")
|
||||||
|
@ -272,6 +272,7 @@ func TestContainerInfo(t *testing.T) {
|
||||||
|
|
||||||
func TestContainerInfoWithUidNamespace(t *testing.T) {
|
func TestContainerInfoWithUidNamespace(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
expectedInfo := &cadvisorapi.ContainerInfo{}
|
expectedInfo := &cadvisorapi.ContainerInfo{}
|
||||||
podID := "somepod"
|
podID := "somepod"
|
||||||
expectedNamespace := "custom"
|
expectedNamespace := "custom"
|
||||||
|
@ -302,6 +303,7 @@ func TestContainerInfoWithUidNamespace(t *testing.T) {
|
||||||
|
|
||||||
func TestContainerNotFound(t *testing.T) {
|
func TestContainerNotFound(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
podID := "somepod"
|
podID := "somepod"
|
||||||
expectedNamespace := "custom"
|
expectedNamespace := "custom"
|
||||||
expectedContainerName := "slowstartcontainer"
|
expectedContainerName := "slowstartcontainer"
|
||||||
|
@ -321,6 +323,7 @@ func TestContainerNotFound(t *testing.T) {
|
||||||
|
|
||||||
func TestRootInfo(t *testing.T) {
|
func TestRootInfo(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
expectedInfo := &cadvisorapi.ContainerInfo{
|
expectedInfo := &cadvisorapi.ContainerInfo{
|
||||||
ContainerReference: cadvisorapi.ContainerReference{
|
ContainerReference: cadvisorapi.ContainerReference{
|
||||||
Name: "/",
|
Name: "/",
|
||||||
|
@ -349,6 +352,7 @@ func TestRootInfo(t *testing.T) {
|
||||||
|
|
||||||
func TestSubcontainerContainerInfo(t *testing.T) {
|
func TestSubcontainerContainerInfo(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
const kubeletContainer = "/kubelet"
|
const kubeletContainer = "/kubelet"
|
||||||
const kubeletSubContainer = "/kubelet/sub"
|
const kubeletSubContainer = "/kubelet/sub"
|
||||||
expectedInfo := map[string]*cadvisorapi.ContainerInfo{
|
expectedInfo := map[string]*cadvisorapi.ContainerInfo{
|
||||||
|
@ -394,6 +398,7 @@ func TestSubcontainerContainerInfo(t *testing.T) {
|
||||||
|
|
||||||
func TestMachineInfo(t *testing.T) {
|
func TestMachineInfo(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
expectedInfo := &cadvisorapi.MachineInfo{
|
expectedInfo := &cadvisorapi.MachineInfo{
|
||||||
NumCores: 4,
|
NumCores: 4,
|
||||||
MemoryCapacity: 1024,
|
MemoryCapacity: 1024,
|
||||||
|
@ -419,6 +424,7 @@ func TestMachineInfo(t *testing.T) {
|
||||||
|
|
||||||
func TestServeLogs(t *testing.T) {
|
func TestServeLogs(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
|
|
||||||
content := string(`<pre><a href="kubelet.log">kubelet.log</a><a href="google.log">google.log</a></pre>`)
|
content := string(`<pre><a href="kubelet.log">kubelet.log</a><a href="google.log">google.log</a></pre>`)
|
||||||
|
|
||||||
|
@ -447,6 +453,7 @@ func TestServeLogs(t *testing.T) {
|
||||||
|
|
||||||
func TestServeRunInContainer(t *testing.T) {
|
func TestServeRunInContainer(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
output := "foo bar"
|
output := "foo bar"
|
||||||
podNamespace := "other"
|
podNamespace := "other"
|
||||||
podName := "foo"
|
podName := "foo"
|
||||||
|
@ -487,6 +494,7 @@ func TestServeRunInContainer(t *testing.T) {
|
||||||
|
|
||||||
func TestServeRunInContainerWithUID(t *testing.T) {
|
func TestServeRunInContainerWithUID(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
output := "foo bar"
|
output := "foo bar"
|
||||||
podNamespace := "other"
|
podNamespace := "other"
|
||||||
podName := "foo"
|
podName := "foo"
|
||||||
|
@ -531,6 +539,7 @@ func TestServeRunInContainerWithUID(t *testing.T) {
|
||||||
|
|
||||||
func TestHealthCheck(t *testing.T) {
|
func TestHealthCheck(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
fw.fakeKubelet.hostnameFunc = func() string {
|
fw.fakeKubelet.hostnameFunc = func() string {
|
||||||
return "127.0.0.1"
|
return "127.0.0.1"
|
||||||
}
|
}
|
||||||
|
@ -563,6 +572,7 @@ type authTestCase struct {
|
||||||
|
|
||||||
func TestAuthFilters(t *testing.T) {
|
func TestAuthFilters(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
|
|
||||||
testcases := []authTestCase{}
|
testcases := []authTestCase{}
|
||||||
|
|
||||||
|
@ -665,6 +675,7 @@ func TestAuthenticationFailure(t *testing.T) {
|
||||||
)
|
)
|
||||||
|
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
fw.fakeAuth.authenticateFunc = func(req *http.Request) (user.Info, bool, error) {
|
fw.fakeAuth.authenticateFunc = func(req *http.Request) (user.Info, bool, error) {
|
||||||
calledAuthenticate = true
|
calledAuthenticate = true
|
||||||
return nil, false, nil
|
return nil, false, nil
|
||||||
|
@ -702,6 +713,7 @@ func TestAuthorizationSuccess(t *testing.T) {
|
||||||
)
|
)
|
||||||
|
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
fw.fakeAuth.authenticateFunc = func(req *http.Request) (user.Info, bool, error) {
|
fw.fakeAuth.authenticateFunc = func(req *http.Request) (user.Info, bool, error) {
|
||||||
calledAuthenticate = true
|
calledAuthenticate = true
|
||||||
return expectedUser, true, nil
|
return expectedUser, true, nil
|
||||||
|
@ -730,6 +742,7 @@ func TestAuthorizationSuccess(t *testing.T) {
|
||||||
|
|
||||||
func TestSyncLoopCheck(t *testing.T) {
|
func TestSyncLoopCheck(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
fw.fakeKubelet.hostnameFunc = func() string {
|
fw.fakeKubelet.hostnameFunc = func() string {
|
||||||
return "127.0.0.1"
|
return "127.0.0.1"
|
||||||
}
|
}
|
||||||
|
@ -746,6 +759,7 @@ func TestSyncLoopCheck(t *testing.T) {
|
||||||
|
|
||||||
func TestPLEGHealthCheck(t *testing.T) {
|
func TestPLEGHealthCheck(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
fw.fakeKubelet.hostnameFunc = func() string {
|
fw.fakeKubelet.hostnameFunc = func() string {
|
||||||
return "127.0.0.1"
|
return "127.0.0.1"
|
||||||
}
|
}
|
||||||
|
@ -814,6 +828,7 @@ func setGetContainerLogsFunc(fw *serverTestFramework, t *testing.T, expectedPodN
|
||||||
// TODO: I really want to be a table driven test
|
// TODO: I really want to be a table driven test
|
||||||
func TestContainerLogs(t *testing.T) {
|
func TestContainerLogs(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
output := "foo bar"
|
output := "foo bar"
|
||||||
podNamespace := "other"
|
podNamespace := "other"
|
||||||
podName := "foo"
|
podName := "foo"
|
||||||
|
@ -839,6 +854,7 @@ func TestContainerLogs(t *testing.T) {
|
||||||
|
|
||||||
func TestContainerLogsWithLimitBytes(t *testing.T) {
|
func TestContainerLogsWithLimitBytes(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
output := "foo bar"
|
output := "foo bar"
|
||||||
podNamespace := "other"
|
podNamespace := "other"
|
||||||
podName := "foo"
|
podName := "foo"
|
||||||
|
@ -865,6 +881,7 @@ func TestContainerLogsWithLimitBytes(t *testing.T) {
|
||||||
|
|
||||||
func TestContainerLogsWithTail(t *testing.T) {
|
func TestContainerLogsWithTail(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
output := "foo bar"
|
output := "foo bar"
|
||||||
podNamespace := "other"
|
podNamespace := "other"
|
||||||
podName := "foo"
|
podName := "foo"
|
||||||
|
@ -891,6 +908,7 @@ func TestContainerLogsWithTail(t *testing.T) {
|
||||||
|
|
||||||
func TestContainerLogsWithLegacyTail(t *testing.T) {
|
func TestContainerLogsWithLegacyTail(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
output := "foo bar"
|
output := "foo bar"
|
||||||
podNamespace := "other"
|
podNamespace := "other"
|
||||||
podName := "foo"
|
podName := "foo"
|
||||||
|
@ -917,6 +935,7 @@ func TestContainerLogsWithLegacyTail(t *testing.T) {
|
||||||
|
|
||||||
func TestContainerLogsWithTailAll(t *testing.T) {
|
func TestContainerLogsWithTailAll(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
output := "foo bar"
|
output := "foo bar"
|
||||||
podNamespace := "other"
|
podNamespace := "other"
|
||||||
podName := "foo"
|
podName := "foo"
|
||||||
|
@ -942,6 +961,7 @@ func TestContainerLogsWithTailAll(t *testing.T) {
|
||||||
|
|
||||||
func TestContainerLogsWithInvalidTail(t *testing.T) {
|
func TestContainerLogsWithInvalidTail(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
output := "foo bar"
|
output := "foo bar"
|
||||||
podNamespace := "other"
|
podNamespace := "other"
|
||||||
podName := "foo"
|
podName := "foo"
|
||||||
|
@ -961,6 +981,7 @@ func TestContainerLogsWithInvalidTail(t *testing.T) {
|
||||||
|
|
||||||
func TestContainerLogsWithFollow(t *testing.T) {
|
func TestContainerLogsWithFollow(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
output := "foo bar"
|
output := "foo bar"
|
||||||
podNamespace := "other"
|
podNamespace := "other"
|
||||||
podName := "foo"
|
podName := "foo"
|
||||||
|
@ -986,6 +1007,7 @@ func TestContainerLogsWithFollow(t *testing.T) {
|
||||||
|
|
||||||
func TestServeExecInContainerIdleTimeout(t *testing.T) {
|
func TestServeExecInContainerIdleTimeout(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
|
|
||||||
fw.fakeKubelet.streamingConnectionIdleTimeoutFunc = func() time.Duration {
|
fw.fakeKubelet.streamingConnectionIdleTimeoutFunc = func() time.Duration {
|
||||||
return 100 * time.Millisecond
|
return 100 * time.Millisecond
|
||||||
|
@ -1041,6 +1063,7 @@ func testExecAttach(t *testing.T, verb string) {
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
|
|
||||||
fw.fakeKubelet.streamingConnectionIdleTimeoutFunc = func() time.Duration {
|
fw.fakeKubelet.streamingConnectionIdleTimeoutFunc = func() time.Duration {
|
||||||
return 0
|
return 0
|
||||||
|
@ -1293,6 +1316,7 @@ func TestServeAttachContainer(t *testing.T) {
|
||||||
|
|
||||||
func TestServePortForwardIdleTimeout(t *testing.T) {
|
func TestServePortForwardIdleTimeout(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
|
|
||||||
fw.fakeKubelet.streamingConnectionIdleTimeoutFunc = func() time.Duration {
|
fw.fakeKubelet.streamingConnectionIdleTimeoutFunc = func() time.Duration {
|
||||||
return 100 * time.Millisecond
|
return 100 * time.Millisecond
|
||||||
|
@ -1351,6 +1375,7 @@ func TestServePortForward(t *testing.T) {
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
|
defer fw.testHTTPServer.Close()
|
||||||
|
|
||||||
fw.fakeKubelet.streamingConnectionIdleTimeoutFunc = func() time.Duration {
|
fw.fakeKubelet.streamingConnectionIdleTimeoutFunc = func() time.Duration {
|
||||||
return 0
|
return 0
|
||||||
|
|
|
@ -121,10 +121,11 @@ func TestHTTPProbeChecker(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
// TODO: Close() this when fix #19254
|
func() {
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
test.handler(w, r)
|
test.handler(w, r)
|
||||||
}))
|
}))
|
||||||
|
defer server.Close()
|
||||||
u, err := url.Parse(server.URL)
|
u, err := url.Parse(server.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
@ -150,5 +151,6 @@ func TestHTTPProbeChecker(t *testing.T) {
|
||||||
if !containsAny(output, test.accBodies) {
|
if !containsAny(output, test.accBodies) {
|
||||||
t.Errorf("Expected one of %#v, got %v", test.accBodies, output)
|
t.Errorf("Expected one of %#v, got %v", test.accBodies, output)
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -85,16 +86,17 @@ func waitForClosedPortUDP(p *Proxier, proxyPort int) error {
|
||||||
var tcpServerPort int32
|
var tcpServerPort int32
|
||||||
var udpServerPort int32
|
var udpServerPort int32
|
||||||
|
|
||||||
func init() {
|
func TestMain(m *testing.M) {
|
||||||
// Don't handle panics
|
// Don't handle panics
|
||||||
runtime.ReallyCrash = true
|
runtime.ReallyCrash = true
|
||||||
|
|
||||||
// TCP setup.
|
// TCP setup.
|
||||||
// TODO: Close() this when fix #19254
|
|
||||||
tcp := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
tcp := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte(r.URL.Path[1:]))
|
w.Write([]byte(r.URL.Path[1:]))
|
||||||
}))
|
}))
|
||||||
|
defer tcp.Close()
|
||||||
|
|
||||||
u, err := url.Parse(tcp.URL)
|
u, err := url.Parse(tcp.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("failed to parse: %v", err))
|
panic(fmt.Sprintf("failed to parse: %v", err))
|
||||||
|
@ -124,6 +126,11 @@ func init() {
|
||||||
}
|
}
|
||||||
udpServerPort = int32(udpServerPortValue)
|
udpServerPort = int32(udpServerPortValue)
|
||||||
go udp.Loop()
|
go udp.Loop()
|
||||||
|
|
||||||
|
ret := m.Run()
|
||||||
|
// it should be safe to call Close() multiple times.
|
||||||
|
tcp.Close()
|
||||||
|
os.Exit(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testEchoTCP(t *testing.T, address string, port int) {
|
func testEchoTCP(t *testing.T, address string, port int) {
|
||||||
|
|
Loading…
Reference in New Issue