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
d178bc76
Commit
d178bc76
authored
Nov 22, 2016
by
ymqytw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert "add a unit test"
This reverts commit
11653b11
.
parent
d5402e63
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
11 additions
and
131 deletions
+11
-131
BUILD
pkg/kubectl/cmd/BUILD
+0
-1
apply_test.go
pkg/kubectl/cmd/apply_test.go
+0
-113
fake.go
pkg/kubectl/cmd/testing/fake.go
+2
-6
patch.go
pkg/util/strategicpatch/patch.go
+9
-9
rc.yaml
test/fixtures/pkg/kubectl/cmd/apply/rc.yaml
+0
-2
No files found.
pkg/kubectl/cmd/BUILD
View file @
d178bc76
...
@@ -191,7 +191,6 @@ go_test(
...
@@ -191,7 +191,6 @@ go_test(
"//pkg/runtime/serializer/streaming:go_default_library",
"//pkg/runtime/serializer/streaming:go_default_library",
"//pkg/types:go_default_library",
"//pkg/types:go_default_library",
"//pkg/util/intstr:go_default_library",
"//pkg/util/intstr:go_default_library",
"//pkg/util/strategicpatch:go_default_library",
"//pkg/util/strings:go_default_library",
"//pkg/util/strings:go_default_library",
"//pkg/util/term:go_default_library",
"//pkg/util/term:go_default_library",
"//pkg/util/wait:go_default_library",
"//pkg/util/wait:go_default_library",
...
...
pkg/kubectl/cmd/apply_test.go
View file @
d178bc76
...
@@ -23,7 +23,6 @@ import (
...
@@ -23,7 +23,6 @@ import (
"io/ioutil"
"io/ioutil"
"net/http"
"net/http"
"os"
"os"
"strings"
"testing"
"testing"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
...
@@ -38,7 +37,6 @@ import (
...
@@ -38,7 +37,6 @@ import (
cmdtesting
"k8s.io/kubernetes/pkg/kubectl/cmd/testing"
cmdtesting
"k8s.io/kubernetes/pkg/kubectl/cmd/testing"
cmdutil
"k8s.io/kubernetes/pkg/kubectl/cmd/util"
cmdutil
"k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/strategicpatch"
)
)
func
TestApplyExtraArgsFail
(
t
*
testing
.
T
)
{
func
TestApplyExtraArgsFail
(
t
*
testing
.
T
)
{
...
@@ -144,58 +142,6 @@ func readAndAnnotateService(t *testing.T, filename string) (string, []byte) {
...
@@ -144,58 +142,6 @@ func readAndAnnotateService(t *testing.T, filename string) (string, []byte) {
return
annotateRuntimeObject
(
t
,
svc1
,
svc2
,
"Service"
)
return
annotateRuntimeObject
(
t
,
svc1
,
svc2
,
"Service"
)
}
}
func
setFinalizersRuntimeObject
(
t
*
testing
.
T
,
originalObj
,
currentObj
runtime
.
Object
)
(
string
,
[]
byte
)
{
originalAccessor
,
err
:=
meta
.
Accessor
(
originalObj
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
originalFinalizers
:=
[]
string
{
"a/a"
}
originalAccessor
.
SetFinalizers
(
originalFinalizers
)
original
,
err
:=
runtime
.
Encode
(
testapi
.
Default
.
Codec
(),
originalObj
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
currentAccessor
,
err
:=
meta
.
Accessor
(
currentObj
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
currentFinalizers
:=
[]
string
{
"b/b"
}
currentAccessor
.
SetFinalizers
(
currentFinalizers
)
currentAnnotations
:=
currentAccessor
.
GetAnnotations
()
if
currentAnnotations
==
nil
{
currentAnnotations
=
make
(
map
[
string
]
string
)
}
currentAnnotations
[
annotations
.
LastAppliedConfigAnnotation
]
=
string
(
original
)
currentAccessor
.
SetAnnotations
(
currentAnnotations
)
current
,
err
:=
runtime
.
Encode
(
testapi
.
Default
.
Codec
(),
currentObj
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
return
currentAccessor
.
GetName
(),
current
}
func
readAndSetFinalizersReplicationController
(
t
*
testing
.
T
,
filename
string
)
(
string
,
[]
byte
)
{
rc1
:=
readReplicationControllerFromFile
(
t
,
filename
)
rc2
:=
readReplicationControllerFromFile
(
t
,
filename
)
name
,
rcBytes
:=
setFinalizersRuntimeObject
(
t
,
rc1
,
rc2
)
return
name
,
rcBytes
}
func
isSMPatchVersion_1_5
(
t
*
testing
.
T
,
req
*
http
.
Request
)
bool
{
patch
,
err
:=
ioutil
.
ReadAll
(
req
.
Body
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// SMPatchVersion_1_5 patch should has string "mergeprimitiveslist"
return
strings
.
Contains
(
string
(
patch
),
strategicpatch
.
MergePrimitivesListDirective
)
}
func
validatePatchApplication
(
t
*
testing
.
T
,
req
*
http
.
Request
)
{
func
validatePatchApplication
(
t
*
testing
.
T
,
req
*
http
.
Request
)
{
patch
,
err
:=
ioutil
.
ReadAll
(
req
.
Body
)
patch
,
err
:=
ioutil
.
ReadAll
(
req
.
Body
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -277,65 +223,6 @@ func TestApplyObject(t *testing.T) {
...
@@ -277,65 +223,6 @@ func TestApplyObject(t *testing.T) {
}
}
}
}
func
TestApplyRetryWithSMPatchVersion_1_5
(
t
*
testing
.
T
)
{
initTestErrorHandler
(
t
)
nameRC
,
currentRC
:=
readAndSetFinalizersReplicationController
(
t
,
filenameRC
)
pathRC
:=
"/namespaces/test/replicationcontrollers/"
+
nameRC
firstPatch
:=
true
retry
:=
false
f
,
tf
,
_
,
ns
:=
cmdtesting
.
NewAPIFactory
()
tf
.
Printer
=
&
testPrinter
{}
tf
.
Client
=
&
fake
.
RESTClient
{
NegotiatedSerializer
:
ns
,
Client
:
fake
.
CreateHTTPClient
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
switch
p
,
m
:=
req
.
URL
.
Path
,
req
.
Method
;
{
case
p
==
pathRC
&&
m
==
"GET"
:
bodyRC
:=
ioutil
.
NopCloser
(
bytes
.
NewReader
(
currentRC
))
return
&
http
.
Response
{
StatusCode
:
200
,
Header
:
defaultHeader
(),
Body
:
bodyRC
},
nil
case
p
==
pathRC
&&
m
==
"PATCH"
:
if
firstPatch
{
if
!
isSMPatchVersion_1_5
(
t
,
req
)
{
t
.
Fatalf
(
"apply didn't try to send SMPatchVersion_1_5 for the first time"
)
}
firstPatch
=
false
statusErr
:=
kubeerr
.
NewInternalError
(
fmt
.
Errorf
(
"Server encountered internal error."
))
bodyBytes
,
_
:=
json
.
Marshal
(
statusErr
)
bodyErr
:=
ioutil
.
NopCloser
(
bytes
.
NewReader
(
bodyBytes
))
return
&
http
.
Response
{
StatusCode
:
http
.
StatusInternalServerError
,
Header
:
defaultHeader
(),
Body
:
bodyErr
},
nil
}
retry
=
true
if
isSMPatchVersion_1_5
(
t
,
req
)
{
t
.
Fatalf
(
"apply didn't try to send SMPatchVersion_1_0 after SMPatchVersion_1_5 patch encounter an Internal Error (500)"
)
}
bodyRC
:=
ioutil
.
NopCloser
(
bytes
.
NewReader
(
currentRC
))
return
&
http
.
Response
{
StatusCode
:
200
,
Header
:
defaultHeader
(),
Body
:
bodyRC
},
nil
default
:
t
.
Fatalf
(
"unexpected request: %#v
\n
%#v"
,
req
.
URL
,
req
)
return
nil
,
nil
}
}),
}
tf
.
Namespace
=
"test"
tf
.
ClientConfig
=
defaultClientConfig
()
buf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
NewCmdApply
(
f
,
buf
)
cmd
.
Flags
()
.
Set
(
"filename"
,
filenameRC
)
cmd
.
Flags
()
.
Set
(
"output"
,
"name"
)
cmd
.
Run
(
cmd
,
[]
string
{})
if
!
retry
{
t
.
Fatalf
(
"apply didn't retry when get Internal Error (500)"
)
}
// uses the name from the file, not the response
expectRC
:=
"replicationcontroller/"
+
nameRC
+
"
\n
"
if
buf
.
String
()
!=
expectRC
{
t
.
Fatalf
(
"unexpected output: %s
\n
expected: %s"
,
buf
.
String
(),
expectRC
)
}
}
func
TestApplyRetry
(
t
*
testing
.
T
)
{
func
TestApplyRetry
(
t
*
testing
.
T
)
{
initTestErrorHandler
(
t
)
initTestErrorHandler
(
t
)
nameRC
,
currentRC
:=
readAndAnnotateReplicationController
(
t
,
filenameRC
)
nameRC
,
currentRC
:=
readAndAnnotateReplicationController
(
t
,
filenameRC
)
...
...
pkg/kubectl/cmd/testing/fake.go
View file @
d178bc76
...
@@ -427,12 +427,8 @@ func (f *fakeAPIFactory) UnstructuredObject() (meta.RESTMapper, runtime.ObjectTy
...
@@ -427,12 +427,8 @@ func (f *fakeAPIFactory) UnstructuredObject() (meta.RESTMapper, runtime.ObjectTy
return
cmdutil
.
NewShortcutExpander
(
mapper
,
nil
),
typer
,
nil
return
cmdutil
.
NewShortcutExpander
(
mapper
,
nil
),
typer
,
nil
}
}
func
(
f
*
fakeAPIFactory
)
Decoder
(
toInternal
bool
)
runtime
.
Decoder
{
func
(
f
*
fakeAPIFactory
)
Decoder
(
bool
)
runtime
.
Decoder
{
if
toInternal
{
return
testapi
.
Default
.
Codec
()
return
api
.
Codecs
.
UniversalDecoder
()
}
else
{
return
api
.
Codecs
.
UniversalDeserializer
()
}
}
}
func
(
f
*
fakeAPIFactory
)
JSONEncoder
()
runtime
.
Encoder
{
func
(
f
*
fakeAPIFactory
)
JSONEncoder
()
runtime
.
Encoder
{
...
...
pkg/util/strategicpatch/patch.go
View file @
d178bc76
...
@@ -45,7 +45,7 @@ const (
...
@@ -45,7 +45,7 @@ const (
deleteDirective
=
"delete"
deleteDirective
=
"delete"
replaceDirective
=
"replace"
replaceDirective
=
"replace"
mergeDirective
=
"merge"
mergeDirective
=
"merge"
M
ergePrimitivesListDirective
=
"mergeprimitiveslist"
m
ergePrimitivesListDirective
=
"mergeprimitiveslist"
// different versions of StrategicMergePatch
// different versions of StrategicMergePatch
SMPatchVersion_1_0
StrategicMergePatchVersion
=
"v1.0.0"
SMPatchVersion_1_0
StrategicMergePatchVersion
=
"v1.0.0"
...
@@ -393,7 +393,7 @@ loopB:
...
@@ -393,7 +393,7 @@ loopB:
func
diffListsOfScalarsIntoMap
(
originalScalars
,
modifiedScalars
[]
interface
{},
ignoreChangesAndAdditions
,
ignoreDeletions
bool
)
(
map
[
string
]
interface
{},
error
)
{
func
diffListsOfScalarsIntoMap
(
originalScalars
,
modifiedScalars
[]
interface
{},
ignoreChangesAndAdditions
,
ignoreDeletions
bool
)
(
map
[
string
]
interface
{},
error
)
{
originalIndex
,
modifiedIndex
:=
0
,
0
originalIndex
,
modifiedIndex
:=
0
,
0
patch
:=
map
[
string
]
interface
{}{}
patch
:=
map
[
string
]
interface
{}{}
patch
[
directiveMarker
]
=
M
ergePrimitivesListDirective
patch
[
directiveMarker
]
=
m
ergePrimitivesListDirective
for
originalIndex
<
len
(
originalScalars
)
&&
modifiedIndex
<
len
(
modifiedScalars
)
{
for
originalIndex
<
len
(
originalScalars
)
&&
modifiedIndex
<
len
(
modifiedScalars
)
{
originalString
:=
fmt
.
Sprintf
(
"%v"
,
originalScalars
[
originalIndex
])
originalString
:=
fmt
.
Sprintf
(
"%v"
,
originalScalars
[
originalIndex
])
...
@@ -627,7 +627,7 @@ func mergeMap(original, patch map[string]interface{}, t reflect.Type) (map[strin
...
@@ -627,7 +627,7 @@ func mergeMap(original, patch map[string]interface{}, t reflect.Type) (map[strin
return
map
[
string
]
interface
{}{},
nil
return
map
[
string
]
interface
{}{},
nil
}
}
if
v
==
M
ergePrimitivesListDirective
{
if
v
==
m
ergePrimitivesListDirective
{
// delete the directiveMarker's key-value pair to avoid delta map and delete map
// delete the directiveMarker's key-value pair to avoid delta map and delete map
// overlaping with each other when calculating a ThreeWayDiff for list of Primitives.
// overlaping with each other when calculating a ThreeWayDiff for list of Primitives.
// Otherwise, the overlaping will cause it calling LookupPatchMetadata() which will
// Otherwise, the overlaping will cause it calling LookupPatchMetadata() which will
...
@@ -718,7 +718,7 @@ func mergeMap(original, patch map[string]interface{}, t reflect.Type) (map[strin
...
@@ -718,7 +718,7 @@ func mergeMap(original, patch map[string]interface{}, t reflect.Type) (map[strin
// the patch because getting a deep copy of a slice in golang is highly
// the patch because getting a deep copy of a slice in golang is highly
// non-trivial.
// non-trivial.
// The patch could be a map[string]interface{} representing a slice of primitives.
// The patch could be a map[string]interface{} representing a slice of primitives.
// If the patch map doesn't has the specific directiveMarker (
M
ergePrimitivesListDirective),
// If the patch map doesn't has the specific directiveMarker (
m
ergePrimitivesListDirective),
// it returns an error. Please check patch_test.go and find the test case named
// it returns an error. Please check patch_test.go and find the test case named
// "merge lists of scalars for list of primitives" to see what the patch looks like.
// "merge lists of scalars for list of primitives" to see what the patch looks like.
// Patch is still []interface{} for all the other types.
// Patch is still []interface{} for all the other types.
...
@@ -731,7 +731,7 @@ func mergeSlice(original []interface{}, patch interface{}, elemType reflect.Type
...
@@ -731,7 +731,7 @@ func mergeSlice(original []interface{}, patch interface{}, elemType reflect.Type
if
patchMap
,
ok
:=
patch
.
(
map
[
string
]
interface
{});
ok
{
if
patchMap
,
ok
:=
patch
.
(
map
[
string
]
interface
{});
ok
{
// We try to merge the original slice with a patch map only when the map has
// We try to merge the original slice with a patch map only when the map has
// a specific directiveMarker. Otherwise, this patch will be treated as invalid.
// a specific directiveMarker. Otherwise, this patch will be treated as invalid.
if
directiveValue
,
ok
:=
patchMap
[
directiveMarker
];
ok
&&
directiveValue
==
M
ergePrimitivesListDirective
{
if
directiveValue
,
ok
:=
patchMap
[
directiveMarker
];
ok
&&
directiveValue
==
m
ergePrimitivesListDirective
{
return
mergeSliceOfScalarsWithPatchMap
(
original
,
patchMap
)
return
mergeSliceOfScalarsWithPatchMap
(
original
,
patchMap
)
}
else
{
}
else
{
return
nil
,
fmt
.
Errorf
(
"Unable to merge a slice with an invalid map"
)
return
nil
,
fmt
.
Errorf
(
"Unable to merge a slice with an invalid map"
)
...
@@ -838,10 +838,10 @@ func mergeSlice(original []interface{}, patch interface{}, elemType reflect.Type
...
@@ -838,10 +838,10 @@ func mergeSlice(original []interface{}, patch interface{}, elemType reflect.Type
// mergeSliceOfScalarsWithPatchMap merges the original slice with a patch map and
// mergeSliceOfScalarsWithPatchMap merges the original slice with a patch map and
// returns an uniqified and sorted slice of primitives.
// returns an uniqified and sorted slice of primitives.
// The patch map must have the specific directiveMarker (
M
ergePrimitivesListDirective).
// The patch map must have the specific directiveMarker (
m
ergePrimitivesListDirective).
func
mergeSliceOfScalarsWithPatchMap
(
original
[]
interface
{},
patch
map
[
string
]
interface
{})
([]
interface
{},
error
)
{
func
mergeSliceOfScalarsWithPatchMap
(
original
[]
interface
{},
patch
map
[
string
]
interface
{})
([]
interface
{},
error
)
{
// make sure the patch has the specific directiveMarker ()
// make sure the patch has the specific directiveMarker ()
if
directiveValue
,
ok
:=
patch
[
directiveMarker
];
ok
&&
directiveValue
!=
M
ergePrimitivesListDirective
{
if
directiveValue
,
ok
:=
patch
[
directiveMarker
];
ok
&&
directiveValue
!=
m
ergePrimitivesListDirective
{
return
nil
,
fmt
.
Errorf
(
"Unable to merge a slice with an invalid map"
)
return
nil
,
fmt
.
Errorf
(
"Unable to merge a slice with an invalid map"
)
}
}
delete
(
patch
,
directiveMarker
)
delete
(
patch
,
directiveMarker
)
...
@@ -1181,7 +1181,7 @@ func mergingMapFieldsHaveConflicts(
...
@@ -1181,7 +1181,7 @@ func mergingMapFieldsHaveConflicts(
return
true
,
nil
return
true
,
nil
}
}
if
leftMarker
==
MergePrimitivesListDirective
&&
rightMarker
==
M
ergePrimitivesListDirective
{
if
leftMarker
==
mergePrimitivesListDirective
&&
rightMarker
==
m
ergePrimitivesListDirective
{
return
false
,
nil
return
false
,
nil
}
}
}
}
...
@@ -1209,7 +1209,7 @@ func mapsHaveConflicts(typedLeft, typedRight map[string]interface{}, structType
...
@@ -1209,7 +1209,7 @@ func mapsHaveConflicts(typedLeft, typedRight map[string]interface{}, structType
isForListOfPrimitives
:=
false
isForListOfPrimitives
:=
false
if
leftDirective
,
ok
:=
typedLeft
[
directiveMarker
];
ok
{
if
leftDirective
,
ok
:=
typedLeft
[
directiveMarker
];
ok
{
if
rightDirective
,
ok
:=
typedRight
[
directiveMarker
];
ok
{
if
rightDirective
,
ok
:=
typedRight
[
directiveMarker
];
ok
{
if
leftDirective
==
M
ergePrimitivesListDirective
&&
rightDirective
==
rightDirective
{
if
leftDirective
==
m
ergePrimitivesListDirective
&&
rightDirective
==
rightDirective
{
isForListOfPrimitives
=
true
isForListOfPrimitives
=
true
}
}
}
}
...
...
test/fixtures/pkg/kubectl/cmd/apply/rc.yaml
View file @
d178bc76
apiVersion
:
v1
apiVersion
:
v1
kind
:
ReplicationController
kind
:
ReplicationController
metadata
:
metadata
:
finalizers
:
-
b/b
name
:
test-rc
name
:
test-rc
labels
:
labels
:
name
:
test-rc
name
:
test-rc
...
...
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