blob: 6411cc3b6f9365c53bda5310b5f1d39f73768c3e [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Serge Bazanski5839e972021-11-16 15:46:19 +01004package consensus
5
6import (
7 "context"
8 "testing"
9
Lorenz Brund13c1c62022-03-30 19:58:58 +020010 clientv3 "go.etcd.io/etcd/client/v3"
Serge Bazanski5839e972021-11-16 15:46:19 +010011
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020012 "source.monogon.dev/osbase/event/memory"
Serge Bazanski5839e972021-11-16 15:46:19 +010013)
14
15type testServiceHandle struct {
Serge Bazanski37110c32023-03-01 13:57:27 +000016 s memory.Value[*Status]
Serge Bazanski5839e972021-11-16 15:46:19 +010017}
18
19// TestServiceHandle builds a somewhat functioning ServiceHandle from a bare
20// etcd connection, effectively creating a fake Consensus service. This must
21// only be used in test code to perform dependency injection of a etcd client
22// into code which expects a Consensus service instance, eg. for testing the
23// Curator.
24//
25// The 'somewhat functioning' description above should serve as a hint to the
26// API stability and backwards/forwards compatibility of this function: there is
27// none.
28func TestServiceHandle(t *testing.T, cl *clientv3.Client) ServiceHandle {
29 ca := pkiCA()
30
Tim Windelschmidt3b5a9172024-05-23 13:33:52 +020031 var tsh testServiceHandle
Serge Bazanski5839e972021-11-16 15:46:19 +010032 st := &Status{
Mateusz Zalegabb2edbe2022-06-08 11:57:09 +020033 cl: cl,
34 ca: ca,
35 noClusterMemberManagement: true,
Serge Bazanski5839e972021-11-16 15:46:19 +010036 }
37 etcdPKI, err := st.pkiClient()
38 if err != nil {
39 t.Fatalf("failed to get PKI etcd client: %v", err)
40 }
41 ctx, ctxC := context.WithCancel(context.Background())
42 defer ctxC()
43 if _, err := ca.Ensure(ctx, etcdPKI); err != nil {
44 t.Fatalf("failed to ensure PKI CA: %v", err)
45 }
46 tsh.s.Set(st)
Serge Bazanski37110c32023-03-01 13:57:27 +000047 return &tsh.s
Serge Bazanski5839e972021-11-16 15:46:19 +010048}