spec.go 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  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 test
  14. import (
  15. "fmt"
  16. "strings"
  17. "testing"
  18. "github.com/coreos/pkg/capnslog"
  19. "github.com/stretchr/testify/assert"
  20. )
  21. var logger = capnslog.NewPackageLogger("github.com/rook/rook", "ceph-op-testlib")
  22. // ArgumentsMatchExpected returns a descriptive error if any of the expected arguments do not exist.
  23. // This supports arguments in which flags appear multiple times with different values but does not
  24. // support multiple instances of the same flag-value. This test is designed to fail if the list
  25. // of actual arguments contains extra arguments not specified in the expected args.
  26. // The expected arguments are given as an array of string arrays. This is to support flags which
  27. // may have multiple values. Examples:
  28. //
  29. // expectedArgs := [][]string{
  30. // {"-h"}, // test for a short flag
  31. // {"-vvv"}, // test for a short flag with value(s) specified
  32. // {"-d", "3"}, // test for a short flag with a value specified
  33. // {"--verbose"}, // test for a --bool flag
  34. // {"--name=alex"}, // test for a --flag=value flag
  35. // {"--name", "sam"}, // test for a --flag with a value after a space
  36. // {"--full-name", "sam", "goodhuman"}, // test for a --flag with 2 values separated by spaces
  37. // }
  38. func ArgumentsMatchExpected(actualArgs []string, expectedArgs [][]string) error {
  39. // join all args into a big space-separated arg string so we can use string search on it
  40. // this is simpler than a bunch of nested for loops and if statements with continues in them
  41. fullArgString := strings.Join(actualArgs, " ")
  42. logger.Infof("testing that actual args: %s\nmatch expected args:%v", fullArgString, actualArgs)
  43. for _, arg := range expectedArgs {
  44. validArgMatcher := strings.Join(arg, " ")
  45. // We join each individual argument together the same was as the big string
  46. if validArgMatcher == "" {
  47. return fmt.Errorf("Expected argument %v evaluated to empty string; ArgumentsMatchExpected() doesn't know what to do", arg)
  48. }
  49. matches := strings.Count(fullArgString, validArgMatcher)
  50. if matches > 1 {
  51. return fmt.Errorf("More than one instance of flag '%s' in: %s; ArgumentsMatchExpected() doesn't know what to do",
  52. validArgMatcher, fullArgString)
  53. } else if matches == 1 {
  54. // Remove the instance of the valid match so we can't match to it any more
  55. fullArgString = strings.Replace(fullArgString, validArgMatcher, "", 1)
  56. } else { // zero matches
  57. return fmt.Errorf("Expected argument '%s' missing in: %s\n(It's possible the same arg is in expectedArgs twice.)",
  58. validArgMatcher, strings.Join(actualArgs, " "))
  59. }
  60. }
  61. if remainingArgs := strings.Trim(fullArgString, " "); remainingArgs != "" {
  62. return fmt.Errorf("The actual arguments have additional args specified: %s", remainingArgs)
  63. }
  64. return nil
  65. }
  66. // AssertLabelsContainRookRequirements asserts that the labels under test contain the labels
  67. // which all Rook pods should have. This can be used with labels for Kubernetes Deployments,
  68. // DaemonSets, etc.
  69. func AssertLabelsContainRookRequirements(t *testing.T, labels map[string]string, appName string) {
  70. resourceLabels := []string{}
  71. for k, v := range labels {
  72. resourceLabels = append(resourceLabels, fmt.Sprintf("%s=%s", k, v))
  73. }
  74. expectedLabels := []string{
  75. "app=" + appName,
  76. }
  77. assert.Subset(t, resourceLabels, expectedLabels,
  78. "labels on resource do not match Rook requirements", labels)
  79. }