From 9d19238f6c343c436328f517fda4ca690978ff85 Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Wed, 18 Nov 2015 13:24:54 -0500 Subject: [PATCH] Fix tests to pass with changed external types --- .../mesos/pkg/service/endpoints_controller.go | 4 +- pkg/api/serialization_test.go | 12 ++++- pkg/api/testing/fuzzer.go | 46 +++++++++++-------- pkg/api/v1/defaults_test.go | 12 ++--- pkg/apis/extensions/v1beta1/defaults_test.go | 6 +-- pkg/client/unversioned/request_test.go | 2 +- pkg/kubectl/sorting_printer_test.go | 2 +- 7 files changed, 51 insertions(+), 33 deletions(-) diff --git a/contrib/mesos/pkg/service/endpoints_controller.go b/contrib/mesos/pkg/service/endpoints_controller.go index 2f737b3184e..c8ae99f18da 100644 --- a/contrib/mesos/pkg/service/endpoints_controller.go +++ b/contrib/mesos/pkg/service/endpoints_controller.go @@ -430,8 +430,8 @@ func findPort(pod *api.Pod, svcPort *api.ServicePort) (int, int, error) { p := portName.IntVal for _, container := range pod.Spec.Containers { for _, port := range container.Ports { - if port.ContainerPort == p && port.Protocol == svcPort.Protocol { - hostPort, err := findMappedPort(pod, port.Protocol, p) + if port.ContainerPort == int(p) && port.Protocol == svcPort.Protocol { + hostPort, err := findMappedPort(pod, port.Protocol, int(p)) return hostPort, port.ContainerPort, err } } diff --git a/pkg/api/serialization_test.go b/pkg/api/serialization_test.go index 247f2dbcac2..38f1d275455 100644 --- a/pkg/api/serialization_test.go +++ b/pkg/api/serialization_test.go @@ -110,8 +110,13 @@ func TestSpecificKind(t *testing.T) { api.Scheme.Log(t) defer api.Scheme.Log(nil) - kind := "Pod" - doRoundTripTest(kind, t) + kind := "JobList" + for i := 0; i < *fuzzIters; i++ { + doRoundTripTest(kind, t) + if t.Failed() { + break + } + } } func TestList(t *testing.T) { @@ -142,6 +147,9 @@ func TestRoundTripTypes(t *testing.T) { // Try a few times, since runTest uses random values. for i := 0; i < *fuzzIters; i++ { doRoundTripTest(kind, t) + if t.Failed() { + break + } } } } diff --git a/pkg/api/testing/fuzzer.go b/pkg/api/testing/fuzzer.go index 1d1e71b9dca..99b8ab94b2a 100644 --- a/pkg/api/testing/fuzzer.go +++ b/pkg/api/testing/fuzzer.go @@ -45,6 +45,17 @@ func FuzzerFor(t *testing.T, version string, src rand.Source) *fuzz.Fuzzer { f.RandSource(src) } f.Funcs( + func(j *int, c fuzz.Continue) { + *j = int(c.Int31()) + }, + func(j **int, c fuzz.Continue) { + if c.RandBool() { + i := int(c.Int31()) + *j = &i + } else { + *j = nil + } + }, func(j *runtime.PluginBase, c fuzz.Continue) { // Do nothing; this struct has only a Kind field and it must stay blank in memory. }, @@ -140,8 +151,8 @@ func FuzzerFor(t *testing.T, version string, src rand.Source) *fuzz.Fuzzer { }, func(j *extensions.JobSpec, c fuzz.Continue) { c.FuzzNoCustom(j) // fuzz self without calling this function again - completions := c.Rand.Int() - parallelism := c.Rand.Int() + completions := int(c.Rand.Int31()) + parallelism := int(c.Rand.Int31()) j.Completions = &completions j.Parallelism = ¶llelism }, @@ -227,24 +238,23 @@ func FuzzerFor(t *testing.T, version string, src rand.Source) *fuzz.Fuzzer { policies := []api.RestartPolicy{api.RestartPolicyAlways, api.RestartPolicyNever, api.RestartPolicyOnFailure} *rp = policies[c.Rand.Intn(len(policies))] }, + // Only api.DownwardAPIVolumeFile needs to have a specific func since FieldRef has to be + // defaulted to a version otherwise roundtrip will fail + // For the remaining volume plugins the default fuzzer is enough. + func(m *api.DownwardAPIVolumeFile, c fuzz.Continue) { + m.Path = c.RandString() + versions := []string{"v1"} + m.FieldRef.APIVersion = versions[c.Rand.Intn(len(versions))] + m.FieldRef.FieldPath = c.RandString() + }, func(vs *api.VolumeSource, c fuzz.Continue) { // Exactly one of the fields must be set. v := reflect.ValueOf(vs).Elem() i := int(c.RandUint64() % uint64(v.NumField())) - v = v.Field(i).Addr() - // Use a new fuzzer which cannot populate nil to ensure one field will be set. - f := fuzz.New().NilChance(0).NumElements(1, 1) - f.Funcs( - // Only api.DownwardAPIVolumeFile needs to have a specific func since FieldRef has to be - // defaulted to a version otherwise roundtrip will fail - // For the remaining volume plugins the default fuzzer is enough. - func(m *api.DownwardAPIVolumeFile, c fuzz.Continue) { - m.Path = c.RandString() - versions := []string{"v1"} - m.FieldRef.APIVersion = versions[c.Rand.Intn(len(versions))] - m.FieldRef.FieldPath = c.RandString() - }, - ).Fuzz(v.Interface()) + t := v.Field(i).Addr() + for v.Field(i).IsNil() { + c.Fuzz(t.Interface()) + } }, func(d *api.DNSPolicy, c fuzz.Continue) { policies := []api.DNSPolicy{api.DNSClusterFirst, api.DNSDefault} @@ -363,9 +373,9 @@ func FuzzerFor(t *testing.T, version string, src rand.Source) *fuzz.Fuzzer { }, func(s *extensions.HorizontalPodAutoscalerSpec, c fuzz.Continue) { c.FuzzNoCustom(s) // fuzz self without calling this function again - minReplicas := c.Rand.Int() + minReplicas := int(c.Rand.Int31()) s.MinReplicas = &minReplicas - s.CPUUtilization = &extensions.CPUTargetUtilization{TargetPercentage: int(c.RandUint64())} + s.CPUUtilization = &extensions.CPUTargetUtilization{TargetPercentage: int(c.Int31())} }, ) return f diff --git a/pkg/api/v1/defaults_test.go b/pkg/api/v1/defaults_test.go index aa34acb3570..a84381a68bb 100644 --- a/pkg/api/v1/defaults_test.go +++ b/pkg/api/v1/defaults_test.go @@ -156,8 +156,8 @@ func TestSetDefaultReplicationController(t *testing.T) { } } -func newInt(val int) *int { - p := new(int) +func newInt(val int32) *int32 { + p := new(int32) *p = val return p } @@ -165,7 +165,7 @@ func newInt(val int) *int { func TestSetDefaultReplicationControllerReplicas(t *testing.T) { tests := []struct { rc versioned.ReplicationController - expectReplicas int + expectReplicas int32 }{ { rc: versioned.ReplicationController{ @@ -345,13 +345,13 @@ func TestSetDefaultServicePort(t *testing.T) { if out.Spec.Ports[0].Protocol != versioned.ProtocolTCP { t.Errorf("Expected protocol %s, got %s", versioned.ProtocolTCP, out.Spec.Ports[0].Protocol) } - if out.Spec.Ports[0].TargetPort != intstr.FromInt(in.Spec.Ports[0].Port) { + if out.Spec.Ports[0].TargetPort != intstr.FromInt(int(in.Spec.Ports[0].Port)) { t.Errorf("Expected port %v, got %v", in.Spec.Ports[0].Port, out.Spec.Ports[0].TargetPort) } if out.Spec.Ports[1].Protocol != versioned.ProtocolTCP { t.Errorf("Expected protocol %s, got %s", versioned.ProtocolTCP, out.Spec.Ports[1].Protocol) } - if out.Spec.Ports[1].TargetPort != intstr.FromInt(in.Spec.Ports[1].Port) { + if out.Spec.Ports[1].TargetPort != intstr.FromInt(int(in.Spec.Ports[1].Port)) { t.Errorf("Expected port %v, got %v", in.Spec.Ports[1].Port, out.Spec.Ports[1].TargetPort) } } @@ -367,7 +367,7 @@ func TestSetDefaultNamespace(t *testing.T) { } func TestSetDefaultPodSpecHostNetwork(t *testing.T) { - portNum := 8080 + portNum := int32(8080) s := versioned.PodSpec{} s.HostNetwork = true s.Containers = []versioned.Container{ diff --git a/pkg/apis/extensions/v1beta1/defaults_test.go b/pkg/apis/extensions/v1beta1/defaults_test.go index 98c28d61eb1..11d9f03c384 100644 --- a/pkg/apis/extensions/v1beta1/defaults_test.go +++ b/pkg/apis/extensions/v1beta1/defaults_test.go @@ -294,9 +294,9 @@ func roundTrip(t *testing.T, obj runtime.Object) runtime.Object { return obj3 } -func newInt(val int) *int { - p := new(int) - *p = val +func newInt(val int) *int32 { + p := new(int32) + *p = int32(val) return p } diff --git a/pkg/client/unversioned/request_test.go b/pkg/client/unversioned/request_test.go index a368e75d8e1..3be1b09b09d 100644 --- a/pkg/client/unversioned/request_test.go +++ b/pkg/client/unversioned/request_test.go @@ -347,7 +347,7 @@ func TestTransformResponse(t *testing.T) { t.Errorf("%d: response should have been transformable into APIStatus: %v", i, err) continue } - if status.Status().Code != test.Response.StatusCode { + if int(status.Status().Code) != test.Response.StatusCode { t.Errorf("%d: status code did not match response: %#v", i, status.Status()) } } diff --git a/pkg/kubectl/sorting_printer_test.go b/pkg/kubectl/sorting_printer_test.go index e7d2510e67c..8aed8a5b0af 100644 --- a/pkg/kubectl/sorting_printer_test.go +++ b/pkg/kubectl/sorting_printer_test.go @@ -33,7 +33,7 @@ func encodeOrDie(obj runtime.Object) []byte { } func TestSortingPrinter(t *testing.T) { - intPtr := func(val int) *int { return &val } + intPtr := func(val int32) *int32 { return &val } a := &api.Pod{ ObjectMeta: api.ObjectMeta{