util.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package awstesting
  2. import (
  3. "io"
  4. "os"
  5. "strings"
  6. "time"
  7. "github.com/aws/aws-sdk-go/private/util"
  8. )
  9. // ZeroReader is a io.Reader which will always write zeros to the byte slice provided.
  10. type ZeroReader struct{}
  11. // Read fills the provided byte slice with zeros returning the number of bytes written.
  12. func (r *ZeroReader) Read(b []byte) (int, error) {
  13. for i := 0; i < len(b); i++ {
  14. b[i] = 0
  15. }
  16. return len(b), nil
  17. }
  18. // ReadCloser is a io.ReadCloser for unit testing.
  19. // Designed to test for leaks and whether a handle has
  20. // been closed
  21. type ReadCloser struct {
  22. Size int
  23. Closed bool
  24. set bool
  25. FillData func(bool, []byte, int, int)
  26. }
  27. // Read will call FillData and fill it with whatever data needed.
  28. // Decrements the size until zero, then return io.EOF.
  29. func (r *ReadCloser) Read(b []byte) (int, error) {
  30. if r.Closed {
  31. return 0, io.EOF
  32. }
  33. delta := len(b)
  34. if delta > r.Size {
  35. delta = r.Size
  36. }
  37. r.Size -= delta
  38. for i := 0; i < delta; i++ {
  39. b[i] = 'a'
  40. }
  41. if r.FillData != nil {
  42. r.FillData(r.set, b, r.Size, delta)
  43. }
  44. r.set = true
  45. if r.Size > 0 {
  46. return delta, nil
  47. }
  48. return delta, io.EOF
  49. }
  50. // Close sets Closed to true and returns no error
  51. func (r *ReadCloser) Close() error {
  52. r.Closed = true
  53. return nil
  54. }
  55. // SortedKeys returns a sorted slice of keys of a map.
  56. func SortedKeys(m map[string]interface{}) []string {
  57. return util.SortedKeys(m)
  58. }
  59. // A FakeContext provides a simple stub implementation of a Context
  60. type FakeContext struct {
  61. Error error
  62. DoneCh chan struct{}
  63. }
  64. // Deadline always will return not set
  65. func (c *FakeContext) Deadline() (deadline time.Time, ok bool) {
  66. return time.Time{}, false
  67. }
  68. // Done returns a read channel for listening to the Done event
  69. func (c *FakeContext) Done() <-chan struct{} {
  70. return c.DoneCh
  71. }
  72. // Err returns the error, is nil if not set.
  73. func (c *FakeContext) Err() error {
  74. return c.Error
  75. }
  76. // Value ignores the Value and always returns nil
  77. func (c *FakeContext) Value(key interface{}) interface{} {
  78. return nil
  79. }
  80. // StashEnv stashes the current environment variables and returns an array of
  81. // all environment values as key=val strings.
  82. func StashEnv() []string {
  83. env := os.Environ()
  84. os.Clearenv()
  85. return env
  86. }
  87. // PopEnv takes the list of the environment values and injects them into the
  88. // process's environment variable data. Clears any existing environment values
  89. // that may already exist.
  90. func PopEnv(env []string) {
  91. os.Clearenv()
  92. for _, e := range env {
  93. p := strings.SplitN(e, "=", 2)
  94. k, v := p[0], ""
  95. if len(p) > 1 {
  96. v = p[1]
  97. }
  98. os.Setenv(k, v)
  99. }
  100. }