Skip to content

Commit 4b6af8e

Browse files
authored
test(internal/sidekick): add more test cases to cover errors (#3155)
Adding some test cases to cover error handling in internal/sidekick/sidekick/downloads_cache.go. Fix #3020
1 parent 69d48f2 commit 4b6af8e

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

internal/sidekick/sidekick/downloads_cache.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import (
2828
"github.com/googleapis/librarian/internal/sidekick/config"
2929
)
3030

31+
var (
32+
downloadTarball = fetch.DownloadTarball
33+
)
34+
3135
func makeSourceRoot(ctx context.Context, rootConfig *config.Config, configPrefix string) (string, error) {
3236
sourceRoot, ok := rootConfig.Source[fmt.Sprintf("%s-root", configPrefix)]
3337
if !ok {
@@ -55,7 +59,7 @@ func makeSourceRoot(ctx context.Context, rootConfig *config.Config, configPrefix
5559
return target, nil
5660
}
5761
tgz := target + ".tar.gz"
58-
if err := fetch.DownloadTarball(ctx, tgz, sourceRoot, source); err != nil {
62+
if err := downloadTarball(ctx, tgz, sourceRoot, source); err != nil {
5963
return "", err
6064
}
6165

internal/sidekick/sidekick/downloads_cache_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,127 @@ func checkDownloadsCacheDir(t *testing.T, got, root string) {
220220
t.Errorf("mismatched downloadsCacheDir, want=%s, got=%s", "sidekick", root)
221221
}
222222
}
223+
224+
func TestMakeSourceRootErrors(t *testing.T) {
225+
t.Run("invalid-source-root", func(t *testing.T) {
226+
rootConfig := config.Config{
227+
Source: map[string]string{
228+
"googleapis-root": "this-is-not-a-valid-path-and-not-a-url",
229+
},
230+
}
231+
_, err := makeSourceRoot(context.Background(), &rootConfig, "googleapis")
232+
if err == nil {
233+
t.Fatal("expected error")
234+
}
235+
if !strings.Contains(err.Error(), "only directories and https URLs are supported") {
236+
t.Errorf("unexpected error message: %v", err)
237+
}
238+
})
239+
240+
t.Run("download-fails", func(t *testing.T) {
241+
// Temporarily replace the download function with a mock to simulate failure
242+
oldDownloadTarball := downloadTarball
243+
downloadTarball = func(ctx context.Context, target, url, expectedSha256 string) error {
244+
return fmt.Errorf("download failed after 3 attempts")
245+
}
246+
t.Cleanup(func() {
247+
downloadTarball = oldDownloadTarball
248+
})
249+
rootConfig := &config.Config{
250+
Source: map[string]string{
251+
"googleapis-root": "https://some-url",
252+
"googleapis-sha256": "somesha",
253+
"cachedir": t.TempDir(),
254+
},
255+
}
256+
_, err := makeSourceRoot(context.Background(), rootConfig, "googleapis")
257+
if err == nil {
258+
t.Fatal("expected an error")
259+
}
260+
if !strings.Contains(err.Error(), "download failed after 3 attempts") {
261+
t.Errorf("expected 'download failed after 3 attempts' in error, got %v", err)
262+
}
263+
})
264+
265+
t.Run("extract-fails", func(t *testing.T) {
266+
testDir := t.TempDir()
267+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
268+
w.WriteHeader(http.StatusOK)
269+
w.Write([]byte("this is not a tarball"))
270+
}))
271+
defer server.Close()
272+
273+
hasher := sha256.New()
274+
hasher.Write([]byte("this is not a tarball"))
275+
sha := fmt.Sprintf("%x", hasher.Sum(nil))
276+
277+
rootConfig := &config.Config{
278+
Source: map[string]string{
279+
"googleapis-root": server.URL,
280+
"googleapis-sha256": sha,
281+
"cachedir": testDir,
282+
},
283+
}
284+
_, err := makeSourceRoot(context.Background(), rootConfig, "googleapis")
285+
if err == nil {
286+
t.Fatal("expected an error")
287+
}
288+
})
289+
290+
t.Run("rename-fails", func(t *testing.T) {
291+
testDir := t.TempDir()
292+
simulatedSha := "2d08f07eab9bbe8300cd20b871d0811bbb693fab"
293+
simulatedSubdir := fmt.Sprintf("googleapis-%s", simulatedSha)
294+
simulatedPath := fmt.Sprintf("/archive/%s.tar.gz", simulatedSha)
295+
tarball, err := makeTestTarball(t, testDir, simulatedSubdir)
296+
if err != nil {
297+
t.Fatal(err)
298+
}
299+
300+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
301+
w.WriteHeader(http.StatusOK)
302+
w.Write(tarball.Contents)
303+
}))
304+
defer server.Close()
305+
306+
rootConfig := &config.Config{
307+
Source: map[string]string{
308+
"googleapis-root": server.URL + simulatedPath,
309+
"googleapis-sha256": tarball.Sha256,
310+
"cachedir": testDir,
311+
},
312+
}
313+
cacheDir, err := getCacheDir(rootConfig)
314+
if err != nil {
315+
t.Fatal(err)
316+
}
317+
if err := os.MkdirAll(cacheDir, 0755); err != nil {
318+
t.Fatal(err)
319+
}
320+
targetFile := path.Join(cacheDir, tarball.Sha256)
321+
if err := os.WriteFile(targetFile, []byte("dummy"), 0644); err != nil {
322+
t.Fatal(err)
323+
}
324+
325+
_, err = makeSourceRoot(context.Background(), rootConfig, "googleapis")
326+
if err == nil {
327+
t.Fatal("expected an error")
328+
}
329+
})
330+
}
331+
332+
func TestGetCacheDirFails(t *testing.T) {
333+
t.Setenv("HOME", "")
334+
t.Setenv("XDG_CACHE_HOME", "")
335+
336+
_, err := getCacheDir(&config.Config{Source: map[string]string{}})
337+
if err == nil {
338+
t.Fatal("expected an error when HOME and XDG_CACHE_HOME are not set")
339+
}
340+
}
341+
342+
func TestIsDirectoryFails(t *testing.T) {
343+
if isDirectory("a-path-with\x00-null-byte") {
344+
t.Error("isDirectory returned true for a path with a null byte, expected false")
345+
}
346+
}

0 commit comments

Comments
 (0)