|
| 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.hdfs; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.text.ParseException; |
| 23 | +import java.util.Date; |
| 24 | + |
| 25 | +import org.junit.AfterClass; |
| 26 | +import org.junit.BeforeClass; |
| 27 | +import org.junit.Test; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +import org.apache.hadoop.conf.Configuration; |
| 32 | +import org.apache.hadoop.fs.FileStatus; |
| 33 | +import org.apache.hadoop.fs.FsShell; |
| 34 | +import org.apache.hadoop.fs.Path; |
| 35 | +import org.apache.hadoop.fs.shell.TouchCommands; |
| 36 | +import org.apache.hadoop.test.GenericTestUtils; |
| 37 | +import org.apache.hadoop.util.StringUtils; |
| 38 | + |
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
| 40 | + |
| 41 | +/** |
| 42 | + * Tests to perform Touch operations on DFS. |
| 43 | + */ |
| 44 | +public class TestDFSShellTouch { |
| 45 | + |
| 46 | + private static final Logger LOG = LoggerFactory.getLogger(TestDFSShellTouch.class); |
| 47 | + |
| 48 | + private static MiniDFSCluster miniCluster; |
| 49 | + private static DistributedFileSystem dfs; |
| 50 | + private static FsShell shell; |
| 51 | + |
| 52 | + @BeforeClass |
| 53 | + public static void setup() throws IOException { |
| 54 | + final Configuration conf = new Configuration(); |
| 55 | + conf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, |
| 56 | + GenericTestUtils.getTestDir("TestDFSShellTouch").getAbsolutePath()); |
| 57 | + |
| 58 | + miniCluster = new MiniDFSCluster.Builder(conf).numDataNodes(2).build(); |
| 59 | + miniCluster.waitActive(); |
| 60 | + dfs = miniCluster.getFileSystem(); |
| 61 | + shell = new FsShell(dfs.getConf()); |
| 62 | + } |
| 63 | + |
| 64 | + @AfterClass |
| 65 | + public static void tearDown() { |
| 66 | + if (miniCluster != null) { |
| 67 | + miniCluster.shutdown(true, true); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testTouch() throws Exception { |
| 73 | + final String newFileName = "newFile1"; |
| 74 | + final Path newFile = new Path(newFileName); |
| 75 | + dfs.delete(newFile, true); |
| 76 | + assertThat(dfs.exists(newFile)).isFalse(); |
| 77 | + dfs.create(newFile); |
| 78 | + assertThat(dfs.exists(newFile)).isTrue(); |
| 79 | + |
| 80 | + String strTime = formatTimestamp(System.currentTimeMillis()); |
| 81 | + Date dateObj = parseTimestamp(strTime); |
| 82 | + |
| 83 | + assertThat(shellRun("-touch", "-t", strTime, newFileName)).as( |
| 84 | + "Expected successful touch on a new file" + " with a specified timestamp").isEqualTo(0); |
| 85 | + FileStatus newStatus = dfs.getFileStatus(newFile); |
| 86 | + assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime()); |
| 87 | + assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime()); |
| 88 | + |
| 89 | + FileStatus fileStatus = dfs.getFileStatus(newFile); |
| 90 | + Thread.sleep(500); |
| 91 | + |
| 92 | + strTime = formatTimestamp(System.currentTimeMillis()); |
| 93 | + dateObj = parseTimestamp(strTime); |
| 94 | + |
| 95 | + assertThat(shellRun("-touch", "-a", "-t", strTime, newFileName)).as( |
| 96 | + "Expected successful touch with a specified access time").isEqualTo(0); |
| 97 | + newStatus = dfs.getFileStatus(newFile); |
| 98 | + // Verify if access time is recorded correctly (and modification time |
| 99 | + // remains unchanged). |
| 100 | + assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime()); |
| 101 | + assertThat(newStatus.getModificationTime()).isEqualTo(fileStatus.getModificationTime()); |
| 102 | + |
| 103 | + fileStatus = dfs.getFileStatus(newFile); |
| 104 | + Thread.sleep(500); |
| 105 | + |
| 106 | + strTime = formatTimestamp(System.currentTimeMillis()); |
| 107 | + dateObj = parseTimestamp(strTime); |
| 108 | + |
| 109 | + assertThat(shellRun("-touch", "-m", "-t", strTime, newFileName)).as( |
| 110 | + "Expected successful touch with a specified modification time").isEqualTo(0); |
| 111 | + // Verify if modification time is recorded correctly (and access time |
| 112 | + // remains unchanged). |
| 113 | + newStatus = dfs.getFileStatus(newFile); |
| 114 | + assertThat(newStatus.getAccessTime()).isEqualTo(fileStatus.getAccessTime()); |
| 115 | + assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime()); |
| 116 | + |
| 117 | + strTime = formatTimestamp(System.currentTimeMillis()); |
| 118 | + dateObj = parseTimestamp(strTime); |
| 119 | + |
| 120 | + assertThat(shellRun("-touch", "-t", strTime, newFileName)).as( |
| 121 | + "Expected successful touch with a specified timestamp").isEqualTo(0); |
| 122 | + |
| 123 | + // Verify if both modification and access times are recorded correctly |
| 124 | + newStatus = dfs.getFileStatus(newFile); |
| 125 | + assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime()); |
| 126 | + assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime()); |
| 127 | + |
| 128 | + strTime = formatTimestamp(System.currentTimeMillis()); |
| 129 | + dateObj = parseTimestamp(strTime); |
| 130 | + |
| 131 | + assertThat(shellRun("-touch", "-a", "-m", "-t", strTime, newFileName)).as( |
| 132 | + "Expected successful touch with a specified timestamp").isEqualTo(0); |
| 133 | + |
| 134 | + // Verify if both modification and access times are recorded correctly |
| 135 | + newStatus = dfs.getFileStatus(newFile); |
| 136 | + assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime()); |
| 137 | + assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime()); |
| 138 | + |
| 139 | + assertThat(shellRun("-touch", "-t", newFileName)).as( |
| 140 | + "Expected failed touch with a missing timestamp").isNotEqualTo(0); |
| 141 | + |
| 142 | + strTime = formatTimestamp(System.currentTimeMillis()); |
| 143 | + dateObj = parseTimestamp(strTime); |
| 144 | + assertThat(shellRun("-touch", "-c", "-t", strTime, newFileName)).as( |
| 145 | + "Expected successful touch on a non-existent file with -c option").isEqualTo(0); |
| 146 | + fileStatus = dfs.getFileStatus(newFile); |
| 147 | + assertThat(fileStatus.getAccessTime()).isEqualTo(dateObj.getTime()); |
| 148 | + assertThat(fileStatus.getModificationTime()).isEqualTo(dateObj.getTime()); |
| 149 | + |
| 150 | + dfs.delete(newFile, true); |
| 151 | + assertThat(dfs.exists(newFile)).isFalse(); |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void testTouchDirs() throws IOException, ParseException, InterruptedException { |
| 157 | + final String newFileName = "dir2/newFile2"; |
| 158 | + final Path newFile = new Path(newFileName); |
| 159 | + FileStatus newStatus; |
| 160 | + FileStatus fileStatus; |
| 161 | + String strTime; |
| 162 | + Date dateObj; |
| 163 | + Path dirPath = new Path("dir2"); |
| 164 | + dfs.mkdirs(dirPath); |
| 165 | + dfs.delete(newFile, true); |
| 166 | + assertThat(dfs.exists(newFile)).isFalse(); |
| 167 | + |
| 168 | + strTime = formatTimestamp(System.currentTimeMillis()); |
| 169 | + dateObj = parseTimestamp(strTime); |
| 170 | + |
| 171 | + assertThat(shellRun("-touch", "-t", strTime, newFileName)).as( |
| 172 | + "Expected successful touch on a new file with a specified timestamp").isEqualTo(0); |
| 173 | + newStatus = dfs.getFileStatus(newFile); |
| 174 | + assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime()); |
| 175 | + assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime()); |
| 176 | + |
| 177 | + Thread.sleep(500); |
| 178 | + strTime = formatTimestamp(System.currentTimeMillis()); |
| 179 | + dateObj = parseTimestamp(strTime); |
| 180 | + |
| 181 | + assertThat(shellRun("-touch", "-m", "-a", "-t", strTime, "dir2")).as( |
| 182 | + "Expected successful touch with a specified modification time").isEqualTo(0); |
| 183 | + |
| 184 | + newStatus = dfs.getFileStatus(dirPath); |
| 185 | + // Verify if both modification and access times are recorded correctly |
| 186 | + assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime()); |
| 187 | + assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime()); |
| 188 | + |
| 189 | + fileStatus = dfs.getFileStatus(dirPath); |
| 190 | + Thread.sleep(500); |
| 191 | + |
| 192 | + strTime = formatTimestamp(System.currentTimeMillis()); |
| 193 | + dateObj = parseTimestamp(strTime); |
| 194 | + |
| 195 | + assertThat(shellRun("-touch", "-m", "-t", strTime, "dir2")).as( |
| 196 | + "Expected successful touch with a specified modification time").isEqualTo(0); |
| 197 | + |
| 198 | + newStatus = dfs.getFileStatus(dirPath); |
| 199 | + // Verify if modification time is recorded correctly (and access time |
| 200 | + // remains unchanged). |
| 201 | + assertThat(newStatus.getAccessTime()).isEqualTo(fileStatus.getAccessTime()); |
| 202 | + assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime()); |
| 203 | + |
| 204 | + fileStatus = dfs.getFileStatus(dirPath); |
| 205 | + Thread.sleep(500); |
| 206 | + |
| 207 | + strTime = formatTimestamp(System.currentTimeMillis()); |
| 208 | + dateObj = parseTimestamp(strTime); |
| 209 | + |
| 210 | + assertThat(shellRun("-touch", "-a", "-t", strTime, "dir2")).as( |
| 211 | + "Expected successful touch with a specified modification time").isEqualTo(0); |
| 212 | + |
| 213 | + newStatus = dfs.getFileStatus(dirPath); |
| 214 | + // Verify if access time is recorded correctly (and modification time |
| 215 | + // remains unchanged). |
| 216 | + assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime()); |
| 217 | + assertThat(newStatus.getModificationTime()).isEqualTo(fileStatus.getModificationTime()); |
| 218 | + |
| 219 | + dfs.delete(newFile, true); |
| 220 | + dfs.delete(dirPath, true); |
| 221 | + assertThat(dfs.exists(newFile)).isFalse(); |
| 222 | + assertThat(dfs.exists(dirPath)).isFalse(); |
| 223 | + } |
| 224 | + |
| 225 | + private int shellRun(String... args) { |
| 226 | + int exitCode = shell.run(args); |
| 227 | + LOG.info("exit " + exitCode + " - " + StringUtils.join(" ", args)); |
| 228 | + return exitCode; |
| 229 | + } |
| 230 | + |
| 231 | + private String formatTimestamp(long timeInMillis) { |
| 232 | + return (new TouchCommands.Touch()).getDateFormat().format(new Date(timeInMillis)); |
| 233 | + } |
| 234 | + |
| 235 | + private Date parseTimestamp(String tstamp) throws ParseException { |
| 236 | + return (new TouchCommands.Touch()).getDateFormat().parse(tstamp); |
| 237 | + } |
| 238 | + |
| 239 | +} |
0 commit comments