2015-01-15 03:29:13 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-01-15 03:29:13 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-02-05 00:14:48 +00:00
|
|
|
package util
|
2015-01-15 03:29:13 +00:00
|
|
|
|
|
|
|
import (
|
2015-06-30 02:30:14 +00:00
|
|
|
"fmt"
|
2015-03-07 10:00:45 +00:00
|
|
|
"io/ioutil"
|
2016-09-28 08:52:55 +00:00
|
|
|
"os"
|
2015-07-15 12:10:47 +00:00
|
|
|
"strings"
|
2015-03-07 10:00:45 +00:00
|
|
|
"syscall"
|
2015-01-15 03:29:13 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-10-03 05:29:35 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2017-03-21 03:07:57 +00:00
|
|
|
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
2017-01-13 17:48:50 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
2018-10-03 05:29:35 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/scheme"
|
2017-07-19 05:58:53 +00:00
|
|
|
"k8s.io/utils/exec"
|
2015-01-15 03:29:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMerge(t *testing.T) {
|
2015-08-19 23:59:43 +00:00
|
|
|
grace := int64(30)
|
2018-10-03 05:29:35 +00:00
|
|
|
enableServiceLinks := corev1.DefaultEnableServiceLinks
|
2015-01-15 03:29:13 +00:00
|
|
|
tests := []struct {
|
|
|
|
obj runtime.Object
|
|
|
|
fragment string
|
|
|
|
expected runtime.Object
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
2018-10-03 05:29:35 +00:00
|
|
|
obj: &corev1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-01-15 03:29:13 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
2018-05-01 14:54:37 +00:00
|
|
|
fragment: fmt.Sprintf(`{ "apiVersion": "%s" }`, "v1"),
|
2018-10-03 05:29:35 +00:00
|
|
|
expected: &corev1.Pod{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "Pod",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-01-15 03:29:13 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
2018-10-03 05:29:35 +00:00
|
|
|
Spec: corev1.PodSpec{
|
|
|
|
RestartPolicy: corev1.RestartPolicyAlways,
|
|
|
|
DNSPolicy: corev1.DNSClusterFirst,
|
|
|
|
TerminationGracePeriodSeconds: &grace,
|
|
|
|
SecurityContext: &corev1.PodSecurityContext{},
|
|
|
|
SchedulerName: corev1.DefaultSchedulerName,
|
|
|
|
EnableServiceLinks: &enableServiceLinks,
|
|
|
|
},
|
2015-01-15 03:29:13 +00:00
|
|
|
},
|
|
|
|
},
|
2015-05-29 00:25:44 +00:00
|
|
|
/* TODO: uncomment this test once Merge is updated to use
|
2015-06-04 17:35:10 +00:00
|
|
|
strategic-merge-patch. See #8449.
|
2015-05-29 00:25:44 +00:00
|
|
|
{
|
2018-10-03 05:29:35 +00:00
|
|
|
obj: &corev1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-05-29 00:25:44 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
2018-10-03 05:29:35 +00:00
|
|
|
Spec: corev1.PodSpec{
|
|
|
|
Containers: []corev1.Container{
|
|
|
|
corev1.Container{
|
2015-05-29 00:25:44 +00:00
|
|
|
Name: "c1",
|
|
|
|
Image: "red-image",
|
|
|
|
},
|
2018-10-03 05:29:35 +00:00
|
|
|
corev1.Container{
|
2015-05-29 00:25:44 +00:00
|
|
|
Name: "c2",
|
|
|
|
Image: "blue-image",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-05-01 14:54:37 +00:00
|
|
|
fragment: fmt.Sprintf(`{ "apiVersion": "%s", "spec": { "containers": [ { "name": "c1", "image": "green-image" } ] } }`, schema.GroupVersion{Group:"", Version: "v1"}.String()),
|
2018-10-03 05:29:35 +00:00
|
|
|
expected: &corev1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-05-29 00:25:44 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
2018-10-03 05:29:35 +00:00
|
|
|
Spec: corev1.PodSpec{
|
|
|
|
Containers: []corev1.Container{
|
|
|
|
corev1.Container{
|
2015-05-29 00:25:44 +00:00
|
|
|
Name: "c1",
|
|
|
|
Image: "green-image",
|
|
|
|
},
|
2018-10-03 05:29:35 +00:00
|
|
|
corev1.Container{
|
2015-05-29 00:25:44 +00:00
|
|
|
Name: "c2",
|
|
|
|
Image: "blue-image",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, */
|
2015-01-15 03:29:13 +00:00
|
|
|
{
|
2018-10-03 05:29:35 +00:00
|
|
|
obj: &corev1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-01-15 03:29:13 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
2018-05-01 14:54:37 +00:00
|
|
|
fragment: fmt.Sprintf(`{ "apiVersion": "%s", "spec": { "volumes": [ {"name": "v1"}, {"name": "v2"} ] } }`, "v1"),
|
2018-10-03 05:29:35 +00:00
|
|
|
expected: &corev1.Pod{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "Pod",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-01-15 03:29:13 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
2018-10-03 05:29:35 +00:00
|
|
|
Spec: corev1.PodSpec{
|
|
|
|
Volumes: []corev1.Volume{
|
2015-01-15 03:29:13 +00:00
|
|
|
{
|
2015-03-03 22:48:55 +00:00
|
|
|
Name: "v1",
|
2018-10-03 05:29:35 +00:00
|
|
|
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
|
2015-01-15 03:29:13 +00:00
|
|
|
},
|
|
|
|
{
|
2015-03-03 22:48:55 +00:00
|
|
|
Name: "v2",
|
2018-10-03 05:29:35 +00:00
|
|
|
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
|
2015-01-15 03:29:13 +00:00
|
|
|
},
|
|
|
|
},
|
2018-10-03 05:29:35 +00:00
|
|
|
RestartPolicy: corev1.RestartPolicyAlways,
|
|
|
|
DNSPolicy: corev1.DNSClusterFirst,
|
2015-08-19 23:59:43 +00:00
|
|
|
TerminationGracePeriodSeconds: &grace,
|
2018-10-03 05:29:35 +00:00
|
|
|
SecurityContext: &corev1.PodSecurityContext{},
|
|
|
|
SchedulerName: corev1.DefaultSchedulerName,
|
2018-09-17 20:27:36 +00:00
|
|
|
EnableServiceLinks: &enableServiceLinks,
|
2015-01-15 03:29:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-10-03 05:29:35 +00:00
|
|
|
obj: &corev1.Pod{},
|
2015-01-15 03:29:13 +00:00
|
|
|
fragment: "invalid json",
|
2018-10-03 05:29:35 +00:00
|
|
|
expected: &corev1.Pod{},
|
2015-01-15 03:29:13 +00:00
|
|
|
expectErr: true,
|
|
|
|
},
|
2015-02-25 18:06:18 +00:00
|
|
|
{
|
2018-10-03 05:29:35 +00:00
|
|
|
obj: &corev1.Service{},
|
2015-02-25 18:06:18 +00:00
|
|
|
fragment: `{ "apiVersion": "badVersion" }`,
|
|
|
|
expectErr: true,
|
|
|
|
},
|
2015-01-31 18:59:08 +00:00
|
|
|
{
|
2018-10-03 05:29:35 +00:00
|
|
|
obj: &corev1.Service{
|
|
|
|
Spec: corev1.ServiceSpec{},
|
2015-01-31 18:59:08 +00:00
|
|
|
},
|
2018-05-01 14:54:37 +00:00
|
|
|
fragment: fmt.Sprintf(`{ "apiVersion": "%s", "spec": { "ports": [ { "port": 0 } ] } }`, "v1"),
|
2018-10-03 05:29:35 +00:00
|
|
|
expected: &corev1.Service{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "Service",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
2015-01-31 18:59:08 +00:00
|
|
|
SessionAffinity: "None",
|
2018-10-03 05:29:35 +00:00
|
|
|
Type: corev1.ServiceTypeClusterIP,
|
|
|
|
Ports: []corev1.ServicePort{
|
2015-05-22 15:20:27 +00:00
|
|
|
{
|
2018-10-03 05:29:35 +00:00
|
|
|
Protocol: corev1.ProtocolTCP,
|
2015-05-22 15:20:27 +00:00
|
|
|
Port: 0,
|
|
|
|
},
|
|
|
|
},
|
2015-01-31 18:59:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-10-03 05:29:35 +00:00
|
|
|
obj: &corev1.Service{
|
|
|
|
Spec: corev1.ServiceSpec{
|
2015-01-31 18:59:08 +00:00
|
|
|
Selector: map[string]string{
|
|
|
|
"version": "v1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-05-01 14:54:37 +00:00
|
|
|
fragment: fmt.Sprintf(`{ "apiVersion": "%s", "spec": { "selector": { "version": "v2" } } }`, "v1"),
|
2018-10-03 05:29:35 +00:00
|
|
|
expected: &corev1.Service{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "Service",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
2015-06-03 18:54:50 +00:00
|
|
|
SessionAffinity: "None",
|
2018-10-03 05:29:35 +00:00
|
|
|
Type: corev1.ServiceTypeClusterIP,
|
2015-06-03 18:54:50 +00:00
|
|
|
Selector: map[string]string{
|
|
|
|
"version": "v2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-01-15 03:29:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 05:29:35 +00:00
|
|
|
codec := runtime.NewCodec(scheme.DefaultJSONEncoder(),
|
|
|
|
scheme.Codecs.UniversalDecoder(scheme.Scheme.PrioritizedVersionsAllGroups()...))
|
2015-01-26 17:52:50 +00:00
|
|
|
for i, test := range tests {
|
2018-10-03 05:29:35 +00:00
|
|
|
out, err := Merge(codec, test.obj, test.fragment)
|
2015-01-15 03:29:13 +00:00
|
|
|
if !test.expectErr {
|
|
|
|
if err != nil {
|
2015-01-31 18:59:08 +00:00
|
|
|
t.Errorf("testcase[%d], unexpected error: %v", i, err)
|
2018-10-03 05:29:35 +00:00
|
|
|
} else if !apiequality.Semantic.DeepEqual(test.expected, out) {
|
2015-01-31 18:59:08 +00:00
|
|
|
t.Errorf("\n\ntestcase[%d]\nexpected:\n%+v\nsaw:\n%+v", i, test.expected, out)
|
2015-01-15 03:29:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if test.expectErr && err == nil {
|
2015-01-31 18:59:08 +00:00
|
|
|
t.Errorf("testcase[%d], unexpected non-error", i)
|
2015-01-15 03:29:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-07 10:00:45 +00:00
|
|
|
}
|
|
|
|
|
2016-08-08 18:24:01 +00:00
|
|
|
type checkErrTestCase struct {
|
|
|
|
err error
|
|
|
|
expectedErr string
|
|
|
|
expectedCode int
|
|
|
|
}
|
|
|
|
|
2015-06-03 16:13:01 +00:00
|
|
|
func TestCheckInvalidErr(t *testing.T) {
|
2016-08-08 18:24:01 +00:00
|
|
|
testCheckError(t, []checkErrTestCase{
|
2015-06-03 16:13:01 +00:00
|
|
|
{
|
2018-10-03 05:29:35 +00:00
|
|
|
errors.NewInvalid(corev1.SchemeGroupVersion.WithKind("Invalid1").GroupKind(), "invalidation", field.ErrorList{field.Invalid(field.NewPath("field"), "single", "details")}),
|
2016-08-08 18:24:01 +00:00
|
|
|
"The Invalid1 \"invalidation\" is invalid: field: Invalid value: \"single\": details\n",
|
|
|
|
DefaultErrorExitCode,
|
2015-06-03 16:13:01 +00:00
|
|
|
},
|
|
|
|
{
|
2018-10-03 05:29:35 +00:00
|
|
|
errors.NewInvalid(corev1.SchemeGroupVersion.WithKind("Invalid2").GroupKind(), "invalidation", field.ErrorList{field.Invalid(field.NewPath("field1"), "multi1", "details"), field.Invalid(field.NewPath("field2"), "multi2", "details")}),
|
2016-08-08 18:24:01 +00:00
|
|
|
"The Invalid2 \"invalidation\" is invalid: \n* field1: Invalid value: \"multi1\": details\n* field2: Invalid value: \"multi2\": details\n",
|
|
|
|
DefaultErrorExitCode,
|
2015-06-03 16:13:01 +00:00
|
|
|
},
|
|
|
|
{
|
2018-10-03 05:29:35 +00:00
|
|
|
errors.NewInvalid(corev1.SchemeGroupVersion.WithKind("Invalid3").GroupKind(), "invalidation", field.ErrorList{}),
|
2016-08-08 18:24:01 +00:00
|
|
|
"The Invalid3 \"invalidation\" is invalid",
|
|
|
|
DefaultErrorExitCode,
|
2016-08-12 21:54:30 +00:00
|
|
|
},
|
|
|
|
{
|
2018-10-03 05:29:35 +00:00
|
|
|
errors.NewInvalid(corev1.SchemeGroupVersion.WithKind("Invalid4").GroupKind(), "invalidation", field.ErrorList{field.Invalid(field.NewPath("field4"), "multi4", "details"), field.Invalid(field.NewPath("field4"), "multi4", "details")}),
|
2016-08-08 18:24:01 +00:00
|
|
|
"The Invalid4 \"invalidation\" is invalid: field4: Invalid value: \"multi4\": details\n",
|
|
|
|
DefaultErrorExitCode,
|
2015-06-03 16:13:01 +00:00
|
|
|
},
|
2016-08-08 18:24:01 +00:00
|
|
|
})
|
2015-06-03 16:13:01 +00:00
|
|
|
}
|
2015-07-15 12:10:47 +00:00
|
|
|
|
2016-02-08 15:41:31 +00:00
|
|
|
func TestCheckNoResourceMatchError(t *testing.T) {
|
2016-08-08 18:24:01 +00:00
|
|
|
testCheckError(t, []checkErrTestCase{
|
2016-02-08 15:41:31 +00:00
|
|
|
{
|
2016-11-21 02:55:31 +00:00
|
|
|
&meta.NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "foo"}},
|
2016-02-08 15:41:31 +00:00
|
|
|
`the server doesn't have a resource type "foo"`,
|
2016-08-08 18:24:01 +00:00
|
|
|
DefaultErrorExitCode,
|
2016-02-08 15:41:31 +00:00
|
|
|
},
|
|
|
|
{
|
2016-11-21 02:55:31 +00:00
|
|
|
&meta.NoResourceMatchError{PartialResource: schema.GroupVersionResource{Version: "theversion", Resource: "foo"}},
|
2016-02-08 15:41:31 +00:00
|
|
|
`the server doesn't have a resource type "foo" in version "theversion"`,
|
2016-08-08 18:24:01 +00:00
|
|
|
DefaultErrorExitCode,
|
2016-02-08 15:41:31 +00:00
|
|
|
},
|
|
|
|
{
|
2016-11-21 02:55:31 +00:00
|
|
|
&meta.NoResourceMatchError{PartialResource: schema.GroupVersionResource{Group: "thegroup", Version: "theversion", Resource: "foo"}},
|
2016-02-08 15:41:31 +00:00
|
|
|
`the server doesn't have a resource type "foo" in group "thegroup" and version "theversion"`,
|
2016-08-08 18:24:01 +00:00
|
|
|
DefaultErrorExitCode,
|
2016-02-08 15:41:31 +00:00
|
|
|
},
|
|
|
|
{
|
2016-11-21 02:55:31 +00:00
|
|
|
&meta.NoResourceMatchError{PartialResource: schema.GroupVersionResource{Group: "thegroup", Resource: "foo"}},
|
2016-02-08 15:41:31 +00:00
|
|
|
`the server doesn't have a resource type "foo" in group "thegroup"`,
|
2016-08-08 18:24:01 +00:00
|
|
|
DefaultErrorExitCode,
|
2016-02-08 15:41:31 +00:00
|
|
|
},
|
2016-08-08 18:24:01 +00:00
|
|
|
})
|
|
|
|
}
|
2016-02-08 15:41:31 +00:00
|
|
|
|
2016-08-08 18:25:10 +00:00
|
|
|
func TestCheckExitError(t *testing.T) {
|
|
|
|
testCheckError(t, []checkErrTestCase{
|
|
|
|
{
|
2017-07-19 05:58:53 +00:00
|
|
|
exec.CodeExitError{Err: fmt.Errorf("pod foo/bar terminated"), Code: 42},
|
2017-07-07 02:46:22 +00:00
|
|
|
"pod foo/bar terminated",
|
2016-08-08 18:25:10 +00:00
|
|
|
42,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:24:01 +00:00
|
|
|
func testCheckError(t *testing.T, tests []checkErrTestCase) {
|
2016-02-08 15:41:31 +00:00
|
|
|
var errReturned string
|
2016-08-08 18:24:01 +00:00
|
|
|
var codeReturned int
|
|
|
|
errHandle := func(err string, code int) {
|
2016-02-08 15:41:31 +00:00
|
|
|
errReturned = err
|
2016-08-08 18:24:01 +00:00
|
|
|
codeReturned = code
|
2016-02-08 15:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2017-06-14 21:14:42 +00:00
|
|
|
checkErr(test.err, errHandle)
|
2016-02-08 15:41:31 +00:00
|
|
|
|
2016-08-08 18:24:01 +00:00
|
|
|
if errReturned != test.expectedErr {
|
|
|
|
t.Fatalf("Got: %s, expected: %s", errReturned, test.expectedErr)
|
|
|
|
}
|
|
|
|
if codeReturned != test.expectedCode {
|
|
|
|
t.Fatalf("Got: %d, expected: %d", codeReturned, test.expectedCode)
|
2016-02-08 15:41:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 12:10:47 +00:00
|
|
|
func TestDumpReaderToFile(t *testing.T) {
|
|
|
|
testString := "TEST STRING"
|
|
|
|
tempFile, err := ioutil.TempFile("", "hlpers_test_dump_")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error setting up a temporary file %v", err)
|
|
|
|
}
|
|
|
|
defer syscall.Unlink(tempFile.Name())
|
|
|
|
defer tempFile.Close()
|
2016-09-28 08:52:55 +00:00
|
|
|
defer func() {
|
|
|
|
if !t.Failed() {
|
|
|
|
os.Remove(tempFile.Name())
|
|
|
|
}
|
|
|
|
}()
|
2015-07-15 12:10:47 +00:00
|
|
|
err = DumpReaderToFile(strings.NewReader(testString), tempFile.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error in DumpReaderToFile: %v", err)
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadFile(tempFile.Name())
|
|
|
|
if err != nil {
|
2015-08-08 01:52:23 +00:00
|
|
|
t.Errorf("error when reading %s: %v", tempFile.Name(), err)
|
2015-07-15 12:10:47 +00:00
|
|
|
}
|
|
|
|
stringData := string(data)
|
|
|
|
if stringData != testString {
|
|
|
|
t.Fatalf("Wrong file content %s != %s", testString, stringData)
|
|
|
|
}
|
|
|
|
}
|