Merge pull request #9225 from caesarxuchao/add-v1-tests-general

Add v1 tests
This commit is contained in:
Brian Grant
2015-06-03 22:18:21 -07:00
19 changed files with 78 additions and 254 deletions

View File

@@ -21,6 +21,7 @@ import (
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/registered"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/securitycontext"
@@ -56,11 +57,6 @@ func TestDecodeSinglePod(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
parsed, podOut, err := tryDecodeSinglePod(json, noDefault)
if testapi.Version() == "v1beta1" {
// v1beta1 conversion leaves empty lists that should be nil
podOut.Spec.Containers[0].Resources.Limits = nil
podOut.Spec.Containers[0].Resources.Requests = nil
}
if !parsed {
t.Errorf("expected to have parsed file: (%s)", string(json))
}
@@ -71,24 +67,26 @@ func TestDecodeSinglePod(t *testing.T) {
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(json))
}
externalPod, err := testapi.Converter().ConvertToVersion(pod, "v1beta3")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
yaml, err := yaml.Marshal(externalPod)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
for _, version := range registered.RegisteredVersions {
externalPod, err := testapi.Converter().ConvertToVersion(pod, version)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
yaml, err := yaml.Marshal(externalPod)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
parsed, podOut, err = tryDecodeSinglePod(yaml, noDefault)
if !parsed {
t.Errorf("expected to have parsed file: (%s)", string(yaml))
}
if err != nil {
t.Errorf("unexpected error: %v (%s)", err, string(yaml))
}
if !reflect.DeepEqual(pod, podOut) {
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(yaml))
parsed, podOut, err = tryDecodeSinglePod(yaml, noDefault)
if !parsed {
t.Errorf("expected to have parsed file: (%s)", string(yaml))
}
if err != nil {
t.Errorf("unexpected error: %v (%s)", err, string(yaml))
}
if !reflect.DeepEqual(pod, podOut) {
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(yaml))
}
}
}
@@ -137,23 +135,25 @@ func TestDecodePodList(t *testing.T) {
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", podList, &podListOut, string(json))
}
externalPodList, err := testapi.Converter().ConvertToVersion(podList, "v1beta3")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
yaml, err := yaml.Marshal(externalPodList)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
for _, version := range registered.RegisteredVersions {
externalPodList, err := testapi.Converter().ConvertToVersion(podList, version)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
yaml, err := yaml.Marshal(externalPodList)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
parsed, podListOut, err = tryDecodePodList(yaml, noDefault)
if !parsed {
t.Errorf("expected to have parsed file: (%s)", string(yaml))
}
if err != nil {
t.Errorf("unexpected error: %v (%s)", err, string(yaml))
}
if !reflect.DeepEqual(podList, &podListOut) {
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, &podListOut, string(yaml))
parsed, podListOut, err = tryDecodePodList(yaml, noDefault)
if !parsed {
t.Errorf("expected to have parsed file: (%s)", string(yaml))
}
if err != nil {
t.Errorf("unexpected error: %v (%s)", err, string(yaml))
}
if !reflect.DeepEqual(podList, &podListOut) {
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, &podListOut, string(yaml))
}
}
}

View File

