Serge Bazanski | 5839e97 | 2021-11-16 15:46:19 +0100 | [diff] [blame] | 1 | package consensus |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 7 | clientv3 "go.etcd.io/etcd/client/v3" |
Serge Bazanski | 5839e97 | 2021-11-16 15:46:19 +0100 | [diff] [blame] | 8 | |
| 9 | "source.monogon.dev/metropolis/pkg/event/memory" |
| 10 | ) |
| 11 | |
| 12 | type 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. |
| 25 | func TestServiceHandle(t *testing.T, cl *clientv3.Client) ServiceHandle { |
| 26 | ca := pkiCA() |
| 27 | |
| 28 | tsh := testServiceHandle{} |
| 29 | st := &Status{ |
Mateusz Zalega | bb2edbe | 2022-06-08 11:57:09 +0200 | [diff] [blame] | 30 | cl: cl, |
| 31 | ca: ca, |
| 32 | noClusterMemberManagement: true, |
Serge Bazanski | 5839e97 | 2021-11-16 15:46:19 +0100 | [diff] [blame] | 33 | } |
| 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 | |
| 47 | func (h *testServiceHandle) Watch() Watcher { |
| 48 | return Watcher{h.s.Watch()} |
| 49 | } |