本文整理匯總了Golang中github.com/google/gofuzz.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: getContainer
func getContainer(name string) source_api.Container {
f := fuzz.New().NumElements(1, 1).NilChance(0)
containerSpec := cadvisor.ContainerSpec{
CreationTime: time.Unix(fakeContainerCreationTime, 0),
HasCpu: true,
HasMemory: true,
HasNetwork: true,
HasFilesystem: true,
HasDiskIo: true,
Cpu: cadvisor.CpuSpec{
Limit: 100,
},
Memory: cadvisor.MemorySpec{
Limit: 100,
},
}
containerStats := make([]*cadvisor.ContainerStats, 1)
f.Fuzz(&containerStats)
return source_api.Container{
Name: name,
Image: "gcr.io/" + name,
Spec: containerSpec,
Stats: containerStats,
}
}
示例2: TestAnonymousConfig
func TestAnonymousConfig(t *testing.T) {
f := fuzz.New().NilChance(0.0).NumElements(1, 1)
f.Funcs(
func(r *runtime.Codec, f fuzz.Continue) {},
func(r *http.RoundTripper, f fuzz.Continue) {},
func(fn *func(http.RoundTripper) http.RoundTripper, f fuzz.Continue) {},
)
for i := 0; i < 20; i++ {
original := &restclient.Config{}
f.Fuzz(original)
actual := AnonymousClientConfig(original)
expected := *original
// this is the list of known security related fields, add to this list if a new field
// is added to restclient.Config, update AnonymousClientConfig to preserve the field otherwise.
expected.Impersonate = ""
expected.BearerToken = ""
expected.Username = ""
expected.Password = ""
expected.TLSClientConfig.CertData = nil
expected.TLSClientConfig.CertFile = ""
expected.TLSClientConfig.KeyData = nil
expected.TLSClientConfig.KeyFile = ""
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("AnonymousClientConfig dropped unexpected fields, identify whether they are security related or not: %s", diff.ObjectGoPrintDiff(expected, actual))
}
}
}
示例3: TestFuzzMapToType
// This contains maps.
// Since map order is random, we can expect the encoding order to be random
// Therefore we cannot use binary compare.
func TestFuzzMapToType(t *testing.T) {
base := &TTestMaps{}
ff := &XTestMaps{}
f := fuzz.New()
f.NumElements(0, 50)
f.NilChance(0.1)
f.Funcs(fuzzTime)
for i := 0; i < 100; i++ {
f.RandSource(rand.New(rand.NewSource(int64(i * 5275))))
f.Fuzz(base)
ff = &XTestMaps{*base}
bufbase, err := json.Marshal(base)
require.NoError(t, err, "base[%T] failed to Marshal", base)
bufff, err := json.Marshal(ff)
require.NoError(t, err, "ff[%T] failed to Marshal", ff)
var baseD map[string]interface{}
var ffD map[string]interface{}
err = json.Unmarshal(bufbase, &baseD)
require.NoError(t, err, "ff[%T] failed to Unmarshal", base)
err = json.Unmarshal(bufff, &ffD)
require.NoError(t, err, "ff[%T] failed to Unmarshal", ff)
require.Equal(t, baseD, ffD, "Inspected struct difference of base[%T] != ff[%T]", base, ff)
}
}
示例4: TestFuzzInput
func TestFuzzInput(t *testing.T) {
var pods []*cache.PodElement
f := fuzz.New().NumElements(2, 10)
f.Fuzz(&pods)
_, err := NewDecoder().TimeseriesFromPods(pods)
assert.NoError(t, err)
}
示例5: getContainerElement
func getContainerElement(name string) *cache.ContainerElement {
f := fuzz.New().NumElements(1, 1).NilChance(0)
containerSpec := &cadvisor_api.ContainerSpec{
CreationTime: time.Unix(fakeContainerCreationTime, 0),
HasCpu: true,
HasMemory: true,
HasNetwork: true,
HasFilesystem: true,
HasDiskIo: true,
Cpu: cadvisor_api.CpuSpec{
Limit: 100,
},
Memory: cadvisor_api.MemorySpec{
Limit: 100,
},
}
containerStats := make([]*cadvisor_api.ContainerStats, 1)
f.Fuzz(&containerStats)
return &cache.ContainerElement{
Metadata: cache.Metadata{
Name: name,
},
Metrics: []*cache.ContainerMetricElement{
{
Spec: containerSpec,
Stats: containerStats[0],
},
},
}
}
示例6: TestFuzzInput
func TestFuzzInput(t *testing.T) {
var input source_api.AggregateData
fuzz.New().Fuzz(&input)
timeseries, err := NewDecoder().Timeseries(input)
assert.NoError(t, err)
assert.NotEmpty(t, timeseries)
}
示例7: TestGC
func TestGC(t *testing.T) {
var podEvictedCount int
var containerEvictedCount int
cache := NewCache(time.Millisecond, time.Second)
cache.AddCacheListener(CacheListener{
PodEvicted: func(namespace string, name string) {
podEvictedCount += 1
},
FreeContainerEvicted: func(hostname string, name string) {
containerEvictedCount += 1
},
})
var (
pods []source_api.Pod
containers []source_api.Container
)
f := fuzz.New().NumElements(2, 10).NilChance(0)
f.Fuzz(&pods)
f.Fuzz(&containers)
assert := assert.New(t)
assert.NoError(cache.StorePods(pods))
assert.NoError(cache.StoreContainers(containers))
zeroTime := time.Time{}
assert.NotEmpty(cache.GetFreeContainers(zeroTime, zeroTime))
assert.NotEmpty(cache.GetPods(zeroTime, zeroTime))
// Expect all data to be deleted after 2 seconds.
time.Sleep(10 * time.Second)
assert.Empty(cache.GetFreeContainers(zeroTime, zeroTime))
assert.Empty(cache.GetPods(zeroTime, zeroTime))
assert.Equal(len(pods), podEvictedCount)
assert.Equal(len(containers), containerEvictedCount)
}
示例8: getContainer
func getContainer(name string) source_api.Container {
f := fuzz.New().NumElements(2, 2).NilChance(0)
now := time.Now()
containerSpec := source_api.ContainerSpec{
ContainerSpec: cadvisor.ContainerSpec{
CreationTime: now,
HasCpu: true,
HasMemory: true,
HasNetwork: true,
HasFilesystem: true,
HasDiskIo: true,
},
}
containerStats := make([]*source_api.ContainerStats, 1)
f.Fuzz(&containerStats)
for idx := range containerStats {
containerStats[idx].Timestamp = now
}
return source_api.Container{
Name: name,
Spec: containerSpec,
Stats: containerStats,
Image: "gcr.io/" + name,
}
}
示例9: testTypeFuzzN
// Fuzz test for N iterations
func testTypeFuzzN(t *testing.T, base interface{}, ff interface{}, n int) {
require.Implements(t, (*json.Marshaler)(nil), ff)
require.Implements(t, (*json.Unmarshaler)(nil), ff)
require.Implements(t, (*marshalerFaster)(nil), ff)
require.Implements(t, (*unmarshalFaster)(nil), ff)
if _, ok := base.(unmarshalFaster); ok {
require.FailNow(t, "base should not have a UnmarshalJSONFFLexer")
}
if _, ok := base.(marshalerFaster); ok {
require.FailNow(t, "base should not have a MarshalJSONBuf")
}
f := fuzz.New()
f.NumElements(0, 1+n/40)
f.NilChance(0.2)
f.Funcs(fuzzTime, fuzzTimeSlice)
for i := 0; i < n; i++ {
f.RandSource(rand.New(rand.NewSource(int64(i * 5275))))
f.Fuzz(base)
f.RandSource(rand.New(rand.NewSource(int64(i * 5275))))
f.Fuzz(ff)
testSameMarshal(t, base, ff)
testCycle(t, base, ff)
}
}
示例10: TestSyncLastUpdated
func TestSyncLastUpdated(t *testing.T) {
as := assert.New(t)
s1 := &DummySink{}
c := cache.NewCache(time.Hour, time.Minute)
m, err := newExternalSinkManager([]sink_api.ExternalSink{s1}, c, time.Microsecond)
as.Nil(err)
var (
pods []source_api.Pod
containers []source_api.Container
events []*cache.Event
expectedESync, expectedPSync, expectedNSync time.Time
)
f := fuzz.New().NumElements(10, 10).NilChance(0)
f.Fuzz(&pods)
now := time.Now()
for pidx := range pods {
for cidx := range pods[pidx].Containers {
for sidx := range pods[pidx].Containers[cidx].Stats {
ts := now.Add(time.Duration(sidx) * time.Minute)
pods[pidx].Containers[cidx].Stats[sidx].Timestamp = ts
expectedPSync = hUtil.GetLatest(expectedPSync, ts)
}
}
}
f.Fuzz(&containers)
for cidx := range containers {
for sidx := range containers[cidx].Stats {
ts := now.Add(time.Duration(sidx) * time.Minute)
containers[cidx].Stats[sidx].Timestamp = ts
expectedNSync = hUtil.GetLatest(expectedNSync, ts)
}
}
f.Fuzz(&events)
for eidx := range events {
ts := now.Add(time.Duration(eidx) * time.Minute)
events[eidx].LastUpdate = ts
events[eidx].UID = fmt.Sprintf("id:%d", eidx)
expectedESync = hUtil.GetLatest(expectedESync, ts)
}
err = c.StorePods(pods)
if err != nil {
glog.Fatalf("Failed to store pods %v", err)
}
err = c.StoreContainers(containers)
if err != nil {
glog.Fatalf("Failed to store containers %v", err)
}
err = c.StoreEvents(events)
if err != nil {
glog.Fatalf("Failed to store events %v", err)
}
m.store()
as.Equal(m.lastSync.eventsSync, expectedESync, "Event now: %v, eventSync: %v, expected: %v", now, m.lastSync.eventsSync, expectedESync)
as.Equal(m.lastSync.podSync, expectedPSync, "Pod now: %v, podSync: %v, expected: %v", now, m.lastSync.podSync, expectedPSync)
as.Equal(m.lastSync.nodeSync, expectedNSync, "Node now: %v, nodeSync: %v, expected: %v", now, m.lastSync.nodeSync, expectedNSync)
}
示例11: BenchmarkMatchLen256
func BenchmarkMatchLen256(b *testing.B) {
size := 256
ta := make([]byte, size)
f := fuzz.New()
f.NumElements(size, size)
f.NilChance(0.0)
f.Fuzz(&ta)
b.SetBytes(int64(size))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = MatchLen(ta, ta, size)
}
}
示例12: TestGC
func TestGC(t *testing.T) {
var mutex sync.Mutex
var podEvictedCount int
var containerEvictedCount int
cache := NewCache(time.Millisecond, time.Second)
cache.AddCacheListener(CacheListener{
PodEvicted: func(namespace string, name string) {
mutex.Lock()
defer mutex.Unlock()
podEvictedCount += 1
},
FreeContainerEvicted: func(hostname string, name string) {
mutex.Lock()
defer mutex.Unlock()
containerEvictedCount += 1
},
})
var (
pods []source_api.Pod
containers []source_api.Container
)
f := fuzz.New().NumElements(2, 10).NilChance(0)
f.Fuzz(&pods)
f.Fuzz(&containers)
for i := range pods {
pods[i].ID = fmt.Sprintf("ID-%d", i)
pods[i].Name = fmt.Sprintf("%d-%s", i, pods[i].Name)
}
for i := range containers {
containers[i].Hostname = fmt.Sprintf("Node-%d", i%5)
containers[i].Name = fmt.Sprintf("%d-%s", i, containers[i].Name)
}
assert := assert.New(t)
assert.NoError(cache.StorePods(pods))
assert.NoError(cache.StoreContainers(containers))
zeroTime := time.Time{}
assert.NotEmpty(cache.GetFreeContainers(zeroTime, zeroTime))
assert.NotEmpty(cache.GetPods(zeroTime, zeroTime))
// Expect all data to be deleted after 2 seconds.
time.Sleep(10 * time.Second)
assert.Empty(cache.GetFreeContainers(zeroTime, zeroTime))
assert.Empty(cache.GetPods(zeroTime, zeroTime))
mutex.Lock()
defer mutex.Unlock()
assert.Equal(len(pods), podEvictedCount)
assert.Equal(len(containers), containerEvictedCount)
}
示例13: BenchmarkMatch8
func BenchmarkMatch8(b *testing.B) {
size := 32768
ta := make([]byte, size)
found := make([]int, 0, 10)
f := fuzz.New()
f.NumElements(size, size)
f.NilChance(0.0)
f.Fuzz(&ta)
b.SetBytes(int64(size))
b.ResetTimer()
for i := 0; i < b.N; i++ {
found = Match8(ta[800:808], ta, found)
}
}
示例14: Stop
func (reaper *DaemonSetReaper) Stop(namespace, name string, timeout time.Duration, gracePeriod *api.DeleteOptions) (string, error) {
ds, err := reaper.Experimental().DaemonSets(namespace).Get(name)
if err != nil {
return "", err
}
// Update the daemon set to select for a non-existent NodeName.
// The daemon set controller will then kill all the daemon pods corresponding to daemon set.
nodes, err := reaper.Nodes().List(labels.Everything(), fields.Everything())
if err != nil {
return "", err
}
var fuzzer = fuzz.New()
var nameExists bool
var nodeName string
fuzzer.Fuzz(&nodeName)
nameExists = false
for _, node := range nodes.Items {
nameExists = nameExists || node.Name == nodeName
}
if nameExists {
// Probability of reaching here is extremely low, most likely indicates a programming bug/library error.
return "", fmt.Errorf("Name collision generating an unused node name. Please retry this operation.")
}
ds.Spec.Template.Spec.NodeName = nodeName
// force update to avoid version conflict
ds.ResourceVersion = ""
if ds, err = reaper.Experimental().DaemonSets(namespace).Update(ds); err != nil {
return "", err
}
// Wait for the daemon set controller to kill all the daemon pods.
if err := wait.Poll(reaper.pollInterval, reaper.timeout, func() (bool, error) {
updatedDS, err := reaper.Experimental().DaemonSets(namespace).Get(name)
if err != nil {
return false, nil
}
return updatedDS.Status.CurrentNumberScheduled+updatedDS.Status.NumberMisscheduled == 0, nil
}); err != nil {
return "", err
}
if err := reaper.Experimental().DaemonSets(namespace).Delete(name); err != nil {
return "", err
}
return fmt.Sprintf("%s stopped", name), nil
}
示例15: BenchmarkMatch4Convert
// Shows the overhead of converting to bytes.
func BenchmarkMatch4Convert(b *testing.B) {
size := 1024
found := make([]int, 0, 10)
ta := make([]byte, size)
f := fuzz.New()
f.NumElements(size, size)
f.NilChance(0.0)
f.Fuzz(&ta)
txt := string(ta)
b.SetBytes(int64(size))
b.ResetTimer()
for i := 0; i < b.N; i++ {
found = Match4([]byte(txt[800:804]), []byte(txt), found)
}
}