Skip to content

Commit 75954b9

Browse files
cardyokcodex
andcommitted
test(session): cover machine proof persistence
Co-Authored-By: Codex <noreply@openai.com>
1 parent 23e60b5 commit 75954b9

5 files changed

Lines changed: 46 additions & 7 deletions

File tree

cmd/gpud/run/mock_command_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,22 @@ func TestGetSessionCredentialsOptions_AllCredentialsPresent(t *testing.T) {
260260
}).Build()
261261

262262
mockey.Mock(pkgmetadata.ReadMetadata).To(func(ctx context.Context, db *sql.DB, key string) (string, error) {
263-
if key == pkgmetadata.MetadataKeyEndpoint {
263+
switch key {
264+
case pkgmetadata.MetadataKeyEndpoint:
264265
return "https://stored.endpoint.com", nil
266+
case pkgmetadata.MetadataKeyMachineProof:
267+
return "machine-proof", nil
265268
}
266269
return "", nil
267270
}).Build()
268271

269272
opts := getSessionCredentialsOptions(true, tmpDir, "")
270273
assert.NotEmpty(t, opts)
271-
assert.Len(t, opts, 3) // token, machine ID, endpoint
274+
assert.Len(t, opts, 4) // token, machine ID, endpoint, machine proof
275+
276+
op := &config.Op{}
277+
require.NoError(t, op.ApplyOpts(opts))
278+
assert.Equal(t, "machine-proof", op.SessionMachineProof)
272279
})
273280
}
274281

cmd/gpud/run/mock_session_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,18 @@ func TestReadSessionCredentialsFromPersistentFile(t *testing.T) {
3838
require.NoError(t, pkgmetadata.SetMetadata(ctx, dbRW, pkgmetadata.MetadataKeyToken, "session-token"))
3939
require.NoError(t, pkgmetadata.SetMetadata(ctx, dbRW, pkgmetadata.MetadataKeyMachineID, "assigned-machine-id"))
4040
require.NoError(t, pkgmetadata.SetMetadata(ctx, dbRW, pkgmetadata.MetadataKeyEndpoint, "gpud-manager.example.com"))
41+
require.NoError(t, pkgmetadata.SetMetadata(ctx, dbRW, pkgmetadata.MetadataKeyMachineProof, "machine-proof"))
4142

4243
gotToken, gotMachineID, gotEndpoint, err := readSessionCredentialsFromPersistentFile(ctx, tmpDir)
4344
require.NoError(t, err)
4445

4546
assert.Equal(t, "session-token", gotToken)
4647
assert.Equal(t, "assigned-machine-id", gotMachineID)
4748
assert.Equal(t, "gpud-manager.example.com", gotEndpoint)
49+
50+
gotMachineProof, err := readMachineProofFromPersistentFile(ctx, tmpDir)
51+
require.NoError(t, err)
52+
assert.Equal(t, "machine-proof", gotMachineProof)
4853
}
4954

5055
func TestReadSessionCredentialsFromPersistentFile_StateFileNotFound(t *testing.T) {

pkg/config/options_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func TestDBInMemoryWithSessionCredentials(t *testing.T) {
147147
WithDBInMemory(true),
148148
WithSessionToken("session-token-from-login-response"),
149149
WithSessionMachineID("assigned-machine-id-from-login-response"),
150+
WithSessionMachineProof("machine-proof-from-login-response"),
150151
WithSessionEndpoint("https://api.example.com"),
151152
}
152153

@@ -156,6 +157,7 @@ func TestDBInMemoryWithSessionCredentials(t *testing.T) {
156157
assert.True(t, op.DBInMemory)
157158
assert.Equal(t, "session-token-from-login-response", op.SessionToken)
158159
assert.Equal(t, "assigned-machine-id-from-login-response", op.SessionMachineID)
160+
assert.Equal(t, "machine-proof-from-login-response", op.SessionMachineProof)
159161
assert.Equal(t, "https://api.example.com", op.SessionEndpoint)
160162
}
161163

pkg/server/server_test.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ func TestNew_DBInMemory_SeedsSessionCredentialsBeforeAddressValidation(t *testin
9494
MetricsRetentionPeriod: metav1.Duration{Duration: time.Minute},
9595
Components: []string{"-disable-all"},
9696

97-
DBInMemory: true,
98-
SessionToken: "session-token",
99-
SessionMachineID: "assigned-machine-id",
100-
SessionEndpoint: "https://api.example.com",
97+
DBInMemory: true,
98+
SessionToken: "session-token",
99+
SessionMachineID: "assigned-machine-id",
100+
SessionMachineProof: "machine-proof",
101+
SessionEndpoint: "https://api.example.com",
101102
}
102103

103104
s, err := New(ctx, log.NewNopAuditLogger(), cfg, nil)
@@ -106,6 +107,26 @@ func TestNew_DBInMemory_SeedsSessionCredentialsBeforeAddressValidation(t *testin
106107
assert.Contains(t, err.Error(), "failed to create local GPUd server endpoint")
107108
}
108109

110+
func TestReadMachineProof(t *testing.T) {
111+
ctx := context.Background()
112+
assert.Empty(t, (&Server{}).readMachineProof(ctx))
113+
114+
stateFile := filepath.Join(t.TempDir(), "state.db")
115+
dbRW, err := sqlite.Open(stateFile)
116+
require.NoError(t, err)
117+
t.Cleanup(func() { _ = dbRW.Close() })
118+
require.NoError(t, pkgmetadata.CreateTableMetadata(ctx, dbRW))
119+
require.NoError(t, pkgmetadata.SetMetadata(ctx, dbRW, pkgmetadata.MetadataKeyMachineProof, "machine-proof"))
120+
121+
dbRO, err := sqlite.Open(stateFile, sqlite.WithReadOnly(true))
122+
require.NoError(t, err)
123+
s := &Server{dbRO: dbRO}
124+
assert.Equal(t, "machine-proof", s.readMachineProof(ctx))
125+
126+
require.NoError(t, dbRO.Close())
127+
assert.Empty(t, s.readMachineProof(ctx))
128+
}
129+
109130
func TestGenerateSelfSignedCert(t *testing.T) {
110131
s := &Server{}
111132
cert, err := s.generateSelfSignedCert()

pkg/session/session_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ func TestNewSession(t *testing.T) {
8282

8383
endpoint := "test-endpoint.com"
8484
machineID := "test-machine-id"
85+
machineProof := "test-machine-proof"
8586

86-
session, err := NewSession(ctx, "", endpoint, "", WithMachineID(machineID), WithPipeInterval(time.Second), WithEnableAutoUpdate(true), WithComponentsRegistry(components.NewRegistry(nil)))
87+
session, err := NewSession(ctx, "", endpoint, "", WithMachineID(machineID), WithMachineProof(machineProof), WithPipeInterval(time.Second), WithEnableAutoUpdate(true), WithComponentsRegistry(components.NewRegistry(nil)))
8788
if err != nil {
8889
t.Fatalf("error creating session: %v", err)
8990
}
@@ -98,6 +99,9 @@ func TestNewSession(t *testing.T) {
9899
if session.machineID != machineID {
99100
t.Errorf("expected machineID %s, got %s", machineID, session.machineID)
100101
}
102+
if session.machineProof != machineProof {
103+
t.Errorf("expected machineProof %s, got %s", machineProof, session.machineProof)
104+
}
101105
}
102106

103107
func TestCreateSessionRequestIncludesMachineProof(t *testing.T) {

0 commit comments

Comments
 (0)