Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
8b8f5503
Commit
8b8f5503
authored
Nov 05, 2015
by
k8s-merge-robot
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #16778 from hongchaodeng/fix-15952
Auto commit by PR queue bot
parents
a848262d
10cd7f5b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
37 deletions
+31
-37
tcp_test.go
pkg/probe/tcp/tcp_test.go
+31
-37
No files found.
pkg/probe/tcp/tcp_test.go
View file @
8b8f5503
...
@@ -20,7 +20,6 @@ import (
...
@@ -20,7 +20,6 @@ import (
"net"
"net"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
"net/url"
"strconv"
"strconv"
"strings"
"strings"
"testing"
"testing"
...
@@ -39,52 +38,47 @@ func containsAny(s string, substrs []string) bool {
...
@@ -39,52 +38,47 @@ func containsAny(s string, substrs []string) bool {
}
}
func
TestTcpHealthChecker
(
t
*
testing
.
T
)
{
func
TestTcpHealthChecker
(
t
*
testing
.
T
)
{
prober
:=
New
()
// Setup a test server that responds to probing correctly
tests
:=
[]
struct
{
expectedStatus
probe
.
Result
usePort
bool
expectError
bool
// Some errors are different depending on your system, make
// the test pass on all of them
accOutputs
[]
string
}{
// The probe will be filled in below. This is primarily testing that a connection is made.
{
probe
.
Success
,
true
,
false
,
[]
string
{
""
}},
{
probe
.
Failure
,
false
,
false
,
[]
string
{
"unknown port"
,
"Servname not supported for ai_socktype"
}},
}
server
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
server
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
WriteHeader
(
http
.
StatusOK
)
w
.
WriteHeader
(
http
.
StatusOK
)
}))
}))
defer
server
.
Close
()
defer
server
.
Close
()
u
,
err
:=
url
.
Parse
(
server
.
URL
)
tHost
,
tPortStr
,
err
:=
net
.
SplitHostPort
(
server
.
Listener
.
Addr
()
.
String
()
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"
U
nexpected error: %v"
,
err
)
t
.
Errorf
(
"
u
nexpected error: %v"
,
err
)
}
}
host
,
port
,
err
:=
net
.
SplitHostPort
(
u
.
Host
)
tPort
,
err
:=
strconv
.
Atoi
(
tPortStr
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"
U
nexpected error: %v"
,
err
)
t
.
Errorf
(
"
u
nexpected error: %v"
,
err
)
}
}
for
_
,
test
:=
range
tests
{
p
,
err
:=
strconv
.
Atoi
(
port
)
tests
:=
[]
struct
{
if
err
!=
nil
{
host
string
t
.
Errorf
(
"unexpected error: %v"
,
err
)
port
int
}
if
!
test
.
usePort
{
expectedStatus
probe
.
Result
p
=
-
1
expectedError
error
}
// Some errors are different depending on your system.
status
,
output
,
err
:=
prober
.
Probe
(
host
,
p
,
1
*
time
.
Second
)
// The test passes as long as the output matches one of them.
if
status
!=
test
.
expectedStatus
{
expectedOutputs
[]
string
t
.
Errorf
(
"expected: %v, got: %v"
,
test
.
expectedStatus
,
status
)
}{
}
// A connection is made and probing would succeed
if
err
!=
nil
&&
!
test
.
expectError
{
{
tHost
,
tPort
,
probe
.
Success
,
nil
,
[]
string
{
""
}},
t
.
Errorf
(
"unexpected error: %v"
,
err
)
// No connection can be made and probing would fail
{
tHost
,
-
1
,
probe
.
Failure
,
nil
,
[]
string
{
"unknown port"
,
"Servname not supported for ai_socktype"
,
"nodename nor servname provided, or not known"
}},
}
prober
:=
New
()
for
i
,
tt
:=
range
tests
{
status
,
output
,
err
:=
prober
.
Probe
(
tt
.
host
,
tt
.
port
,
1
*
time
.
Second
)
if
status
!=
tt
.
expectedStatus
{
t
.
Errorf
(
"#%d: expected status=%v, get=%v"
,
i
,
tt
.
expectedStatus
,
status
)
}
}
if
err
==
nil
&&
test
.
expect
Error
{
if
err
!=
tt
.
expected
Error
{
t
.
Errorf
(
"
unexpected non-error."
)
t
.
Errorf
(
"
#%d: expected error=%v, get=%v"
,
i
,
tt
.
expectedError
,
err
)
}
}
if
!
containsAny
(
output
,
t
est
.
acc
Outputs
)
{
if
!
containsAny
(
output
,
t
t
.
expected
Outputs
)
{
t
.
Errorf
(
"
expected one of %#v, got %s"
,
test
.
acc
Outputs
,
output
)
t
.
Errorf
(
"
#%d: expected output=one of %#v, get=%s"
,
tt
.
expected
Outputs
,
output
)
}
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment