Fix tests to pass with changed external types

This commit is contained in:
Clayton Coleman 2015-11-18 13:24:54 -05:00
parent af7e7f146d
commit 9d19238f6c
7 changed files with 51 additions and 33 deletions

View File

@ -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
}
}

View File

@ -110,8 +110,13 @@ func TestSpecificKind(t *testing.T) {
api.Scheme.Log(t)
defer api.Scheme.Log(nil)
kind := "Pod"
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
}
}
}
}

View File

@ -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 = &parallelism
},
@ -227,14 +238,6 @@ 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))]
},
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.
@ -244,7 +247,14 @@ func FuzzerFor(t *testing.T, version string, src rand.Source) *fuzz.Fuzzer {
m.FieldRef.APIVersion = versions[c.Rand.Intn(len(versions))]
m.FieldRef.FieldPath = c.RandString()
},
).Fuzz(v.Interface())
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()))
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

View File

@ -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{

View File

@ -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
}

View File

@ -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())
}
}

View File

@ -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{