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
3bf8fc3a
Commit
3bf8fc3a
authored
Aug 03, 2017
by
m1093782566
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
UTs for pkg/kubectl generate_test.go
parent
2495cc60
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
249 additions
and
0 deletions
+249
-0
generate.go
pkg/kubectl/generate.go
+9
-0
generate_test.go
pkg/kubectl/generate_test.go
+240
-0
No files found.
pkg/kubectl/generate.go
View file @
3bf8fc3a
...
@@ -159,6 +159,12 @@ func ParseProtocols(protocols interface{}) (map[string]string, error) {
...
@@ -159,6 +159,12 @@ func ParseProtocols(protocols interface{}) (map[string]string, error) {
if
len
(
portProtocol
)
!=
2
{
if
len
(
portProtocol
)
!=
2
{
return
nil
,
fmt
.
Errorf
(
"unexpected port protocol mapping: %s"
,
protocolsSlice
[
ix
])
return
nil
,
fmt
.
Errorf
(
"unexpected port protocol mapping: %s"
,
protocolsSlice
[
ix
])
}
}
if
len
(
portProtocol
[
0
])
==
0
{
return
nil
,
fmt
.
Errorf
(
"unexpected empty port"
)
}
if
len
(
portProtocol
[
1
])
==
0
{
return
nil
,
fmt
.
Errorf
(
"unexpected empty protocol"
)
}
portProtocolMap
[
portProtocol
[
0
]]
=
portProtocol
[
1
]
portProtocolMap
[
portProtocol
[
0
]]
=
portProtocol
[
1
]
}
}
return
portProtocolMap
,
nil
return
portProtocolMap
,
nil
...
@@ -188,6 +194,9 @@ func ParseLabels(labelSpec interface{}) (map[string]string, error) {
...
@@ -188,6 +194,9 @@ func ParseLabels(labelSpec interface{}) (map[string]string, error) {
if
len
(
labelSpec
)
!=
2
{
if
len
(
labelSpec
)
!=
2
{
return
nil
,
fmt
.
Errorf
(
"unexpected label spec: %s"
,
labelSpecs
[
ix
])
return
nil
,
fmt
.
Errorf
(
"unexpected label spec: %s"
,
labelSpecs
[
ix
])
}
}
if
len
(
labelSpec
[
0
])
==
0
{
return
nil
,
fmt
.
Errorf
(
"unexpected empty label key"
)
}
labels
[
labelSpec
[
0
]]
=
labelSpec
[
1
]
labels
[
labelSpec
[
0
]]
=
labelSpec
[
1
]
}
}
return
labels
,
nil
return
labels
,
nil
...
...
pkg/kubectl/generate_test.go
View file @
3bf8fc3a
...
@@ -138,3 +138,243 @@ func TestMakeParams(t *testing.T) {
...
@@ -138,3 +138,243 @@ func TestMakeParams(t *testing.T) {
t
.
Errorf
(
"
\n
expected:
\n
%v
\n
saw:
\n
%v"
,
expected
,
params
)
t
.
Errorf
(
"
\n
expected:
\n
%v
\n
saw:
\n
%v"
,
expected
,
params
)
}
}
}
}
func
TestGetBool
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
name
string
parameters
map
[
string
]
string
key
string
defaultValue
bool
expected
bool
expectError
bool
}{
{
name
:
"found key in parameters, default value is different from key value"
,
parameters
:
map
[
string
]
string
{
"foo"
:
"false"
,
},
key
:
"foo"
,
defaultValue
:
false
,
expected
:
false
,
expectError
:
false
,
},
{
name
:
"found key in parameters, default value is same with key value"
,
parameters
:
map
[
string
]
string
{
"foo"
:
"true"
,
},
key
:
"foo"
,
defaultValue
:
true
,
expected
:
true
,
expectError
:
false
,
},
{
name
:
"key not found in parameters, default value is true"
,
parameters
:
map
[
string
]
string
{
"foo"
:
"true"
,
"far"
:
"false"
,
},
key
:
"bar"
,
defaultValue
:
true
,
expected
:
true
,
expectError
:
false
,
},
{
name
:
"key not found in parameters, default value is false"
,
parameters
:
map
[
string
]
string
{
"foo"
:
"true"
,
"far"
:
"false"
,
},
key
:
"bar"
,
defaultValue
:
false
,
expected
:
false
,
expectError
:
false
,
},
{
name
:
"parameters is empty"
,
parameters
:
map
[
string
]
string
{},
key
:
"foo"
,
defaultValue
:
true
,
expected
:
true
,
expectError
:
false
,
},
{
name
:
"parameters key is not a valid bool value"
,
parameters
:
map
[
string
]
string
{
"foo"
:
"error"
,
},
key
:
"foo"
,
defaultValue
:
true
,
expected
:
false
,
expectError
:
true
,
},
}
for
_
,
test
:=
range
testCases
{
got
,
err
:=
GetBool
(
test
.
parameters
,
test
.
key
,
test
.
defaultValue
)
if
err
!=
nil
&&
test
.
expectError
==
false
{
t
.
Errorf
(
"%s: unexpected error: %v"
,
test
.
name
,
err
)
}
if
err
==
nil
&&
test
.
expectError
==
true
{
t
.
Errorf
(
"%s: expect error, got nil"
,
test
.
name
)
}
if
got
!=
test
.
expected
{
t
.
Errorf
(
"%s: expect %v, got %v"
,
test
.
name
,
test
.
expected
,
got
)
}
}
}
func
TestMakeParseLabels
(
t
*
testing
.
T
)
{
successCases
:=
[]
struct
{
labels
map
[
string
]
string
expected
map
[
string
]
string
}{
{
labels
:
map
[
string
]
string
{
"foo"
:
"false"
,
},
expected
:
map
[
string
]
string
{
"foo"
:
"false"
,
},
},
{
labels
:
map
[
string
]
string
{
"foo"
:
"true"
,
"bar"
:
"123"
,
},
expected
:
map
[
string
]
string
{
"foo"
:
"true"
,
"bar"
:
"123"
,
},
},
}
for
_
,
test
:=
range
successCases
{
labelString
:=
MakeLabels
(
test
.
labels
)
got
,
err
:=
ParseLabels
(
labelString
)
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error :%v"
,
err
)
}
if
!
reflect
.
DeepEqual
(
test
.
expected
,
got
)
{
t
.
Errorf
(
"
\n
expected:
\n
%v
\n
got:
\n
%v"
,
test
.
expected
,
got
)
}
}
errorCases
:=
[]
struct
{
name
string
labels
interface
{}
}{
{
name
:
"non-string"
,
labels
:
123
,
},
{
name
:
"empty string"
,
labels
:
""
,
},
{
name
:
"error format"
,
labels
:
"abc=456;bcd=789"
,
},
{
name
:
"error format"
,
labels
:
"abc=456.bcd=789"
,
},
{
name
:
"error format"
,
labels
:
"abc,789"
,
},
{
name
:
"error format"
,
labels
:
"abc"
,
},
{
name
:
"error format"
,
labels
:
"=abc"
,
},
}
for
_
,
test
:=
range
errorCases
{
_
,
err
:=
ParseLabels
(
test
.
labels
)
if
err
==
nil
{
t
.
Errorf
(
"labels %s expect error, reason: %s, got nil"
,
test
.
labels
,
test
.
name
)
}
}
}
func
TestMakeParseProtocols
(
t
*
testing
.
T
)
{
successCases
:=
[]
struct
{
protocols
map
[
string
]
string
expected
map
[
string
]
string
}{
{
protocols
:
map
[
string
]
string
{
"101"
:
"TCP"
,
},
expected
:
map
[
string
]
string
{
"101"
:
"TCP"
,
},
},
{
protocols
:
map
[
string
]
string
{
"102"
:
"UDP"
,
"101"
:
"TCP"
,
},
expected
:
map
[
string
]
string
{
"102"
:
"UDP"
,
"101"
:
"TCP"
,
},
},
}
for
_
,
test
:=
range
successCases
{
protocolString
:=
MakeProtocols
(
test
.
protocols
)
got
,
err
:=
ParseProtocols
(
protocolString
)
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error :%v"
,
err
)
}
if
!
reflect
.
DeepEqual
(
test
.
expected
,
got
)
{
t
.
Errorf
(
"
\n
expected:
\n
%v
\n
got:
\n
%v"
,
test
.
expected
,
got
)
}
}
errorCases
:=
[]
struct
{
name
string
protocols
interface
{}
}{
{
name
:
"non-string"
,
protocols
:
123
,
},
{
name
:
"empty string"
,
protocols
:
""
,
},
{
name
:
"error format"
,
protocols
:
"123/TCP;456/UDP"
,
},
{
name
:
"error format"
,
protocols
:
"123/TCP.456/UDP"
,
},
{
name
:
"error format"
,
protocols
:
"123=456"
,
},
{
name
:
"error format"
,
protocols
:
"123"
,
},
{
name
:
"error format"
,
protocols
:
"123="
,
},
{
name
:
"error format"
,
protocols
:
"=TCP"
,
},
}
for
_
,
test
:=
range
errorCases
{
_
,
err
:=
ParseProtocols
(
test
.
protocols
)
if
err
==
nil
{
t
.
Errorf
(
"protocols %s expect error, reason: %s, got nil"
,
test
.
protocols
,
test
.
name
)
}
}
}
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