mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 10:33:35 +00:00
vendor update for CSI 0.3.0
This commit is contained in:
9
vendor/k8s.io/utils/CONTRIBUTING.md
generated
vendored
Normal file
9
vendor/k8s.io/utils/CONTRIBUTING.md
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
# Contributing
|
||||
|
||||
Thanks for taking the time to join our community and start contributing!
|
||||
|
||||
The [Contributor Guide](https://github.com/kubernetes/community/blob/master/contributors/guide/README.md)
|
||||
provides detailed instructions on how to get your ideas and bug fixes seen and accepted.
|
||||
|
||||
Please remember to sign the [CNCF CLA](https://github.com/kubernetes/community/blob/master/CLA.md) and
|
||||
read and observe the [Code of Conduct](https://github.com/cncf/foundation/blob/master/code-of-conduct.md).
|
6
vendor/k8s.io/utils/README.md
generated
vendored
6
vendor/k8s.io/utils/README.md
generated
vendored
@ -45,6 +45,8 @@ an existing package to this repository.
|
||||
|
||||
- [Clock](/clock) provides an interface for time-based operations. It allows
|
||||
mocking time for testing.
|
||||
|
||||
- [Pointers](/pointers) provides some functions for pointer-based operations.
|
||||
|
||||
[Build Status]: https://travis-ci.org/kubernetes/utils.svg?branch=master
|
||||
[Go standard libs]: https://golang.org/pkg/#stdlib
|
||||
@ -55,3 +57,7 @@ an existing package to this repository.
|
||||
[kubeadm]: https://github.com/kubernetes/kubeadm
|
||||
[kubectl]: https://github.com/kubernetes/kubectl
|
||||
[instructions for moving]: ./HOWTOMOVE.md
|
||||
|
||||
## Contributing
|
||||
|
||||
Please see [CONTRIBUTING.md](CONTRIBUTING.md) for instructions on how to contribute.
|
||||
|
56
vendor/k8s.io/utils/clock/testing/fake_clock.go
generated
vendored
56
vendor/k8s.io/utils/clock/testing/fake_clock.go
generated
vendored
@ -34,7 +34,7 @@ type FakeClock struct {
|
||||
time time.Time
|
||||
|
||||
// waiters are waiting for the fake time to pass their specified time
|
||||
waiters []fakeClockWaiter
|
||||
waiters []*fakeClockWaiter
|
||||
}
|
||||
|
||||
type fakeClockWaiter struct {
|
||||
@ -45,6 +45,7 @@ type fakeClockWaiter struct {
|
||||
fired bool
|
||||
}
|
||||
|
||||
// NewFakeClock constructs a fake clock set to the provided time.
|
||||
func NewFakeClock(t time.Time) *FakeClock {
|
||||
return &FakeClock{
|
||||
time: t,
|
||||
@ -65,20 +66,20 @@ func (f *FakeClock) Since(ts time.Time) time.Duration {
|
||||
return f.time.Sub(ts)
|
||||
}
|
||||
|
||||
// Fake version of time.After(d).
|
||||
// After is the fake version of time.After(d).
|
||||
func (f *FakeClock) After(d time.Duration) <-chan time.Time {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
stopTime := f.time.Add(d)
|
||||
ch := make(chan time.Time, 1) // Don't block!
|
||||
f.waiters = append(f.waiters, fakeClockWaiter{
|
||||
f.waiters = append(f.waiters, &fakeClockWaiter{
|
||||
targetTime: stopTime,
|
||||
destChan: ch,
|
||||
})
|
||||
return ch
|
||||
}
|
||||
|
||||
// Fake version of time.NewTimer(d).
|
||||
// NewTimer constructs a fake timer, akin to time.NewTimer(d).
|
||||
func (f *FakeClock) NewTimer(d time.Duration) clock.Timer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
@ -91,16 +92,20 @@ func (f *FakeClock) NewTimer(d time.Duration) clock.Timer {
|
||||
destChan: ch,
|
||||
},
|
||||
}
|
||||
f.waiters = append(f.waiters, timer.waiter)
|
||||
f.waiters = append(f.waiters, &timer.waiter)
|
||||
return timer
|
||||
}
|
||||
|
||||
// Tick constructs a fake ticker, akin to time.Tick
|
||||
func (f *FakeClock) Tick(d time.Duration) <-chan time.Time {
|
||||
if d <= 0 {
|
||||
return nil
|
||||
}
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
tickTime := f.time.Add(d)
|
||||
ch := make(chan time.Time, 1) // hold one tick
|
||||
f.waiters = append(f.waiters, fakeClockWaiter{
|
||||
f.waiters = append(f.waiters, &fakeClockWaiter{
|
||||
targetTime: tickTime,
|
||||
stepInterval: d,
|
||||
skipIfBlocked: true,
|
||||
@ -110,14 +115,15 @@ func (f *FakeClock) Tick(d time.Duration) <-chan time.Time {
|
||||
return ch
|
||||
}
|
||||
|
||||
// Move clock by Duration, notify anyone that's called After, Tick, or NewTimer
|
||||
// Step moves the clock by Duration and notifies anyone that's called After,
|
||||
// Tick, or NewTimer.
|
||||
func (f *FakeClock) Step(d time.Duration) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
f.setTimeLocked(f.time.Add(d))
|
||||
}
|
||||
|
||||
// Sets the time.
|
||||
// SetTime sets the time.
|
||||
func (f *FakeClock) SetTime(t time.Time) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
@ -127,9 +133,9 @@ func (f *FakeClock) SetTime(t time.Time) {
|
||||
// Actually changes the time and checks any waiters. f must be write-locked.
|
||||
func (f *FakeClock) setTimeLocked(t time.Time) {
|
||||
f.time = t
|
||||
newWaiters := make([]fakeClockWaiter, 0, len(f.waiters))
|
||||
newWaiters := make([]*fakeClockWaiter, 0, len(f.waiters))
|
||||
for i := range f.waiters {
|
||||
w := &f.waiters[i]
|
||||
w := f.waiters[i]
|
||||
if !w.targetTime.After(t) {
|
||||
|
||||
if w.skipIfBlocked {
|
||||
@ -147,7 +153,7 @@ func (f *FakeClock) setTimeLocked(t time.Time) {
|
||||
for !w.targetTime.After(t) {
|
||||
w.targetTime = w.targetTime.Add(w.stepInterval)
|
||||
}
|
||||
newWaiters = append(newWaiters, *w)
|
||||
newWaiters = append(newWaiters, w)
|
||||
}
|
||||
|
||||
} else {
|
||||
@ -157,7 +163,7 @@ func (f *FakeClock) setTimeLocked(t time.Time) {
|
||||
f.waiters = newWaiters
|
||||
}
|
||||
|
||||
// Returns true if After has been called on f but not yet satisfied (so you can
|
||||
// HasWaiters returns true if After has been called on f but not yet satisfied (so you can
|
||||
// write race-free tests).
|
||||
func (f *FakeClock) HasWaiters() bool {
|
||||
f.lock.RLock()
|
||||
@ -165,6 +171,7 @@ func (f *FakeClock) HasWaiters() bool {
|
||||
return len(f.waiters) > 0
|
||||
}
|
||||
|
||||
// Sleep is akin to time.Sleep
|
||||
func (f *FakeClock) Sleep(d time.Duration) {
|
||||
f.Step(d)
|
||||
}
|
||||
@ -186,24 +193,25 @@ func (i *IntervalClock) Since(ts time.Time) time.Duration {
|
||||
return i.Time.Sub(ts)
|
||||
}
|
||||
|
||||
// Unimplemented, will panic.
|
||||
// After is unimplemented, will panic.
|
||||
// TODO: make interval clock use FakeClock so this can be implemented.
|
||||
func (*IntervalClock) After(d time.Duration) <-chan time.Time {
|
||||
panic("IntervalClock doesn't implement After")
|
||||
}
|
||||
|
||||
// Unimplemented, will panic.
|
||||
// NewTimer is unimplemented, will panic.
|
||||
// TODO: make interval clock use FakeClock so this can be implemented.
|
||||
func (*IntervalClock) NewTimer(d time.Duration) clock.Timer {
|
||||
panic("IntervalClock doesn't implement NewTimer")
|
||||
}
|
||||
|
||||
// Unimplemented, will panic.
|
||||
// Tick is unimplemented, will panic.
|
||||
// TODO: make interval clock use FakeClock so this can be implemented.
|
||||
func (*IntervalClock) Tick(d time.Duration) <-chan time.Time {
|
||||
panic("IntervalClock doesn't implement Tick")
|
||||
}
|
||||
|
||||
// Sleep is unimplemented, will panic.
|
||||
func (*IntervalClock) Sleep(d time.Duration) {
|
||||
panic("IntervalClock doesn't implement Sleep")
|
||||
}
|
||||
@ -226,11 +234,11 @@ func (f *fakeTimer) Stop() bool {
|
||||
f.fakeClock.lock.Lock()
|
||||
defer f.fakeClock.lock.Unlock()
|
||||
|
||||
newWaiters := make([]fakeClockWaiter, 0, len(f.fakeClock.waiters))
|
||||
newWaiters := make([]*fakeClockWaiter, 0, len(f.fakeClock.waiters))
|
||||
for i := range f.fakeClock.waiters {
|
||||
w := &f.fakeClock.waiters[i]
|
||||
w := f.fakeClock.waiters[i]
|
||||
if w != &f.waiter {
|
||||
newWaiters = append(newWaiters, *w)
|
||||
newWaiters = append(newWaiters, w)
|
||||
}
|
||||
}
|
||||
|
||||
@ -250,5 +258,17 @@ func (f *fakeTimer) Reset(d time.Duration) bool {
|
||||
f.waiter.fired = false
|
||||
f.waiter.targetTime = f.fakeClock.time.Add(d)
|
||||
|
||||
var isWaiting bool
|
||||
for i := range f.fakeClock.waiters {
|
||||
w := f.fakeClock.waiters[i]
|
||||
if w == &f.waiter {
|
||||
isWaiting = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isWaiting {
|
||||
f.fakeClock.waiters = append(f.fakeClock.waiters, &f.waiter)
|
||||
}
|
||||
|
||||
return active
|
||||
}
|
||||
|
90
vendor/k8s.io/utils/clock/testing/fake_clock_test.go
generated
vendored
90
vendor/k8s.io/utils/clock/testing/fake_clock_test.go
generated
vendored
@ -182,3 +182,93 @@ func TestFakeTick(t *testing.T) {
|
||||
t.Errorf("unexpected number of accumulated ticks: %d", accumulatedTicks)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFakeStop(t *testing.T) {
|
||||
tc := NewFakeClock(time.Now())
|
||||
timer := tc.NewTimer(time.Second)
|
||||
if !tc.HasWaiters() {
|
||||
t.Errorf("expected a waiter to be present, but it is not")
|
||||
}
|
||||
timer.Stop()
|
||||
if tc.HasWaiters() {
|
||||
t.Errorf("expected existing waiter to be cleaned up, but it is still present")
|
||||
}
|
||||
}
|
||||
|
||||
// This tests the pattern documented in the go docs here: https://golang.org/pkg/time/#Timer.Stop
|
||||
// This pattern is required to safely reset a timer, so should be common.
|
||||
// This also tests resetting the timer
|
||||
func TestFakeStopDrain(t *testing.T) {
|
||||
start := time.Time{}
|
||||
tc := NewFakeClock(start)
|
||||
timer := tc.NewTimer(time.Second)
|
||||
tc.Step(1 * time.Second)
|
||||
// Effectively `if !timer.Stop { <-t.C }` but with more asserts
|
||||
if timer.Stop() {
|
||||
t.Errorf("stop should report the timer had triggered")
|
||||
}
|
||||
if readTime := assertReadTime(t, timer.C()); !readTime.Equal(start.Add(1 * time.Second)) {
|
||||
t.Errorf("timer should have ticked after 1 second, got %v", readTime)
|
||||
}
|
||||
|
||||
timer.Reset(time.Second)
|
||||
if !tc.HasWaiters() {
|
||||
t.Errorf("expected a waiter to be present, but it is not")
|
||||
}
|
||||
select {
|
||||
case <-timer.C():
|
||||
t.Fatal("got time early on clock; haven't stepped yet")
|
||||
default:
|
||||
}
|
||||
tc.Step(1 * time.Second)
|
||||
if readTime := assertReadTime(t, timer.C()); !readTime.Equal(start.Add(2 * time.Second)) {
|
||||
t.Errorf("timer should have ticked again after reset + 1 more second, got %v", readTime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimerNegative(t *testing.T) {
|
||||
tc := NewFakeClock(time.Now())
|
||||
timer := tc.NewTimer(-1 * time.Second)
|
||||
if !tc.HasWaiters() {
|
||||
t.Errorf("expected a waiter to be present, but it is not")
|
||||
}
|
||||
// force waiters to be called
|
||||
tc.Step(0)
|
||||
tick := assertReadTime(t, timer.C())
|
||||
if tick != tc.Now() {
|
||||
t.Errorf("expected -1s to turn into now: %v != %v", tick, tc.Now())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTickNegative(t *testing.T) {
|
||||
// The stdlib 'Tick' returns nil for negative and zero values, so our fake
|
||||
// should too.
|
||||
tc := NewFakeClock(time.Now())
|
||||
if tick := tc.Tick(-1 * time.Second); tick != nil {
|
||||
t.Errorf("expected negative tick to be nil: %v", tick)
|
||||
}
|
||||
if tick := tc.Tick(0); tick != nil {
|
||||
t.Errorf("expected negative tick to be nil: %v", tick)
|
||||
}
|
||||
}
|
||||
|
||||
// assertReadTime asserts that the channel can be read and returns the time it
|
||||
// reads from the channel.
|
||||
func assertReadTime(t testing.TB, c <-chan time.Time) time.Time {
|
||||
type helper interface {
|
||||
Helper()
|
||||
}
|
||||
if h, ok := t.(helper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
select {
|
||||
case ti, ok := <-c:
|
||||
if !ok {
|
||||
t.Fatalf("expected to read time from channel, but it was closed")
|
||||
}
|
||||
return ti
|
||||
default:
|
||||
t.Fatalf("expected to read time from channel, but couldn't")
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
|
76
vendor/k8s.io/utils/pointer/pointer.go
generated
vendored
Normal file
76
vendor/k8s.io/utils/pointer/pointer.go
generated
vendored
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 pointer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// AllPtrFieldsNil tests whether all pointer fields in a struct are nil. This is useful when,
|
||||
// for example, an API struct is handled by plugins which need to distinguish
|
||||
// "no plugin accepted this spec" from "this spec is empty".
|
||||
//
|
||||
// This function is only valid for structs and pointers to structs. Any other
|
||||
// type will cause a panic. Passing a typed nil pointer will return true.
|
||||
func AllPtrFieldsNil(obj interface{}) bool {
|
||||
v := reflect.ValueOf(obj)
|
||||
if !v.IsValid() {
|
||||
panic(fmt.Sprintf("reflect.ValueOf() produced a non-valid Value for %#v", obj))
|
||||
}
|
||||
if v.Kind() == reflect.Ptr {
|
||||
if v.IsNil() {
|
||||
return true
|
||||
}
|
||||
v = v.Elem()
|
||||
}
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
if v.Field(i).Kind() == reflect.Ptr && !v.Field(i).IsNil() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Int32Ptr returns a pointer to an int32
|
||||
func Int32Ptr(i int32) *int32 {
|
||||
return &i
|
||||
}
|
||||
|
||||
// Int64Ptr returns a pointer to an int64
|
||||
func Int64Ptr(i int64) *int64 {
|
||||
return &i
|
||||
}
|
||||
|
||||
// Int32PtrDerefOr dereference the int32 ptr and returns it i not nil,
|
||||
// else returns def.
|
||||
func Int32PtrDerefOr(ptr *int32, def int32) int32 {
|
||||
if ptr != nil {
|
||||
return *ptr
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
// BoolPtr returns a pointer to a bool
|
||||
func BoolPtr(b bool) *bool {
|
||||
return &b
|
||||
}
|
||||
|
||||
// StringPtr returns a pointer to the passed string.
|
||||
func StringPtr(s string) *string {
|
||||
return &s
|
||||
}
|
66
vendor/k8s.io/utils/pointer/pointer_test.go
generated
vendored
Normal file
66
vendor/k8s.io/utils/pointer/pointer_test.go
generated
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 pointer
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAllPtrFieldsNil(t *testing.T) {
|
||||
testCases := []struct {
|
||||
obj interface{}
|
||||
expected bool
|
||||
}{
|
||||
{struct{}{}, true},
|
||||
{struct{ Foo int }{12345}, true},
|
||||
{&struct{ Foo int }{12345}, true},
|
||||
{struct{ Foo *int }{nil}, true},
|
||||
{&struct{ Foo *int }{nil}, true},
|
||||
{struct {
|
||||
Foo int
|
||||
Bar *int
|
||||
}{12345, nil}, true},
|
||||
{&struct {
|
||||
Foo int
|
||||
Bar *int
|
||||
}{12345, nil}, true},
|
||||
{struct {
|
||||
Foo *int
|
||||
Bar *int
|
||||
}{nil, nil}, true},
|
||||
{&struct {
|
||||
Foo *int
|
||||
Bar *int
|
||||
}{nil, nil}, true},
|
||||
{struct{ Foo *int }{new(int)}, false},
|
||||
{&struct{ Foo *int }{new(int)}, false},
|
||||
{struct {
|
||||
Foo *int
|
||||
Bar *int
|
||||
}{nil, new(int)}, false},
|
||||
{&struct {
|
||||
Foo *int
|
||||
Bar *int
|
||||
}{nil, new(int)}, false},
|
||||
{(*struct{})(nil), true},
|
||||
}
|
||||
for i, tc := range testCases {
|
||||
if AllPtrFieldsNil(tc.obj) != tc.expected {
|
||||
t.Errorf("case[%d]: expected %t, got %t", i, tc.expected, !tc.expected)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user