|
| 1 | +package client_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/google/go-cmp/cmp" |
| 13 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 14 | + "github.com/metal-stack/api/go/client" |
| 15 | + apiv2 "github.com/metal-stack/api/go/metalstack/api/v2" |
| 16 | + infra "github.com/metal-stack/api/go/metalstack/infra/v2" |
| 17 | + "github.com/metal-stack/api/go/metalstack/infra/v2/infrav2connect" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | + "google.golang.org/protobuf/testing/protocmp" |
| 20 | + "google.golang.org/protobuf/types/known/durationpb" |
| 21 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 22 | +) |
| 23 | + |
| 24 | +func Test_Ping(t *testing.T) { |
| 25 | + var ( |
| 26 | + mux = http.NewServeMux() |
| 27 | + log = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})) |
| 28 | + cs = &mockComponentService{log: log} |
| 29 | + ) |
| 30 | + |
| 31 | + mux.Handle(infrav2connect.NewComponentServiceHandler(cs)) |
| 32 | + server := httptest.NewTLSServer(mux) |
| 33 | + server.EnableHTTP2 = true |
| 34 | + defer server.Close() |
| 35 | + |
| 36 | + ctx := t.Context() |
| 37 | + |
| 38 | + tokenString, err := generateToken(2 * time.Second) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + c, err := client.New(&client.DialConfig{ |
| 42 | + BaseURL: server.URL, |
| 43 | + Token: tokenString, |
| 44 | + Transport: server.Client().Transport, |
| 45 | + Log: log, |
| 46 | + }) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + start := time.Now() |
| 50 | + config := &client.PingConfig{ |
| 51 | + ComponentType: apiv2.ComponentType_COMPONENT_TYPE_METAL_CORE, |
| 52 | + Identifier: new("server01"), |
| 53 | + StartedAt: start, |
| 54 | + Interval: 5 * time.Second, |
| 55 | + } |
| 56 | + |
| 57 | + want := []*infra.ComponentServicePingRequest{ |
| 58 | + {Type: apiv2.ComponentType_COMPONENT_TYPE_METAL_CORE, Identifier: "server01", StartedAt: timestamppb.New(start), Interval: durationpb.New(5 * time.Second), Version: &apiv2.Version{}}, |
| 59 | + {Type: apiv2.ComponentType_COMPONENT_TYPE_METAL_CORE, Identifier: "server01", StartedAt: timestamppb.New(start), Interval: durationpb.New(5 * time.Second), Version: &apiv2.Version{}}, |
| 60 | + } |
| 61 | + |
| 62 | + c.Ping(ctx, config) |
| 63 | + time.Sleep(3 * config.Interval) |
| 64 | + if diff := cmp.Diff( |
| 65 | + cs.pings, want, |
| 66 | + protocmp.Transform(), |
| 67 | + cmpopts.IgnoreUnexported(), |
| 68 | + ); diff != "" { |
| 69 | + t.Errorf("%v, want %v diff: %s", cs.pings, want, diff) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +type mockComponentService struct { |
| 74 | + log *slog.Logger |
| 75 | + pings []*infra.ComponentServicePingRequest |
| 76 | +} |
| 77 | + |
| 78 | +func (m *mockComponentService) Ping(_ context.Context, req *infra.ComponentServicePingRequest) (*infra.ComponentServicePingResponse, error) { |
| 79 | + m.log.Debug("ping", "req", req) |
| 80 | + m.pings = append(m.pings, req) |
| 81 | + return nil, nil |
| 82 | +} |
0 commit comments