health_test.go raw

   1  package bridge
   2  
   3  import (
   4  	"net"
   5  	"testing"
   6  	"time"
   7  
   8  	"git.smesh.lol/nostrino/pkg/release"
   9  )
  10  
  11  // TestHealthDistinguishesIdleFromBroken: a process that is up but has never announced, or whose
  12  // node has not answered, is not healthy — that is the state a dead gateway looks like from the
  13  // relay's side, and a health report has to say so.
  14  func TestHealthDistinguishesIdleFromBroken(t *testing.T) {
  15  	srv := &Server{
  16  		cfg: Config{
  17  			Version: "test", Chains: []string{"btc", "blake"},
  18  			Nodes: map[string]Node{
  19  				"btc":   {Addr: "127.0.0.1:1"},
  20  				"blake": {Addr: "127.0.0.1:2"},
  21  			},
  22  			Logf: func(string, ...any) {},
  23  		},
  24  		conns:  make(map[net.Conn]struct{}),
  25  		done:   make(chan struct{}),
  26  		pid:    42,
  27  		health: newHealthState(time.Now().Add(-90*time.Second), []string{"btc", "blake"}),
  28  	}
  29  	health := srv.Health()
  30  
  31  	if health.Healthy {
  32  		t.Fatal("a gateway that has never published presence reported healthy")
  33  	}
  34  
  35  	requireField(t, health, "btc")
  36  	requireField(t, health, "blake")
  37  
  38  	if health.PID != 42 || health.UptimeSeconds < 80 {
  39  		t.Fatalf("health %+v", health)
  40  	}
  41  }
  42  
  43  func requireField(t *testing.T, health Health, chain string) {
  44  	t.Helper()
  45  
  46  	if _, ok := health.Nodes[chain]; !ok {
  47  		t.Fatalf("health has no node report for %s", chain)
  48  	}
  49  }
  50  
  51  // TestHealthReportsWhetherItCanUpdateItself: the first question about a gateway that will not
  52  // update is whether it was started with the channel at all, and the second is whether it could
  53  // write beside its own binary. Both are reported rather than inferred from a log.
  54  func TestHealthReportsWhetherItCanUpdateItself(t *testing.T) {
  55  	srv := &Server{
  56  		cfg:    Config{Version: "test", Chains: []string{"btc"}, Logf: func(string, ...any) {}},
  57  		health: newHealthState(time.Now(), []string{"btc"}),
  58  	}
  59  	health := srv.Health()
  60  
  61  	if health.Exe == "" {
  62  		t.Fatal("health does not name the running binary")
  63  	}
  64  	if len(health.Admins) != 0 {
  65  		t.Fatalf("a gateway with no -admin flags lists administrators: %v", health.Admins)
  66  	}
  67  	if health.UpdateConfigured || health.AdminEnabled {
  68  		t.Fatalf("a gateway with no admin or update flags reports them enabled: %+v", health)
  69  	}
  70  
  71  	srv.cfg.Admin = true
  72  	srv.cfg.Admins = []string{"00"}
  73  	health = srv.Health()
  74  
  75  	if !health.AdminEnabled {
  76  		t.Fatal("admin channel is configured but health says it is not")
  77  	}
  78  	if len(health.Admins) != 1 || health.Admins[0] != "00" {
  79  		t.Fatalf("health does not list the configured administrator: %v", health.Admins)
  80  	}
  81  	if health.UpdateConfigured {
  82  		t.Fatal("update channel is not configured but health says it is")
  83  	}
  84  
  85  	srv.cfg.Update = release.Options{ManifestURL: "https://example/manifest.json", PublicKey: []byte{0}}
  86  	health = srv.Health()
  87  
  88  	if !health.UpdateConfigured || health.UpdateURL != "https://example/manifest.json" {
  89  		t.Fatalf("update channel is configured but health does not say so: %+v", health)
  90  	}
  91  }
  92