github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/monitor/internal/windows/processor_test.go (about) 1 // +build windows 2 3 package windowsmonitor 4 5 import ( 6 "context" 7 "errors" 8 "testing" 9 10 "github.com/golang/mock/gomock" 11 . "github.com/smartystreets/goconvey/convey" 12 "go.aporeto.io/enforcerd/trireme-lib/collector" 13 "go.aporeto.io/enforcerd/trireme-lib/common" 14 "go.aporeto.io/enforcerd/trireme-lib/monitor/config" 15 "go.aporeto.io/enforcerd/trireme-lib/monitor/extractors" 16 "go.aporeto.io/enforcerd/trireme-lib/policy" 17 "go.aporeto.io/enforcerd/trireme-lib/policy/mockpolicy" 18 ) 19 20 func testWindowsProcessor(puHandler policy.Resolver) *windowsProcessor { 21 w := New(context.Background()) 22 w.SetupHandlers(&config.ProcessorConfig{ 23 Collector: &collector.DefaultCollector{}, 24 Policy: puHandler, 25 }) 26 if err := w.SetupConfig(nil, &Config{ 27 EventMetadataExtractor: extractors.DefaultHostMetadataExtractor, 28 }); err != nil { 29 return nil 30 } 31 return w.proc 32 } 33 34 func TestCreate(t *testing.T) { 35 ctrl := gomock.NewController(t) 36 defer ctrl.Finish() 37 38 Convey("Given a valid processor", t, func() { 39 puHandler := mockpolicy.NewMockResolver(ctrl) 40 41 p := testWindowsProcessor(puHandler) 42 43 Convey("When I try a create event with invalid PU ID, ", func() { 44 event := &common.EventInfo{ 45 PUID: "/@#$@", 46 } 47 Convey("I should get an error", func() { 48 err := p.Create(context.Background(), event) 49 So(err, ShouldNotBeNil) 50 }) 51 }) 52 53 Convey("When I get a create event that is valid", func() { 54 event := &common.EventInfo{ 55 PUID: "1234", 56 } 57 Convey("I should get an error - create not supported", func() { 58 err := p.Create(context.Background(), event) 59 So(err, ShouldNotBeNil) 60 }) 61 }) 62 }) 63 } 64 65 func TestStop(t *testing.T) { 66 ctrl := gomock.NewController(t) 67 defer ctrl.Finish() 68 69 Convey("Given a valid processor", t, func() { 70 puHandler := mockpolicy.NewMockResolver(ctrl) 71 72 p := testWindowsProcessor(puHandler) 73 74 Convey("When I get a stop event that is valid", func() { 75 event := &common.EventInfo{ 76 PUID: "/trireme/1234", 77 } 78 79 puHandler.EXPECT().HandlePUEvent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 80 81 Convey("I should get the status of the upstream function", func() { 82 err := p.Stop(context.Background(), event) 83 So(err, ShouldBeNil) 84 }) 85 }) 86 87 }) 88 } 89 90 // TODO: remove nolint 91 // nolint 92 func TestDestroy(t *testing.T) { 93 ctrl := gomock.NewController(t) 94 defer ctrl.Finish() 95 96 Convey("Given a valid processor", t, func() { 97 puHandler := mockpolicy.NewMockResolver(ctrl) 98 99 p := testWindowsProcessor(puHandler) 100 101 Convey("When I get a destroy event that is valid", func() { 102 event := &common.EventInfo{ 103 PUID: "1234", 104 } 105 106 puHandler.EXPECT().HandlePUEvent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 107 Convey("I should get the status of the upstream function", func() { 108 err := p.Destroy(context.Background(), event) 109 So(err, ShouldBeNil) 110 }) 111 }) 112 113 Convey("When I get a destroy event that is valid for hostpu", func() { 114 event := &common.EventInfo{ 115 PUID: "123", 116 HostService: true, 117 NetworkOnlyTraffic: true, 118 } 119 120 puHandler.EXPECT().HandlePUEvent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 121 Convey("I should get the status of the upstream function", func() { 122 err := p.Destroy(context.Background(), event) 123 So(err, ShouldBeNil) 124 }) 125 }) 126 }) 127 } 128 129 func TestPause(t *testing.T) { 130 ctrl := gomock.NewController(t) 131 defer ctrl.Finish() 132 133 Convey("Given a valid processor", t, func() { 134 puHandler := mockpolicy.NewMockResolver(ctrl) 135 136 p := testWindowsProcessor(puHandler) 137 138 Convey("When I get a pause event that is valid", func() { 139 event := &common.EventInfo{ 140 PUID: "/trireme/1234", 141 } 142 143 Convey("I should get the status of the upstream function", func() { 144 err := p.Pause(context.Background(), event) 145 So(err, ShouldNotBeNil) // Pause does nothing on Windows 146 }) 147 }) 148 }) 149 } 150 151 func TestStart(t *testing.T) { 152 ctrl := gomock.NewController(t) 153 defer ctrl.Finish() 154 155 Convey("Given a valid processor", t, func() { 156 puHandler := mockpolicy.NewMockResolver(ctrl) 157 p := testWindowsProcessor(puHandler) 158 159 Convey("When I get a start event with no PUID", func() { 160 event := &common.EventInfo{ 161 PUID: "", 162 } 163 Convey("I should get an error", func() { 164 err := p.Start(context.Background(), event) 165 So(err, ShouldNotBeNil) 166 }) 167 }) 168 169 Convey("When I get a start event that is valid that fails on the generation of PU ID", func() { 170 event := &common.EventInfo{ 171 Name: "^^^", 172 } 173 Convey("I should get an error", func() { 174 err := p.Start(context.Background(), event) 175 So(err, ShouldNotBeNil) 176 }) 177 }) 178 179 Convey("When I get a start event that is valid that fails on the metadata extractor", func() { 180 event := &common.EventInfo{ 181 Name: "service", 182 Tags: []string{"badtag"}, 183 } 184 Convey("I should get an error", func() { 185 err := p.Start(context.Background(), event) 186 So(err, ShouldNotBeNil) 187 }) 188 }) 189 190 Convey("When I get a start event and the upstream returns an error ", func() { 191 event := &common.EventInfo{ 192 Name: "PU", 193 PID: 1, 194 PUID: "12345", 195 EventType: common.EventStart, 196 PUType: common.LinuxProcessPU, 197 } 198 Convey("I should get an error ", func() { 199 puHandler.EXPECT().HandlePUEvent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("error")) 200 err := p.Start(context.Background(), event) 201 So(err, ShouldNotBeNil) 202 }) 203 }) 204 205 Convey("When I get a start event and the runtime options don't have a mark value", func() { 206 event := &common.EventInfo{ 207 Name: "PU", 208 PID: 1, 209 PUID: "12345", 210 EventType: common.EventStop, 211 PUType: common.LinuxProcessPU, 212 } 213 214 Convey("I should not get an error ", func() { 215 puHandler.EXPECT().HandlePUEvent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(2).Return(nil) 216 err := p.Start(context.Background(), event) 217 So(err, ShouldBeNil) 218 }) 219 }) 220 }) 221 } 222 223 func TestResync(t *testing.T) { 224 ctrl := gomock.NewController(t) 225 defer ctrl.Finish() 226 227 Convey("Given a valid processor", t, func() { 228 puHandler := mockpolicy.NewMockResolver(ctrl) 229 p := testWindowsProcessor(puHandler) 230 231 Convey("When I get a resync event ", func() { 232 event := &common.EventInfo{ 233 Name: "PU", 234 PID: 1, 235 PUID: "12345", 236 EventType: common.EventStart, 237 PUType: common.LinuxProcessPU, 238 } 239 240 Convey("I should not get an error ", func() { 241 err := p.Resync(context.Background(), event) 242 So(err, ShouldBeNil) 243 }) 244 }) 245 246 Convey("When I get a resync event for hostservice", func() { 247 event := &common.EventInfo{ 248 Name: "PU", 249 PID: 1, 250 PUID: "12345", 251 EventType: common.EventStart, 252 HostService: true, 253 NetworkOnlyTraffic: true, 254 PUType: common.LinuxProcessPU, 255 } 256 257 Convey("I should not get an error ", func() { 258 puHandler.EXPECT().HandlePUEvent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 259 err := p.Resync(context.Background(), event) 260 So(err, ShouldBeNil) 261 }) 262 }) 263 }) 264 }