modelset_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "testing"
  17. compute_models "yunion.io/x/onecloud/pkg/compute/models"
  18. )
  19. func TestLoadbalancerListenerRules_OrderedEnabledList(t *testing.T) {
  20. set := LoadbalancerListenerRules(map[string]*LoadbalancerListenerRule{
  21. "empty": {
  22. SLoadbalancerListenerRule: &compute_models.SLoadbalancerListenerRule{
  23. Domain: "",
  24. Path: "",
  25. },
  26. },
  27. "/": {
  28. SLoadbalancerListenerRule: &compute_models.SLoadbalancerListenerRule{
  29. Domain: "",
  30. Path: "/",
  31. },
  32. },
  33. "/img": {
  34. SLoadbalancerListenerRule: &compute_models.SLoadbalancerListenerRule{
  35. Domain: "",
  36. Path: "/img",
  37. },
  38. },
  39. "a.com": {
  40. SLoadbalancerListenerRule: &compute_models.SLoadbalancerListenerRule{
  41. Domain: "a.com",
  42. Path: "",
  43. },
  44. },
  45. "a.com/": {
  46. SLoadbalancerListenerRule: &compute_models.SLoadbalancerListenerRule{
  47. Domain: "a.com",
  48. Path: "/",
  49. },
  50. },
  51. "a.com/img": {
  52. SLoadbalancerListenerRule: &compute_models.SLoadbalancerListenerRule{
  53. Domain: "a.com",
  54. Path: "/img",
  55. },
  56. },
  57. "m.a.com": {
  58. SLoadbalancerListenerRule: &compute_models.SLoadbalancerListenerRule{
  59. Domain: "m.a.com",
  60. Path: "",
  61. },
  62. },
  63. "m.a.com/": {
  64. SLoadbalancerListenerRule: &compute_models.SLoadbalancerListenerRule{
  65. Domain: "m.a.com",
  66. Path: "/",
  67. },
  68. },
  69. "m.a.com/img": {
  70. SLoadbalancerListenerRule: &compute_models.SLoadbalancerListenerRule{
  71. Domain: "m.a.com",
  72. Path: "/img",
  73. },
  74. },
  75. })
  76. for _, rule := range set {
  77. rule.Status = "enabled"
  78. }
  79. rules := set.OrderedEnabledList()
  80. for i, rule := range rules {
  81. ok := true
  82. if i > 0 {
  83. rulep := rules[i-1]
  84. if len(rulep.Domain) < len(rule.Domain) {
  85. ok = false
  86. } else if len(rulep.Domain) == len(rule.Domain) {
  87. if len(rulep.Path) < len(rule.Path) {
  88. ok = false
  89. }
  90. }
  91. }
  92. if ok {
  93. t.Logf("%v: %s%s", ok, rule.Domain, rule.Path)
  94. } else {
  95. t.Errorf("%v: %s%s", ok, rule.Domain, rule.Path)
  96. }
  97. }
  98. }