opencost_codecs_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. package opencost
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestAllocation_BinaryEncoding(t *testing.T) {
  7. // TODO niko
  8. }
  9. func TestAllocationSet_BinaryEncoding(t *testing.T) {
  10. // TODO niko
  11. }
  12. func BenchmarkAllocationSetRange_BinaryEncoding(b *testing.B) {
  13. endYesterday := time.Now().UTC().Truncate(day)
  14. startYesterday := endYesterday.Add(-day)
  15. startD2 := startYesterday
  16. startD1 := startD2.Add(-day)
  17. startD0 := startD1.Add(-day)
  18. var asr0, asr1 *AllocationSetRange
  19. var bs []byte
  20. var err error
  21. asr0 = NewAllocationSetRange(
  22. GenerateMockAllocationSetClusterIdle(startD0),
  23. GenerateMockAllocationSetClusterIdle(startD1),
  24. GenerateMockAllocationSetClusterIdle(startD2),
  25. )
  26. for it := 0; it < b.N; it++ {
  27. bs, err = asr0.MarshalBinary()
  28. if err != nil {
  29. b.Fatalf("AllocationSetRange.Binary: unexpected error: %s", err)
  30. return
  31. }
  32. asr1 = &AllocationSetRange{}
  33. err = asr1.UnmarshalBinary(bs)
  34. if err != nil {
  35. b.Fatalf("AllocationSetRange.Binary: unexpected error: %s", err)
  36. return
  37. }
  38. if asr0.Length() != asr1.Length() {
  39. b.Fatalf("AllocationSetRange.Binary: expected %d; found %d", asr0.Length(), asr1.Length())
  40. }
  41. if !asr0.Window().Equal(asr1.Window()) {
  42. b.Fatalf("AllocationSetRange.Binary: expected %s; found %s", asr0.Window(), asr1.Window())
  43. }
  44. for i, as0 := range asr0.Allocations {
  45. as1, err := asr1.Get(i)
  46. if err != nil {
  47. b.Fatalf("AllocationSetRange.Binary: unexpected error: %s", err)
  48. }
  49. if as0.Length() != as1.Length() {
  50. b.Fatalf("AllocationSetRange.Binary: expected %d; found %d", as0.Length(), as1.Length())
  51. }
  52. if !as0.Window.Equal(as1.Window) {
  53. b.Fatalf("AllocationSetRange.Binary: expected %s; found %s", as0.Window, as1.Window)
  54. }
  55. for k, a0 := range as0.Allocations {
  56. a1 := as1.Get(k)
  57. if a1 == nil {
  58. b.Fatalf("AllocationSetRange.Binary: missing Allocation: %s", a0)
  59. }
  60. if !a0.Equal(a1) {
  61. b.Fatalf("AllocationSetRange.Binary: unequal Allocations \"%s\": expected %s; found %s", k, a0, a1)
  62. }
  63. }
  64. }
  65. }
  66. }
  67. func TestAllocationSetRange_BinaryEncoding(t *testing.T) {
  68. endYesterday := time.Now().UTC().Truncate(day)
  69. startYesterday := endYesterday.Add(-day)
  70. startD2 := startYesterday
  71. startD1 := startD2.Add(-day)
  72. startD0 := startD1.Add(-day)
  73. var asr0, asr1 *AllocationSetRange
  74. var bs []byte
  75. var err error
  76. asr0 = NewAllocationSetRange(
  77. GenerateMockAllocationSetClusterIdle(startD0),
  78. GenerateMockAllocationSetClusterIdle(startD1),
  79. GenerateMockAllocationSetClusterIdle(startD2),
  80. )
  81. bs, err = asr0.MarshalBinary()
  82. if err != nil {
  83. t.Fatalf("AllocationSetRange.Binary: unexpected error: %s", err)
  84. return
  85. }
  86. asr1 = &AllocationSetRange{}
  87. err = asr1.UnmarshalBinary(bs)
  88. if err != nil {
  89. t.Fatalf("AllocationSetRange.Binary: unexpected error: %s", err)
  90. return
  91. }
  92. if asr0.Length() != asr1.Length() {
  93. t.Fatalf("AllocationSetRange.Binary: expected %d; found %d", asr0.Length(), asr1.Length())
  94. }
  95. if !asr0.Window().Equal(asr1.Window()) {
  96. t.Fatalf("AllocationSetRange.Binary: expected %s; found %s", asr0.Window(), asr1.Window())
  97. }
  98. for i, as0 := range asr0.Allocations {
  99. as1, err := asr1.Get(i)
  100. if err != nil {
  101. t.Fatalf("AllocationSetRange.Binary: unexpected error: %s", err)
  102. }
  103. if as0.Length() != as1.Length() {
  104. t.Fatalf("AllocationSetRange.Binary: expected %d; found %d", as0.Length(), as1.Length())
  105. }
  106. if !as0.Window.Equal(as1.Window) {
  107. t.Fatalf("AllocationSetRange.Binary: expected %s; found %s", as0.Window, as1.Window)
  108. }
  109. for k, a0 := range as0.Allocations {
  110. a1 := as1.Get(k)
  111. if a1 == nil {
  112. t.Fatalf("AllocationSetRange.Binary: missing Allocation: %s", a0)
  113. }
  114. // TODO Sean: fix JSON marshaling of PVs
  115. a1.PVs = a0.PVs
  116. if !a0.Equal(a1) {
  117. t.Fatalf("AllocationSetRange.Binary: unequal Allocations \"%s\": expected \"%s\"; found \"%s\"", k, a0, a1)
  118. }
  119. }
  120. }
  121. }
  122. func TestAny_BinaryEncoding(t *testing.T) {
  123. start := time.Date(2020, time.September, 16, 0, 0, 0, 0, time.UTC)
  124. end := start.Add(24 * time.Hour)
  125. window := NewWindow(&start, &end)
  126. var a0, a1 *Any
  127. var bs []byte
  128. var err error
  129. a0 = NewAsset(*window.start, *window.end, window)
  130. a0.SetProperties(&AssetProperties{
  131. Name: "any1",
  132. Cluster: "cluster1",
  133. ProviderID: "世界",
  134. })
  135. a0.Cost = 123.45
  136. a0.SetAdjustment(1.23)
  137. bs, err = a0.MarshalBinary()
  138. if err != nil {
  139. t.Fatalf("Any.Binary: unexpected error: %s", err)
  140. }
  141. a1 = &Any{}
  142. err = a1.UnmarshalBinary(bs)
  143. if err != nil {
  144. t.Fatalf("Any.Binary: unexpected error: %s", err)
  145. }
  146. if a1.Properties.Name != a0.Properties.Name {
  147. t.Fatalf("Any.Binary: expected %s, found %s", a0.Properties.Name, a1.Properties.Name)
  148. }
  149. if a1.Properties.Cluster != a0.Properties.Cluster {
  150. t.Fatalf("Any.Binary: expected %s, found %s", a0.Properties.Cluster, a1.Properties.Cluster)
  151. }
  152. if a1.Properties.ProviderID != a0.Properties.ProviderID {
  153. t.Fatalf("Any.Binary: expected %s, found %s", a0.Properties.ProviderID, a1.Properties.ProviderID)
  154. }
  155. if a1.Adjustment != a0.Adjustment {
  156. t.Fatalf("Any.Binary: expected %f, found %f", a0.Adjustment, a1.Adjustment)
  157. }
  158. if a1.TotalCost() != a0.TotalCost() {
  159. t.Fatalf("Any.Binary: expected %f, found %f", a0.TotalCost(), a1.TotalCost())
  160. }
  161. if !a1.Window.Equal(a0.Window) {
  162. t.Fatalf("Any.Binary: expected %s, found %s", a0.Window, a1.Window)
  163. }
  164. }
  165. func TestAsset_BinaryEncoding(t *testing.T) {
  166. // TODO niko
  167. }
  168. func TestAssetSet_BinaryEncoding(t *testing.T) {
  169. // TODO niko
  170. }
  171. func TestAssetSetRange_BinaryEncoding(t *testing.T) {
  172. endYesterday := time.Now().UTC().Truncate(day)
  173. startYesterday := endYesterday.Add(-day)
  174. startD2 := startYesterday
  175. startD1 := startD2.Add(-day)
  176. startD0 := startD1.Add(-day)
  177. var asr0, asr1 *AssetSetRange
  178. var bs []byte
  179. var err error
  180. asr0 = NewAssetSetRange(
  181. GenerateMockAssetSet(startD0, day),
  182. GenerateMockAssetSet(startD1, day),
  183. GenerateMockAssetSet(startD2, day),
  184. )
  185. bs, err = asr0.MarshalBinary()
  186. if err != nil {
  187. t.Fatalf("AssetSetRange.Binary: unexpected error: %s", err)
  188. return
  189. }
  190. asr1 = &AssetSetRange{}
  191. err = asr1.UnmarshalBinary(bs)
  192. if err != nil {
  193. t.Fatalf("AssetSetRange.Binary: unexpected error: %s", err)
  194. return
  195. }
  196. if asr0.Length() != asr1.Length() {
  197. t.Fatalf("AssetSetRange.Binary: expected %d; found %d", asr0.Length(), asr1.Length())
  198. }
  199. if !asr0.Window().Equal(asr1.Window()) {
  200. t.Fatalf("AssetSetRange.Binary: expected %s; found %s", asr0.Window(), asr1.Window())
  201. }
  202. for i, as0 := range asr0.Assets {
  203. as1, err := asr1.Get(i)
  204. if err != nil {
  205. t.Fatalf("AssetSetRange.Binary: unexpected error: %s", err)
  206. }
  207. if as0.Length() != as1.Length() {
  208. t.Fatalf("AssetSetRange.Binary: expected %d; found %d", as0.Length(), as1.Length())
  209. }
  210. if !as0.Window.Equal(as1.Window) {
  211. t.Fatalf("AssetSetRange.Binary: expected %s; found %s", as0.Window, as1.Window)
  212. }
  213. for k, a0 := range as0.Assets {
  214. a1, ok := as1.Get(k)
  215. if !ok {
  216. t.Fatalf("AssetSetRange.Binary: missing Asset: %s", a0)
  217. }
  218. if !a0.Equal(a1) {
  219. t.Fatalf("AssetSetRange.Binary: unequal Assets \"%s\": expected %s; found %s", k, a0, a1)
  220. }
  221. }
  222. }
  223. }
  224. func TestBreakdown_BinaryEncoding(t *testing.T) {
  225. var b0, b1 *Breakdown
  226. var bs []byte
  227. var err error
  228. b0 = &Breakdown{
  229. Idle: 0.75,
  230. Other: 0.1,
  231. System: 0.0,
  232. User: 0.15,
  233. }
  234. bs, err = b0.MarshalBinary()
  235. if err != nil {
  236. t.Fatalf("Breakdown.Binary: unexpected error: %s", err)
  237. }
  238. b1 = &Breakdown{}
  239. err = b1.UnmarshalBinary(bs)
  240. if err != nil {
  241. t.Fatalf("Breakdown.Binary: unexpected error: %s", err)
  242. }
  243. if b1.Idle != b0.Idle {
  244. t.Fatalf("Breakdown.Binary: expected %f, found %f", b0.Idle, b1.Idle)
  245. }
  246. if b1.Other != b0.Other {
  247. t.Fatalf("Breakdown.Binary: expected %f, found %f", b0.Other, b1.Other)
  248. }
  249. if b1.System != b0.System {
  250. t.Fatalf("Breakdown.Binary: expected %f, found %f", b0.System, b1.System)
  251. }
  252. if b1.User != b0.User {
  253. t.Fatalf("Breakdown.Binary: expected %f, found %f", b0.User, b1.User)
  254. }
  255. }
  256. func TestCloudAny_BinaryEncoding(t *testing.T) {
  257. ws := time.Date(2020, time.September, 16, 0, 0, 0, 0, time.UTC)
  258. we := ws.Add(24 * time.Hour)
  259. window := NewWindow(&ws, &we)
  260. var a0, a1 *Cloud
  261. var bs []byte
  262. var err error
  263. a0 = NewCloud(ComputeCategory, "providerid1", *window.start, *window.end, window)
  264. a0.Cost = 6.09
  265. a0.SetAdjustment(-1.23)
  266. bs, err = a0.MarshalBinary()
  267. if err != nil {
  268. t.Fatalf("CloudAny.Binary: unexpected error: %s", err)
  269. }
  270. a1 = &Cloud{}
  271. err = a1.UnmarshalBinary(bs)
  272. if err != nil {
  273. t.Fatalf("CloudAny.Binary: unexpected error: %s", err)
  274. }
  275. if !a0.Equal(a1) {
  276. t.Fatalf("CloudAny.Binary: expected %v, found %v", a0, a1)
  277. }
  278. }
  279. func TestClusterManagement_BinaryEncoding(t *testing.T) {
  280. ws := time.Date(2020, time.September, 16, 0, 0, 0, 0, time.UTC)
  281. we := ws.Add(24 * time.Hour)
  282. window := NewWindow(&ws, &we)
  283. var a0, a1 *ClusterManagement
  284. var bs []byte
  285. var err error
  286. a0 = NewClusterManagement(AWSProvider, "cluster1", window)
  287. a0.Cost = 4.003
  288. a0.SetAdjustment(-3.23)
  289. bs, err = a0.MarshalBinary()
  290. if err != nil {
  291. t.Fatalf("ClusterManagement.Binary: unexpected error: %s", err)
  292. }
  293. a1 = &ClusterManagement{}
  294. err = a1.UnmarshalBinary(bs)
  295. if err != nil {
  296. t.Fatalf("ClusterManagement.Binary: unexpected error: %s", err)
  297. }
  298. if !a0.Equal(a1) {
  299. t.Fatalf("ClusterManagement.Binary: expected %v, found %v", a0, a1)
  300. }
  301. }
  302. func TestDisk_BinaryEncoding(t *testing.T) {
  303. ws := time.Date(2020, time.September, 16, 0, 0, 0, 0, time.UTC)
  304. we := ws.Add(24 * time.Hour)
  305. window := NewWindow(&ws, &we)
  306. hours := window.Duration().Hours()
  307. start := time.Date(2020, time.September, 16, 3, 0, 0, 0, time.UTC)
  308. end := time.Date(2020, time.September, 16, 15, 12, 0, 0, time.UTC)
  309. var a0, a1 *Disk
  310. var bs []byte
  311. var err error
  312. a0 = NewDisk("any1", "cluster1", "世界", start, end, window)
  313. a0.ByteHours = 100 * 1024 * 1024 * 1024 * hours
  314. a0.Cost = 4.003
  315. a0.Local = 0.4
  316. a0.Breakdown = &Breakdown{
  317. Idle: 0.9,
  318. Other: 0.05,
  319. System: 0.05,
  320. User: 0.0,
  321. }
  322. a0.SetAdjustment(-3.23)
  323. bs, err = a0.MarshalBinary()
  324. if err != nil {
  325. t.Fatalf("Disk.Binary: unexpected error: %s", err)
  326. }
  327. a1 = &Disk{}
  328. err = a1.UnmarshalBinary(bs)
  329. if err != nil {
  330. t.Fatalf("Disk.Binary: unexpected error: %s", err)
  331. }
  332. if !a0.Equal(a1) {
  333. t.Fatalf("Disk.Binary: expected %v, found %v", a0, a1)
  334. }
  335. }
  336. func TestNode_BinaryEncoding(t *testing.T) {
  337. ws := time.Date(2020, time.September, 16, 0, 0, 0, 0, time.UTC)
  338. we := ws.Add(24 * time.Hour)
  339. window := NewWindow(&ws, &we)
  340. hours := window.Duration().Hours()
  341. start := time.Date(2020, time.September, 16, 3, 0, 0, 0, time.UTC)
  342. end := time.Date(2020, time.September, 16, 15, 12, 0, 0, time.UTC)
  343. var a0, a1 *Node
  344. var bs []byte
  345. var err error
  346. a0 = NewNode("any1", "cluster1", "世界", start, end, window)
  347. a0.NodeType = "n2-standard"
  348. a0.Preemptible = 1.0
  349. a0.CPUCoreHours = 2.0 * hours
  350. a0.RAMByteHours = 12.0 * gb * hours
  351. a0.CPUCost = 1.50
  352. a0.GPUCost = 30.44
  353. a0.RAMCost = 15.0
  354. a0.Discount = 0.9
  355. a0.CPUBreakdown = &Breakdown{
  356. Idle: 0.9,
  357. Other: 0.05,
  358. System: 0.05,
  359. User: 0.0,
  360. }
  361. a0.RAMBreakdown = &Breakdown{
  362. Idle: 0.4,
  363. Other: 0.05,
  364. System: 0.05,
  365. User: 0.5,
  366. }
  367. a0.SetAdjustment(1.23)
  368. bs, err = a0.MarshalBinary()
  369. if err != nil {
  370. t.Fatalf("Node.Binary: unexpected error: %s", err)
  371. }
  372. a1 = &Node{}
  373. err = a1.UnmarshalBinary(bs)
  374. if err != nil {
  375. t.Fatalf("Node.Binary: unexpected error: %s", err)
  376. }
  377. if !a0.Equal(a1) {
  378. t.Fatalf("Node.Binary: expected %v, found %v", a0, a1)
  379. }
  380. }
  381. func TestProperties_BinaryEncoding(t *testing.T) {
  382. var p0, p1 *AllocationProperties
  383. var bs []byte
  384. var err error
  385. // empty properties
  386. p0 = &AllocationProperties{}
  387. bs, err = p0.MarshalBinary()
  388. if err != nil {
  389. t.Fatalf("AllocationProperties.Binary: unexpected error: %s", err)
  390. }
  391. p1 = &AllocationProperties{}
  392. err = p1.UnmarshalBinary(bs)
  393. if err != nil {
  394. t.Fatalf("AllocationProperties.Binary: unexpected error: %s", err)
  395. }
  396. if !p0.Equal(p1) {
  397. t.Fatalf("AllocationProperties.Binary: expected %s; found %s", p0, p1)
  398. }
  399. // complete properties
  400. p0 = &AllocationProperties{}
  401. p0.Cluster = "cluster1"
  402. p0.Container = "container-abc-1"
  403. p0.Controller = "daemonset-abc"
  404. p0.ControllerKind = "daemonset"
  405. p0.Namespace = "namespace1"
  406. p0.NamespaceLabels = map[string]string{
  407. "app": "cost-analyzer-namespace",
  408. "kubernetes.io/name": "cost-analyzer",
  409. }
  410. p0.NamespaceAnnotations = map[string]string{
  411. "com.kubernetes.io/managed-by": "helm",
  412. "kubernetes.io/last-applied-configuration": "cost-analyzer",
  413. }
  414. p0.Node = "node1"
  415. p0.Pod = "daemonset-abc-123"
  416. p0.Labels = map[string]string{
  417. "app": "cost-analyzer",
  418. "tier": "frontend",
  419. }
  420. p0.Services = []string{"kubecost-frontend"}
  421. bs, err = p0.MarshalBinary()
  422. if err != nil {
  423. t.Fatalf("AllocationProperties.Binary: unexpected error: %s", err)
  424. }
  425. p1 = &AllocationProperties{}
  426. err = p1.UnmarshalBinary(bs)
  427. if err != nil {
  428. t.Fatalf("AllocationProperties.Binary: unexpected error: %s", err)
  429. }
  430. if !p0.Equal(p1) {
  431. t.Fatalf("AllocationProperties.Binary: expected %s; found %s", p0, p1)
  432. }
  433. // incomplete properties
  434. p0 = &AllocationProperties{}
  435. p0.Cluster = ("cluster1")
  436. p0.Controller = "daemonset-abc"
  437. p0.ControllerKind = "daemonset"
  438. p0.Namespace = "namespace1"
  439. p0.NamespaceAnnotations = map[string]string{
  440. "com.kubernetes.io/managed-by": "helm",
  441. "kubernetes.io/last-applied-configuration": "cost-analyzer",
  442. }
  443. p0.Services = []string{}
  444. bs, err = p0.MarshalBinary()
  445. if err != nil {
  446. t.Fatalf("AllocationProperties.Binary: unexpected error: %s", err)
  447. }
  448. p1 = &AllocationProperties{}
  449. err = p1.UnmarshalBinary(bs)
  450. if err != nil {
  451. t.Fatalf("AllocationProperties.Binary: unexpected error: %s", err)
  452. }
  453. if !p0.Equal(p1) {
  454. t.Fatalf("AllocationProperties.Binary: expected %s; found %s", p0, p1)
  455. }
  456. }
  457. func TestShared_BinaryEncoding(t *testing.T) {
  458. ws := time.Date(2020, time.September, 16, 0, 0, 0, 0, time.UTC)
  459. we := ws.Add(24 * time.Hour)
  460. window := NewWindow(&ws, &we)
  461. var a0, a1 *SharedAsset
  462. var bs []byte
  463. var err error
  464. a0 = NewSharedAsset("any1", window)
  465. a0.Cost = 4.04
  466. a0.SetAdjustment(1.23)
  467. bs, err = a0.MarshalBinary()
  468. if err != nil {
  469. t.Fatalf("SharedAsset.Binary: unexpected error: %s", err)
  470. }
  471. a1 = &SharedAsset{}
  472. err = a1.UnmarshalBinary(bs)
  473. if err != nil {
  474. t.Fatalf("SharedAsset.Binary: unexpected error: %s", err)
  475. }
  476. if !a0.Equal(a1) {
  477. t.Fatalf("SharedAsset.Binary: expected %v, found %v", a0, a1)
  478. }
  479. }
  480. func TestWindow_BinaryEncoding(t *testing.T) {
  481. var w0, w1 Window
  482. var bs []byte
  483. var err error
  484. // Window (nil, nil)
  485. w0 = NewWindow(nil, nil)
  486. bs, err = w0.MarshalBinary()
  487. if err != nil {
  488. t.Fatalf("Window.Binary: unexpected error: %s", err)
  489. }
  490. err = w1.UnmarshalBinary(bs)
  491. if err != nil {
  492. t.Fatalf("Window.Binary: unexpected error: %s", err)
  493. }
  494. if w1.Start() != w0.Start() {
  495. t.Fatalf("Window.Binary: expected %v; found %v", w0.Start(), w1.Start())
  496. }
  497. if w1.End() != w0.End() {
  498. t.Fatalf("Window.Binary: expected %v; found %v", w0.End(), w1.End())
  499. }
  500. // Window (time, nil)
  501. ts := time.Now()
  502. w0 = NewWindow(&ts, nil)
  503. bs, err = w0.MarshalBinary()
  504. if err != nil {
  505. t.Fatalf("Window.Binary: unexpected error: %s", err)
  506. }
  507. err = w1.UnmarshalBinary(bs)
  508. if err != nil {
  509. t.Fatalf("Window.Binary: unexpected error: %s", err)
  510. }
  511. if !w1.Start().Equal(*w0.Start()) {
  512. t.Fatalf("Window.Binary: expected %v; found %v", w0.Start(), w1.Start())
  513. }
  514. if w1.End() != w0.End() {
  515. t.Fatalf("Window.Binary: expected %v; found %v", w0.End(), w1.End())
  516. }
  517. // Window (nil, time)
  518. te := time.Now()
  519. w0 = NewWindow(nil, &te)
  520. bs, err = w0.MarshalBinary()
  521. if err != nil {
  522. t.Fatalf("Window.Binary: unexpected error: %s", err)
  523. }
  524. err = w1.UnmarshalBinary(bs)
  525. if err != nil {
  526. t.Fatalf("Window.Binary: unexpected error: %s", err)
  527. }
  528. if w1.Start() != w0.Start() {
  529. t.Fatalf("Window.Binary: expected %v; found %v", w0.Start(), w1.Start())
  530. }
  531. if !w1.End().Equal(*w0.End()) {
  532. t.Fatalf("Window.Binary: expected %v; found %v", w0.End(), w1.End())
  533. }
  534. // Window (time, time)
  535. ts, te = time.Now(), time.Now()
  536. w0 = NewWindow(&ts, &te)
  537. bs, err = w0.MarshalBinary()
  538. if err != nil {
  539. t.Fatalf("Window.Binary: unexpected error: %s", err)
  540. }
  541. err = w1.UnmarshalBinary(bs)
  542. if err != nil {
  543. t.Fatalf("Window.Binary: unexpected error: %s", err)
  544. }
  545. if !w1.Start().Equal(*w0.Start()) {
  546. t.Fatalf("Window.Binary: expected %v; found %v", w0.Start(), w1.Start())
  547. }
  548. if !w1.End().Equal(*w0.End()) {
  549. t.Fatalf("Window.Binary: expected %v; found %v", w0.End(), w1.End())
  550. }
  551. }