-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The upload completer scans for uploads that need to be completed, likely due to an error or process restart. Prior to this change, it only completed uploads that had 1 or more parts. Since completing an upload is what cleans up the directory on disk, it was possible for us to leave behind empty directories for uploads with no parts. This change makes it valid to complete uploads with no parts, which ensures that these directories get cleaned up. In addition, the upload completer checks the upload for a session.end event, and emits one if there is not already an end event present. This logic only handled session.end, and not windows.desktop.session.end, so that has been updated as well. Updates #9646
- Loading branch information
Showing
4 changed files
with
191 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
Copyright 2022 Gravitational, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package events | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
apievents "github.com/gravitational/teleport/api/types/events" | ||
"github.com/gravitational/teleport/lib/session" | ||
"github.com/jonboulle/clockwork" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestUploadCompleterEmitsSessionEnd verifies that the upload completer | ||
// emits session.end or windows.desktop.session.end events for sessions | ||
// that are completed. | ||
func TestUploadCompleterEmitsSessionEnd(t *testing.T) { | ||
for _, test := range []struct { | ||
startEvent, endEvent string | ||
}{ | ||
{SessionStartEvent, SessionEndEvent}, | ||
{WindowsDesktopSessionStartEvent, WindowsDesktopSessionEndEvent}, | ||
} { | ||
t.Run(test.endEvent, func(t *testing.T) { | ||
clock := clockwork.NewFakeClock() | ||
mu := NewMemoryUploader() | ||
mu.Clock = clock | ||
|
||
log := &MockAuditLog{ | ||
sessionEvents: []EventFields{ | ||
{ | ||
EventType: test.startEvent, | ||
SessionClusterName: "cluster-name", | ||
}, | ||
}, | ||
} | ||
|
||
uc, err := NewUploadCompleter(UploadCompleterConfig{ | ||
Unstarted: true, | ||
Uploader: mu, | ||
AuditLog: log, | ||
GracePeriod: 2 * time.Hour, | ||
}) | ||
require.NoError(t, err) | ||
|
||
upload, err := mu.CreateUpload(context.Background(), session.NewID()) | ||
require.NoError(t, err) | ||
clock.Advance(3 * time.Hour) | ||
|
||
// session end events are only emitted if there's at least one | ||
// part to be uploaded, so create that here | ||
_, err = mu.UploadPart(context.Background(), *upload, 0, strings.NewReader("part")) | ||
require.NoError(t, err) | ||
|
||
err = uc.CheckUploads(context.Background()) | ||
require.NoError(t, err) | ||
|
||
// expect two events - a session end and a session upload | ||
require.Len(t, log.emitter.Events(), 2) | ||
require.Equal(t, test.endEvent, log.emitter.Events()[0].GetType()) | ||
require.Equal(t, "cluster-name", log.emitter.Events()[0].GetClusterName()) | ||
|
||
require.IsType(t, &apievents.SessionUpload{}, log.emitter.Events()[1]) | ||
}) | ||
} | ||
} | ||
|
||
// TestUploadCompleterCompletesEmptyUploads verifies that the upload completer | ||
// completes uploads that have no parts. This ensures that we don't leave empty | ||
// directories behind. | ||
func TestUploadCompleterCompletesEmptyUploads(t *testing.T) { | ||
clock := clockwork.NewFakeClock() | ||
mu := NewMemoryUploader() | ||
mu.Clock = clock | ||
|
||
log := &MockAuditLog{} | ||
|
||
uc, err := NewUploadCompleter(UploadCompleterConfig{ | ||
Unstarted: true, | ||
Uploader: mu, | ||
AuditLog: log, | ||
GracePeriod: 2 * time.Hour, | ||
}) | ||
require.NoError(t, err) | ||
|
||
upload, err := mu.CreateUpload(context.Background(), session.NewID()) | ||
require.NoError(t, err) | ||
clock.Advance(3 * time.Hour) | ||
|
||
err = uc.CheckUploads(context.Background()) | ||
require.NoError(t, err) | ||
|
||
require.True(t, mu.uploads[upload.ID].completed) | ||
} | ||
|
||
type MockAuditLog struct { | ||
DiscardAuditLog | ||
|
||
emitter MockEmitter | ||
sessionEvents []EventFields | ||
} | ||
|
||
func (m *MockAuditLog) GetSessionEvents(namespace string, sid session.ID, after int, includePrintEvents bool) ([]EventFields, error) { | ||
return m.sessionEvents, nil | ||
} | ||
|
||
func (m *MockAuditLog) EmitAuditEvent(ctx context.Context, event apievents.AuditEvent) error { | ||
return m.emitter.EmitAuditEvent(ctx, event) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters