1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.apache.hadoop.fs.viewfs;
20 
21 import java.io.IOException;
22 import java.net.URI;
23 import java.util.Collections;
24 import java.util.List;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.fs.FileSystem;
27 import org.apache.hadoop.fs.FileSystemTestHelper;
28 import org.apache.hadoop.fs.FsConstants;
29 import org.apache.hadoop.fs.LocalFileSystem;
30 import org.apache.hadoop.fs.Path;
31 import org.apache.hadoop.fs.permission.AclEntry;
32 import org.apache.hadoop.fs.viewfs.TestChRootedFileSystem.MockFileSystem;
33 import org.junit.*;
34 import static org.junit.Assert.*;
35 import static org.mockito.Mockito.*;
36 
37 /**
38  * Verify that viewfs propagates certain methods to the underlying fs
39  */
40 public class TestViewFileSystemDelegation { //extends ViewFileSystemTestSetup {
41   static Configuration conf;
42   static FileSystem viewFs;
43   static FakeFileSystem fs1;
44   static FakeFileSystem fs2;
45 
46   @BeforeClass
setup()47   public static void setup() throws Exception {
48     conf = ViewFileSystemTestSetup.createConfig();
49     fs1 = setupFileSystem(new URI("fs1:/"), FakeFileSystem.class);
50     fs2 = setupFileSystem(new URI("fs2:/"), FakeFileSystem.class);
51     viewFs = FileSystem.get(FsConstants.VIEWFS_URI, conf);
52   }
53 
setupFileSystem(URI uri, Class clazz)54   static FakeFileSystem setupFileSystem(URI uri, Class clazz)
55       throws Exception {
56     String scheme = uri.getScheme();
57     conf.set("fs."+scheme+".impl", clazz.getName());
58     FakeFileSystem fs = (FakeFileSystem)FileSystem.get(uri, conf);
59     assertEquals(uri, fs.getUri());
60     Path targetPath = new FileSystemTestHelper().getAbsoluteTestRootPath(fs);
61     ConfigUtil.addLink(conf, "/mounts/"+scheme, targetPath.toUri());
62     return fs;
63   }
64 
setupMockFileSystem(Configuration conf, URI uri)65   private static FileSystem setupMockFileSystem(Configuration conf, URI uri)
66       throws Exception {
67     String scheme = uri.getScheme();
68     conf.set("fs." + scheme + ".impl", MockFileSystem.class.getName());
69     FileSystem fs = FileSystem.get(uri, conf);
70     ConfigUtil.addLink(conf, "/mounts/" + scheme, uri);
71     return ((MockFileSystem)fs).getRawFileSystem();
72   }
73 
74   @Test
testSanity()75   public void testSanity() {
76     assertEquals("fs1:/", fs1.getUri().toString());
77     assertEquals("fs2:/", fs2.getUri().toString());
78   }
79 
80   @Test
testVerifyChecksum()81   public void testVerifyChecksum() throws Exception {
82     checkVerifyChecksum(false);
83     checkVerifyChecksum(true);
84   }
85 
86   /**
87    * Tests that ViewFileSystem dispatches calls for every ACL method through the
88    * mount table to the correct underlying FileSystem with all Path arguments
89    * translated as required.
90    */
91   @Test
testAclMethods()92   public void testAclMethods() throws Exception {
93     Configuration conf = ViewFileSystemTestSetup.createConfig();
94     FileSystem mockFs1 = setupMockFileSystem(conf, new URI("mockfs1:/"));
95     FileSystem mockFs2 = setupMockFileSystem(conf, new URI("mockfs2:/"));
96     FileSystem viewFs = FileSystem.get(FsConstants.VIEWFS_URI, conf);
97 
98     Path viewFsPath1 = new Path("/mounts/mockfs1/a/b/c");
99     Path mockFsPath1 = new Path("/a/b/c");
100     Path viewFsPath2 = new Path("/mounts/mockfs2/d/e/f");
101     Path mockFsPath2 = new Path("/d/e/f");
102     List<AclEntry> entries = Collections.emptyList();
103 
104     viewFs.modifyAclEntries(viewFsPath1, entries);
105     verify(mockFs1).modifyAclEntries(mockFsPath1, entries);
106     viewFs.modifyAclEntries(viewFsPath2, entries);
107     verify(mockFs2).modifyAclEntries(mockFsPath2, entries);
108 
109     viewFs.removeAclEntries(viewFsPath1, entries);
110     verify(mockFs1).removeAclEntries(mockFsPath1, entries);
111     viewFs.removeAclEntries(viewFsPath2, entries);
112     verify(mockFs2).removeAclEntries(mockFsPath2, entries);
113 
114     viewFs.removeDefaultAcl(viewFsPath1);
115     verify(mockFs1).removeDefaultAcl(mockFsPath1);
116     viewFs.removeDefaultAcl(viewFsPath2);
117     verify(mockFs2).removeDefaultAcl(mockFsPath2);
118 
119     viewFs.removeAcl(viewFsPath1);
120     verify(mockFs1).removeAcl(mockFsPath1);
121     viewFs.removeAcl(viewFsPath2);
122     verify(mockFs2).removeAcl(mockFsPath2);
123 
124     viewFs.setAcl(viewFsPath1, entries);
125     verify(mockFs1).setAcl(mockFsPath1, entries);
126     viewFs.setAcl(viewFsPath2, entries);
127     verify(mockFs2).setAcl(mockFsPath2, entries);
128 
129     viewFs.getAclStatus(viewFsPath1);
130     verify(mockFs1).getAclStatus(mockFsPath1);
131     viewFs.getAclStatus(viewFsPath2);
132     verify(mockFs2).getAclStatus(mockFsPath2);
133   }
134 
checkVerifyChecksum(boolean flag)135   void checkVerifyChecksum(boolean flag) {
136     viewFs.setVerifyChecksum(flag);
137     assertEquals(flag, fs1.getVerifyChecksum());
138     assertEquals(flag, fs2.getVerifyChecksum());
139   }
140 
141   static class FakeFileSystem extends LocalFileSystem {
142     boolean verifyChecksum = true;
143     URI uri;
144 
145     @Override
initialize(URI uri, Configuration conf)146     public void initialize(URI uri, Configuration conf) throws IOException {
147       super.initialize(uri, conf);
148       this.uri = uri;
149     }
150 
151     @Override
getUri()152     public URI getUri() {
153       return uri;
154     }
155 
156     @Override
setVerifyChecksum(boolean verifyChecksum)157     public void setVerifyChecksum(boolean verifyChecksum) {
158       this.verifyChecksum = verifyChecksum;
159     }
160 
getVerifyChecksum()161     public boolean getVerifyChecksum(){
162       return verifyChecksum;
163     }
164   }
165 }
166