github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/tests/parse_bckobj_test.go (about) 1 // Package test provides tests for common low-level types and utilities for all aistore projects 2 /* 3 * Copyright (c) 2021-2023, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package tests_test 6 7 import ( 8 "testing" 9 10 "github.com/NVIDIA/aistore/api/apc" 11 "github.com/NVIDIA/aistore/cmn" 12 "github.com/NVIDIA/aistore/tools/tassert" 13 ) 14 15 func TestParseBckObjectURI(t *testing.T) { 16 positiveTests := []struct { 17 uri string 18 opts cmn.ParseURIOpts 19 20 expectedBck cmn.Bck 21 expectedObj string 22 }{ 23 { 24 uri: "", 25 }, 26 { 27 uri: "ais://", 28 expectedBck: cmn.Bck{Provider: apc.AIS}, 29 }, 30 { 31 uri: "ais://@uuid", 32 expectedBck: cmn.Bck{Provider: apc.AIS, Ns: cmn.Ns{UUID: "uuid"}}, 33 }, 34 { 35 uri: "ais://@uuid#namespace/bucket/object", 36 expectedBck: cmn.Bck{Provider: apc.AIS, Name: "bucket", Ns: cmn.Ns{UUID: "uuid", Name: "namespace"}}, 37 expectedObj: "object", 38 }, 39 { 40 uri: "ais://@uuid#", 41 expectedBck: cmn.Bck{Provider: apc.AIS, Ns: cmn.Ns{UUID: "uuid"}}, 42 }, 43 { 44 uri: "ais://@uuid#/", 45 expectedBck: cmn.Bck{Provider: apc.AIS, Ns: cmn.Ns{UUID: "uuid"}}, 46 }, 47 { 48 uri: "ais://@#/", 49 expectedBck: cmn.Bck{Provider: apc.AIS}, 50 }, 51 { 52 uri: "ais://@#", 53 expectedBck: cmn.Bck{Provider: apc.AIS}, 54 }, 55 { 56 uri: "ais://@", 57 expectedBck: cmn.Bck{Provider: apc.AIS}, 58 }, 59 { 60 uri: "ais://@", 61 opts: cmn.ParseURIOpts{IsQuery: true}, 62 expectedBck: cmn.Bck{Provider: apc.AIS, Ns: cmn.NsAnyRemote}, 63 }, 64 { 65 uri: "@uuid#namespace", 66 opts: cmn.ParseURIOpts{IsQuery: true}, 67 expectedBck: cmn.Bck{Ns: cmn.Ns{UUID: "uuid", Name: "namespace"}}, 68 }, 69 { 70 uri: "@", 71 opts: cmn.ParseURIOpts{IsQuery: true}, 72 expectedBck: cmn.Bck{Ns: cmn.NsAnyRemote}, 73 }, 74 { 75 uri: "@#", 76 opts: cmn.ParseURIOpts{IsQuery: true}, 77 expectedBck: cmn.Bck{}, 78 }, 79 { 80 uri: "ais://@#/bucket", 81 expectedBck: cmn.Bck{Provider: apc.AIS, Name: "bucket"}, 82 }, 83 { 84 uri: "ais://bucket", 85 expectedBck: cmn.Bck{Provider: apc.AIS, Name: "bucket"}, 86 }, 87 { 88 uri: "bucket", 89 opts: cmn.ParseURIOpts{DefaultProvider: apc.AIS}, 90 expectedBck: cmn.Bck{Provider: apc.AIS, Name: "bucket"}, 91 }, 92 { 93 uri: "bucket", 94 opts: cmn.ParseURIOpts{DefaultProvider: apc.AWS}, 95 expectedBck: cmn.Bck{Provider: apc.AWS, Name: "bucket"}, 96 }, 97 { 98 uri: "ais://bucket/objname", 99 expectedBck: cmn.Bck{Provider: apc.AIS, Name: "bucket"}, 100 expectedObj: "objname", 101 }, 102 { 103 uri: "ais://@uuid#namespace/bucket", 104 expectedBck: cmn.Bck{Provider: apc.AIS, Name: "bucket", Ns: cmn.Ns{UUID: "uuid", Name: "namespace"}}, 105 }, 106 { 107 uri: "@uuid#namespace/bucket", 108 opts: cmn.ParseURIOpts{DefaultProvider: apc.AWS}, 109 expectedBck: cmn.Bck{Provider: apc.AWS, Name: "bucket", Ns: cmn.Ns{UUID: "uuid", Name: "namespace"}}, 110 }, 111 { 112 uri: "ais://bucket", 113 expectedBck: cmn.Bck{Provider: apc.AIS, Name: "bucket"}, 114 }, 115 { 116 uri: "aws://", 117 expectedBck: cmn.Bck{Provider: apc.AWS}, 118 }, 119 { 120 uri: "az:///", 121 expectedBck: cmn.Bck{Provider: apc.Azure}, 122 }, 123 } 124 125 for i := range positiveTests { 126 var ( 127 test = positiveTests[i] 128 bck, obj, err = cmn.ParseBckObjectURI(test.uri, test.opts) 129 ) 130 tassert.Errorf(t, err == nil, "unexpected error for input: %s, err: %v", test.uri, err) 131 132 if !bck.Equal(&test.expectedBck) { 133 t.Errorf("buckets does not match got: %v, expected: %v (input: %s)", bck, test.expectedBck, test.uri) 134 } 135 if obj != test.expectedObj { 136 t.Errorf("object names does not match got: %s, expected: %s (input: %s)", obj, test.expectedObj, test.uri) 137 } 138 } 139 140 negativeTests := []struct { 141 uri string 142 opts cmn.ParseURIOpts 143 }{ 144 {uri: "ais://%something"}, 145 {uri: "aiss://"}, 146 {uri: "ais:/"}, 147 {uri: "bucket"}, 148 {uri: "bucket/object"}, 149 {uri: "@#"}, 150 {uri: "#@"}, 151 {uri: "#@", opts: cmn.ParseURIOpts{IsQuery: true}}, 152 {uri: "@uuid"}, 153 {uri: "@uuid#namespace"}, 154 {uri: "@uuid#namespace/bucket"}, 155 {uri: "@uuid#namespace/bucket/object"}, 156 } 157 158 for _, test := range negativeTests { 159 _, _, err := cmn.ParseBckObjectURI(test.uri, test.opts) 160 tassert.Errorf(t, err != nil, "expected error for input: %s", test.uri) 161 } 162 }