blob: f69f73ecf6a9e4e62d88c83fec0f9dc0667f1e8b [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{
Mateusz Zalegabb2edbe2022-06-08 11:57:09 +020030 cl: cl,
31 ca: ca,
32 noClusterMemberManagement: true,
Serge Bazanski5839e972021-11-16 15:46:19 +010033 }
34 etcdPKI, err := st.pkiClient()
35 if err != nil {
36 t.Fatalf("failed to get PKI etcd client: %v", err)
37 }
38 ctx, ctxC := context.WithCancel(context.Background())
39 defer ctxC()
40 if _, err := ca.Ensure(ctx, etcdPKI); err != nil {
41 t.Fatalf("failed to ensure PKI CA: %v", err)
42 }
43 tsh.s.Set(st)
44 return &tsh
45}
46
47func (h *testServiceHandle) Watch() Watcher {
48 return Watcher{h.s.Watch()}
49}