2015-01-29 00:57:33 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
2015-04-20 23:29:26 +00:00
|
|
|
"reflect"
|
2015-01-29 00:57:33 +00:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
|
2015-03-22 21:43:00 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2015-01-29 00:57:33 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2015-01-28 04:50:01 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
2015-01-29 00:57:33 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
|
|
"github.com/fsouza/go-dockerclient"
|
|
|
|
"github.com/google/gofuzz"
|
2015-03-20 20:50:06 +00:00
|
|
|
|
2015-01-29 00:57:33 +00:00
|
|
|
"speter.net/go/exp/math/dec/inf"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FuzzerFor can randomly populate api objects that are destined for version.
|
|
|
|
func FuzzerFor(t *testing.T, version string, src rand.Source) *fuzz.Fuzzer {
|
|
|
|
f := fuzz.New().NilChance(.5).NumElements(1, 1)
|
|
|
|
if src != nil {
|
|
|
|
f.RandSource(src)
|
|
|
|
}
|
|
|
|
f.Funcs(
|
|
|
|
func(j *runtime.PluginBase, c fuzz.Continue) {
|
|
|
|
// Do nothing; this struct has only a Kind field and it must stay blank in memory.
|
|
|
|
},
|
|
|
|
func(j *runtime.TypeMeta, c fuzz.Continue) {
|
|
|
|
// We have to customize the randomization of TypeMetas because their
|
|
|
|
// APIVersion and Kind must remain blank in memory.
|
|
|
|
j.APIVersion = ""
|
|
|
|
j.Kind = ""
|
|
|
|
},
|
|
|
|
func(j *api.TypeMeta, c fuzz.Continue) {
|
|
|
|
// We have to customize the randomization of TypeMetas because their
|
|
|
|
// APIVersion and Kind must remain blank in memory.
|
|
|
|
j.APIVersion = ""
|
|
|
|
j.Kind = ""
|
|
|
|
},
|
|
|
|
func(j *api.ObjectMeta, c fuzz.Continue) {
|
|
|
|
j.Name = c.RandString()
|
|
|
|
j.ResourceVersion = strconv.FormatUint(c.RandUint64(), 10)
|
|
|
|
j.SelfLink = c.RandString()
|
2015-01-28 04:50:01 +00:00
|
|
|
j.UID = types.UID(c.RandString())
|
|
|
|
j.GenerateName = c.RandString()
|
2015-01-29 00:57:33 +00:00
|
|
|
|
|
|
|
var sec, nsec int64
|
|
|
|
c.Fuzz(&sec)
|
|
|
|
c.Fuzz(&nsec)
|
|
|
|
j.CreationTimestamp = util.Unix(sec, nsec).Rfc3339Copy()
|
|
|
|
},
|
|
|
|
func(j *api.ObjectReference, c fuzz.Continue) {
|
|
|
|
// We have to customize the randomization of TypeMetas because their
|
|
|
|
// APIVersion and Kind must remain blank in memory.
|
|
|
|
j.APIVersion = c.RandString()
|
|
|
|
j.Kind = c.RandString()
|
|
|
|
j.Namespace = c.RandString()
|
|
|
|
j.Name = c.RandString()
|
|
|
|
j.ResourceVersion = strconv.FormatUint(c.RandUint64(), 10)
|
|
|
|
j.FieldPath = c.RandString()
|
|
|
|
},
|
|
|
|
func(j *api.ListMeta, c fuzz.Continue) {
|
|
|
|
j.ResourceVersion = strconv.FormatUint(c.RandUint64(), 10)
|
|
|
|
j.SelfLink = c.RandString()
|
|
|
|
},
|
2015-03-22 21:43:00 +00:00
|
|
|
func(j *api.ListOptions, c fuzz.Continue) {
|
|
|
|
// TODO: add some parsing
|
|
|
|
j.LabelSelector, _ = labels.Parse("a=b")
|
|
|
|
j.FieldSelector, _ = fields.ParseSelector("a=b")
|
|
|
|
},
|
2015-01-29 00:57:33 +00:00
|
|
|
func(j *api.PodPhase, c fuzz.Continue) {
|
|
|
|
statuses := []api.PodPhase{api.PodPending, api.PodRunning, api.PodFailed, api.PodUnknown}
|
|
|
|
*j = statuses[c.Rand.Intn(len(statuses))]
|
|
|
|
},
|
2015-01-26 17:52:50 +00:00
|
|
|
func(j *api.PodTemplateSpec, c fuzz.Continue) {
|
|
|
|
// TODO: v1beta1/2 can't round trip a nil template correctly, fix by having v1beta1/2
|
|
|
|
// conversion compare converted object to nil via DeepEqual
|
|
|
|
j.ObjectMeta = api.ObjectMeta{}
|
|
|
|
c.Fuzz(&j.ObjectMeta)
|
|
|
|
j.ObjectMeta = api.ObjectMeta{Labels: j.ObjectMeta.Labels}
|
|
|
|
j.Spec = api.PodSpec{}
|
|
|
|
c.Fuzz(&j.Spec)
|
|
|
|
},
|
2015-03-04 20:55:41 +00:00
|
|
|
func(j *api.Binding, c fuzz.Continue) {
|
|
|
|
c.Fuzz(&j.ObjectMeta)
|
|
|
|
j.Target.Name = c.RandString()
|
|
|
|
},
|
2015-01-29 00:57:33 +00:00
|
|
|
func(j *api.ReplicationControllerSpec, c fuzz.Continue) {
|
2015-03-07 08:04:14 +00:00
|
|
|
c.FuzzNoCustom(j) // fuzz self without calling this function again
|
|
|
|
j.TemplateRef = nil // this is required for round trip
|
2015-01-29 00:57:33 +00:00
|
|
|
},
|
|
|
|
func(j *api.ReplicationControllerStatus, c fuzz.Continue) {
|
|
|
|
// only replicas round trips
|
|
|
|
j.Replicas = int(c.RandUint64())
|
|
|
|
},
|
|
|
|
func(j *api.List, c fuzz.Continue) {
|
2015-03-07 08:04:14 +00:00
|
|
|
c.FuzzNoCustom(j) // fuzz self without calling this function again
|
2015-01-29 00:57:33 +00:00
|
|
|
if j.Items == nil {
|
|
|
|
j.Items = []runtime.Object{}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
func(j *runtime.Object, c fuzz.Continue) {
|
|
|
|
if c.RandBool() {
|
|
|
|
*j = &runtime.Unknown{
|
|
|
|
TypeMeta: runtime.TypeMeta{Kind: "Something", APIVersion: "unknown"},
|
|
|
|
RawJSON: []byte(`{"apiVersion":"unknown","kind":"Something","someKey":"someValue"}`),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
types := []runtime.Object{&api.Pod{}, &api.ReplicationController{}}
|
|
|
|
t := types[c.Rand.Intn(len(types))]
|
|
|
|
c.Fuzz(t)
|
|
|
|
*j = t
|
|
|
|
}
|
|
|
|
},
|
|
|
|
func(pb map[docker.Port][]docker.PortBinding, c fuzz.Continue) {
|
|
|
|
// This is necessary because keys with nil values get omitted.
|
|
|
|
// TODO: Is this a bug?
|
|
|
|
pb[docker.Port(c.RandString())] = []docker.PortBinding{
|
|
|
|
{c.RandString(), c.RandString()},
|
|
|
|
{c.RandString(), c.RandString()},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
func(pm map[string]docker.PortMapping, c fuzz.Continue) {
|
|
|
|
// This is necessary because keys with nil values get omitted.
|
|
|
|
// TODO: Is this a bug?
|
|
|
|
pm[c.RandString()] = docker.PortMapping{
|
|
|
|
c.RandString(): c.RandString(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
func(q *resource.Quantity, c fuzz.Continue) {
|
|
|
|
// Real Quantity fuzz testing is done elsewhere;
|
|
|
|
// this limited subset of functionality survives
|
|
|
|
// round-tripping to v1beta1/2.
|
|
|
|
q.Amount = &inf.Dec{}
|
|
|
|
q.Format = resource.DecimalExponent
|
|
|
|
//q.Amount.SetScale(inf.Scale(-c.Intn(12)))
|
|
|
|
q.Amount.SetUnscaled(c.Int63n(1000))
|
|
|
|
},
|
|
|
|
func(p *api.PullPolicy, c fuzz.Continue) {
|
|
|
|
policies := []api.PullPolicy{api.PullAlways, api.PullNever, api.PullIfNotPresent}
|
|
|
|
*p = policies[c.Rand.Intn(len(policies))]
|
|
|
|
},
|
2015-01-26 17:52:50 +00:00
|
|
|
func(rp *api.RestartPolicy, c fuzz.Continue) {
|
2015-03-14 01:38:07 +00:00
|
|
|
policies := []api.RestartPolicy{api.RestartPolicyAlways, api.RestartPolicyNever, api.RestartPolicyOnFailure}
|
|
|
|
*rp = policies[c.Rand.Intn(len(policies))]
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
|
|
|
func(vs *api.VolumeSource, c fuzz.Continue) {
|
2015-04-20 23:29:26 +00:00
|
|
|
// 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.
|
|
|
|
fuzz.New().NilChance(0).NumElements(1, 1).Fuzz(v.Interface())
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
|
|
|
func(d *api.DNSPolicy, c fuzz.Continue) {
|
|
|
|
policies := []api.DNSPolicy{api.DNSClusterFirst, api.DNSDefault}
|
|
|
|
*d = policies[c.Rand.Intn(len(policies))]
|
|
|
|
},
|
|
|
|
func(p *api.Protocol, c fuzz.Continue) {
|
|
|
|
protocols := []api.Protocol{api.ProtocolTCP, api.ProtocolUDP}
|
|
|
|
*p = protocols[c.Rand.Intn(len(protocols))]
|
|
|
|
},
|
|
|
|
func(p *api.AffinityType, c fuzz.Continue) {
|
|
|
|
types := []api.AffinityType{api.AffinityTypeClientIP, api.AffinityTypeNone}
|
|
|
|
*p = types[c.Rand.Intn(len(types))]
|
|
|
|
},
|
|
|
|
func(ct *api.Container, c fuzz.Continue) {
|
2015-03-07 08:04:14 +00:00
|
|
|
c.FuzzNoCustom(ct) // fuzz self without calling this function again
|
|
|
|
ct.TerminationMessagePath = "/" + ct.TerminationMessagePath // Must be non-empty
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
2015-02-06 02:21:01 +00:00
|
|
|
func(e *api.Event, c fuzz.Continue) {
|
2015-03-07 08:04:14 +00:00
|
|
|
c.FuzzNoCustom(e) // fuzz self without calling this function again
|
2015-02-06 02:21:01 +00:00
|
|
|
// Fix event count to 1, otherwise, if a v1beta1 or v1beta2 event has a count set arbitrarily, it's count is ignored
|
|
|
|
if e.FirstTimestamp.IsZero() {
|
|
|
|
e.Count = 1
|
|
|
|
} else {
|
|
|
|
c.Fuzz(&e.Count)
|
|
|
|
}
|
|
|
|
},
|
2015-02-18 01:24:50 +00:00
|
|
|
func(s *api.Secret, c fuzz.Continue) {
|
2015-03-07 08:04:14 +00:00
|
|
|
c.FuzzNoCustom(s) // fuzz self without calling this function again
|
2015-02-18 01:24:50 +00:00
|
|
|
s.Type = api.SecretTypeOpaque
|
|
|
|
},
|
2015-03-20 16:48:12 +00:00
|
|
|
func(s *api.NamespaceSpec, c fuzz.Continue) {
|
|
|
|
s.Finalizers = []api.FinalizerName{api.FinalizerKubernetes}
|
|
|
|
},
|
2015-03-10 15:21:09 +00:00
|
|
|
func(s *api.NamespaceStatus, c fuzz.Continue) {
|
|
|
|
s.Phase = api.NamespaceActive
|
|
|
|
},
|
2015-02-28 01:33:58 +00:00
|
|
|
func(http *api.HTTPGetAction, c fuzz.Continue) {
|
2015-03-07 08:04:14 +00:00
|
|
|
c.FuzzNoCustom(http) // fuzz self without calling this function again
|
|
|
|
http.Path = "/" + http.Path // can't be blank
|
2015-02-28 01:33:58 +00:00
|
|
|
},
|
2015-03-01 21:34:57 +00:00
|
|
|
func(ss *api.ServiceSpec, c fuzz.Continue) {
|
2015-03-07 08:04:14 +00:00
|
|
|
c.FuzzNoCustom(ss) // fuzz self without calling this function again
|
2015-03-13 15:16:41 +00:00
|
|
|
if len(ss.Ports) == 0 {
|
|
|
|
// There must be at least 1 port.
|
|
|
|
ss.Ports = append(ss.Ports, api.ServicePort{})
|
|
|
|
c.Fuzz(&ss.Ports[0])
|
|
|
|
}
|
|
|
|
for i := range ss.Ports {
|
|
|
|
switch ss.Ports[i].TargetPort.Kind {
|
|
|
|
case util.IntstrInt:
|
|
|
|
ss.Ports[i].TargetPort.IntVal = 1 + ss.Ports[i].TargetPort.IntVal%65535 // non-zero
|
|
|
|
case util.IntstrString:
|
|
|
|
ss.Ports[i].TargetPort.StrVal = "x" + ss.Ports[i].TargetPort.StrVal // non-empty
|
|
|
|
}
|
2015-03-01 21:34:57 +00:00
|
|
|
}
|
|
|
|
},
|
2015-04-01 23:11:33 +00:00
|
|
|
func(n *api.Node, c fuzz.Continue) {
|
|
|
|
c.FuzzNoCustom(n)
|
|
|
|
n.Spec.ExternalID = "external"
|
|
|
|
},
|
2015-01-29 00:57:33 +00:00
|
|
|
)
|
|
|
|
return f
|
|
|
|
}
|