inbound_node_sweep_log_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package service
  2. import (
  3. "strconv"
  4. "strings"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  9. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  10. "github.com/op/go-logging"
  11. )
  12. // Removing a central inbound also drops its clients' traffic history, so the
  13. // sweep must name what it removed — a silent drop is undiagnosable in the field.
  14. func TestNodeSnapshotSweepLogsRemovedInbound(t *testing.T) {
  15. logger.InitLogger(logging.WARNING)
  16. setupBulkDB(t)
  17. nodeID, _ := setupNodeRuntime(t)
  18. gone := nodeInbound(t, nodeID, 31777, nil)
  19. survivor := nodeInbound(t, nodeID, 31778, nil)
  20. // The snapshot reports only the survivor, so the other row is swept.
  21. snap := &runtime.TrafficSnapshot{Inbounds: []*model.Inbound{{
  22. Tag: survivor.Tag, Port: survivor.Port, Protocol: model.VLESS, Enable: true,
  23. Settings: survivor.Settings,
  24. }}}
  25. if _, err := (&InboundService{}).setRemoteTrafficLocked(nodeID, snap, false); err != nil {
  26. t.Fatalf("setRemoteTrafficLocked: %v", err)
  27. }
  28. var remaining int64
  29. if err := database.GetDB().Model(model.Inbound{}).Where("id = ?", gone.Id).Count(&remaining).Error; err != nil {
  30. t.Fatalf("count swept inbound: %v", err)
  31. }
  32. if remaining != 0 {
  33. t.Fatalf("fixture did not sweep inbound %d; the log assertion below would be meaningless", gone.Id)
  34. }
  35. want := strconv.Itoa(gone.Id)
  36. for _, line := range logger.GetLogs(200, "warning") {
  37. if strings.Contains(line, gone.Tag) && strings.Contains(line, want) {
  38. return
  39. }
  40. }
  41. t.Fatalf("sweep removed inbound %q (id %d) without naming it in the log", gone.Tag, gone.Id)
  42. }