|
| 1 | +// Copyright 2026 Edgeless Systems GmbH |
| 2 | +// SPDX-License-Identifier: BUSL-1.1 |
| 3 | + |
| 4 | +//go:build contrast_unstable_api |
| 5 | + |
| 6 | +package sdk |
| 7 | + |
| 8 | +import ( |
| 9 | + "encoding/json" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "sync/atomic" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/edgelesssys/contrast/apitypes" |
| 16 | + "github.com/edgelesssys/contrast/sdk/apiv1" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | +) |
| 20 | + |
| 21 | +func TestNegotiateAPIVersion(t *testing.T) { |
| 22 | + for name, tc := range map[string]struct { |
| 23 | + coordinatorVersions []string |
| 24 | + handler http.Handler |
| 25 | + |
| 26 | + wantVersion string |
| 27 | + wantErr string |
| 28 | + }{ |
| 29 | + "coordinator supports v1": { |
| 30 | + coordinatorVersions: []string{apiv1.Version}, |
| 31 | + wantVersion: apiv1.Version, |
| 32 | + }, |
| 33 | + "coordinator supports a newer version, too": { |
| 34 | + coordinatorVersions: []string{apiv1.Version, "v2"}, |
| 35 | + wantVersion: apiv1.Version, |
| 36 | + }, |
| 37 | + "no common version": { |
| 38 | + coordinatorVersions: []string{"v99"}, |
| 39 | + wantErr: "no common API version", |
| 40 | + }, |
| 41 | + "coordinator has no capabilities endpoint": { |
| 42 | + handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 43 | + w.WriteHeader(http.StatusNotFound) |
| 44 | + }), |
| 45 | + wantErr: "getting capabilities", |
| 46 | + }, |
| 47 | + "capabilities response is malformed": { |
| 48 | + handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 49 | + _, _ = w.Write([]byte("not json")) |
| 50 | + }), |
| 51 | + wantErr: "unmarshalling capabilities", |
| 52 | + }, |
| 53 | + } { |
| 54 | + t.Run(name, func(t *testing.T) { |
| 55 | + require := require.New(t) |
| 56 | + assert := assert.New(t) |
| 57 | + |
| 58 | + handler := tc.handler |
| 59 | + if handler == nil { |
| 60 | + handler = capabilitiesHandler(tc.coordinatorVersions) |
| 61 | + } |
| 62 | + srv := httptest.NewServer(handler) |
| 63 | + t.Cleanup(srv.Close) |
| 64 | + |
| 65 | + version, err := New().WithBaseURL(srv.URL).NegotiateAPIVersion(t.Context()) |
| 66 | + |
| 67 | + if tc.wantErr != "" { |
| 68 | + require.Error(err) |
| 69 | + assert.Contains(err.Error(), tc.wantErr) |
| 70 | + return |
| 71 | + } |
| 72 | + require.NoError(err) |
| 73 | + assert.Equal(tc.wantVersion, version) |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// TestNegotiateAPIVersionCaching ensures the Coordinator is only asked once per Client. |
| 79 | +func TestNegotiateAPIVersionCaching(t *testing.T) { |
| 80 | + require := require.New(t) |
| 81 | + assert := assert.New(t) |
| 82 | + |
| 83 | + var calls atomic.Int32 |
| 84 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 85 | + calls.Add(1) |
| 86 | + capabilitiesHandler([]string{apiv1.Version}).ServeHTTP(w, r) |
| 87 | + })) |
| 88 | + t.Cleanup(srv.Close) |
| 89 | + |
| 90 | + client := New().WithBaseURL(srv.URL) |
| 91 | + for range 3 { |
| 92 | + version, err := client.NegotiateAPIVersion(t.Context()) |
| 93 | + require.NoError(err) |
| 94 | + assert.Equal(apiv1.Version, version) |
| 95 | + } |
| 96 | + assert.Equal(int32(1), calls.Load()) |
| 97 | +} |
| 98 | + |
| 99 | +// TestNegotiateAPIVersionErrorNotCached ensures a transient failure doesn't poison the Client. |
| 100 | +func TestNegotiateAPIVersionErrorNotCached(t *testing.T) { |
| 101 | + require := require.New(t) |
| 102 | + |
| 103 | + var fail atomic.Bool |
| 104 | + fail.Store(true) |
| 105 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 106 | + if fail.Load() { |
| 107 | + w.WriteHeader(http.StatusServiceUnavailable) |
| 108 | + return |
| 109 | + } |
| 110 | + capabilitiesHandler([]string{apiv1.Version}).ServeHTTP(w, r) |
| 111 | + })) |
| 112 | + t.Cleanup(srv.Close) |
| 113 | + |
| 114 | + client := New().WithBaseURL(srv.URL) |
| 115 | + _, err := client.NegotiateAPIVersion(t.Context()) |
| 116 | + require.Error(err) |
| 117 | + |
| 118 | + fail.Store(false) |
| 119 | + version, err := client.NegotiateAPIVersion(t.Context()) |
| 120 | + require.NoError(err) |
| 121 | + require.Equal(apiv1.Version, version) |
| 122 | +} |
| 123 | + |
| 124 | +// TestWithAPIVersionSkipsNegotiation ensures a pinned version doesn't contact the Coordinator. |
| 125 | +func TestWithAPIVersionSkipsNegotiation(t *testing.T) { |
| 126 | + require := require.New(t) |
| 127 | + |
| 128 | + var contacted atomic.Bool |
| 129 | + srv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { |
| 130 | + contacted.Store(true) |
| 131 | + })) |
| 132 | + t.Cleanup(srv.Close) |
| 133 | + |
| 134 | + version, err := New().WithBaseURL(srv.URL).WithAPIVersion(apiv1.Version).NegotiateAPIVersion(t.Context()) |
| 135 | + require.NoError(err) |
| 136 | + require.Equal(apiv1.Version, version) |
| 137 | + require.False(contacted.Load(), "Coordinator must not be contacted for a pinned API version") |
| 138 | +} |
| 139 | + |
| 140 | +func capabilitiesHandler(versions []string) http.Handler { |
| 141 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 142 | + if r.URL.Path != capabilitiesPath || r.Method != http.MethodGet { |
| 143 | + w.WriteHeader(http.StatusNotFound) |
| 144 | + return |
| 145 | + } |
| 146 | + if err := json.NewEncoder(w).Encode(apitypes.CapabilitiesResponse{APIVersions: versions}); err != nil { |
| 147 | + panic(err) |
| 148 | + } |
| 149 | + }) |
| 150 | +} |
0 commit comments