MCPcopy Index your code
hub / github.com/coder/coder / TestUploadFileErrorScenarios

Function TestUploadFileErrorScenarios

coderd/provisionerdserver/upload_file_test.go:84–141  ·  view source on GitHub ↗

TestUploadFileErrorScenarios tests various error conditions in file upload

(t *testing.T)

Source from the content-addressed store, hash-verified

82
83// TestUploadFileErrorScenarios tests various error conditions in file upload
84func TestUploadFileErrorScenarios(t *testing.T) {
85 t.Parallel()
86
87 //nolint:dogsled
88 server, _, _, _ := setup(t, false, &overrides{
89 externalAuthConfigs: []*externalauth.Config{{}},
90 })
91
92 // Generate test data
93 moduleData := make([]byte, sdkproto.ChunkSize*2)
94 _, err := crand.Read(moduleData)
95 require.NoError(t, err)
96
97 upload, chunks, err := sdkproto.BytesToDataUpload(sdkproto.DataUploadType_UPLOAD_TYPE_MODULE_FILES, moduleData)
98 require.NoError(t, err)
99
100 t.Run("chunk_before_upload", func(t *testing.T) {
101 t.Parallel()
102
103 stream := newMockUploadStream(nil, chunks[0])
104
105 err := server.UploadFile(stream)
106 require.ErrorContains(t, err, "unexpected chunk piece while waiting for file upload")
107 require.True(t, stream.isDone(), "stream should be done after error")
108 })
109
110 t.Run("duplicate_upload", func(t *testing.T) {
111 t.Parallel()
112
113 stream := &mockUploadStream{
114 done: make(chan struct{}),
115 messages: make(chan *sdkproto.FileUpload, 2),
116 }
117
118 up := &sdkproto.FileUpload{Type: &sdkproto.FileUpload_DataUpload{DataUpload: upload}}
119
120 // Send it twice
121 stream.messages <- up
122 stream.messages <- up
123
124 err := server.UploadFile(stream)
125 require.ErrorContains(t, err, "unexpected file download while waiting for file completion")
126 require.True(t, stream.isDone(), "stream should be done after error")
127 })
128
129 t.Run("unsupported_upload_type", func(t *testing.T) {
130 t.Parallel()
131
132 //nolint:govet // Ignore lock copy
133 cpy := *upload
134 cpy.UploadType = sdkproto.DataUploadType_UPLOAD_TYPE_UNKNOWN // Set to an unsupported type
135 stream := newMockUploadStream(&cpy, chunks...)
136
137 err := server.UploadFile(stream)
138 require.ErrorContains(t, err, "unsupported file upload type")
139 require.True(t, stream.isDone(), "stream should be done after error")
140 })
141}

Callers

nothing calls this directly

Calls 6

isDoneMethod · 0.95
newMockUploadStreamFunction · 0.85
setupFunction · 0.70
ReadMethod · 0.65
RunMethod · 0.65
UploadFileMethod · 0.65

Tested by

no test coverage detected