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
f079399a
Commit
f079399a
authored
Feb 13, 2017
by
Derek McQuay
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kubeadm: adding tests for util/tokens.go
Included a fix for a logic error in tokens.go found through writing tests
parent
beaf5ffa
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
1 deletion
+78
-1
tokens.go
cmd/kubeadm/app/util/tokens.go
+1
-1
tokens_test.go
cmd/kubeadm/app/util/tokens_test.go
+77
-0
No files found.
cmd/kubeadm/app/util/tokens.go
View file @
f079399a
...
...
@@ -120,7 +120,7 @@ func DiscoveryPort(d *kubeadmapi.TokenDiscovery) int32 {
if
len
(
split
)
==
1
{
return
kubeadmapiext
.
DefaultDiscoveryBindPort
}
if
i
,
err
:=
strconv
.
Atoi
(
split
[
1
]);
err
!
=
nil
{
if
i
,
err
:=
strconv
.
Atoi
(
split
[
1
]);
err
=
=
nil
{
return
int32
(
i
)
}
return
kubeadmapiext
.
DefaultDiscoveryBindPort
...
...
cmd/kubeadm/app/util/tokens_test.go
View file @
f079399a
...
...
@@ -17,7 +17,9 @@ limitations under the License.
package
util
import
(
"bytes"
"testing"
"time"
kubeadmapi
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
)
...
...
@@ -148,3 +150,78 @@ func TestRandBytes(t *testing.T) {
}
}
}
func
TestDiscoveryPort
(
t
*
testing
.
T
)
{
var
tests
=
[]
struct
{
token
*
kubeadmapi
.
TokenDiscovery
expected
int32
}{
{
token
:
&
kubeadmapi
.
TokenDiscovery
{},
expected
:
9898
},
// should use default
{
token
:
&
kubeadmapi
.
TokenDiscovery
{
Addresses
:
[]
string
{
"foobar:1234"
}},
expected
:
1234
},
{
token
:
&
kubeadmapi
.
TokenDiscovery
{
Addresses
:
[]
string
{
"doesnothaveport"
}},
expected
:
9898
},
// should use default
{
token
:
&
kubeadmapi
.
TokenDiscovery
{
Addresses
:
[]
string
{
"foorbar:abcd"
}},
expected
:
9898
},
// since abcd isn't an int, should use default
}
for
_
,
rt
:=
range
tests
{
actual
:=
DiscoveryPort
(
rt
.
token
)
if
actual
!=
rt
.
expected
{
t
.
Errorf
(
"failed DiscoveryPort:
\n\t
expected: %d
\n\t
actual: %d"
,
rt
.
expected
,
actual
,
)
}
}
}
func
TestBearerToken
(
t
*
testing
.
T
)
{
var
tests
=
[]
struct
{
token
*
kubeadmapi
.
TokenDiscovery
expected
string
}{
{
token
:
&
kubeadmapi
.
TokenDiscovery
{
ID
:
"foo"
,
Secret
:
"bar"
},
expected
:
"foo:bar"
},
// should use default
}
for
_
,
rt
:=
range
tests
{
actual
:=
BearerToken
(
rt
.
token
)
if
actual
!=
rt
.
expected
{
t
.
Errorf
(
"failed BearerToken:
\n\t
expected: %s
\n\t
actual: %s"
,
rt
.
expected
,
actual
,
)
}
}
}
func
TestEncodeTokenSecretData
(
t
*
testing
.
T
)
{
var
tests
=
[]
struct
{
token
*
kubeadmapi
.
TokenDiscovery
t
time
.
Duration
}{
{
token
:
&
kubeadmapi
.
TokenDiscovery
{
ID
:
"foo"
,
Secret
:
"bar"
}},
// should use default
{
token
:
&
kubeadmapi
.
TokenDiscovery
{
ID
:
"foo"
,
Secret
:
"bar"
},
t
:
time
.
Second
},
// should use default
}
for
_
,
rt
:=
range
tests
{
actual
:=
encodeTokenSecretData
(
rt
.
token
,
rt
.
t
)
if
!
bytes
.
Equal
(
actual
[
"token-id"
],
[]
byte
(
rt
.
token
.
ID
))
{
t
.
Errorf
(
"failed EncodeTokenSecretData:
\n\t
expected: %s
\n\t
actual: %s"
,
rt
.
token
.
ID
,
actual
[
"token-id"
],
)
}
if
!
bytes
.
Equal
(
actual
[
"token-secret"
],
[]
byte
(
rt
.
token
.
Secret
))
{
t
.
Errorf
(
"failed EncodeTokenSecretData:
\n\t
expected: %s
\n\t
actual: %s"
,
rt
.
token
.
Secret
,
actual
[
"token-secret"
],
)
}
if
rt
.
t
>
0
{
if
actual
[
"expiration"
]
==
nil
{
t
.
Errorf
(
"failed EncodeTokenSecretData, duration was not added to time"
,
)
}
}
}
}
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