73 lines
2.2 KiB
Go
73 lines
2.2 KiB
Go
package oss_test
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"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(err)
|
|
}
|
|
payload := []byte(strings.Repeat("agent-call-oss-integration\n", 1024))
|
|
digest := sha256.Sum256(payload)
|
|
checksum := hex.EncodeToString(digest[:])
|
|
objectKey := "agent-call/integration-tests/" + checksum + ".txt"
|
|
grant, err := client.Grant(context.Background(), "integration-"+checksum[:16], objectKey, checksum, int64(len(payload)), time.Now())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
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(context.Background(), grant, file.Name())
|
|
if err != nil {
|
|
t.Fatalf("upload failed without exposing the presigned URL: %s", redactError(err, config.AccessKeyID))
|
|
}
|
|
if result.SizeBytes != int64(len(payload)) || !strings.EqualFold(result.SHA256, checksum) {
|
|
t.Fatalf("upload result mismatch: %+v", result)
|
|
}
|
|
}
|
|
|
|
func redactError(err error, accessKeyID string) string {
|
|
if err == nil {
|
|
return ""
|
|
}
|
|
message := err.Error()
|
|
if accessKeyID != "" {
|
|
message = strings.ReplaceAll(message, accessKeyID, "<access-key-id>")
|
|
}
|
|
return message
|
|
}
|