github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/prometheus/stubgadgets_test.go (about) 1 // Copyright 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 prometheus 16 17 import ( 18 "sync" 19 20 "github.com/inspektor-gadget/inspektor-gadget/pkg/columns" 21 gadgetcontext "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-context" 22 gadgetregistry "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-registry" 23 "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets" 24 "github.com/inspektor-gadget/inspektor-gadget/pkg/params" 25 "github.com/inspektor-gadget/inspektor-gadget/pkg/parser" 26 ) 27 28 type key string 29 30 const ( 31 valuekey key = "pkey" 32 ) 33 34 // stubEvent is shared by both stub gadgets just to keep it simpler 35 type stubEvent struct { 36 Comm string `json:"comm,omitempty" column:"comm"` 37 Uid uint32 `json:"uid,omitempty" column:"uid"` 38 IntVal uint32 `json:"intval,omitempty" column:"intval"` 39 FloatVal float32 `json:"floatval,omitempty" column:"floatval"` 40 } 41 42 // stubTracer is a fake tracer gadget used for testing 43 type stubTracer struct { 44 eventCallback func(ev *stubEvent) 45 } 46 47 func (t *stubTracer) Name() string { 48 return "stubtracer" 49 } 50 51 func (t *stubTracer) Description() string { 52 return "fake tracer gadget" 53 } 54 55 func (t *stubTracer) Category() string { 56 return gadgets.CategoryTrace 57 } 58 59 func (t *stubTracer) Type() gadgets.GadgetType { 60 return gadgets.TypeTrace 61 } 62 63 func (t *stubTracer) ParamDescs() params.ParamDescs { 64 return nil 65 } 66 67 func (t *stubTracer) Parser() parser.Parser { 68 cols := columns.MustCreateColumns[stubEvent]() 69 return parser.NewParser(cols) 70 } 71 72 func (t *stubTracer) EventPrototype() any { 73 return &stubEvent{} 74 } 75 76 func (t *stubTracer) SetEventHandler(handler any) { 77 nh, ok := handler.(func(ev *stubEvent)) 78 if !ok { 79 panic("event handler invalid") 80 } 81 t.eventCallback = nh 82 } 83 84 func (t *stubTracer) Run(gadgetCtx gadgets.GadgetContext) error { 85 for _, ev := range testEvents { 86 t.eventCallback(ev) 87 } 88 89 ctx := gadgetCtx.Context() 90 91 // Tell the caller test that events were generated 92 if val := ctx.Value(valuekey); val != nil { 93 wg := val.(*sync.WaitGroup) 94 wg.Done() 95 } 96 97 gadgetcontext.WaitForTimeoutOrDone(gadgetCtx) 98 99 return nil 100 } 101 102 func (t *stubTracer) NewInstance() (gadgets.Gadget, error) { 103 // stubTracer is both the GadgetDesc and the Gadget implementation. We can keep those 104 // separated but there is not any reason to complicate this further. 105 return &stubTracer{}, nil 106 } 107 108 /*** stub snapshotter ***/ 109 type stubSnapshotter struct { 110 eventCallback func(ev []*stubEvent) 111 } 112 113 func (t *stubSnapshotter) Name() string { 114 return "stubsnapshotter" 115 } 116 117 func (t *stubSnapshotter) Description() string { 118 return "fake snapshotter gadget" 119 } 120 121 func (t *stubSnapshotter) Category() string { 122 return gadgets.CategorySnapshot 123 } 124 125 func (t *stubSnapshotter) Type() gadgets.GadgetType { 126 return gadgets.TypeOneShot 127 } 128 129 func (t *stubSnapshotter) ParamDescs() params.ParamDescs { 130 return nil 131 } 132 133 func (t *stubSnapshotter) Parser() parser.Parser { 134 cols := columns.MustCreateColumns[stubEvent]() 135 return parser.NewParser(cols) 136 } 137 138 func (t *stubSnapshotter) EventPrototype() any { 139 return &stubEvent{} 140 } 141 142 func (t *stubSnapshotter) SetEventHandlerArray(handler any) { 143 nh, ok := handler.(func(ev []*stubEvent)) 144 if !ok { 145 panic("event handler invalid") 146 } 147 t.eventCallback = nh 148 } 149 150 func (t *stubSnapshotter) Run(gadgetCtx gadgets.GadgetContext) error { 151 t.eventCallback(testEvents) 152 153 ctx := gadgetCtx.Context() 154 155 // Tell the caller test that events were generated 156 if val := ctx.Value(valuekey); val != nil { 157 wg := val.(*sync.WaitGroup) 158 wg.Done() 159 } 160 161 return nil 162 } 163 164 func (t *stubSnapshotter) NewInstance() (gadgets.Gadget, error) { 165 // stubSnapshotter is both the GadgetDesc and the Gadget implementation. We can keep those 166 // separated but there is not any reason to complicate this further. 167 return &stubSnapshotter{}, nil 168 } 169 170 func init() { 171 gadgetregistry.Register(&stubTracer{}) 172 gadgetregistry.Register(&stubSnapshotter{}) 173 }