How to use InAnyOrder method of gomock Package

Best Mock code snippet using gomock.InAnyOrder

controller_test.go

Source:controller_test.go Github

copy

Full Screen

...287 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {288 mockOVSBridgeClient.EXPECT().CreatePort(networkDeviceName, networkDeviceName, externalIDs)289 mockOVSBridgeClient.EXPECT().GetOFPort(networkDeviceName, false)290 mockOVSCtlClient.EXPECT().SetPortNoFlood(0)291 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, gomock.InAnyOrder([]uint32{pod1OFPort, pod3OFPort}), gomock.Any(), directionIngress, actionMirror)292 },293 },294 {295 name: "Add TrafficControl with non-existing target port (VXLAN)",296 tc: generateTrafficControl(tc1Name, nil, labels1, directionIngress, actionMirror, udpTunnel, true, nil),297 expectedCalls: func(mockOFClient *openflowtest.MockClient,298 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,299 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {300 extraOptions := map[string]interface{}{"key": strconv.Itoa(int(vni)), "dst_port": strconv.Itoa(int(destinationPort))}301 mockOVSBridgeClient.EXPECT().CreateTunnelPortExt(gomock.Any(), ovsconfig.TunnelType(ovsconfig.VXLANTunnel), int32(0), false, "", remoteIP, "", "", extraOptions, externalIDs)302 mockOVSBridgeClient.EXPECT().GetOFPort(gomock.Any(), false)303 mockOVSCtlClient.EXPECT().SetPortNoFlood(gomock.Any())304 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, gomock.InAnyOrder([]uint32{pod1OFPort, pod3OFPort}), gomock.Any(), directionIngress, actionMirror)305 },306 },307 {308 name: "Add TrafficControl with non-existing target port (GENEVE)",309 tc: generateTrafficControl(tc1Name, nil, labels1, directionIngress, actionMirror, udpTunnel, false, nil),310 expectedCalls: func(mockOFClient *openflowtest.MockClient,311 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,312 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {313 extraOptions := map[string]interface{}{"key": strconv.Itoa(int(vni)), "dst_port": strconv.Itoa(int(destinationPort))}314 mockOVSBridgeClient.EXPECT().CreateTunnelPortExt(gomock.Any(), ovsconfig.TunnelType(ovsconfig.GeneveTunnel), int32(0), false, "", remoteIP, "", "", extraOptions, externalIDs)315 mockOVSBridgeClient.EXPECT().GetOFPort(gomock.Any(), false)316 mockOVSCtlClient.EXPECT().SetPortNoFlood(gomock.Any())317 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, gomock.InAnyOrder([]uint32{pod1OFPort, pod3OFPort}), gomock.Any(), directionIngress, actionMirror)318 },319 },320 {321 name: "Add TrafficControl with non-existing target port (GRE)",322 tc: generateTrafficControl(tc1Name, nil, labels1, directionIngress, actionMirror, greTunnel, false, nil),323 expectedCalls: func(mockOFClient *openflowtest.MockClient,324 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,325 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {326 extraOptions := map[string]interface{}{"key": strconv.Itoa(int(greKey))}327 mockOVSBridgeClient.EXPECT().CreateTunnelPortExt(gomock.Any(), ovsconfig.TunnelType(ovsconfig.GRETunnel), int32(0), false, "", remoteIP, "", "", extraOptions, externalIDs)328 mockOVSBridgeClient.EXPECT().GetOFPort(gomock.Any(), false)329 mockOVSCtlClient.EXPECT().SetPortNoFlood(gomock.Any())330 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, gomock.InAnyOrder([]uint32{pod1OFPort, pod3OFPort}), gomock.Any(), directionIngress, actionMirror)331 },332 },333 {334 name: "Add TrafficControl with non-existing target port (ERSPAN)",335 tc: generateTrafficControl(tc1Name, nil, labels1, directionIngress, actionMirror, erspanTunnel, false, nil),336 expectedCalls: func(mockOFClient *openflowtest.MockClient,337 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,338 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {339 extraOptions := map[string]interface{}{"erspan_ver": "2", "erspan_dir": strconv.Itoa(int(erspanDir)), "erspan_hwid": strconv.Itoa(int(erspanHwID))}340 mockOVSBridgeClient.EXPECT().CreateTunnelPortExt(gomock.Any(), ovsconfig.TunnelType(ovsconfig.ERSPANTunnel), int32(0), false, "", remoteIP, "", "", extraOptions, externalIDs)341 mockOVSBridgeClient.EXPECT().GetOFPort(gomock.Any(), false)342 mockOVSCtlClient.EXPECT().SetPortNoFlood(gomock.Any())343 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, gomock.InAnyOrder([]uint32{pod1OFPort, pod3OFPort}), gomock.Any(), directionIngress, actionMirror)344 },345 },346 {347 name: "Add TrafficControl with existing target port and return port",348 tc: generateTrafficControl(tc1Name, nil, labels1, directionIngress, actionRedirect, targetPort2, false, returnPort2),349 extraInterfaces: []*interfacestore.InterfaceConfig{targetInterface2, returnInterface2},350 portToTCBindings: map[string]*portToTCBinding{351 targetPort2Name: {targetInterface2, sets.NewString(tc2Name)},352 returnPort2Name: {returnInterface2, sets.NewString(tc2Name)},353 },354 expectedCalls: func(mockOFClient *openflowtest.MockClient,355 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,356 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {357 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, gomock.InAnyOrder([]uint32{pod1OFPort, pod3OFPort}), targetPort2OFPort, directionIngress, actionRedirect)358 },359 },360 {361 name: "Add TrafficControl with only Pod selector",362 tc: generateTrafficControl(tc1Name, nil, labels1, directionIngress, actionMirror, targetPort1, false, nil),363 extraInterfaces: []*interfacestore.InterfaceConfig{targetInterface1},364 portToTCBindings: map[string]*portToTCBinding{365 targetPort1Name: {targetInterface1, sets.NewString(tc2Name)},366 },367 expectedCalls: func(mockOFClient *openflowtest.MockClient,368 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,369 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {370 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, gomock.InAnyOrder([]uint32{pod1OFPort, pod3OFPort}), targetPort1OFPort, directionIngress, actionMirror)371 },372 },373 {374 name: "Add TrafficControl with only Namespace selector",375 extraInterfaces: []*interfacestore.InterfaceConfig{targetInterface1},376 portToTCBindings: map[string]*portToTCBinding{377 targetPort1Name: {targetInterface1, sets.NewString(tc2Name)},378 },379 tc: generateTrafficControl(tc1Name, labels1, nil, directionIngress, actionMirror, targetPort1, false, nil),380 expectedCalls: func(mockOFClient *openflowtest.MockClient,381 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,382 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {383 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, gomock.InAnyOrder([]uint32{pod1OFPort, pod2OFPort}), targetPort1OFPort, directionIngress, actionMirror)384 },385 },386 {387 name: "Add TrafficControl with Pod selector and Namespace selector",388 extraInterfaces: []*interfacestore.InterfaceConfig{targetInterface1},389 portToTCBindings: map[string]*portToTCBinding{390 targetPort1Name: {targetInterface1, sets.NewString(tc2Name)},391 },392 tc: generateTrafficControl(tc1Name, labels1, labels2, directionIngress, actionRedirect, targetPort1, false, nil),393 expectedCalls: func(mockOFClient *openflowtest.MockClient,394 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,395 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {396 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, []uint32{pod2OFPort}, targetPort1OFPort, directionIngress, actionRedirect)397 },398 },399 {400 name: "Add TrafficControl with nil Pod selector and nil Namespace selector",401 extraInterfaces: []*interfacestore.InterfaceConfig{targetInterface1},402 portToTCBindings: map[string]*portToTCBinding{403 targetPort1Name: {targetInterface1, sets.NewString(tc2Name)},404 },405 tc: generateTrafficControl(tc1Name, nil, nil, directionIngress, actionRedirect, targetPort1, false, nil),406 expectedCalls: func(mockOFClient *openflowtest.MockClient,407 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,408 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {409 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, nil, targetPort1OFPort, directionIngress, actionRedirect)410 },411 },412 {413 name: "Add TrafficControl with empty Pod selector and empty Namespace selector",414 extraInterfaces: []*interfacestore.InterfaceConfig{targetInterface1},415 portToTCBindings: map[string]*portToTCBinding{416 targetPort1Name: {targetInterface1, sets.NewString(tc2Name)},417 },418 tc: generateTrafficControl(tc1Name, map[string]string{}, map[string]string{}, directionIngress, actionRedirect, targetPort1, false, nil),419 expectedCalls: func(mockOFClient *openflowtest.MockClient,420 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,421 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {422 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, []uint32{pod1OFPort, pod2OFPort, pod3OFPort, pod4OFPort}, targetPort1OFPort, directionIngress, actionRedirect)423 },424 },425 }426 for _, tt := range testcases {427 t.Run(tt.name, func(t *testing.T) {428 c := newFakeController(t, []runtime.Object{ns1, ns2, pod1, pod2, pod3, pod4}, []runtime.Object{tt.tc}, append(interfaces, tt.extraInterfaces...))429 defer c.mockController.Finish()430 if tt.portToTCBindings != nil {431 c.portToTCBindings = tt.portToTCBindings432 }433 stopCh := make(chan struct{})434 defer close(stopCh)435 c.informerFactory.Start(stopCh)436 c.informerFactory.WaitForCacheSync(stopCh)437 go c.localPodInformer.Run(stopCh)438 c.crdInformerFactory.Start(stopCh)439 c.crdInformerFactory.WaitForCacheSync(stopCh)440 tt.expectedCalls(c.mockOFClient, c.mockOVSBridgeClient, c.mockOVSCtlClient)441 assert.NoError(t, c.syncTrafficControl(tt.tc.Name))442 })443 }444}445func TestTrafficControlUpdate(t *testing.T) {446 tc1 := generateTrafficControl(tc1Name, nil, labels1, directionIngress, actionMirror, targetPort1, false, nil)447 interfaces := []*interfacestore.InterfaceConfig{448 podInterface1,449 podInterface2,450 podInterface3,451 podInterface4,452 targetInterface1,453 }454 testcases := []struct {455 name string456 updatedTrafficControl *v1alpha2.TrafficControl457 expectedState *trafficControlState458 expectedCalls func(mockOFClient *openflowtest.MockClient,459 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,460 MockOVSCtlClient *ovsctltest.MockOVSCtlClient)461 }{462 {463 name: "Update TrafficControl target port (NetworkDevice)",464 updatedTrafficControl: generateTrafficControl(tc1Name, nil, labels1, directionIngress, actionMirror, targetPort2, false, nil),465 expectedState: generateTrafficControlState(directionIngress, actionMirror, targetPort2Name, 0, "", sets.NewInt32(int32(pod1OFPort), int32(pod3OFPort)), sets.NewString(pod1NN, pod3NN)),466 expectedCalls: func(mockOFClient *openflowtest.MockClient,467 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,468 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {469 mockOVSBridgeClient.EXPECT().DeletePort(gomock.Any())470 mockOVSBridgeClient.EXPECT().CreatePort(targetPort2Name, targetPort2Name, externalIDs)471 mockOVSBridgeClient.EXPECT().GetOFPort(targetPort2Name, false)472 mockOVSCtlClient.EXPECT().SetPortNoFlood(gomock.Any())473 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, gomock.InAnyOrder([]uint32{pod1OFPort, pod3OFPort}), gomock.Any(), directionIngress, actionMirror)474 },475 },476 {477 name: "Update TrafficControl action",478 updatedTrafficControl: generateTrafficControl(tc1Name, nil, labels1, directionIngress, actionRedirect, targetPort1, false, returnPort1),479 expectedState: generateTrafficControlState(directionIngress, actionRedirect, targetPort1Name, targetPort1OFPort, returnPort1Name, sets.NewInt32(int32(pod1OFPort), int32(pod3OFPort)), sets.NewString(pod1NN, pod3NN)),480 expectedCalls: func(mockOFClient *openflowtest.MockClient,481 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,482 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {483 mockOVSBridgeClient.EXPECT().CreatePort(returnPort1Name, returnPort1Name, externalIDs)484 mockOVSBridgeClient.EXPECT().GetOFPort(returnPort1Name, false)485 mockOVSCtlClient.EXPECT().SetPortNoFlood(gomock.Any())486 mockOFClient.EXPECT().InstallTrafficControlReturnPortFlow(gomock.Any())487 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, gomock.InAnyOrder([]uint32{pod1OFPort, pod3OFPort}), targetPort1OFPort, directionIngress, actionRedirect)488 },489 },490 {491 name: "Update TrafficControl direction",492 updatedTrafficControl: generateTrafficControl(tc1Name, nil, labels1, directionEgress, actionMirror, targetPort1, false, nil),493 expectedState: generateTrafficControlState(directionEgress, actionMirror, targetPort1Name, targetPort1OFPort, "", sets.NewInt32(int32(pod1OFPort), int32(pod3OFPort)), sets.NewString(pod1NN, pod3NN)),494 expectedCalls: func(mockOFClient *openflowtest.MockClient,495 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,496 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {497 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, gomock.InAnyOrder([]uint32{pod1OFPort, pod3OFPort}), targetPort1OFPort, directionEgress, actionMirror)498 },499 },500 {501 name: "Update TrafficControl Pod selector",502 updatedTrafficControl: generateTrafficControl(tc1Name, nil, labels2, directionIngress, actionMirror, targetPort1, false, nil),503 expectedState: generateTrafficControlState(directionIngress, actionMirror, targetPort1Name, targetPort1OFPort, "", sets.NewInt32(int32(pod2OFPort), int32(pod4OFPort)), sets.NewString(pod2NN, pod4NN)),504 expectedCalls: func(mockOFClient *openflowtest.MockClient,505 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,506 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {507 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, gomock.InAnyOrder([]uint32{pod2OFPort, pod4OFPort}), targetPort1OFPort, directionIngress, actionMirror)508 },509 },510 {511 name: "Update TrafficControl Namespace selector",512 updatedTrafficControl: generateTrafficControl(tc1Name, labels2, labels1, directionIngress, actionMirror, targetPort1, false, nil),513 expectedState: generateTrafficControlState(directionIngress, actionMirror, targetPort1Name, targetPort1OFPort, "", sets.NewInt32(int32(pod3OFPort)), sets.NewString(pod3NN)),514 expectedCalls: func(mockOFClient *openflowtest.MockClient,515 mockOVSBridgeClient *ovsconfigtest.MockOVSBridgeClient,516 mockOVSCtlClient *ovsctltest.MockOVSCtlClient) {517 mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, []uint32{pod3OFPort}, targetPort1OFPort, directionIngress, actionMirror)518 },519 },520 }521 for _, tt := range testcases {522 t.Run(tt.name, func(t *testing.T) {523 c := newFakeController(t, []runtime.Object{ns1, ns2, pod1, pod2, pod3, pod4}, []runtime.Object{tc1}, interfaces)524 defer c.mockController.Finish()525 stopCh := make(chan struct{})526 defer close(stopCh)527 c.informerFactory.Start(stopCh)528 c.informerFactory.WaitForCacheSync(stopCh)529 go c.localPodInformer.Run(stopCh)530 c.crdInformerFactory.Start(stopCh)531 c.crdInformerFactory.WaitForCacheSync(stopCh)532 // Fake the status after TrafficControl tc1 is added.533 c.portToTCBindings = map[string]*portToTCBinding{534 targetPort1Name: {targetInterface1, sets.NewString(tc1Name)},535 }536 c.tcStates = map[string]*trafficControlState{537 tc1Name: {538 targetPortName: targetPort1Name,539 targetOFPort: targetPort1OFPort,540 action: actionMirror,541 direction: directionIngress,542 ofPorts: sets.NewInt32(int32(pod1OFPort), int32(pod3OFPort)),543 pods: sets.NewString(pod1NN, pod3NN),544 },545 }546 c.podToTCBindings = map[string]*podToTCBinding{547 pod1NN: {effectiveTC: tc1Name, alternativeTCs: sets.NewString()},548 pod3NN: {effectiveTC: tc1Name, alternativeTCs: sets.NewString()},549 }550 // Ignore the TrafficControl ADD events for TrafficControl tc1.551 waitEvents(t, 1, c)552 item, _ := c.queue.Get()553 c.queue.Done(item)554 _, err := c.crdClient.CrdV1alpha2().TrafficControls().Update(context.TODO(), tt.updatedTrafficControl, metav1.UpdateOptions{})555 require.NoError(t, err)556 // Functions are expected to be called after updating TrafficControl tc1.557 tt.expectedCalls(c.mockOFClient, c.mockOVSBridgeClient, c.mockOVSCtlClient)558 time.Sleep(time.Second)559 require.NoError(t, c.syncTrafficControl(tc1Name))560 require.Equal(t, tt.expectedState, c.tcStates[tc1Name])561 })562 }563}564func TestSharedTargetPort(t *testing.T) {565 tc1 := generateTrafficControl(tc1Name, nil, labels1, directionIngress, actionMirror, targetPort1, false, nil)566 tc2 := generateTrafficControl(tc2Name, nil, labels2, directionIngress, actionMirror, targetPort1, false, nil)567 interfaces := []*interfacestore.InterfaceConfig{568 podInterface1,569 podInterface2,570 podInterface3,571 podInterface4,572 }573 c := newFakeController(t, []runtime.Object{pod1, pod2, pod3, pod4}, []runtime.Object{tc1, tc2}, interfaces)574 defer c.mockController.Finish()575 stopCh := make(chan struct{})576 defer close(stopCh)577 c.informerFactory.Start(stopCh)578 c.informerFactory.WaitForCacheSync(stopCh)579 go c.localPodInformer.Run(stopCh)580 c.crdInformerFactory.Start(stopCh)581 c.crdInformerFactory.WaitForCacheSync(stopCh)582 // Target port is expected to be crated if it doesn't exist.583 c.mockOVSBridgeClient.EXPECT().CreatePort(targetPort1Name, targetPort1Name, externalIDs)584 c.mockOVSBridgeClient.EXPECT().GetOFPort(targetPort1Name, false).Times(1)585 c.mockOVSCtlClient.EXPECT().SetPortNoFlood(gomock.Any())586 // Mark flows for TrafficControl tc1 and tc2 are expected to be installed.587 c.mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc1Name, gomock.InAnyOrder([]uint32{pod1OFPort, pod3OFPort}), gomock.Any(), directionIngress, actionMirror)588 c.mockOFClient.EXPECT().InstallTrafficControlMarkFlows(tc2Name, gomock.InAnyOrder([]uint32{pod2OFPort, pod4OFPort}), gomock.Any(), directionIngress, actionMirror)589 // Process the TrafficControl ADD events for TrafficControl tc1 and tc2.590 waitEvents(t, 2, c)591 for i := 0; i < 2; i++ {592 item, _ := c.queue.Get()593 require.NoError(t, c.syncTrafficControl(item.(string)))594 c.queue.Done(item)595 }596 // If TrafficControl tc1 is deleted, then TrafficControl tc2 is deleted, the created target port is expected to be597 // deleted after delete all TrafficControls using the target port.598 s1 := c.mockOFClient.EXPECT().UninstallTrafficControlMarkFlows(tc1Name)599 s2 := c.mockOFClient.EXPECT().UninstallTrafficControlMarkFlows(tc2Name)600 s3 := c.mockOVSBridgeClient.EXPECT().DeletePort(gomock.Any())601 gomock.InOrder(s1, s2, s3)602 // Delete TrafficControl tc1....

Full Screen

Full Screen

scan_test.go

Source:scan_test.go Github

copy

Full Screen

...119 return ref120 }()121 s.repo.EXPECT().GetLastImageScan(gomock.Any(), image).Return(&db.ImageScanDto{ScanTime: time.Now().Add(-1 * time.Hour)}, nil).Times(1)122 s.scanner.EXPECT().Scan(gomock.Any(), image).Return(scanResult, nil).Times(1)123 s.repo.EXPECT().RegisterImageScan(gomock.Any(), image, scanResult.Docker.BaseImage, skewTimeNowMatcher{skewFrom: -5 * time.Second, skewTo: 5 * time.Second}, true, gomock.InAnyOrder(expectedVulnerabilities), gomock.InAnyOrder(expectedIdentifiers), gomock.InAnyOrder(expectedReferences)).Return(nil).Times(1)124 sut := NewVulnerabilityScanner(s.scanner, s.repo, WithRescanAge(time.Minute))125 err := sut.Handle(context.TODO(), image)126 s.Require().NoError(err)127}128func (s *scanTestSuite) Test_FailedScanStoresScanFailed() {129 image := "image:latest"130 s.repo.EXPECT().GetLastImageScan(gomock.Any(), image).Return(&db.ImageScanDto{ScanTime: time.Now().Add(-1 * time.Hour)}, nil).Times(1)131 s.scanner.EXPECT().Scan(gomock.Any(), gomock.Any()).Return(nil, errors.New("any error")).Times(1)132 s.repo.EXPECT().RegisterImageScan(gomock.Any(), image, gomock.Any(), skewTimeNowMatcher{skewFrom: -5 * time.Second, skewTo: 5 * time.Second}, false, []db.VulnerabilityBulkDto{}, []db.VulnerabilityIdentifierBulkDto{}, []db.VulnerabilityReferenceBulkDto{}).Return(nil).Times(1)133 sut := NewVulnerabilityScanner(s.scanner, s.repo, WithRescanAge(time.Minute))134 err := sut.Handle(context.TODO(), image)135 s.Require().NoError(err)136}137func (s *scanTestSuite) Test_FailingToStoreScanResultsReturnsError() {...

Full Screen

Full Screen

retranslator_test.go

Source:retranslator_test.go Github

copy

Full Screen

...73 repo.EXPECT().Remove(gomock.Any()).Times(5).DoAndReturn(func(eventIDs []uint64) error {74 removedIDs = append(removedIDs, eventIDs...)75 return nil76 })77 if !gomock.InAnyOrder(sentIDs).Matches(removedIDs) {78 t.Errorf("Sent IDs aren't match with removed IDs")79 }80 ctx := context.Background()81 cfg := Config{82 ChannelSize: 512,83 ConsumerCount: 2,84 ConsumeSize: 5,85 ConsumeTimeout: 1 * time.Second,86 ProducerCount: 2,87 WorkerCount: 2,88 Repo: repo,89 Sender: sender,90 }91 retranslator := NewRetranslator(ctx, cfg)92 retranslator.Start()93 time.Sleep(time.Second * 2)94 retranslator.Close()95}96func TestLockUnlock(t *testing.T) {97 ctrl := gomock.NewController(t)98 repo := mocks.NewMockEventRepo(ctrl)99 sender := mocks.NewMockEventSender(ctrl)100 events := getEvents()101 lockEventsCall := repo.EXPECT().Lock(uint64(5)).Return(events, nil).Times(1)102 repo.EXPECT().Lock(gomock.Any()).Return(nil, errors.New("no new event available")).AnyTimes().After(lockEventsCall)103 sender.EXPECT().Send(gomock.Any()).Times(5).DoAndReturn(func(certificate *model.CertificateEvent) error {104 return errors.New("test error")105 })106 repo.EXPECT().Remove(gomock.Any()).Times(0)107 repo.EXPECT().Unlock(gomock.Any()).Times(5)108 ctx := context.Background()109 cfg := Config{110 ChannelSize: 512,111 ConsumerCount: 2,112 ConsumeSize: 5,113 ConsumeTimeout: 1 * time.Second,114 ProducerCount: 2,115 WorkerCount: 2,116 Repo: repo,117 Sender: sender,118 }119 retranslator := NewRetranslator(ctx, cfg)120 retranslator.Start()121 time.Sleep(time.Second * 2)122 retranslator.Close()123}124func TestLockUnlockMatchIDs(t *testing.T) {125 ctrl := gomock.NewController(t)126 repo := mocks.NewMockEventRepo(ctrl)127 sender := mocks.NewMockEventSender(ctrl)128 events := getEvents()129 var sentIDs, unlockedIDs []uint64130 lockEventsCall := repo.EXPECT().Lock(uint64(5)).Return(events, nil).Times(1)131 repo.EXPECT().Lock(gomock.Any()).Return(nil, errors.New("no new event available")).AnyTimes().After(lockEventsCall)132 sender.EXPECT().Send(gomock.Any()).Times(5).DoAndReturn(func(certificate *model.CertificateEvent) error {133 sentIDs = append(sentIDs, certificate.ID)134 return errors.New("test error")135 })136 repo.EXPECT().Unlock(gomock.Any()).Times(5).DoAndReturn(func(eventIDs []uint64) error {137 unlockedIDs = append(unlockedIDs, eventIDs...)138 return nil139 })140 repo.EXPECT().Remove(gomock.Any()).Times(0)141 if !gomock.InAnyOrder(sentIDs).Matches(unlockedIDs) {142 t.Errorf("Sent IDs aren't match with unlocked IDs")143 }144 ctx := context.Background()145 cfg := Config{146 ChannelSize: 512,147 ConsumerCount: 2,148 ConsumeSize: 5,149 ConsumeTimeout: 1 * time.Second,150 ProducerCount: 2,151 WorkerCount: 2,152 Repo: repo,153 Sender: sender,154 }155 retranslator := NewRetranslator(ctx, cfg)...

Full Screen

Full Screen

InAnyOrder

Using AI Code Generation

copy

Full Screen

1import (2func TestInAnyOrder(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mock := NewMockInterface(ctrl)6 mock.EXPECT().InAnyOrder(gomock.InAnyOrder("a", "b", "c")).Return(1, nil)7 mock.EXPECT().InAnyOrder(gomock.InAnyOrder("a", "b", "c")).Return(2, nil)8 mock.EXPECT().InAnyOrder(gomock.InAnyOrder("a", "b", "c")).Return(3, nil)9 mock.EXPECT().InAnyOrder(gomock.InAnyOrder("a", "b", "c")).Return(4, nil)10 mock.EXPECT().InAnyOrder(gomock.InAnyOrder("a", "b", "c")).Return(5, nil)11 mock.EXPECT().InAnyOrder(gomock.InAnyOrder("a", "b", "c")).Return(6, nil)12 assert.Equal(t, 1, mock.InAnyOrder("d", "a", "b", "c"))13}14--- FAIL: TestInAnyOrder (0.00s)15testing.tRunner.func1(0xc0000a8000)16panic(0x4c0e00, 0xc0000d8000)17github.com/golang/mock/gomock.(*Call).Return(0x0, 0xc0000d8000, 0x0, 0x0, 0x0, 0x0)

Full Screen

Full Screen

InAnyOrder

Using AI Code Generation

copy

Full Screen

1import (2func TestInAnyOrder(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 m := NewMockInterface(ctrl)6 m.EXPECT().InAnyOrder(gomock.InAnyOrder(1, 2, 3)).Return(true)7 fmt.Println(m.InAnyOrder(2, 1, 3))8}9import (10func TestInAnyOrder(t *testing.T) {11 ctrl := gomock.NewController(t)12 defer ctrl.Finish()13 m := NewMockInterface(ctrl)14 m.EXPECT().InAnyOrder(gomock.InAnyOrder(1, 2, 3)).Return(true)15 fmt.Println(m.InAnyOrder(2, 1, 3))16}17import (18func TestInAnyOrder(t *testing.T) {19 ctrl := gomock.NewController(t)20 defer ctrl.Finish()21 m := NewMockInterface(ctrl)22 m.EXPECT().InAnyOrder(gomock.InAnyOrder(1, 2, 3)).Return(true)23 fmt.Println(m.InAnyOrder(2, 1, 3))24}25import (26func TestInAnyOrder(t *testing.T) {27 ctrl := gomock.NewController(t)28 defer ctrl.Finish()29 m := NewMockInterface(ctrl)30 m.EXPECT().InAnyOrder(gomock.InAnyOrder(1, 2, 3)).Return(true)31 fmt.Println(m.InAnyOrder(2, 1, 3))32}33import (

Full Screen

Full Screen

InAnyOrder

Using AI Code Generation

copy

Full Screen

1import (2type MockFoo struct {3}4func (m *MockFoo) Bar(i int) string {5 ret := m.Called(i)6 return ret.String(0)7}8func TestFoo(t *testing.T) {9 ctrl := gomock.NewController(t)10 defer ctrl.Finish()11 mockFoo := NewMockFoo(ctrl)12 mockFoo.EXPECT().Bar(gomock.InAnyOrder(1, 2, 3)).Return("foo").AnyTimes()13 assert.Equal(t, "foo", mockFoo.Bar(1))14 assert.Equal(t, "foo", mockFoo.Bar(2))15 assert.Equal(t, "foo", mockFoo.Bar(3))16}17import (18type MockFoo struct {19}20func (m *MockFoo) Bar(i int) string {21 ret := m.Called(i)22 return ret.String(0)23}24func TestFoo(t *testing.T) {25 ctrl := gomock.NewController(t)26 defer ctrl.Finish()27 mockFoo := NewMockFoo(ctrl)28 mockFoo.EXPECT().Bar(mock.InAnyOrder(1, 2, 3)).Return("foo").AnyTimes()29 assert.Equal(t, "foo", mockFoo.Bar(1))30 assert.Equal(t, "foo", mockFoo.Bar(2))31 assert.Equal(t, "foo", mockFoo.Bar(3))32}33import (

Full Screen

Full Screen

InAnyOrder

Using AI Code Generation

copy

Full Screen

1import (2func TestInAnyOrder(t *testing.T) {3 mockCtrl := gomock.NewController(t)4 defer mockCtrl.Finish()5 mock := NewMockInterface(mockCtrl)6 s := []int{1, 2, 3}7 mock.EXPECT().InterfaceFunc(s).InAnyOrder()8 fmt.Println("Calling the function")9 mock.InterfaceFunc(s)10}11import (12type Interface interface {13 InterfaceFunc([]int)14}15type MockInterface struct {16}17type MockInterfaceMockRecorder struct {18}19func NewMockInterface(ctrl *gomock.Controller) *MockInterface {20 mock := &MockInterface{ctrl: ctrl}21 mock.recorder = &MockInterfaceMockRecorder{mock}22}23func (m *MockInterface) EXPECT() *MockInterfaceMockRecorder {24}25func (m *MockInterface) InterfaceFunc(arg0 []int) {26 m.ctrl.T.Helper()27 m.ctrl.Call(m, "InterfaceFunc", arg0)28}29func (mr *MockInterfaceMockRecorder) InterfaceFunc(arg0 interface{}) *gomock.Call {30 mr.mock.ctrl.T.Helper()31 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InterfaceFunc", reflect.TypeOf((*MockInterface)(nil).InterfaceFunc), arg0)32}33func main() {34 fmt.Println("Hello World!")35}

Full Screen

Full Screen

InAnyOrder

Using AI Code Generation

copy

Full Screen

1import (2func TestInAnyOrder(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mock := NewMockFoo(ctrl)6 mock.EXPECT().Bar(1).Return("one").Times(1)7 mock.EXPECT().Bar(2).Return("two").Times(1)8 mock.EXPECT().Bar(3).Return("three").Times(1)9 fmt.Println(mock.Bar(1))10 fmt.Println(mock.Bar(2))11 fmt.Println(mock.Bar(3))12 assert.Equal(t, mock.Bar(1), "one")13 assert.Equal(t, mock.Bar(2), "two")14 assert.Equal(t, mock.Bar(3), "three")15}16import (17func TestInOrder(t *testing.T) {18 ctrl := gomock.NewController(t)19 defer ctrl.Finish()20 mock := NewMockFoo(ctrl)21 mock.EXPECT().Bar(1).Return("one").Times(1)22 mock.EXPECT().Bar(2).Return("two").Times(1)23 mock.EXPECT().Bar(3).Return("three").Times(1)24 fmt.Println(mock.Bar(1))25 fmt.Println(mock.Bar(2))26 fmt.Println(mock.Bar(3))27 assert.Equal(t, mock.Bar(1), "one")28 assert.Equal(t, mock.Bar(2), "two")29 assert.Equal(t, mock.Bar(3), "three")30}31import (32func TestDoAndReturn(t *testing.T) {33 ctrl := gomock.NewController(t)34 defer ctrl.Finish()35 mock := NewMockFoo(ctrl)36 mock.EXPECT().Bar(1).DoAndReturn(func(i int) string {

Full Screen

Full Screen

InAnyOrder

Using AI Code Generation

copy

Full Screen

1import (2type MockInterface interface {3 DoSomething(ctx context.Context, input string) (string, error)4}5type Mock struct {6}7func (m *Mock) DoSomething(ctx context.Context, input string) (string, error) {8 args := m.Called(ctx, input)9 return args.String(0), args.Error(1)10}11func TestMock(t *testing.T) {12 ctrl := gomock.NewController(t)13 defer ctrl.Finish()14 m := NewMockInterface(ctrl)15 m.EXPECT().DoSomething(gomock.Any(), gomock.Any()).Return("Hello", nil).AnyTimes()16 result, err := m.DoSomething(context.Background(), "World")17 assert.NoError(t, err)18 assert.Equal(t, "Hello", result)19}20import (21type MockInterface interface {22 DoSomething(ctx context.Context, input string) (string, error)23}24type Mock struct {25}26func (m *Mock) DoSomething(ctx context.Context, input string) (string, error) {27 args := m.Called(ctx, input)28 return args.String(0), args.Error(1)29}30func TestMock(t *testing.T) {31 ctrl := gomock.NewController(t)32 defer ctrl.Finish()33 m := NewMockInterface(ctrl)34 m.EXPECT().DoSomething(gomock.Any(), gomock.Any()).Return("Hello", nil).Times(3)35 result, err := m.DoSomething(context.Background(), "World")36 assert.NoError(t, err)37 assert.Equal(t, "Hello", result)38}

Full Screen

Full Screen

InAnyOrder

Using AI Code Generation

copy

Full Screen

1import (2type MyStruct struct {3}4func (m *MyStruct) MyFunc() {5 fmt.Println("MyFunc called")6}7func TestMyFunc(t *testing.T) {8 ctrl := gomock.NewController(t)9 defer ctrl.Finish()10 myStruct := NewMockMyStruct(ctrl)11 myStruct.EXPECT().MyFunc().InAnyOrder()12 myStruct.MyFunc()13}14import (15type MyStruct struct {16}17func (m *MyStruct) MyFunc() {18 fmt.Println("MyFunc called")19}20func TestMyFunc(t *testing.T) {21 ctrl := gomock.NewController(t)22 defer ctrl.Finish()23 myStruct := NewMockMyStruct(ctrl)24 myStruct.EXPECT().MyFunc().InOrder()25 myStruct.MyFunc()26}27import (28type MyStruct struct {29}30func (m *MyStruct) MyFunc() {31 fmt.Println("MyFunc called")32}33func TestMyFunc(t *testing.T) {34 ctrl := gomock.NewController(t)35 defer ctrl.Finish()36 myStruct := NewMockMyStruct(ctrl)37 myStruct.EXPECT().MyFunc().After(time.Second)38 myStruct.MyFunc()39}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful