Added: hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/native_mini_dfs.h URL: http://svn.apache.org/viewvc/hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/native_mini_dfs.h?rev=1602280&view=auto ============================================================================== --- hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/native_mini_dfs.h (added) +++ hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/native_mini_dfs.h Thu Jun 12 19:56:23 2014 @@ -0,0 +1,122 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIBHDFS_NATIVE_MINI_DFS_H +#define LIBHDFS_NATIVE_MINI_DFS_H + +#include <jni.h> /* for jboolean */ + +struct hdfsBuilder; +struct NativeMiniDfsCluster; + +/** + * Represents a configuration to use for creating a Native MiniDFSCluster + */ +struct NativeMiniDfsConf { + /** + * Nonzero if the cluster should be formatted prior to startup. + */ + jboolean doFormat; + + /** + * Whether or not to enable webhdfs in MiniDfsCluster + */ + jboolean webhdfsEnabled; + + /** + * The http port of the namenode in MiniDfsCluster + */ + jint namenodeHttpPort; + + /** + * Nonzero if we should configure short circuit. + */ + jboolean configureShortCircuit; +}; + +/** + * Create a NativeMiniDfsBuilder + * + * @param conf (inout) The cluster configuration + * + * @return a NativeMiniDfsBuilder, or a NULL pointer on error. + */ +struct NativeMiniDfsCluster* nmdCreate(struct NativeMiniDfsConf *conf); + +/** + * Wait until a MiniDFSCluster comes out of safe mode. + * + * @param cl The cluster + * + * @return 0 on success; a non-zero error code if the cluster fails to + * come out of safe mode. + */ +int nmdWaitClusterUp(struct NativeMiniDfsCluster *cl); + +/** + * Shut down a NativeMiniDFS cluster + * + * @param cl The cluster + * + * @return 0 on success; a non-zero error code if an exception is + * thrown. + */ +int nmdShutdown(struct NativeMiniDfsCluster *cl); + +/** + * Destroy a Native MiniDFSCluster + * + * @param cl The cluster to destroy + */ +void nmdFree(struct NativeMiniDfsCluster* cl); + +/** + * Get the port that's in use by the given (non-HA) nativeMiniDfs + * + * @param cl The initialized NativeMiniDfsCluster + * + * @return the port, or a negative error code + */ +int nmdGetNameNodePort(const struct NativeMiniDfsCluster *cl); + +/** + * Get the http address that's in use by the given (non-HA) nativeMiniDfs + * + * @param cl The initialized NativeMiniDfsCluster + * @param port Used to capture the http port of the NameNode + * of the NativeMiniDfsCluster + * @param hostName Used to capture the http hostname of the NameNode + * of the NativeMiniDfsCluster + * + * @return 0 on success; a non-zero error code if failing to + * get the information. + */ +int nmdGetNameNodeHttpAddress(const struct NativeMiniDfsCluster *cl, + int *port, const char **hostName); + +/** + * Configure the HDFS builder appropriately to connect to this cluster. + * + * @param bld The hdfs builder + * + * @return the port, or a negative error code + */ +int nmdConfigureHdfsBuilder(struct NativeMiniDfsCluster *cl, + struct hdfsBuilder *bld); + +#endif
Added: hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/posix_util.c URL: http://svn.apache.org/viewvc/hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/posix_util.c?rev=1602280&view=auto ============================================================================== --- hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/posix_util.c (added) +++ hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/posix_util.c Thu Jun 12 19:56:23 2014 @@ -0,0 +1,157 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "test/posix_util.h" + +#include <dirent.h> +#include <errno.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> +#include <limits.h> + +static pthread_mutex_t gTempdirLock = PTHREAD_MUTEX_INITIALIZER; + +static int gTempdirNonce = 0; + +int recursiveDeleteContents(const char *path) +{ + int ret; + DIR *dp; + struct dirent *de; + char tmp[PATH_MAX]; + + dp = opendir(path); + if (!dp) { + ret = -errno; + fprintf(stderr, "recursiveDelete(%s) failed with error %d\n", path, ret); + return ret; + } + while (1) { + de = readdir(dp); + if (!de) { + ret = 0; + break; + } + if ((de->d_name[0] == '.') && (de->d_name[1] == '\0')) + continue; + if ((de->d_name[0] == '.') && (de->d_name[1] == '.') && + (de->d_name[2] == '\0')) + continue; + snprintf(tmp, sizeof(tmp), "%s/%s", path, de->d_name); + ret = recursiveDelete(tmp); + if (ret) + break; + } + if (closedir(dp)) { + ret = -errno; + fprintf(stderr, "recursiveDelete(%s): closedir failed with " + "error %d\n", path, ret); + return ret; + } + return ret; +} + +/* + * Simple recursive delete implementation. + * It could be optimized, but there is no need at the moment. + * TODO: use fstat, etc. + */ +int recursiveDelete(const char *path) +{ + int ret; + struct stat stBuf; + + ret = stat(path, &stBuf); + if (ret != 0) { + ret = -errno; + fprintf(stderr, "recursiveDelete(%s): stat failed with " + "error %d\n", path, ret); + return ret; + } + if (S_ISDIR(stBuf.st_mode)) { + ret = recursiveDeleteContents(path); + if (ret) + return ret; + ret = rmdir(path); + if (ret) { + ret = errno; + fprintf(stderr, "recursiveDelete(%s): rmdir failed with error %d\n", + path, ret); + return ret; + } + } else { + ret = unlink(path); + if (ret) { + ret = -errno; + fprintf(stderr, "recursiveDelete(%s): unlink failed with " + "error %d\n", path, ret); + return ret; + } + } + return 0; +} + +int createTempDir(char *tempDir, int nameMax, int mode) +{ + char tmp[PATH_MAX]; + int pid, nonce; + const char *base = getenv("TMPDIR"); + if (!base) + base = "/tmp"; + if (base[0] != '/') { + // canonicalize non-absolute TMPDIR + if (realpath(base, tmp) == NULL) { + return -errno; + } + base = tmp; + } + pid = getpid(); + pthread_mutex_lock(&gTempdirLock); + nonce = gTempdirNonce++; + pthread_mutex_unlock(&gTempdirLock); + snprintf(tempDir, nameMax, "%s/temp.%08d.%08d", base, pid, nonce); + if (mkdir(tempDir, mode) == -1) { + int ret = errno; + return -ret; + } + return 0; +} + +void sleepNoSig(int sec) +{ + int ret; + struct timespec req, rem; + + rem.tv_sec = sec; + rem.tv_nsec = 0; + do { + req = rem; + ret = nanosleep(&req, &rem); + } while ((ret == -1) && (errno == EINTR)); + if (ret == -1) { + ret = errno; + fprintf(stderr, "nanosleep error %d (%s)\n", ret, strerror(ret)); + } +} + +// vim: ts=4:sw=4:tw=79:et Added: hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/posix_util.h URL: http://svn.apache.org/viewvc/hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/posix_util.h?rev=1602280&view=auto ============================================================================== --- hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/posix_util.h (added) +++ hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/posix_util.h Thu Jun 12 19:56:23 2014 @@ -0,0 +1,58 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __POSIX_UTIL_H__ +#define __POSIX_UTIL_H__ + +/** + * Recursively delete the contents of a directory + * + * @param path directory whose contents we should delete + * + * @return 0 on success; error code otherwise + */ +int recursiveDeleteContents(const char *path); + +/** + * Recursively delete a local path, using unlink or rmdir as appropriate. + * + * @param path path to delete + * + * @return 0 on success; error code otherwise + */ +int recursiveDelete(const char *path); + +/** + * Get a temporary directory + * + * @param tempDir (out param) path to the temporary directory + * @param nameMax Length of the tempDir buffer + * @param mode Mode to create with + * + * @return 0 on success; error code otherwise + */ +int createTempDir(char *tempDir, int nameMax, int mode); + +/** + * Sleep without using signals + * + * @param sec Number of seconds to sleep + */ +void sleepNoSig(int sec); + +#endif Added: hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/test.c URL: http://svn.apache.org/viewvc/hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/test.c?rev=1602280&view=auto ============================================================================== --- hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/test.c (added) +++ hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/test.c Thu Jun 12 19:56:23 2014 @@ -0,0 +1,136 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "test/test.h" + +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <stdarg.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/types.h> + +void fail(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + exit(1); +} + +static int vexpect_decode(const char *expected, const char *text, int ty, + const char *str) +{ + switch (ty) { + case TEST_ERROR_EQ: + if (strcmp(expected, str) != 0) { + fprintf(stderr, "error: expected '%s', but got '%s', which " + "is not equal. %s\n", expected, str, text); + return -1; + } + break; + case TEST_ERROR_GE: + if (strcmp(expected, str) > 0) { + fprintf(stderr, "error: expected '%s', but got '%s'. " + "Expected something greater or equal. %s\n", + expected, str, text); + return -1; + } + break; + case TEST_ERROR_GT: + if (strcmp(expected, str) >= 0) { + fprintf(stderr, "error: expected '%s', but got '%s'. " + "Expected something greater. %s\n", + expected, str, text); + return -1; + } + break; + case TEST_ERROR_LE: + if (strcmp(expected, str) < 0) { + fprintf(stderr, "error: expected '%s', but got '%s'. " + "Expected something less or equal. %s\n", + expected, str, text); + return -1; + } + break; + case TEST_ERROR_LT: + if (strcmp(expected, str) <= 0) { + fprintf(stderr, "error: expected '%s', but got '%s'. " + "Expected something less. %s\n", + expected, str, text); + return -1; + } + break; + case TEST_ERROR_NE: + if (strcmp(expected, str) == 0) { + fprintf(stderr, "error: did not expect '%s': '%s'\n", + expected, text); + return -1; + } + break; + default: + fprintf(stderr, "runtime error: invalid type %d passed in: '%s'\n", + ty, text); + return -1; + } + return 0; +} + +int vexpect(const char *expected, const char *text, int ty, + const char *fmt, va_list ap) +{ + char *str = NULL; + int ret; + + if (vasprintf(&str, fmt, ap) < 0) { // TODO: portability + fprintf(stderr, "error: vasprintf failed: %s\n", text); + return -1; + } + ret = vexpect_decode(expected, text, ty, str); + free(str); + return ret; +} + +int expect(const char *expected, const char *text, int ty, + const char *fmt, ...) +{ + int ret; + va_list ap; + + va_start(ap, fmt); + ret = vexpect(expected, text, ty, fmt, ap); + va_end(ap); + return ret; +} + +void *xcalloc(size_t len) +{ + void *v = calloc(1, len); + if (!v) { + abort(); + } + return v; +} + +// vim: ts=4:sw=4:tw=79:et Added: hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/test.h URL: http://svn.apache.org/viewvc/hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/test.h?rev=1602280&view=auto ============================================================================== --- hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/test.h (added) +++ hadoop/common/branches/HADOOP-10388/hadoop-native-core/src/main/native/test/test.h Thu Jun 12 19:56:23 2014 @@ -0,0 +1,170 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HADOOP_CORE_COMMON_TEST_H +#define HADOOP_CORE_COMMON_TEST_H + +#include <errno.h> /* for errno */ +#include <inttypes.h> /* for PRIdPTR */ +#include <stdarg.h> /* for va_list */ +#include <unistd.h> /* for size_t */ + +#define TEST_ERROR_EQ 0 +#define TEST_ERROR_GE 1 +#define TEST_ERROR_GT 2 +#define TEST_ERROR_LE 3 +#define TEST_ERROR_LT 4 +#define TEST_ERROR_NE 5 + +/** + * Fail with an error message. + * + * @param fmt printf-style format string. + * @param ... printf-style arguments. + */ +void fail(const char *fmt, ...); + +/** + * Check for a test condition. + * + * @param expected The string which is expected. + * @param text Some text that will be printed out if the test + * condition fails. + * @param ty Comparison type. See TEST_ERROR_* constants. + * @param fmt printf-style format string. + * @param ap printf-style arguments. + * + * @return 0 on success; 1 on failure. + */ +int vexpect(const char *expected, const char *text, int ty, + const char *fmt, va_list ap); + +/** + * Check for a test condition. + * + * @param expected The string which is expected. + * @param text Some text that will be printed out if the test + * condition fails. + * @param ty Comparison type. See TEST_ERROR_* constants. + * @param fmt printf-style format string. + * @param ... printf-style arguments. + * + * @return 0 on success; 1 on failure. + */ +int expect(const char *expected, const char *text, int ty, + const char *fmt, ...); + +/** + * Allocate a zero-initialized region of memory, or die. + * + * @param len The length + * + * @return A pointer to a zero-initialized malloc'ed region. + */ +void *xcalloc(size_t len); + +#define TEST_ERROR_GET_LINE_HELPER2(x) #x +#define TEST_ERROR_GET_LINE_HELPER(x) TEST_ERROR_GET_LINE_HELPER2(x) +#define TEST_ERROR_LOCATION_TEXT __FILE__ " at line " \ + TEST_ERROR_GET_LINE_HELPER(__LINE__) + +#define EXPECT(...) do { if (expect(__VA_ARGS__)) return 1; } while (0); + +#define EXPECT_EQ(expected, fmt, ...) \ + EXPECT(expected, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \ + fmt, __VA_ARGS__); + +#define EXPECT_STR_EQ(expected, str) \ + EXPECT_EQ(expected, "%s", str) + +#define EXPECT_GE(expected, fmt, ...) \ + EXPECT(expected, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_GE, \ + fmt, __VA_ARGS__); + +#define EXPECT_GT(expected, fmt, ...) \ + EXPECT(expected, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_GT, \ + fmt, __VA_ARGS__); + +#define EXPECT_LE(expected, fmt, ...) \ + EXPECT(expected, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_LE, \ + fmt, __VA_ARGS__); + +#define EXPECT_LT(expected, fmt, ...) \ + EXPECT(expected, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_LT, \ + fmt, __VA_ARGS__); + +#define EXPECT_NE(expected, fmt, ...) \ + EXPECT(expected, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_NE, \ + fmt, __VA_ARGS__); + +#define COMMON_TEST__TO_STR(x) #x +#define COMMON_TEST__TO_STR2(x) COMMON_TEST__TO_STR(x) + +#define EXPECT_INT_EQ(expected, x) do { \ + char expected_buf[16] = { 0 }; \ + snprintf(expected_buf, sizeof(expected_buf), "%d", expected); \ + EXPECT(expected_buf, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, "%d", x); \ +} while(0); + +#define EXPECT_INT64_EQ(expected, x) do { \ + char expected_buf[32] = { 0 }; \ + snprintf(expected_buf, sizeof(expected_buf), "%" PRId64, expected); \ + EXPECT(expected_buf, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \ + "%" PRId64, x); \ +} while(0); + +#define EXPECT_NO_HADOOP_ERR(expr) do { \ + struct hadoop_err *err = expr; \ + EXPECT("", TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \ + "%s", (err ? hadoop_err_msg(err) : "")); \ +} while(0); + +#define EXPECT_INT_ZERO(x) \ + EXPECT("0", TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \ + "%d", x); + +#define EXPECT_TRUE(x) \ + EXPECT("1", TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \ + "%d", (!!x)); + +#define EXPECT_FALSE(x) \ + EXPECT("0", TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \ + "%d", (!!x)); + +#define EXPECT_NULL(x) \ + EXPECT("0", TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \ + "%"PRIdPTR, x); + +#define EXPECT_NONNULL(x) \ + EXPECT("0", TEST_ERROR_LOCATION_TEXT, TEST_ERROR_NE, \ + "%"PRIdPTR, x); + +#define EXPECT_NEGATIVE_ONE_WITH_ERRNO(expr, e) do { \ + char expected[80] = { 0 }; \ + int res, err; \ + snprintf(expected, sizeof(expected), "ret -1 with errno %d", e); \ + errno = 0; \ + res = expr; \ + err = errno; \ + EXPECT(expected, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \ + "ret %d with errno %d", res, err); \ +} while(0); + +#endif + +// vim: ts=4:sw=4:tw=79:et
