behaviors_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. Copyright 2020 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 behaviors
  14. import (
  15. "io/ioutil"
  16. "os"
  17. "path/filepath"
  18. "regexp"
  19. "testing"
  20. "gopkg.in/yaml.v2"
  21. )
  22. func TestValidate(t *testing.T) {
  23. var behaviorFiles []string
  24. err := filepath.Walk(".",
  25. func(path string, info os.FileInfo, err error) error {
  26. if err != nil {
  27. t.Errorf("%q", err.Error())
  28. }
  29. r, _ := regexp.Compile(".+.yaml$")
  30. if r.MatchString(path) {
  31. behaviorFiles = append(behaviorFiles, path)
  32. }
  33. return nil
  34. })
  35. if err != nil {
  36. t.Errorf("%q", err.Error())
  37. }
  38. for _, file := range behaviorFiles {
  39. validateSuite(file, t)
  40. }
  41. }
  42. func validateSuite(path string, t *testing.T) {
  43. var suite Suite
  44. yamlFile, err := ioutil.ReadFile(path)
  45. if err != nil {
  46. t.Errorf("%q", err.Error())
  47. }
  48. err = yaml.UnmarshalStrict(yamlFile, &suite)
  49. if err != nil {
  50. t.Errorf("%q", err.Error())
  51. }
  52. behaviorIDList := make(map[string]bool)
  53. for _, behavior := range suite.Behaviors {
  54. // Ensure no behavior IDs are duplicated
  55. if _, ok := behaviorIDList[behavior.ID]; ok {
  56. t.Errorf("Duplicate behavior ID: %s", behavior.ID)
  57. }
  58. behaviorIDList[behavior.ID] = true
  59. }
  60. }