github.com/qiniu/go-sdk/v7@v7.27.0/sandbox/types_resource_test.go (about) 1 //go:build unit 2 3 package sandbox 4 5 import ( 6 "encoding/json" 7 "testing" 8 ) 9 10 func TestSandboxResourceSpecToAPIKodoResource(t *testing.T) { 11 prefix := "datasets/" 12 readOnly := true 13 14 resource, err := sandboxResourceSpecToAPI(SandboxResourceSpec{ 15 Kodo: &KodoResource{ 16 Bucket: "test-bucket", 17 MountPath: "/mnt/kodo", 18 Prefix: &prefix, 19 ReadOnly: &readOnly, 20 }, 21 }) 22 if err != nil { 23 t.Fatalf("unexpected error: %v", err) 24 } 25 26 var got map[string]interface{} 27 if err := json.Unmarshal(mustMarshalJSON(t, resource), &got); err != nil { 28 t.Fatalf("unexpected JSON error: %v", err) 29 } 30 if got["type"] != "kodo" { 31 t.Errorf("expected type kodo, got %v", got["type"]) 32 } 33 if got["bucket"] != "test-bucket" { 34 t.Errorf("expected bucket test-bucket, got %v", got["bucket"]) 35 } 36 if got["mount_path"] != "/mnt/kodo" { 37 t.Errorf("expected mount_path /mnt/kodo, got %v", got["mount_path"]) 38 } 39 if got["prefix"] != "datasets/" { 40 t.Errorf("expected prefix datasets/, got %v", got["prefix"]) 41 } 42 if got["read_only"] != true { 43 t.Errorf("expected read_only true, got %v", got["read_only"]) 44 } 45 if _, ok := got["access_key"]; ok { 46 t.Errorf("expected access_key omitted, got %v", got["access_key"]) 47 } 48 if _, ok := got["secret_key"]; ok { 49 t.Errorf("expected secret_key omitted, got %v", got["secret_key"]) 50 } 51 } 52 53 func TestSandboxResourceSpecToAPIKodoResourceValidation(t *testing.T) { 54 _, err := sandboxResourceSpecToAPI(SandboxResourceSpec{ 55 Kodo: &KodoResource{MountPath: "/mnt/kodo"}, 56 }) 57 if err == nil { 58 t.Fatal("expected error when KodoResource.Bucket is empty") 59 } 60 61 _, err = sandboxResourceSpecToAPI(SandboxResourceSpec{ 62 Kodo: &KodoResource{Bucket: "test-bucket"}, 63 }) 64 if err == nil { 65 t.Fatal("expected error when KodoResource.MountPath is empty") 66 } 67 } 68 69 func mustMarshalJSON(t *testing.T, v interface{}) []byte { 70 t.Helper() 71 72 data, err := json.Marshal(v) 73 if err != nil { 74 t.Fatalf("unexpected JSON marshal error: %v", err) 75 } 76 return data 77 }