networkinsight_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package opencost
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. "time"
  7. filter "github.com/opencost/opencost/core/pkg/filter/networkinsight"
  8. "github.com/opencost/opencost/core/pkg/util"
  9. )
  10. const (
  11. mockCluster1 = "mockCluster1"
  12. mockNamespace1 = "mockNamespace1"
  13. mockPod1 = "mockPod1"
  14. mockNamespace2 = "mockNamespace2"
  15. mockPod2 = "mockPod2"
  16. )
  17. func Test_NetworkDetailsCombineAndAdd(t *testing.T) {
  18. mcsI1inSet1, _ := createMockNetworkDetail(0, 200000, mockCloudService1, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  19. mcsI2inSet1, _ := createMockNetworkDetail(0, 400000, mockCloudService2, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  20. mcsI3inSet1, _ := createMockNetworkDetail(0, 800000, mockCloudService2, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  21. mcsE1inSet1, _ := createMockNetworkDetail(0.12, 200000, mockCloudService1, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  22. mcsE2inSet1, _ := createMockNetworkDetail(0.15, 400000, mockCloudService2, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  23. mcsI1inSet2, _ := createMockNetworkDetail(0, 300000, mockCloudService1, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  24. mcsI2inSet2, _ := createMockNetworkDetail(0, 300000, mockCloudService2, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  25. mcsE1inSet2, _ := createMockNetworkDetail(0.16, 300000, mockCloudService1, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  26. mcsE2inSet2, _ := createMockNetworkDetail(0.24, 300000, mockCloudService2, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  27. mcsE3inSet2, _ := createMockNetworkDetail(0.35, 300000, mockCloudService3, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  28. mcsI1inCombineSet, _ := createMockNetworkDetail(0, 500000, mockCloudService1, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  29. mcsI2inCombineSet, _ := createMockNetworkDetail(0, 700000, mockCloudService2, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  30. mcsE1inCombineSet, _ := createMockNetworkDetail(0.28, 500000, mockCloudService1, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  31. mcsE2inCombineSet, _ := createMockNetworkDetail(0.39, 700000, mockCloudService2, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  32. set1 := make(NetworkDetailsSet, 0)
  33. set1[mcsI1inSet1.Key()] = mcsI1inSet1
  34. set1[mcsI2inSet1.Key()] = mcsI2inSet1
  35. set1[mcsI3inSet1.Key()] = mcsI3inSet1
  36. set1[mcsE1inSet1.Key()] = mcsE1inSet1
  37. set1[mcsE2inSet1.Key()] = mcsE2inSet1
  38. set2 := make(NetworkDetailsSet, 0)
  39. set2[mcsI1inSet2.Key()] = mcsI1inSet2
  40. set2[mcsI2inSet2.Key()] = mcsI2inSet2
  41. set2[mcsE1inSet2.Key()] = mcsE1inSet2
  42. set2[mcsE2inSet2.Key()] = mcsE2inSet2
  43. set2[mcsE3inSet2.Key()] = mcsE3inSet2
  44. expected := make(NetworkDetailsSet, 0)
  45. expected[mcsI1inCombineSet.Key()] = mcsI1inCombineSet
  46. expected[mcsI2inCombineSet.Key()] = mcsI2inCombineSet
  47. expected[mcsI3inSet1.Key()] = mcsI3inSet1
  48. expected[mcsE1inCombineSet.Key()] = mcsE1inCombineSet
  49. expected[mcsE2inCombineSet.Key()] = mcsE2inCombineSet
  50. expected[mcsE3inSet2.Key()] = mcsE3inSet2
  51. set1.Combine(set2)
  52. if !reflect.DeepEqual(set1, expected) {
  53. t.Fatalf("Test_NetworkIngressCombineAndAdd: NetworkIngressDetailSet:combine not working as expected")
  54. }
  55. }
  56. func Test_NetworkInsightSetInsertFn(t *testing.T) {
  57. mockInsight1 := MockNetworkInsightImportantKeys{
  58. Cluster: mockCluster1,
  59. Namespace: mockNamespace1,
  60. Controller: "",
  61. Pod: mockPod1,
  62. Node: "",
  63. Labels: map[string]string{},
  64. Region: "",
  65. Zone: "",
  66. }
  67. mockInsight2 := MockNetworkInsightImportantKeys{
  68. Cluster: mockCluster1,
  69. Namespace: mockNamespace2,
  70. Controller: "",
  71. Pod: mockPod2,
  72. Node: "",
  73. Labels: map[string]string{},
  74. Region: "",
  75. Zone: "",
  76. }
  77. mcs1inIngress, _ := createMockNetworkDetail(0, 200000, mockCloudService1, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  78. mcs2inIngress, _ := createMockNetworkDetail(0, 400000, mockCloudService2, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  79. mcs3inIngress, _ := createMockNetworkDetail(0, 800000, mockCloudService2, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  80. mcs1inEgress, _ := createMockNetworkDetail(0.16, 300000, mockCloudService1, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  81. mcs2inEgress, _ := createMockNetworkDetail(0.24, 300000, mockCloudService2, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  82. mcs3inEgress, _ := createMockNetworkDetail(0.35, 300000, mockCloudService3, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  83. set1 := make(NetworkDetailsSet, 0)
  84. set1[mcs1inIngress.Key()] = mcs1inIngress
  85. set1[mcs2inIngress.Key()] = mcs2inIngress
  86. set1[mcs3inIngress.Key()] = mcs3inIngress
  87. set1[mcs1inEgress.Key()] = mcs1inEgress
  88. set1[mcs2inEgress.Key()] = mcs2inEgress
  89. set1[mcs3inEgress.Key()] = mcs3inEgress
  90. set2 := make(NetworkDetailsSet, 0)
  91. set2[mcs1inIngress.Key()] = mcs1inIngress.Clone()
  92. set2[mcs2inIngress.Key()] = mcs2inIngress.Clone()
  93. set2[mcs3inIngress.Key()] = mcs3inIngress.Clone()
  94. set2[mcs1inEgress.Key()] = mcs1inEgress.Clone()
  95. set2[mcs2inEgress.Key()] = mcs2inEgress.Clone()
  96. set2[mcs3inEgress.Key()] = mcs3inEgress.Clone()
  97. ni1 := createMockNetworkInsight(mockInsight1, set1)
  98. ni2 := createMockNetworkInsight(mockInsight1, set2)
  99. ni3 := createMockNetworkInsight(mockInsight2, set1.Clone())
  100. e := time.Now()
  101. s := e.Add(-1 * time.Hour)
  102. nis := &NetworkInsightSet{
  103. NetworkInsights: make(map[string]*NetworkInsight, 0),
  104. Window: NewWindow(&s, &e),
  105. }
  106. nis.Insert(ni1, []NetworkInsightProperty{NetworkInsightsPod})
  107. nis.Insert(ni2, []NetworkInsightProperty{NetworkInsightsPod})
  108. nis.Insert(ni3, []NetworkInsightProperty{NetworkInsightsPod})
  109. aggregatedNis := nis.NetworkInsights
  110. if val, ok := aggregatedNis[mockPod1]; !ok {
  111. t.Fatalf("Test_NetworkInsightSetInsertFn: %s not found after insert", mockPod1)
  112. } else {
  113. if !util.IsApproximately(val.NetworkDetails.GetTotalInternetCost(), 1.50) {
  114. t.Logf("1 is %f", val.NetworkDetails.GetTotalInternetCost())
  115. t.Fatalf("Test_NetworkInsightSetInsertFn: failed to insert and add properly for pod %s", mockPod1)
  116. }
  117. }
  118. if val, ok := aggregatedNis[mockPod2]; !ok {
  119. t.Fatalf("Test_NetworkInsightSetInsertFn: %s not found after insert", mockPod2)
  120. } else {
  121. if !util.IsApproximately(val.NetworkDetails.GetTotalInternetCost(), 0.75) {
  122. t.Logf("2 is %f", val.NetworkDetails.GetTotalInternetCost())
  123. t.Fatalf("Test_NetworkInsightSetInsertFn: failed to insert and add properly for pod %s", mockPod1)
  124. }
  125. }
  126. }
  127. func Test_NetworkDetailSetFilterZeroCost(t *testing.T) {
  128. mcs1inIngress, _ := createMockNetworkDetail(0, 200000, mockCloudService1, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  129. mcs2inIngress, _ := createMockNetworkDetail(0, 400000, mockCloudService2, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  130. mcs3inIngress, _ := createMockNetworkDetail(0, 800000, mockCloudService2, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  131. mcs1inEgress, _ := createMockNetworkDetail(0.16, 300000, mockCloudService1, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  132. mcs2inEgress, _ := createMockNetworkDetail(0.24, 300000, mockCloudService2, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  133. mcs3inEgress, _ := createMockNetworkDetail(0.35, 300000, mockCloudService3, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  134. set1 := make(NetworkDetailsSet, 0)
  135. set1[mcs1inIngress.Key()] = mcs1inIngress
  136. set1[mcs2inIngress.Key()] = mcs2inIngress
  137. set1[mcs3inIngress.Key()] = mcs3inIngress
  138. set1[mcs1inEgress.Key()] = mcs1inEgress
  139. set1[mcs2inEgress.Key()] = mcs2inEgress
  140. set1[mcs3inEgress.Key()] = mcs3inEgress
  141. set2 := make(NetworkDetailsSet, 0)
  142. set2[mcs1inEgress.Key()] = mcs1inEgress
  143. set2[mcs2inEgress.Key()] = mcs2inEgress
  144. set2[mcs3inEgress.Key()] = mcs3inEgress
  145. set3 := set1.filterZeroCost()
  146. if !reflect.DeepEqual(set3, set2) {
  147. t.Fatalf("Test_NetworkIngressCombineAndAdd: NetworkIngressDetailSet:filterZeroCost failed with showZeroCost:false option")
  148. }
  149. }
  150. func Test_NetworkInsightFilterNetworkDetails(t *testing.T) {
  151. parserForEqualsTest := filter.NewNetworkInsightDetailFilterParser()
  152. compiler := NewNetworkInsightDetailMatchCompiler()
  153. equalsTestRegex, err := parserForEqualsTest.Parse(fmt.Sprintf("endPoint:\"%s\"", mockCloudService1))
  154. // if error encounter skip the test
  155. if err != nil {
  156. t.Logf("skipping test case to avoid intermittent test failure if not able to parse filter string")
  157. t.Skip()
  158. }
  159. equalsTestFilter, err := compiler.Compile(equalsTestRegex)
  160. // if error encounter skip the test
  161. if err != nil {
  162. t.Logf("skipping test case to avoid intermittent test failure if not able to parse filter string")
  163. t.Skip()
  164. }
  165. parserForContainsTest := filter.NewNetworkInsightDetailFilterParser()
  166. containsTestRegex, err := parserForContainsTest.Parse(fmt.Sprintf("endPoint~:\"%s\"", mockCloudServiceName))
  167. // if error encounter skip the test
  168. if err != nil {
  169. t.Logf("skipping test case to avoid intermittent test failure if not able to parse filter string")
  170. t.Skip()
  171. }
  172. containsTestFilter, err := compiler.Compile(containsTestRegex)
  173. // if error encounter skip the test
  174. if err != nil {
  175. t.Logf("skipping test case to avoid intermittent test failure if not able to parse filter string")
  176. t.Skip()
  177. }
  178. mcs1inIngress, _ := createMockNetworkDetail(0, 200000, mockCloudService1, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  179. mcs2inIngress, _ := createMockNetworkDetail(0, 400000, mockCloudService2, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  180. mcs3inIngress, _ := createMockNetworkDetail(0, 800000, mockCloudService3, NetworkTrafficDirectionIngress, NetworkTrafficTypeInternet)
  181. mcs1inEgress, _ := createMockNetworkDetail(0.16, 300000, mockCloudService1, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  182. mcs2inEgress, _ := createMockNetworkDetail(0.24, 300000, mockCloudService2, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  183. mcs3inEgress, _ := createMockNetworkDetail(0.35, 300000, mockCloudService3, NetworkTrafficDirectionEgress, NetworkTrafficTypeInternet)
  184. set1 := make(NetworkDetailsSet, 0)
  185. set1[mcs1inIngress.Key()] = mcs1inIngress
  186. set1[mcs2inIngress.Key()] = mcs2inIngress
  187. set1[mcs3inIngress.Key()] = mcs3inIngress
  188. set1[mcs1inEgress.Key()] = mcs1inEgress
  189. set1[mcs2inEgress.Key()] = mcs2inEgress
  190. set1[mcs3inEgress.Key()] = mcs3inEgress
  191. // set2 with only mockservice1 network details for validation
  192. set2 := make(NetworkDetailsSet, 0)
  193. set2[mcs1inIngress.Key()] = mcs1inIngress
  194. set2[mcs1inEgress.Key()] = mcs1inEgress
  195. mockInsight1 := MockNetworkInsightImportantKeys{
  196. Cluster: mockCluster1,
  197. Namespace: mockNamespace1,
  198. Controller: "",
  199. Pod: mockPod1,
  200. Node: "",
  201. Labels: map[string]string{},
  202. Region: "",
  203. Zone: "",
  204. }
  205. ni1 := createMockNetworkInsight(mockInsight1, set1)
  206. ni1Copy := ni1.Clone()
  207. ni1.filterNetworkDetails(equalsTestFilter)
  208. // Check to see after filtering the network details
  209. //only have filtered result that is set2
  210. if !reflect.DeepEqual(ni1.NetworkDetails, set2) {
  211. t.Fatalf("Test_NetworkIngressCombineAndAdd: NetworkIngressDetailSet:filterNetworkDetails failed with equal to filter")
  212. }
  213. // Check to see after filtering the network details only have filtered
  214. // result that is original as a combine data that satisfy the regex
  215. ni1Copy.filterNetworkDetails(containsTestFilter)
  216. if !reflect.DeepEqual(ni1Copy.NetworkDetails, set1) {
  217. t.Fatalf("Test_NetworkIngressCombineAndAdd: NetworkIngressDetailSet:filterNetworkDetails failed with contains to filter")
  218. }
  219. }