[120] | 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; |
---|
| 20 | |
---|
| 21 | import java.net.URI; |
---|
| 22 | |
---|
| 23 | import org.apache.hadoop.fs.FileSystem; |
---|
| 24 | import org.apache.hadoop.fs.InMemoryFileSystem; |
---|
| 25 | import org.apache.hadoop.fs.FSDataOutputStream; |
---|
| 26 | import org.apache.hadoop.conf.Configuration; |
---|
| 27 | import junit.framework.TestCase; |
---|
| 28 | |
---|
| 29 | public class TestChecksumFileSystem extends TestCase { |
---|
| 30 | public void testgetChecksumLength() throws Exception { |
---|
| 31 | assertEquals(8, ChecksumFileSystem.getChecksumLength(0L, 512)); |
---|
| 32 | assertEquals(12, ChecksumFileSystem.getChecksumLength(1L, 512)); |
---|
| 33 | assertEquals(12, ChecksumFileSystem.getChecksumLength(512L, 512)); |
---|
| 34 | assertEquals(16, ChecksumFileSystem.getChecksumLength(513L, 512)); |
---|
| 35 | assertEquals(16, ChecksumFileSystem.getChecksumLength(1023L, 512)); |
---|
| 36 | assertEquals(16, ChecksumFileSystem.getChecksumLength(1024L, 512)); |
---|
| 37 | assertEquals(408, ChecksumFileSystem.getChecksumLength(100L, 1)); |
---|
| 38 | assertEquals(4000000000008L, |
---|
| 39 | ChecksumFileSystem.getChecksumLength(10000000000000L, 10)); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | // cehck that the checksum file is deleted for Checksum file system. |
---|
| 43 | public void testDeletionOfCheckSum() throws Exception { |
---|
| 44 | Configuration conf = new Configuration(); |
---|
| 45 | URI uri = URI.create("ramfs://mapoutput" + "_tmp"); |
---|
| 46 | InMemoryFileSystem inMemFs = (InMemoryFileSystem)FileSystem.get(uri, conf); |
---|
| 47 | Path testPath = new Path("/file_1"); |
---|
| 48 | inMemFs.reserveSpaceWithCheckSum(testPath, 1024); |
---|
| 49 | FSDataOutputStream fout = inMemFs.create(testPath); |
---|
| 50 | fout.write("testing".getBytes()); |
---|
| 51 | fout.close(); |
---|
| 52 | assertTrue("checksum exists", inMemFs.exists(inMemFs.getChecksumFile(testPath))); |
---|
| 53 | inMemFs.delete(testPath, true); |
---|
| 54 | assertTrue("checksum deleted", !inMemFs.exists(inMemFs.getChecksumFile(testPath))); |
---|
| 55 | // check for directories getting deleted. |
---|
| 56 | testPath = new Path("/tesdir/file_1"); |
---|
| 57 | inMemFs.reserveSpaceWithCheckSum(testPath, 1024); |
---|
| 58 | fout = inMemFs.create(testPath); |
---|
| 59 | fout.write("testing".getBytes()); |
---|
| 60 | fout.close(); |
---|
| 61 | testPath = new Path("/testdir/file_2"); |
---|
| 62 | inMemFs.reserveSpaceWithCheckSum(testPath, 1024); |
---|
| 63 | fout = inMemFs.create(testPath); |
---|
| 64 | fout.write("testing".getBytes()); |
---|
| 65 | fout.close(); |
---|
| 66 | inMemFs.delete(testPath, true); |
---|
| 67 | assertTrue("nothing in the namespace", inMemFs.listStatus(new Path("/")).length == 0); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | public void testVerifyChecksum() throws Exception { |
---|
| 71 | String TEST_ROOT_DIR |
---|
| 72 | = System.getProperty("test.build.data","build/test/data/work-dir/localfs"); |
---|
| 73 | |
---|
| 74 | Configuration conf = new Configuration(); |
---|
| 75 | LocalFileSystem localFs = FileSystem.getLocal(conf); |
---|
| 76 | Path testPath = new Path(TEST_ROOT_DIR, "testPath"); |
---|
| 77 | Path testPath11 = new Path(TEST_ROOT_DIR, "testPath11"); |
---|
| 78 | FSDataOutputStream fout = localFs.create(testPath); |
---|
| 79 | fout.write("testing".getBytes()); |
---|
| 80 | fout.close(); |
---|
| 81 | |
---|
| 82 | fout = localFs.create(testPath11); |
---|
| 83 | fout.write("testing you".getBytes()); |
---|
| 84 | fout.close(); |
---|
| 85 | |
---|
| 86 | localFs.delete(localFs.getChecksumFile(testPath), true); |
---|
| 87 | assertTrue("checksum deleted", !localFs.exists(localFs.getChecksumFile(testPath))); |
---|
| 88 | |
---|
| 89 | //copying the wrong checksum file |
---|
| 90 | FileUtil.copy(localFs, localFs.getChecksumFile(testPath11), localFs, |
---|
| 91 | localFs.getChecksumFile(testPath),false,true,conf); |
---|
| 92 | assertTrue("checksum exists", localFs.exists(localFs.getChecksumFile(testPath))); |
---|
| 93 | |
---|
| 94 | boolean errorRead = false; |
---|
| 95 | try { |
---|
| 96 | TestLocalFileSystem.readFile(localFs, testPath); |
---|
| 97 | }catch(ChecksumException ie) { |
---|
| 98 | errorRead = true; |
---|
| 99 | } |
---|
| 100 | assertTrue("error reading", errorRead); |
---|
| 101 | |
---|
| 102 | //now setting verify false, the read should succeed |
---|
| 103 | localFs.setVerifyChecksum(false); |
---|
| 104 | String str = TestLocalFileSystem.readFile(localFs, testPath); |
---|
| 105 | assertTrue("read", "testing".equals(str)); |
---|
| 106 | |
---|
| 107 | } |
---|
| 108 | } |
---|