search_aggs_bucket_ip_range_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "encoding/json"
  7. "testing"
  8. )
  9. func TestIPRangeAggregation(t *testing.T) {
  10. agg := NewIPRangeAggregation().Field("remote_ip")
  11. agg = agg.AddRange("", "10.0.0.0")
  12. agg = agg.AddRange("10.1.0.0", "10.1.255.255")
  13. agg = agg.AddRange("10.2.0.0", "")
  14. src, err := agg.Source()
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. data, err := json.Marshal(src)
  19. if err != nil {
  20. t.Fatalf("marshaling to JSON failed: %v", err)
  21. }
  22. got := string(data)
  23. expected := `{"ip_range":{"field":"remote_ip","ranges":[{"to":"10.0.0.0"},{"from":"10.1.0.0","to":"10.1.255.255"},{"from":"10.2.0.0"}]}}`
  24. if got != expected {
  25. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  26. }
  27. }
  28. func TestIPRangeAggregationMask(t *testing.T) {
  29. agg := NewIPRangeAggregation().Field("remote_ip")
  30. agg = agg.AddMaskRange("10.0.0.0/25")
  31. agg = agg.AddMaskRange("10.0.0.127/25")
  32. src, err := agg.Source()
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. data, err := json.Marshal(src)
  37. if err != nil {
  38. t.Fatalf("marshaling to JSON failed: %v", err)
  39. }
  40. got := string(data)
  41. expected := `{"ip_range":{"field":"remote_ip","ranges":[{"mask":"10.0.0.0/25"},{"mask":"10.0.0.127/25"}]}}`
  42. if got != expected {
  43. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  44. }
  45. }
  46. func TestIPRangeAggregationWithKeyedFlag(t *testing.T) {
  47. agg := NewIPRangeAggregation().Field("remote_ip")
  48. agg = agg.Keyed(true)
  49. agg = agg.AddRange("", "10.0.0.0")
  50. agg = agg.AddRange("10.1.0.0", "10.1.255.255")
  51. agg = agg.AddRange("10.2.0.0", "")
  52. src, err := agg.Source()
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. data, err := json.Marshal(src)
  57. if err != nil {
  58. t.Fatalf("marshaling to JSON failed: %v", err)
  59. }
  60. got := string(data)
  61. expected := `{"ip_range":{"field":"remote_ip","keyed":true,"ranges":[{"to":"10.0.0.0"},{"from":"10.1.0.0","to":"10.1.255.255"},{"from":"10.2.0.0"}]}}`
  62. if got != expected {
  63. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  64. }
  65. }
  66. func TestIPRangeAggregationWithKeys(t *testing.T) {
  67. agg := NewIPRangeAggregation().Field("remote_ip")
  68. agg = agg.Keyed(true)
  69. agg = agg.LtWithKey("infinity", "10.0.0.5")
  70. agg = agg.GtWithKey("and-beyond", "10.0.0.5")
  71. src, err := agg.Source()
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. data, err := json.Marshal(src)
  76. if err != nil {
  77. t.Fatalf("marshaling to JSON failed: %v", err)
  78. }
  79. got := string(data)
  80. expected := `{"ip_range":{"field":"remote_ip","keyed":true,"ranges":[{"key":"infinity","to":"10.0.0.5"},{"from":"10.0.0.5","key":"and-beyond"}]}}`
  81. if got != expected {
  82. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  83. }
  84. }