Building tests for journaling and backup

This commit is contained in:
jmjatlanta 2017-08-28 13:04:27 -05:00
parent 0066670f60
commit b3af783f4e
7 changed files with 356 additions and 28 deletions

76
test/config.test1 Normal file
View file

@ -0,0 +1,76 @@
{
"Identity": {
"PeerID": "QmZVoAZGFfinB7MQQiDzB84kWaDPQ95GLuXdemJFM2r9b4",
"PrivKey": "CAASpgkwggSiAgEAAoIBAQCApYAN/RWPZqfuLp96dB7wyR11r58Yy2v7l0ebAW/Sq5zBtg6Jrvmg5Nwx9lLUWa75VmwC6B+70cbdE7g+668VknjkyDxODYimGMsMeAcr0IKRludPjSYThbP3UOmeQdu+5ifSBcJ9Atsr/b1kNSntEyixm33boUIv+m2VI5rU9I81bzbPsnBEx4I4BDduJB/tJ0SSCQs3SQJdFtj+XoypZeShtPW/L2p8K9q/GI6dLNxHIKqJwaED6gBhmlRHAA9hqV3RJZ1Hh9ztYa9IM1k13A7SjvedDIdqrAIlAAUW2uKO1HiAEyLCykPqZsm/y8f4tfsHjnSk3RpDhaNlRGX5AgMBAAECggEAKWcaQ2ZEXhaHzVwr71kX7B6N4KZY6b3uTtZtMTHRL1gT5zQUkbNCm8mMJHhGSBlk+tIZiQXz0Jq/Cb+i+H0ZqDOvvNHEtsocRHiOQMEQbcV5gCm04CFpM7dAKEReK5S+iD3CaxF3eoxExW+wWGPTznzwB9knNFw91Psf8XO4DPkBAZfe3prmiKBDg74Cy1hAzBHGp15/4oU9gyaZop0Oe1UA+NEeGCTZgDcIOLYbPjH6tRF0xw2Usuh2IiwkUqrdRWwO0ZlaBD9jSCxQ4evbgIAOtni0+qbbeIlXQGPn3XFSZjwsdS7b/CPe8Gbtoy+akuoHOV9Rje/0ZAJTN4KjQQKBgQDgH76D7yr57Tcga18EoWJcGJvvvt/m0NfjtSoz901uK60qf5MnNbUXYKF33j5XW9V9+WRde0IqxZkwDIAuk6Bx/oYqI/DgvQ/Me/8np4R7K6Tjb3PNtHCGNtuGU2qJthCcCZ629lej3y5bwLtwfhjUwHSRzwQXlnJnHa49KZ1AvQKBgQCS8XY9aA6cBaCk8WDoTxMVSrr5P0QMKWbgHqylsUvZsTCj07+Ob87gnZjyGgPfIn9pF9gMeuExyHj/+c1P54Z0dPBeL4KQx1UpvgyJTLuFe1csR2Q8qAp6GOiEojXJ0Tc4yUsVwSKmCCsW9f2EiXCP24TqwedllfKoazvbaJZD7QKBgCjugzvTNTUZ1qSMF3lgA/ev4uLBKGEeKR+EGaYN9000mtaVIAncLCuN0U9z/ky0D+SG5IaSGT54ggadcdePzJClHFmb4MUaokUFLE9Qr8hS6zVCSJL77kUExcjdeSqXXqcwtpTRnqXl/7BwHYto7B4G3AM5HJU2O8SLTiQLcpW9AoGASXYl4rUu+Jlnoo56ICnIJJDjwM/8OQhJwioTrhJhFUgcRQsLbS5AgtLHL+IJpXgdsnCVNkgIZVcQBwjft97LvVTpBm1CHRdKYD//DHUsGqN7BUv7tMdd1YS2WfiuY2MQ9HbN7uZLaC4VPLlmMOh1ObZJUQP3pzW7xq81kY9L8FECgYA92LvEamYLzbisdb6XvA6d5+jUXtt3v8/2e1tNKSUuvBCLxWy5IqObgHTCaCHJH8E/bAEmOVaxXdzpS484Q8Hbai+NBVjnPB/yINCN4WgScBRFr3691HeQ1ejeEKSzELg1QDau7jo7CSZFO1ZHCR01T7HZCrcuLaG7DxAx/Y+i8A=="
},
"Datastore": {
"Type": "lmdb",
"Path": "/Users/JohnJones/.ipfs/datastore",
"StorageMax": "10GB",
"StorageGCWatermark": 90,
"GCPeriod": "1h",
"Params": null,
"NoSync": false,
"HashOnRead": false,
"BloomFilterSize": 0
},
"Addresses": {
"Swarm": [
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001"
],
"API": "(null)",
"Gateway": "(null)"
},
"Mounts": {
"IPFS": "/ipfs",
"IPNS": "/ipns",
"FuseAllowOther": false
},
"Discovery": {
"MDNS": {
"Enabled": true,
"Interval": 10
}
},
"Ipns": {
"RepublishedPeriod": "",
"RecordLifetime": "",
"ResolveCacheSize": 128
},
"Bootstrap": [
],
"Tour": {
"Last": ""
},
"Gateway": {
"HTTPHeaders": {
"Access-Control-Allow-Origin": [
"*"
],
"Access-Control-Allow-Methods": [
"GET"
],
"Access-Control-Allow-Headers": [
"X-Requested-With"
]
},
"RootRedirect": ""
"Writable": false
"PathPrefixes": []
},
"SupernodeRouting": {
"Servers": null
}, "API": {
"HTTPHeaders": null
},
"Swarm": {
"AddrFilters": null
},
"Replication": {
"AnnounceMinutes": "60",
"Nodes": [
"/ip4/127.0.0.1/tcp/4002/ipfs/QmcDW1t4QQBGAs2HSig8xkUhPxFZzmewAeFdsmp6q6nyY5"
]
}
}

