go-hep.org/x/hep@v0.38.1/xrootd/xrdproto/query/query_test.go (about) 1 // Copyright ©2018 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package query_test 6 7 import ( 8 "context" 9 "fmt" 10 "log" 11 "reflect" 12 "strings" 13 "testing" 14 15 "go-hep.org/x/hep/xrootd" 16 "go-hep.org/x/hep/xrootd/internal/xrdenc" 17 "go-hep.org/x/hep/xrootd/xrdfs" 18 "go-hep.org/x/hep/xrootd/xrdproto" 19 "go-hep.org/x/hep/xrootd/xrdproto/query" 20 ) 21 22 func TestRequest(t *testing.T) { 23 for _, want := range []query.Request{ 24 { 25 Query: query.Config, 26 Args: []byte("bind_max chksum"), 27 }, 28 { 29 Query: query.Visa, 30 Handle: [4]byte{1, 2, 3, 4}, 31 }, 32 } { 33 t.Run("", func(t *testing.T) { 34 var ( 35 err error 36 w = new(xrdenc.WBuffer) 37 got query.Request 38 ) 39 40 if want.ReqID() != query.RequestID { 41 t.Fatalf("invalid request ID: got=%d want=%d", want.ReqID(), query.RequestID) 42 } 43 44 if want.ShouldSign() { 45 t.Fatalf("invalid") 46 } 47 48 err = want.MarshalXrd(w) 49 if err != nil { 50 t.Fatalf("could not marshal request: %v", err) 51 } 52 53 r := xrdenc.NewRBuffer(w.Bytes()) 54 err = got.UnmarshalXrd(r) 55 if err != nil { 56 t.Fatalf("could not unmarshal request: %v", err) 57 } 58 59 if !reflect.DeepEqual(got, want) { 60 t.Fatalf("round trip failed:\ngot = %#v\nwant= %#v\n", got, want) 61 } 62 }) 63 } 64 } 65 66 func TestResponse(t *testing.T) { 67 for _, want := range []query.Response{ 68 { 69 Data: []byte("1234"), 70 }, 71 { 72 Data: []byte("oss.cgroup=public&oss.space=499337216&oss.free=444280832&oss.maxf=444280832&oss.used=55056384&oss.quota=-1\x00"), 73 }, 74 } { 75 t.Run("", func(t *testing.T) { 76 var ( 77 err error 78 w = new(xrdenc.WBuffer) 79 got query.Response 80 ) 81 82 if want.RespID() != query.RequestID { 83 t.Fatalf("invalid response ID: got=%d want=%d", want.RespID(), query.RequestID) 84 } 85 86 err = want.MarshalXrd(w) 87 if err != nil { 88 t.Fatalf("could not marshal response: %v", err) 89 } 90 91 r := xrdenc.NewRBuffer(w.Bytes()) 92 err = got.UnmarshalXrd(r) 93 if err != nil { 94 t.Fatalf("could not unmarshal response: %v", err) 95 } 96 97 if !reflect.DeepEqual(got, want) { 98 t.Fatalf("round trip failed:\ngot = %#v\nwant= %#v\n", got, want) 99 } 100 }) 101 } 102 } 103 104 func Example() { 105 bkg := context.Background() 106 107 cl, err := xrootd.NewClient(bkg, "ccxrootdgotest.in2p3.fr:9001", "gopher") 108 if err != nil { 109 log.Fatalf("could not create client: %v", err) 110 } 111 defer cl.Close() 112 113 fs := cl.FS() 114 f, err := fs.Open(bkg, "/tmp/dir1/file1.txt", xrdfs.OpenModeOwnerRead, xrdfs.OpenOptionsOpenRead) 115 if err != nil { 116 log.Fatalf("open error: %v", err) 117 } 118 defer f.Close(bkg) 119 120 var ( 121 resp query.Response 122 req = query.Request{ 123 Query: query.Space, 124 Handle: f.Handle(), 125 Args: []byte("/tmp/dir1/file1.txt"), 126 } 127 ) 128 129 id, err := cl.Send(bkg, &resp, &req) 130 if err != nil { 131 log.Fatalf("space request error: %v", err) 132 } 133 fmt.Printf("sess: %s\n", id) 134 // fmt.Printf("space: %q\n", resp.Data) 135 136 cfg := []string{ 137 "bind_max", 138 "chksum", 139 "cid", "cms", "pio_max", 140 "readv_ior_max", 141 "readv_iov_max", 142 "role", 143 "sitename", 144 "tpc", 145 "version", 146 "wan_port", 147 "wan_window", 148 "window", 149 } 150 151 req = query.Request{ 152 Query: query.Config, 153 Args: []byte(strings.Join(cfg, " ")), 154 } 155 156 id, err = cl.Send(bkg, &resp, &req) 157 if err != nil { 158 log.Fatalf("config request error: %v", err) 159 } 160 fmt.Printf("sess: %s\n", id) 161 for i, v := range strings.Split(strings.TrimRight(string(resp.Data), "\n"), "\n") { 162 if v == cfg[i] { 163 fmt.Printf("config: %s=N/A\n", v) 164 continue 165 } 166 fmt.Printf("config: %s=%q\n", cfg[i], v) 167 } 168 169 // Output: 170 // sess: ccxrootdgotest.in2p3.fr:9001 171 // sess: ccxrootdgotest.in2p3.fr:9001 172 // config: bind_max="15" 173 // config: chksum="0:md5" 174 // config: cid=N/A 175 // config: cms="none|" 176 // config: pio_max="5" 177 // config: readv_ior_max="2097136" 178 // config: readv_iov_max="1024" 179 // config: role="server" 180 // config: sitename=N/A 181 // config: tpc=N/A 182 // config: version="v4.8.5" 183 // config: wan_port=N/A 184 // config: wan_window=N/A 185 // config: window="87380" 186 } 187 188 var ( 189 _ xrdproto.Request = (*query.Request)(nil) 190 _ xrdproto.Response = (*query.Response)(nil) 191 )