@@ -17,7 +17,6 @@ limitations under the License.
package config
import (
"fmt"
"io/ioutil"
"os"
"testing"
@@ -68,99 +67,6 @@ func writeTestFile(t *testing.T, dir, name string, contents string) *os.File {
return file
}
func TestReadContainerManifestFromFile(t *testing.T) {
// ContainerManifest is supported only for pre v1beta3 versions.
if !api.PreV1Beta3(testapi.Version()) {
return
}
hostname := "random-test-hostname"
var testCases = []struct {
desc string
fileContents string
expected kubelet.PodUpdate
}{
{
desc: "Manifest",
fileContents: fmt.Sprintf(`{
"version": "%s",
"uuid": "12345",
"id": "test",
"containers": [{ "name": "image", "image": "test/image", "imagePullPolicy": "PullAlways"}]
}`, testapi.Version()),
expected: CreatePodUpdate(kubelet.SET, kubelet.FileSource, &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "test-" + hostname,
UID: "12345",
Namespace: kubelet.NamespaceDefault,
SelfLink: getSelfLink("test-"+hostname, kubelet.NamespaceDefault),
},
Spec: api.PodSpec{
NodeName: hostname,
RestartPolicy: api.RestartPolicyAlways,
DNSPolicy: api.DNSClusterFirst,
Containers: []api.Container{{
Name: "image",
Image: "test/image",
TerminationMessagePath: "/dev/termination-log",
ImagePullPolicy: "Always",
SecurityContext: securitycontext.ValidSecurityContextWithContainerDefaults()}},
},
}),
},
{
desc: "Manifest without ID",
fileContents: fmt.Sprintf(`{
"version": "%s",
"uuid": "12345",
"containers": [{ "name": "image", "image": "test/image", "imagePullPolicy": "PullAlways"}]
}`, testapi.Version()),
expected: CreatePodUpdate(kubelet.SET, kubelet.FileSource, &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "12345-" + hostname,
UID: "12345",
Namespace: kubelet.NamespaceDefault,
SelfLink: getSelfLink("12345-"+hostname, kubelet.NamespaceDefault),
},
Spec: api.PodSpec{
NodeName: hostname,
RestartPolicy: api.RestartPolicyAlways,
DNSPolicy: api.DNSClusterFirst,
Containers: []api.Container{{
Name: "image",
Image: "test/image",
TerminationMessagePath: "/dev/termination-log",
ImagePullPolicy: "Always",
SecurityContext: securitycontext.ValidSecurityContextWithContainerDefaults()}},
},
}),
},
}
for _, testCase := range testCases {
func() {
file := writeTestFile(t, os.TempDir(), "test_pod_config", testCase.fileContents)
defer os.Remove(file.Name())
ch := make(chan interface{})
NewSourceFile(file.Name(), hostname, time.Millisecond, ch)
select {
case got := <-ch:
update := got.(kubelet.PodUpdate)
for _, pod := range update.Pods {
if errs := validation.ValidatePod(pod); len(errs) > 0 {
t.Errorf("%s: Invalid pod %#v, %#v", testCase.desc, pod, errs)
}
}
if !api.Semantic.DeepEqual(testCase.expected, update) {
t.Errorf("%s: Expected %#v, Got %#v", testCase.desc, testCase.expected, update)
}
case <-time.After(time.Second):
t.Errorf("%s: Expected update, timeout instead", testCase.desc)
}
}()
}
}
func TestReadPodsFromFile(t *testing.T) {
hostname := "random-test-hostname"
var testCases = []struct {
@@ -276,32 +182,6 @@ func TestReadPodsFromFile(t *testing.T) {
}
}
func TestReadManifestFromFileWithDefaults(t *testing.T) {
if !api.PreV1Beta3(testapi.Version()) {
return
}
file := writeTestFile(t, os.TempDir(), "test_pod_config",
fmt.Sprintf(`{
"version": "%s",
"id": "test",
"containers": [{ "name": "image", "image": "test/image" }]
}`, testapi.Version()))
defer os.Remove(file.Name())
ch := make(chan interface{})
NewSourceFile(file.Name(), "localhost", time.Millisecond, ch)
select {
case got := <-ch:
update := got.(kubelet.PodUpdate)
if update.Pods[0].UID == "" {
t.Errorf("Unexpected UID: %s", update.Pods[0].UID)
}
case <-time.After(time.Second):
t.Errorf("Expected update, timeout instead")
}
}
func TestExtractFromBadDataFile(t *testing.T) {
file := writeTestFile(t, os.TempDir(), "test_pod_config", string([]byte{1, 2, 3}))
defer os.Remove(file.Name())