github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/snapshotcombiner/snapshotcombiner_test.go (about) 1 // Copyright 2022-2023 The Inspektor Gadget authors 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 15 package snapshotcombiner 16 17 import "testing" 18 19 func TestSnapshotCombiner(t *testing.T) { 20 ttl := 2 21 sc := NewSnapshotCombiner[int](ttl) 22 23 type testRun struct { 24 Name string 25 IntervalStats map[string][]*int 26 ExpectedEntries int 27 } 28 29 data1 := 1 30 data2 := 2 31 data3 := 3 32 data4 := 4 33 34 testSteps := []testRun{ 35 { 36 Name: "No snapshots sent, should return an empty result", 37 }, 38 { 39 Name: "one node sends a snapshot", 40 IntervalStats: map[string][]*int{ 41 "node1": {&data1}, 42 }, 43 ExpectedEntries: 1, 44 }, 45 { 46 Name: "no node sends snapshots, old snapshots should still be there", 47 ExpectedEntries: 1, 48 }, 49 { 50 Name: "no node sends snapshots, old snapshots should have gotten deleted", 51 }, 52 53 { 54 Name: "one node sends a snapshot", 55 IntervalStats: map[string][]*int{ 56 "node1": {&data1}, 57 }, 58 ExpectedEntries: 1, 59 }, 60 { 61 Name: "same node sends a snapshot (defaultTTL refresh), still one result", 62 IntervalStats: map[string][]*int{ 63 "node1": {&data1}, 64 }, 65 ExpectedEntries: 1, 66 }, 67 { 68 Name: "no node sends snapshots, old snapshots should still be there", 69 ExpectedEntries: 1, 70 }, 71 { 72 Name: "no node sends snapshots, old snapshots should have gotten deleted", 73 }, 74 75 { 76 Name: "two nodes send snapshots with two entries each", 77 IntervalStats: map[string][]*int{ 78 "node1": {&data1, &data2}, 79 "node2": {&data3, &data4}, 80 }, 81 ExpectedEntries: 4, 82 }, 83 { 84 Name: "only one of the two nodes sends a snapshot in the next interval, still all 4 entries should show", 85 IntervalStats: map[string][]*int{ 86 "node1": {&data1, &data2}, 87 }, 88 ExpectedEntries: 4, 89 }, 90 { 91 Name: "still only one of the two nodes sends a snapshot, two entries from node2 should be lost now", 92 IntervalStats: map[string][]*int{ 93 "node1": {&data1, &data2}, 94 }, 95 ExpectedEntries: 2, 96 }, 97 } 98 99 for _, step := range testSteps { 100 for nodeName, nodeStats := range step.IntervalStats { 101 sc.AddSnapshot(nodeName, nodeStats) 102 } 103 res, _ := sc.GetSnapshots() 104 if len(res) != step.ExpectedEntries { 105 t.Errorf("expected %d, got %d", step.ExpectedEntries, len(res)) 106 } 107 } 108 }