manager_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package mtproto
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/database/model"
  5. )
  6. func TestParseMetricLine(t *testing.T) {
  7. name, labels, val, err := parseMetricLine(`mtg_traffic{direction="to_client"} 12345`)
  8. if err != nil {
  9. t.Fatal(err)
  10. }
  11. if name != "mtg_traffic" {
  12. t.Fatalf("name=%q", name)
  13. }
  14. if labels["direction"] != "to_client" {
  15. t.Fatalf("labels=%v", labels)
  16. }
  17. if val != 12345 {
  18. t.Fatalf("val=%v", val)
  19. }
  20. name2, _, val2, err2 := parseMetricLine(`mtg_concurrency 7`)
  21. if err2 != nil {
  22. t.Fatal(err2)
  23. }
  24. if name2 != "mtg_concurrency" || val2 != 7 {
  25. t.Fatalf("got %q %v", name2, val2)
  26. }
  27. }
  28. func TestInstanceFromInbound(t *testing.T) {
  29. ib := &model.Inbound{
  30. Id: 3,
  31. Tag: "inbound-3",
  32. Listen: "0.0.0.0",
  33. Port: 8443,
  34. Protocol: model.MTProto,
  35. Settings: `{"fakeTlsDomain":"example.com","secret":""}`,
  36. }
  37. inst, ok := InstanceFromInbound(ib)
  38. if !ok {
  39. t.Fatal("expected a usable instance")
  40. }
  41. if inst.Secret == "" {
  42. t.Fatal("secret should be healed to a non-empty value")
  43. }
  44. if inst.Port != 8443 || inst.Id != 3 {
  45. t.Fatalf("bad instance %+v", inst)
  46. }
  47. if _, ok := InstanceFromInbound(&model.Inbound{Protocol: model.VLESS}); ok {
  48. t.Fatal("non-mtproto inbound should not produce an instance")
  49. }
  50. }