1// Copyright 2020 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package bind 16 17import ( 18 "io/ioutil" 19 "os" 20 "path" 21 "reflect" 22 "testing" 23) 24 25func TestServerBind(t *testing.T) { 26 mountTempDir, err := ioutil.TempDir("", "mount") 27 if err != nil { 28 t.Error(err) 29 } 30 fakeBinder := NewFakePathBinder() 31 server := NewServer(fakeBinder) 32 roSourceDir := path.Join(mountTempDir, "path/to/readonly/source") 33 if err = os.MkdirAll(roSourceDir, os.ModePerm); err != nil { 34 t.Error(err) 35 } 36 roDestDir := path.Join(mountTempDir, "path/to/hacksaw/readonly/destination") 37 if err = os.MkdirAll(roDestDir, os.ModePerm); err != nil { 38 t.Error(err) 39 } 40 bindROArgs := BindReadOnlyArgs{ 41 Source: roSourceDir, 42 Destination: roDestDir, 43 } 44 var bindROReply BindReadOnlyReply 45 if err := server.BindReadOnly(&bindROArgs, &bindROReply); err != nil { 46 t.Error(err) 47 } 48 if bindROReply.Err != "" { 49 t.Error(bindROReply.Err) 50 } 51 rwSourceDir := path.Join(mountTempDir, "path/to/readwrite/source") 52 if err = os.MkdirAll(rwSourceDir, os.ModePerm); err != nil { 53 t.Error(err) 54 } 55 rwDestDir := path.Join(mountTempDir, "path/to/hacksaw/readwrite/destination") 56 if err = os.MkdirAll(rwDestDir, os.ModePerm); err != nil { 57 t.Error(err) 58 } 59 bindRWArgs := BindReadWriteArgs{ 60 Source: rwSourceDir, 61 Destination: rwDestDir, 62 } 63 var bindRWReply BindReadWriteReply 64 if err := server.BindReadWrite(&bindRWArgs, &bindRWReply); err != nil { 65 t.Error(err) 66 } 67 if bindRWReply.Err != "" { 68 t.Error(bindRWReply.Err) 69 } 70 var listArgs ListArgs 71 var listReply ListReply 72 err = server.List(&listArgs, &listReply) 73 if err != nil { 74 t.Error(err) 75 } 76 if listReply.Err != "" { 77 t.Error(listReply.Err) 78 } 79 expectedList := []string{ 80 roDestDir, 81 rwDestDir, 82 } 83 if !reflect.DeepEqual(listReply.BindList, expectedList) { 84 t.Errorf("Bind list %v is different than expected bind %v", 85 listReply.BindList, expectedList) 86 } 87 unbindArgs := UnbindArgs{ 88 Destination: rwDestDir, 89 } 90 var unbindReply UnbindReply 91 if err := server.Unbind(&unbindArgs, &unbindReply); err != nil { 92 t.Error(err) 93 } 94 if unbindReply.Err != "" { 95 t.Error(unbindReply.Err) 96 } 97 err = server.List(&listArgs, &listReply) 98 if err != nil { 99 t.Error(err) 100 } 101 if listReply.Err != "" { 102 t.Error(listReply.Err) 103 } 104 expectedList = []string{ 105 roDestDir, 106 } 107 if !reflect.DeepEqual(listReply.BindList, expectedList) { 108 t.Errorf("Bind list %v is different than expected bind %v", 109 listReply.BindList, expectedList) 110 } 111} 112