76
test/config.test2 Normal file
View file

@ -0,0 +1,76 @@
{
"Identity": {
"PeerID": "QmcDW1t4QQBGAs2HSig8xkUhPxFZzmewAeFdsmp6q6nyY5",
"PrivKey": "CAASqQkwggSlAgEAAoIBAQDqghv+79zy4dcP+8GCSa9kfoD9zF1gpOO/gnfjLUd9BTzY1MXX7pzGsztRnLOFtuSAqZmg9iEgnrNbLpGqYdHxSRU7taHv2+aAjE4yU6epP9NWaS3kC06NRzpcH4M+sIXYheioEJrOb4OGnz5KZuAAmRu/0gg+HSHeZTVNz3cRgg1fvpeG68UGSjLhIXg4jYjCso0KOdTZDTKkVqFqDyKJOgDPEGF5sS024Z33Wpkn6+MTX0xIUfn4eKIJztmt7g7/2zi3OnsKmfj7lmh77R93F88hGN5i2g6/GYY6qfQ4HJ/RxEGzXtt+r+jNlV4zGFSAWfR7vBklkGnyL041ewRpAgMBAAECggEAQ/C8AakmfyuU7imHD6MfcGMCJgWOTBCZdwNqjmOw4XSiA0PpqbyUqTCGEAfxLt0k6G26d5q3UEcKOXGNfCpo85mz0B+4MdCowDdl9EiuO2elZ9SDGh14fY/URZpPoNrtfCSXmhFteD9wgJEkL9QMM8An/PPxtWUlSdRSlB92JJjWI5RiPx8u9jVNqsjp/t/Pe3oQ4HvuFXBmSjdRQh7uIvgKC28DMcuROZPRhCAcUcWS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"Params": null,
"NoSync": false,
"HashOnRead": false,
"BloomFilterSize": 0
},
"Addresses": {
"Swarm": [
"/ip4/0.0.0.0/tcp/4002",
"/ip6/::/tcp/4002"
],
"API": "(null)",
"Gateway": "(null)"
},
"Mounts": {
"IPFS": "/ipfs",
"IPNS": "/ipns",
"FuseAllowOther": false
},
"Discovery": {
"MDNS": {
"Enabled": true,
"Interval": 10
}
},
"Ipns": {
"RepublishedPeriod": "",
"RecordLifetime": "",
"ResolveCacheSize": 128
},
"Bootstrap": [
],
"Tour": {
"Last": ""
},
"Gateway": {
"HTTPHeaders": {
"Access-Control-Allow-Origin": [
"*"
],
"Access-Control-Allow-Methods": [
"GET"
],
"Access-Control-Allow-Headers": [
"X-Requested-With"
]
},
"RootRedirect": ""
"Writable": false
"PathPrefixes": []
},
"SupernodeRouting": {
"Servers": null
}, "API": {
"HTTPHeaders": null
},
"Swarm": {
"AddrFilters": null
},
"Replication": {
"AnnounceMinutes": "60",
"Nodes": [
"/ip4/127.0.0.1/tcp/4001/ipfs/QmZVoAZGFfinB7MQQiDzB84kWaDPQ95GLuXdemJFM2r9b4"
]
}
}

