testfiles.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package testfiles provides a wrapper around various optional ways
  14. // of retrieving additional files needed during a test run:
  15. // - builtin bindata
  16. // - filesystem access
  17. //
  18. // Because it is a is self-contained package, it can be used by
  19. // test/e2e/framework and test/e2e/manifest without creating
  20. // a circular dependency.
  21. package testfiles
  22. import (
  23. "errors"
  24. "fmt"
  25. "io/ioutil"
  26. "os"
  27. "path"
  28. "path/filepath"
  29. "sort"
  30. "strings"
  31. )
  32. var filesources []FileSource
  33. // AddFileSource registers another provider for files that may be
  34. // needed at runtime. Should be called during initialization of a test
  35. // binary.
  36. func AddFileSource(filesource FileSource) {
  37. filesources = append(filesources, filesource)
  38. }
  39. // FileSource implements one way of retrieving test file content. For
  40. // example, one file source could read from the original source code
  41. // file tree, another from bindata compiled into a test executable.
  42. type FileSource interface {
  43. // ReadTestFile retrieves the content of a file that gets maintained
  44. // alongside a test's source code. Files are identified by the
  45. // relative path inside the repository containing the tests, for
  46. // example "cluster/gce/upgrade.sh" inside kubernetes/kubernetes.
  47. //
  48. // When the file is not found, a nil slice is returned. An error is
  49. // returned for all fatal errors.
  50. ReadTestFile(filePath string) ([]byte, error)
  51. // DescribeFiles returns a multi-line description of which
  52. // files are available via this source. It is meant to be
  53. // used as part of the error message when a file cannot be
  54. // found.
  55. DescribeFiles() string
  56. }
  57. // Fail is an error handler function with the same prototype and
  58. // semantic as ginkgo.Fail. Typically ginkgo.Fail is what callers
  59. // of ReadOrDie and Exists will pass. This way this package
  60. // avoids depending on Ginkgo.
  61. type Fail func(failure string, callerSkip ...int)
  62. // ReadOrDie tries to retrieve the desired file content from
  63. // one of the registered file sources. In contrast to FileSource, it
  64. // will either return a valid slice or abort the test by calling the fatal function,
  65. // i.e. the caller doesn't have to implement error checking.
  66. func ReadOrDie(filePath string, fail Fail) []byte {
  67. data, err := Read(filePath)
  68. if err != nil {
  69. fail(err.Error(), 1)
  70. }
  71. return data
  72. }
  73. // Read tries to retrieve the desired file content from
  74. // one of the registered file sources.
  75. func Read(filePath string) ([]byte, error) {
  76. if len(filesources) == 0 {
  77. return nil, fmt.Errorf("no file sources registered (yet?), cannot retrieve test file %s", filePath)
  78. }
  79. for _, filesource := range filesources {
  80. data, err := filesource.ReadTestFile(filePath)
  81. if err != nil {
  82. return nil, fmt.Errorf("fatal error retrieving test file %s: %s", filePath, err)
  83. }
  84. if data != nil {
  85. return data, nil
  86. }
  87. }
  88. // Here we try to generate an error that points test authors
  89. // or users in the right direction for resolving the problem.
  90. error := fmt.Sprintf("Test file %q was not found.\n", filePath)
  91. for _, filesource := range filesources {
  92. error += filesource.DescribeFiles()
  93. error += "\n"
  94. }
  95. return nil, errors.New(error)
  96. }
  97. // Exists checks whether a file could be read. Unexpected errors
  98. // are handled by calling the fail function, which then should
  99. // abort the current test.
  100. func Exists(filePath string, fail Fail) bool {
  101. for _, filesource := range filesources {
  102. data, err := filesource.ReadTestFile(filePath)
  103. if err != nil {
  104. fail(fmt.Sprintf("fatal error looking for test file %s: %s", filePath, err), 1)
  105. }
  106. if data != nil {
  107. return true
  108. }
  109. }
  110. return false
  111. }
  112. // RootFileSource looks for files relative to a root directory.
  113. type RootFileSource struct {
  114. Root string
  115. }
  116. // ReadTestFile looks for the file relative to the configured
  117. // root directory. If the path is already absolute, for example
  118. // in a test that has its own method of determining where
  119. // files are, then the path will be used directly.
  120. func (r RootFileSource) ReadTestFile(filePath string) ([]byte, error) {
  121. var fullPath string
  122. if path.IsAbs(filePath) {
  123. fullPath = filePath
  124. } else {
  125. fullPath = filepath.Join(r.Root, filePath)
  126. }
  127. data, err := ioutil.ReadFile(fullPath)
  128. if os.IsNotExist(err) {
  129. // Not an error (yet), some other provider may have the file.
  130. return nil, nil
  131. }
  132. return data, err
  133. }
  134. // DescribeFiles explains that it looks for files inside a certain
  135. // root directory.
  136. func (r RootFileSource) DescribeFiles() string {
  137. description := fmt.Sprintf("Test files are expected in %q", r.Root)
  138. if !path.IsAbs(r.Root) {
  139. // The default in test_context.go is the relative path
  140. // ../../, which doesn't really help locating the
  141. // actual location. Therefore we add also the absolute
  142. // path if necessary.
  143. abs, err := filepath.Abs(r.Root)
  144. if err == nil {
  145. description += fmt.Sprintf(" = %q", abs)
  146. }
  147. }
  148. description += "."
  149. return description
  150. }
  151. // BindataFileSource handles files stored in a package generated with bindata.
  152. type BindataFileSource struct {
  153. Asset func(string) ([]byte, error)
  154. AssetNames func() []string
  155. }
  156. // ReadTestFile looks for an asset with the given path.
  157. func (b BindataFileSource) ReadTestFile(filePath string) ([]byte, error) {
  158. fileBytes, err := b.Asset(filePath)
  159. if err != nil {
  160. // It would be nice to have a better way to detect
  161. // "not found" errors :-/
  162. if strings.HasSuffix(err.Error(), "not found") {
  163. return nil, nil
  164. }
  165. }
  166. return fileBytes, nil
  167. }
  168. // DescribeFiles explains about gobindata and then lists all available files.
  169. func (b BindataFileSource) DescribeFiles() string {
  170. var lines []string
  171. lines = append(lines, "The following files are built into the test executable via gobindata. For questions on maintaining gobindata, contact the sig-testing group.")
  172. assets := b.AssetNames()
  173. sort.Strings(assets)
  174. lines = append(lines, assets...)
  175. description := strings.Join(lines, "\n ")
  176. return description
  177. }