/filter/filter_test.go

https://bitbucket.org/marthjod/ansible-one-inventory · Go · 57 lines · 49 code · 8 blank · 0 comment · 8 complexity · 719bab9a9dac344f6a3741b768ebb687 MD5 · raw file

  1. package filter_test
  2. import (
  3. "github.com/marthjod/ansible-one-inventory/filter"
  4. "github.com/marthjod/ansible-one-inventory/model"
  5. "reflect"
  6. "testing"
  7. )
  8. var expected = []struct {
  9. pattern string
  10. replacement string
  11. result string
  12. }{
  13. {"web-staging-east", "-(we|ea)st", "web-staging"},
  14. {"db-production-west", "-(we|ea)st", "db-production"},
  15. }
  16. var hostnames = []string{
  17. "web-staging-east",
  18. "db-staging-east",
  19. "db-production-west",
  20. }
  21. func TestAdjustPatternName(t *testing.T) {
  22. for _, e := range expected {
  23. actual := filter.AdjustPatternName(e.pattern, e.replacement)
  24. if actual != e.result {
  25. t.Errorf("Unexpected adjustment result %q for %q, %q.", actual, e.pattern, e.replacement)
  26. }
  27. }
  28. }
  29. func TestFilterErr(t *testing.T) {
  30. var err error
  31. regex := "(invalid"
  32. _, err = filter.Filter(hostnames, regex)
  33. if err == nil {
  34. t.Error("No error returned for invalid regexp!")
  35. }
  36. }
  37. func TestFilter(t *testing.T) {
  38. expected := model.InventoryGroup{
  39. "web-staging-east",
  40. }
  41. regex := "^[a-z]{3}"
  42. ig, err := filter.Filter(hostnames, regex)
  43. if err != nil {
  44. t.Fatal(err.Error())
  45. }
  46. if !reflect.DeepEqual(ig, expected) {
  47. t.Errorf("Got unexpected result while filtering. Expected:\n%+v\nActual:\n%+v", expected, ig)
  48. }
  49. }