View file

@ -6,11 +6,6 @@
#include "../test_helper.h"
#include "libp2p/utils/logger.h"
void* test_daemon_start(void* arg) {
ipfs_daemon_start((char*)arg);
return NULL;
}
int test_daemon_startup_shutdown() {
int retVal = 0;
pthread_t daemon_thread;

View file

@ -67,3 +67,75 @@ int test_journal_encode_decode() {
//ipfs_journal_entry_free(result_entry);
return retVal;
}
/***
* Starts a server with a file in it with a specific set of files.
* It also has a specific address, with a config file of another
* known peer to replicate between
*/
int test_journal_server_1() {
int retVal = 0;
pthread_t daemon_thread;
int thread_started = 0;
char* ipfs_path = "/tmp/ipfs_1";
char* config_file = "config.test1";
struct FSRepo* fs_repo = NULL;
libp2p_logger_add_class("test_journal");
if (!drop_build_open_repo(ipfs_path, &fs_repo, config_file)) {
ipfs_repo_fsrepo_free(fs_repo);
libp2p_logger_error("test_journal", "Unable to drop and build repository at %s\n", ipfs_path);
goto exit;
}
libp2p_logger_debug("test_journal", "Changed the server id to %s.\n", fs_repo->config->identity->peer->id);
ipfs_repo_fsrepo_free(fs_repo);
pthread_create(&daemon_thread, NULL, test_daemon_start, (void*)ipfs_path);
thread_started = 1;
retVal = 1;
exit:
ipfs_daemon_stop();
if (thread_started)
pthread_join(daemon_thread, NULL);
return retVal;
}
/***
* Starts a server with a specific set of files.
* It also has a specific address, with a config file of another
* known peer to replicate between
*/
int test_journal_server_2() {
int retVal = 0;
pthread_t daemon_thread;
int thread_started = 0;
char* ipfs_path = "/tmp/ipfs_2";
char* config_file = "config.test2";
struct FSRepo* fs_repo = NULL;
libp2p_logger_add_class("test_journal");
if (!drop_build_open_repo(ipfs_path, &fs_repo, config_file)) {
ipfs_repo_fsrepo_free(fs_repo);
libp2p_logger_error("test_journal", "Unable to drop and build repository at %s\n", ipfs_path);
goto exit;
}
libp2p_logger_debug("test_journal", "Changed the server id to %s.\n", fs_repo->config->identity->peer->id);
ipfs_repo_fsrepo_free(fs_repo);
pthread_create(&daemon_thread, NULL, test_daemon_start, (void*)ipfs_path);
thread_started = 1;
retVal = 1;
exit:
ipfs_daemon_stop();
if (thread_started)
pthread_join(daemon_thread, NULL);
return retVal;
}

View file

@ -1,11 +1,74 @@
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "ipfs/repo/init.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
#include "ipfs/core/daemon.h"
#include "libp2p/os/utils.h"
int cp(const char *to, const char *from)
{
int fd_to, fd_from;
char buf[4096];
ssize_t nread;
int saved_errno;
fd_from = open(from, O_RDONLY);
if (fd_from < 0)
return -1;
fd_to = open(to, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd_to < 0)
goto out_error;
while (nread = read(fd_from, buf, sizeof buf), nread > 0)
{
char *out_ptr = buf;
ssize_t nwritten;
do {
nwritten = write(fd_to, out_ptr, nread);
if (nwritten >= 0)
{
nread -= nwritten;
out_ptr += nwritten;
}
else if (errno != EINTR)
{
goto out_error;
}
} while (nread > 0);
}
if (nread == 0)
{
if (close(fd_to) < 0)
{
fd_to = -1;
goto out_error;
}
close(fd_from);
/* Success! */
return 0;
}
out_error:
saved_errno = errno;
close(fd_from);
if (fd_to >= 0)
close(fd_to);
errno = saved_errno;
return -1;
}
/***
* Helper to create a test file in the OS
*/
@ -104,26 +167,6 @@ int drop_repository(const char* path) {
if (os_utils_file_exists(path)) {
return remove_directory(path);
/*
// the config file
if (!os_utils_filepath_join(path, "config", currDirectory, 1024))
return 0;
unlink(currDirectory);
// the datastore directory
if (!os_utils_filepath_join(path, "datastore", currDirectory, 1024))
return 0;
if (!remove_directory(currDirectory))
return 0;
// the blockstore directory
if (!os_utils_filepath_join(path, "blockstore", currDirectory, 1024))
return 0;
if (!remove_directory(currDirectory))
return 0;
return remove_directory(path);
*/
}
return 1;
@ -149,12 +192,33 @@ int drop_and_build_repository(const char* path, int swarm_port, struct Libp2pVec
return make_ipfs_repository(path, swarm_port, bootstrap_peers, peer_id);
}
int drop_build_and_open_repo(const char* path, struct FSRepo** fs_repo) {
/***
* Drop a repository and build a new one with the specified config file
* @param path where to create it
* @param fs_repo the results
* @param config_file_to_copy where to find the config file to copy
* @returns true(1) on success, otherwise false(0)
*/
int drop_build_open_repo(const char* path, struct FSRepo** fs_repo, const char* config_filename_to_copy) {
if (!drop_and_build_repository(path, 4001, NULL, NULL))
return 0;
if (config_filename_to_copy != NULL) {
// attach config filename to path
char config[strlen(path) + 7];
strcpy(config, path);
// erase slash if there is one
if (config[strlen(path)-1] == '/')
config[strlen(path)-1] = 0;
strcat(config, "/config");
// delete the old
if (unlink(config) != 0)
return 0;
// copy pre-built config file into directory
if (cp(config, config_filename_to_copy) < 0)
return 0;
}
if (!ipfs_repo_fsrepo_new(path, NULL, fs_repo))
return 0;
@ -166,3 +230,22 @@ int drop_build_and_open_repo(const char* path, struct FSRepo** fs_repo) {
return 1;
}
/***
* Drop a repository and build a new one
* @param path where to create it
* @param fs_repo the results
* @returns true(1) on success, otherwise false(0)
*/
int drop_build_and_open_repo(const char* path, struct FSRepo** fs_repo) {
return drop_build_open_repo(path, fs_repo, NULL);
}
/***
* Start a daemon (usefull in a separate thread)
* @param arg a char string of the repo path
* @returns NULL
*/
void* test_daemon_start(void* arg) {
ipfs_daemon_start((char*)arg);
return NULL;
}

View file

@ -15,8 +15,23 @@ int drop_repository(const char* path);
*/
int drop_and_build_repository(const char* dir, int swarm_port, struct Libp2pVector* bootstrap_peers, char** peer_id);
/***
* Drop a repository and build a new one
* @param path where to create it
* @param fs_repo the results
* @returns true(1) on success, otherwise false(0)
*/
int drop_build_and_open_repo(const char* path, struct FSRepo** fs_repo);
/***
* Drop a repository and build a new one with the specified config file
* @param path where to create it
* @param fs_repo the results
* @param config_file_to_copy where to find the config file to copy
* @returns true(1) on success, otherwise false(0)
*/
int drop_build_open_repo(const char* path, struct FSRepo** fs_repo, const char* config_file_to_copy);
/***
* Helper to create a test file in the OS
* @pram fileName the resultant file name
@ -31,3 +46,10 @@ int create_file(const char* fileName, unsigned char* bytes, size_t num_bytes);
* @param num_bytes how much to fill of the buffer
*/
int create_bytes(unsigned char* buffer, size_t num_bytes);
/***
* Start a daemon (usefull in a separate thread)
* @param arg a char string of the repo path
* @returns NULL
*/
void* test_daemon_start(void* arg);

View file

@ -48,6 +48,8 @@ const char* names[] = {
"test_daemon_startup_shutdown",
"test_datastore_list_journal",
"test_journal_encode_decode",
"test_journal_server_1",
"test_journal_server_2",
"test_repo_config_new",
"test_repo_config_init",
"test_repo_config_write",
@ -106,6 +108,8 @@ int (*funcs[])(void) = {
test_daemon_startup_shutdown,
test_datastore_list_journal,
test_journal_encode_decode,
test_journal_server_1,
test_journal_server_2,
test_repo_config_new,
test_repo_config_init,
test_repo_config_write,