71 lines
2.3 KiB
Go
71 lines
2.3 KiB
Go
package oss_test
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.ipao.vip/rogee/go-sip/internal/agent"
|
|
ossclient "git.ipao.vip/rogee/go-sip/internal/oss"
|
|
)
|
|
|
|
func TestAlibabaOSSGrantPutIntegration(t *testing.T) {
|
|
if os.Getenv("AGENT_CALL_OSS_INTEGRATION") != "1" {
|
|
t.Skip("set AGENT_CALL_OSS_INTEGRATION=1 to use the authorized Alibaba OSS test bucket")
|
|
}
|
|
config := ossclient.Config{
|
|
Endpoint: os.Getenv("DISPATCHER_OSS_ENDPOINT"),
|
|
Region: os.Getenv("DISPATCHER_OSS_REGION"),
|
|
Bucket: os.Getenv("DISPATCHER_OSS_BUCKET"),
|
|
AccessKeyID: os.Getenv("DISPATCHER_OSS_ACCESS_KEY_ID"),
|
|
AccessKeySecret: os.Getenv("DISPATCHER_OSS_ACCESS_KEY_SECRET"),
|
|
KeyPrefix: "agent-call/integration-tests",
|
|
GrantTTL: 15 * time.Minute,
|
|
MaxAssetBytes: 1 << 20,
|
|
}
|
|
client, err := ossclient.NewClient(config)
|
|
if err != nil {
|
|
t.Fatal("OSS integration configuration is invalid")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
|
|
defer cancel()
|
|
payload := []byte(strings.Repeat("agent-call-oss-integration\n", 1024))
|
|
digest := sha256.Sum256(payload)
|
|
checksum := hex.EncodeToString(digest[:])
|
|
uniqueID := "integration-" + rand.Text()
|
|
objectKey := config.KeyPrefix + "/" + uniqueID + "-" + checksum[:16] + ".txt"
|
|
grant, err := client.Grant(ctx, uniqueID, objectKey, checksum, int64(len(payload)), time.Now())
|
|
if err != nil {
|
|
t.Fatalf("OSS presign failed: type=%T error_sha256=%x", err, sha256.Sum256([]byte(err.Error())))
|
|
}
|
|
file, err := os.CreateTemp(t.TempDir(), "oss-upload-*.txt")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := file.Write(payload); err != nil {
|
|
_ = file.Close()
|
|
t.Fatal(err)
|
|
}
|
|
if err := file.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
uploader := agent.UploadClient{Now: time.Now, AllowedHosts: map[string]struct{}{}}
|
|
result, err := uploader.UploadFile(ctx, grant, file.Name())
|
|
if err != nil {
|
|
var httpErr *agent.UploadHTTPError
|
|
if errors.As(err, &httpErr) {
|
|
t.Fatalf("OSS PUT failed: HTTP %d", httpErr.StatusCode)
|
|
}
|
|
t.Fatalf("OSS PUT failed: type=%T error_sha256=%x", err, sha256.Sum256([]byte(err.Error())))
|
|
}
|
|
if result.SizeBytes != int64(len(payload)) || !strings.EqualFold(result.SHA256, checksum) {
|
|
t.Fatalf("upload result mismatch: %+v", result)
|
|
}
|
|
}
|