blob: 32bbc46620c2bee446bacfc134f15b0c6d7a5f77 [file] [log] [blame]
Serge Bazanski5839e972021-11-16 15:46:19 +01001package consensus
2
3import (
4 "context"
5 "testing"
6
Lorenz Brund13c1c62022-03-30 19:58:58 +02007 clientv3 "go.etcd.io/etcd/client/v3"
Serge Bazanski5839e972021-11-16 15:46:19 +01008
9 "source.monogon.dev/metropolis/pkg/event/memory"
10)
11
12type testServiceHandle struct {
13 s memory.Value
14}
15
16// TestServiceHandle builds a somewhat functioning ServiceHandle from a bare
17// etcd connection, effectively creating a fake Consensus service. This must
18// only be used in test code to perform dependency injection of a etcd client
19// into code which expects a Consensus service instance, eg. for testing the
20// Curator.
21//
22// The 'somewhat functioning' description above should serve as a hint to the
23// API stability and backwards/forwards compatibility of this function: there is
24// none.
25func TestServiceHandle(t *testing.T, cl *clientv3.Client) ServiceHandle {
26 ca := pkiCA()
27
28 tsh := testServiceHandle{}
29 st := &Status{
30 cl: cl,
31 ca: ca,
32 }
33 etcdPKI, err := st.pkiClient()
34 if err != nil {
35 t.Fatalf("failed to get PKI etcd client: %v", err)
36 }
37 ctx, ctxC := context.WithCancel(context.Background())
38 defer ctxC()
39 if _, err := ca.Ensure(ctx, etcdPKI); err != nil {
40 t.Fatalf("failed to ensure PKI CA: %v", err)
41 }
42 tsh.s.Set(st)
43 return &tsh
44}
45
46func (h *testServiceHandle) Watch() Watcher {
47 return Watcher{h.s.Watch()}
48}