decoder_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package exporter
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/diagnostics"
  7. "github.com/opencost/opencost/core/pkg/heartbeat"
  8. "github.com/opencost/opencost/core/pkg/model"
  9. "github.com/opencost/opencost/core/pkg/model/pb"
  10. "github.com/opencost/opencost/core/pkg/opencost"
  11. "github.com/opencost/opencost/core/pkg/util"
  12. "github.com/opencost/opencost/core/pkg/util/json"
  13. "google.golang.org/protobuf/proto"
  14. )
  15. type decoderTestCase[T any] struct {
  16. name string
  17. data []byte
  18. want *T
  19. wantErr bool
  20. }
  21. func generateBadBytes() []byte {
  22. buff := util.NewBuffer()
  23. for i := 0; i < 10; i++ {
  24. buff.WriteUInt64(9999999)
  25. }
  26. return buff.Bytes()
  27. }
  28. func TestBingenDecoder(t *testing.T) {
  29. badBytes := generateBadBytes()
  30. now := time.Now().UTC().Truncate(24 * time.Hour)
  31. start := now.Add(-(24 * 5) * time.Hour)
  32. // Define and Run Allocation Tests
  33. allocSet := opencost.GenerateMockAllocationSet(start)
  34. allocSetRaw, err := allocSet.MarshalBinary()
  35. if err != nil {
  36. t.Errorf("failed to marshal allocation set: %s", err.Error())
  37. }
  38. allocTests := []decoderTestCase[opencost.AllocationSet]{
  39. {
  40. name: "allocation valid",
  41. data: allocSetRaw,
  42. want: allocSet,
  43. wantErr: false,
  44. },
  45. {
  46. name: "allocation invalid",
  47. data: badBytes,
  48. want: nil,
  49. wantErr: true,
  50. },
  51. }
  52. testDecoder(t, BingenDecoder, allocTests)
  53. // Define and Run Asset Tests
  54. assetSet := opencost.GenerateMockAssetSet(start, 24*time.Hour)
  55. assetSetRaw, err := assetSet.MarshalBinary()
  56. if err != nil {
  57. t.Errorf("failed to marshal asset set: %s", err.Error())
  58. }
  59. assetTests := []decoderTestCase[opencost.AssetSet]{
  60. {
  61. name: "asset valid",
  62. data: assetSetRaw,
  63. want: assetSet,
  64. wantErr: false,
  65. },
  66. {
  67. name: "asset invalid",
  68. data: badBytes,
  69. want: nil,
  70. wantErr: true,
  71. },
  72. }
  73. testDecoder(t, BingenDecoder, assetTests)
  74. // Define and Run Cloud Cost Tests
  75. CloudCostSet := opencost.GenerateMockCloudCostSet(start, start.Add(24*time.Hour), "gcp", "gke")
  76. CloudCostSetRaw, err := CloudCostSet.MarshalBinary()
  77. if err != nil {
  78. t.Errorf("failed to marshal cloud cost set: %s", err.Error())
  79. }
  80. cloudCostTests := []decoderTestCase[opencost.CloudCostSet]{
  81. {
  82. name: "cloud cost valid",
  83. data: CloudCostSetRaw,
  84. want: CloudCostSet,
  85. wantErr: false,
  86. },
  87. {
  88. name: "cloud cost invalid",
  89. data: badBytes,
  90. want: nil,
  91. wantErr: true,
  92. },
  93. }
  94. testDecoder(t, BingenDecoder, cloudCostTests)
  95. // Define and Run Network Insight Tests
  96. networkInsightSet := opencost.GenerateMockNetworkInsightSet(start, start.Add(24*time.Hour))
  97. networkInsightSetRaw, err := networkInsightSet.MarshalBinary()
  98. if err != nil {
  99. t.Errorf("failed to marshal network insight set: %s", err.Error())
  100. }
  101. networkInsightTests := []decoderTestCase[opencost.NetworkInsightSet]{
  102. {
  103. name: "network insight valid",
  104. data: networkInsightSetRaw,
  105. want: networkInsightSet,
  106. wantErr: false,
  107. },
  108. {
  109. name: "network insight invalid",
  110. data: badBytes,
  111. want: nil,
  112. wantErr: true,
  113. },
  114. }
  115. testDecoder(t, BingenDecoder, networkInsightTests)
  116. }
  117. func TestJsonDecoder(t *testing.T) {
  118. badBytes := generateBadBytes()
  119. now := time.Now().UTC().Truncate(24 * time.Hour)
  120. start := now.Add(-(24 * 5) * time.Hour)
  121. hb := heartbeat.Heartbeat{
  122. Id: "heartBeatID",
  123. Timestamp: start,
  124. Uptime: 123,
  125. Application: "mock",
  126. Version: "test",
  127. Metadata: map[string]any{
  128. "str": "test",
  129. "num": 1.0,
  130. },
  131. }
  132. hbraw, err := json.Marshal(hb)
  133. if err != nil {
  134. t.Errorf("failed to marshal heartbeat: %s", err.Error())
  135. }
  136. heartbeatTests := []decoderTestCase[heartbeat.Heartbeat]{
  137. {
  138. name: "heartbeat valid",
  139. data: hbraw,
  140. want: &hb,
  141. wantErr: false,
  142. },
  143. {
  144. name: "heartbeat invalid",
  145. data: badBytes,
  146. want: nil,
  147. wantErr: true,
  148. },
  149. }
  150. testDecoder(t, JSONDecoder, heartbeatTests)
  151. }
  152. func TestGzipDecoder(t *testing.T) {
  153. badBytes := generateBadBytes()
  154. now := time.Now().UTC().Truncate(24 * time.Hour)
  155. start := now.Add(-(24 * 5) * time.Hour)
  156. diag := diagnostics.DiagnosticResult{
  157. ID: "diagnosticID",
  158. Name: "diagnisticName",
  159. Description: "Test Diagnostic",
  160. Category: "test",
  161. Timestamp: start,
  162. Error: "test",
  163. Details: map[string]any{
  164. "str": "test",
  165. "num": 1.0,
  166. },
  167. }
  168. diagRaw, err := json.Marshal(diag)
  169. if err != nil {
  170. t.Errorf("failed to marshal diagnostic: %s", err.Error())
  171. }
  172. diagCompressed, err := gZipEncode(diagRaw)
  173. if err != nil {
  174. t.Errorf("failed to compress diagnostic: %s", err.Error())
  175. }
  176. badCompressed, err := gZipEncode(badBytes)
  177. if err != nil {
  178. t.Errorf("failed to compress bad bytes: %s", err.Error())
  179. }
  180. diagnosticTests := []decoderTestCase[diagnostics.DiagnosticResult]{
  181. {
  182. name: "diagnostic valid",
  183. data: diagCompressed,
  184. want: &diag,
  185. wantErr: false,
  186. },
  187. {
  188. name: "diagnostic invalid",
  189. data: badCompressed,
  190. want: nil,
  191. wantErr: true,
  192. },
  193. {
  194. name: "diagnostic bypass valid",
  195. data: diagRaw,
  196. want: &diag,
  197. wantErr: false,
  198. },
  199. {
  200. name: "diagnostic bypass invalid",
  201. data: badBytes,
  202. want: nil,
  203. wantErr: true,
  204. },
  205. }
  206. testDecoder(t, GetGzipDecoder[diagnostics.DiagnosticResult](JSONDecoder), diagnosticTests)
  207. }
  208. func TestProtobufDecoder(t *testing.T) {
  209. badBytes := generateBadBytes()
  210. now := time.Now().UTC().Truncate(24 * time.Hour)
  211. start := now.Add(-(24 * 5) * time.Hour)
  212. customCostSet := model.GenerateMockCustomCostSet(start, start.Add(24*time.Hour))
  213. customCostSetRaw, err := proto.Marshal(customCostSet)
  214. if err != nil {
  215. t.Errorf("failed to marshal custom cost set: %s", err.Error())
  216. }
  217. customCostTests := []decoderTestCase[pb.CustomCostResponse]{
  218. {
  219. name: "custom cost valid",
  220. data: customCostSetRaw,
  221. want: customCostSet,
  222. wantErr: false,
  223. },
  224. {
  225. name: "custom cost invalid",
  226. data: badBytes,
  227. want: nil,
  228. wantErr: true,
  229. },
  230. }
  231. testProtoBufDecoder(t, ProtobufDecoder, customCostTests)
  232. labelsResponse := model.GenerateMockLabelResponse(start, pb.Resolution_RESOLUTION_1D)
  233. labelsResponseRaw, err := proto.Marshal(labelsResponse)
  234. if err != nil {
  235. t.Errorf("failed to marshal custom cost set: %s", err.Error())
  236. }
  237. labelsResponseTests := []decoderTestCase[pb.LabelsResponse]{
  238. {
  239. name: "labels response valid",
  240. data: labelsResponseRaw,
  241. want: labelsResponse,
  242. wantErr: false,
  243. },
  244. {
  245. name: "labels response invalid",
  246. data: badBytes,
  247. want: nil,
  248. wantErr: true,
  249. },
  250. }
  251. testProtoBufDecoder(t, ProtobufDecoder, labelsResponseTests)
  252. }
  253. func testProtoBufDecoder[T any, U ProtoMessagePtr[T]](t *testing.T, decoder Decoder[T], testCases []decoderTestCase[T]) {
  254. for _, tt := range testCases {
  255. t.Run(tt.name, func(t *testing.T) {
  256. got, err := decoder(tt.data)
  257. if (err != nil) != tt.wantErr {
  258. t.Errorf("Decoder() error = %v, wantErr %v", err, tt.wantErr)
  259. if err != nil {
  260. t.Errorf("Error: %s", err.Error())
  261. }
  262. return
  263. }
  264. if !proto.Equal(U(got), U(tt.want)) {
  265. t.Errorf("Decoder() got = %v, want %v", got, tt.want)
  266. }
  267. })
  268. }
  269. }
  270. func testDecoder[T any](t *testing.T, decoder Decoder[T], testCases []decoderTestCase[T]) {
  271. for _, tt := range testCases {
  272. t.Run(tt.name, func(t *testing.T) {
  273. got, err := decoder(tt.data)
  274. if (err != nil) != tt.wantErr {
  275. t.Errorf("Decoder() error = %v, wantErr %v", err, tt.wantErr)
  276. if err != nil {
  277. t.Errorf("Error: %s", err.Error())
  278. }
  279. return
  280. }
  281. if !reflect.DeepEqual(got, tt.want) {
  282. t.Errorf("Decoder() got = %v, want %v", got, tt.want)
  283. }
  284. })
  285. }
  286. }