in_process.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package samples
  15. import (
  16. "fmt"
  17. "io"
  18. "io/ioutil"
  19. "log"
  20. "os"
  21. "time"
  22. "github.com/jacobsa/fuse"
  23. "github.com/jacobsa/ogletest"
  24. "github.com/jacobsa/timeutil"
  25. "golang.org/x/net/context"
  26. )
  27. // A struct that implements common behavior needed by tests in the samples/
  28. // directory. Use it as an embedded field in your test fixture, calling its
  29. // SetUp method from your SetUp method after setting the Server field.
  30. type SampleTest struct {
  31. // The server under test and the configuration with which it should be
  32. // mounted. These must be set by the user of this type before calling SetUp;
  33. // all the other fields below are set by SetUp itself.
  34. Server fuse.Server
  35. MountConfig fuse.MountConfig
  36. // A context object that can be used for long-running operations.
  37. Ctx context.Context
  38. // A clock with a fixed initial time. The test's set up method may use this
  39. // to wire the server with a clock, if desired.
  40. Clock timeutil.SimulatedClock
  41. // The directory at which the file system is mounted.
  42. Dir string
  43. // Anothing non-nil in this slice will be closed by TearDown. The test will
  44. // fail if closing fails.
  45. ToClose []io.Closer
  46. mfs *fuse.MountedFileSystem
  47. }
  48. // Mount t.Server and initialize the other exported fields of the struct.
  49. // Panics on error.
  50. //
  51. // REQUIRES: t.Server has been set.
  52. func (t *SampleTest) SetUp(ti *ogletest.TestInfo) {
  53. cfg := t.MountConfig
  54. if *fDebug {
  55. cfg.DebugLogger = log.New(os.Stderr, "fuse: ", 0)
  56. }
  57. err := t.initialize(ti.Ctx, t.Server, &cfg)
  58. if err != nil {
  59. panic(err)
  60. }
  61. }
  62. // Like SetUp, but doens't panic.
  63. func (t *SampleTest) initialize(
  64. ctx context.Context,
  65. server fuse.Server,
  66. config *fuse.MountConfig) (err error) {
  67. // Initialize the context used by the test.
  68. t.Ctx = ctx
  69. // Make the server share that context, if the test hasn't already set some
  70. // other one.
  71. if config.OpContext == nil {
  72. config.OpContext = ctx
  73. }
  74. // Initialize the clock.
  75. t.Clock.SetTime(time.Date(2012, 8, 15, 22, 56, 0, 0, time.Local))
  76. // Set up a temporary directory.
  77. t.Dir, err = ioutil.TempDir("", "sample_test")
  78. if err != nil {
  79. err = fmt.Errorf("TempDir: %v", err)
  80. return
  81. }
  82. // Mount the file system.
  83. t.mfs, err = fuse.Mount(t.Dir, server, config)
  84. if err != nil {
  85. err = fmt.Errorf("Mount: %v", err)
  86. return
  87. }
  88. return
  89. }
  90. // Unmount the file system and clean up. Panics on error.
  91. func (t *SampleTest) TearDown() {
  92. err := t.destroy()
  93. if err != nil {
  94. panic(err)
  95. }
  96. }
  97. // Like TearDown, but doesn't panic.
  98. func (t *SampleTest) destroy() (err error) {
  99. // Close what is necessary.
  100. for _, c := range t.ToClose {
  101. if c == nil {
  102. continue
  103. }
  104. ogletest.ExpectEq(nil, c.Close())
  105. }
  106. // Was the file system mounted?
  107. if t.mfs == nil {
  108. return
  109. }
  110. // Unmount the file system.
  111. err = unmount(t.Dir)
  112. if err != nil {
  113. err = fmt.Errorf("unmount: %v", err)
  114. return
  115. }
  116. // Unlink the mount point.
  117. if err = os.Remove(t.Dir); err != nil {
  118. err = fmt.Errorf("Unlinking mount point: %v", err)
  119. return
  120. }
  121. // Join the file system.
  122. err = t.mfs.Join(t.Ctx)
  123. if err != nil {
  124. err = fmt.Errorf("mfs.Join: %v", err)
  125. return
  126. }
  127. return
  128. }