github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/columns/group/group_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 group 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 "github.com/stretchr/testify/require" 22 23 "github.com/inspektor-gadget/inspektor-gadget/pkg/columns" 24 ) 25 26 func TestGroupSum(t *testing.T) { 27 type Embedded struct { 28 EmbeddedInt int64 `column:"embeddedInt,group:sum"` 29 EmbeddedFloat float64 `column:"embeddedFloat,group:sum"` 30 } 31 type testStruct struct { 32 Name string `column:"name"` 33 Int int64 `column:"int,group:sum"` 34 Uint uint64 `column:"uint,group:sum"` 35 Float float64 `column:"float,group:sum"` 36 Secondary int `column:"secondary"` 37 Embedded 38 } 39 type testDefinition struct { 40 Name string 41 GroupBy []string 42 Input []*testStruct 43 ExpectedResult []*testStruct 44 ExpectError bool 45 } 46 47 entries := []*testStruct{ 48 {Name: "a", Int: 1, Float: 1, Uint: 1, Secondary: 1, Embedded: Embedded{EmbeddedInt: 1, EmbeddedFloat: 1}}, 49 {Name: "a", Int: 1, Float: 1, Uint: 1, Secondary: 2, Embedded: Embedded{EmbeddedInt: 1, EmbeddedFloat: 1}}, 50 {Name: "b", Int: 2, Float: 2, Uint: 2, Secondary: 2, Embedded: Embedded{EmbeddedInt: 2, EmbeddedFloat: 2}}, 51 {Name: "b", Int: 2, Float: 2, Uint: 2, Secondary: 3, Embedded: Embedded{EmbeddedInt: 2, EmbeddedFloat: 2}}, 52 nil, 53 } 54 55 tests := []testDefinition{ 56 { 57 Name: "GroupAll", 58 GroupBy: []string{""}, 59 Input: entries, 60 ExpectedResult: []*testStruct{ 61 { 62 Name: "a", 63 Int: 6, 64 Uint: 6, 65 Float: 6, 66 Secondary: 1, 67 Embedded: Embedded{ 68 EmbeddedInt: 6, 69 EmbeddedFloat: 6, 70 }, 71 }, 72 }, 73 }, 74 { 75 Name: "GroupByColumn", 76 GroupBy: []string{"name"}, 77 Input: entries, 78 ExpectedResult: []*testStruct{ 79 { 80 Name: "a", 81 Int: 2, 82 Uint: 2, 83 Float: 2, 84 Secondary: 1, 85 Embedded: Embedded{ 86 EmbeddedInt: 2, 87 EmbeddedFloat: 2, 88 }, 89 }, 90 { 91 Name: "b", 92 Int: 4, 93 Uint: 4, 94 Float: 4, 95 Secondary: 2, 96 Embedded: Embedded{ 97 EmbeddedInt: 4, 98 EmbeddedFloat: 4, 99 }, 100 }, 101 }, 102 }, 103 { 104 Name: "GroupByMultipleColumn", 105 GroupBy: []string{"secondary", "name"}, 106 Input: entries, 107 ExpectedResult: []*testStruct{ 108 { 109 Name: "a", 110 Int: 4, 111 Uint: 4, 112 Float: 4, 113 Secondary: 1, 114 Embedded: Embedded{ 115 EmbeddedInt: 4, 116 EmbeddedFloat: 4, 117 }, 118 }, 119 { 120 Name: "b", 121 Int: 2, 122 Uint: 2, 123 Float: 2, 124 Secondary: 3, 125 Embedded: Embedded{ 126 EmbeddedInt: 2, 127 EmbeddedFloat: 2, 128 }, 129 }, 130 }, 131 }, 132 { 133 Name: "InvalidColumn", 134 GroupBy: []string{"foobar"}, 135 Input: entries, 136 ExpectedResult: nil, 137 ExpectError: true, 138 }, 139 { 140 Name: "NilArray", 141 GroupBy: []string{"name"}, 142 Input: nil, 143 ExpectedResult: nil, 144 ExpectError: false, 145 }, 146 } 147 148 cols, err := columns.NewColumns[testStruct]() 149 require.NoError(t, err) 150 151 cmap := cols.GetColumnMap() 152 153 for _, test := range tests { 154 t.Run(test.Name, func(t *testing.T) { 155 result, err := GroupEntries(cmap, test.Input, test.GroupBy) 156 if !test.ExpectError { 157 require.NoError(t, err) 158 } 159 if test.ExpectError { 160 require.Error(t, err) 161 } 162 assert.Equal(t, result, test.ExpectedResult) 163 }) 164 } 165 }