From 9711e2c8b840443510f682734a44e488156f161d Mon Sep 17 00:00:00 2001 From: Xiao Cui Date: Tue, 16 Jul 2024 14:08:50 -0400 Subject: [PATCH] Fix unit test --- pkg/commands/update/update_test.go | 41 ++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/pkg/commands/update/update_test.go b/pkg/commands/update/update_test.go index 7ad1745..f1cb589 100644 --- a/pkg/commands/update/update_test.go +++ b/pkg/commands/update/update_test.go @@ -1,6 +1,9 @@ package update import ( + "archive/tar" + "bytes" + "compress/gzip" "fmt" "net/http" "net/http/httptest" @@ -74,11 +77,45 @@ func TestUpdateCLI(t *testing.T) { err = os.WriteFile(mockBinaryPath, []byte("old binary"), 0755) assert.NoError(t, err) + // Create a mock binary content + mockBinaryContent := []byte("new binary") + + // Create a buffer to hold the tar.gz content + var buf bytes.Buffer + + // Create a gzip writer + gw := gzip.NewWriter(&buf) + + // Create a tar writer + tw := tar.NewWriter(gw) + + // Add the mock binary to the tar archive + hdr := &tar.Header{ + Name: "mycli", + Mode: 0755, + Size: int64(len(mockBinaryContent)), + } + if err := tw.WriteHeader(hdr); err != nil { + t.Fatal(err) + } + if _, err := tw.Write(mockBinaryContent); err != nil { + t.Fatal(err) + } + // Close the tar writer + if err := tw.Close(); err != nil { + t.Fatal(err) + } + + // Close the gzip writer + if err := gw.Close(); err != nil { + t.Fatal(err) + } + // Mock server server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Content-Type", "application/gzip") w.WriteHeader(http.StatusOK) - if _, err := w.Write([]byte("new binary")); err != nil { + if _, err := w.Write(buf.Bytes()); err != nil { return } }))