mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-06-23 04:10:25 +00:00
c59a413765
CI / lint (push) Has been cancelled
CI / test (agent) (push) Has been cancelled
CI / test (cli) (push) Has been cancelled
CI / test (cmd) (push) Has been cancelled
CI / test (internal) (push) Has been cancelled
CI / test (manager, true) (push) Has been cancelled
CI / test (pkg) (push) Has been cancelled
CI / upload-coverage (push) Has been cancelled
* feat: implement extensible resource downloader framework with support for S3, GCS, and OCI sources Signed-off-by: SammyOina <sammyoina@gmail.com> * refactor: improve resource URL parsing and add support for bare OCI image references Signed-off-by: Sammy Oina <sammyoina@gmail.com> * fix: add empty string check and slash requirement for OCI image inference, and update python unit tests with event mock expectations Signed-off-by: Sammy Oina <sammyoina@gmail.com> * refactor: introduce OCIClient interface, add test coverage for decryption, and improve resource download error handling Signed-off-by: Sammy Oina <sammyoina@gmail.com> * chore: remove trailing whitespace in OCI downloader and HTTP tests Signed-off-by: Sammy Oina <sammyoina@gmail.com> --------- Signed-off-by: SammyOina <sammyoina@gmail.com> Signed-off-by: Sammy Oina <sammyoina@gmail.com>
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package resource
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestHTTPDownloader(t *testing.T) {
|
|
testContent := "test content"
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/test" {
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write([]byte(testContent))
|
|
} else {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
}))
|
|
defer ts.Close()
|
|
|
|
d := NewHTTPDownloader()
|
|
if d.Type() != SourceTypeHTTP {
|
|
t.Fatalf("expected type %s, got %s", SourceTypeHTTP, d.Type())
|
|
}
|
|
|
|
tmpDir := t.TempDir()
|
|
destPath := filepath.Join(tmpDir, "downloaded.txt")
|
|
|
|
ctx := context.Background()
|
|
|
|
// Test successful download
|
|
err := d.Download(ctx, ts.URL+"/test", destPath)
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got %v", err)
|
|
}
|
|
|
|
content, err := os.ReadFile(destPath)
|
|
if err != nil {
|
|
t.Fatalf("failed to read downloaded file: %v", err)
|
|
}
|
|
if string(content) != testContent {
|
|
t.Fatalf("expected content %q, got %q", testContent, string(content))
|
|
}
|
|
|
|
// Test 404
|
|
err = d.Download(ctx, ts.URL+"/notfound", filepath.Join(tmpDir, "notfound.txt"))
|
|
if err == nil {
|
|
t.Fatalf("expected error for 404 response")
|
|
}
|
|
}
|
|
|
|
func TestHTTPSDownloader(t *testing.T) {
|
|
d := NewHTTPSDownloader()
|
|
if d.Type() != SourceTypeHTTPS {
|
|
t.Fatalf("expected type %s, got %s", SourceTypeHTTPS, d.Type())
|
|
}
|
|
}
|