[cache_restore] --debug-override-metadata-version

A flag that deliberately causes the wrong metadata version to be
written.  Useful for testing the kernel module.
This commit is contained in:
Joe Thornber 2013-10-15 10:24:12 +01:00
parent c92aff63e2
commit f8633da296
4 changed files with 62 additions and 16 deletions

View File

@ -5,12 +5,14 @@
#include "caching/xml_format.h" #include "caching/xml_format.h"
#include "persistent-data/file_utils.h" #include "persistent-data/file_utils.h"
#include <boost/lexical_cast.hpp>
#include <fstream> #include <fstream>
#include <getopt.h> #include <getopt.h>
#include <iostream> #include <iostream>
#include <libgen.h> #include <libgen.h>
#include <string> #include <string>
using namespace boost;
using namespace caching; using namespace caching;
using namespace persistent_data; using namespace persistent_data;
using namespace std; using namespace std;
@ -18,14 +20,31 @@ using namespace std;
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace { namespace {
int restore(string const &xml_file, string const &dev) { struct flags {
flags()
: metadata_version(1),
override_metadata_version(false) {
}
optional<string> input;
optional<string> output;
uint32_t metadata_version;
bool override_metadata_version;
};
int restore(flags const &fs) {
try { try {
block_manager<>::ptr bm = open_bm(dev, block_io<>::READ_WRITE); block_manager<>::ptr bm = open_bm(*fs.output, block_io<>::READ_WRITE);
metadata::ptr md(new metadata(bm, metadata::CREATE)); metadata::ptr md(new metadata(bm, metadata::CREATE));
emitter::ptr restorer = create_restore_emitter(md); emitter::ptr restorer = create_restore_emitter(md);
check_file_exists(xml_file); if (fs.override_metadata_version) {
ifstream in(xml_file.c_str(), ifstream::in); cerr << "overriding" << endl;
md->sb_.version = fs.metadata_version;
}
check_file_exists(*fs.input);
ifstream in(fs.input->c_str(), ifstream::in);
parse_xml(in, restorer); parse_xml(in, restorer);
} catch (std::exception &e) { } catch (std::exception &e) {
@ -42,17 +61,21 @@ namespace {
<< " {-h|--help}" << endl << " {-h|--help}" << endl
<< " {-i|--input} <input xml file>" << endl << " {-i|--input} <input xml file>" << endl
<< " {-o|--output} <output device or file>" << endl << " {-o|--output} <output device or file>" << endl
<< " {-V|--version}" << endl; << " {-V|--version}" << endl
<< endl
<< " {--debug-override-metadata-version} <integer>" << endl;
} }
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int c; int c;
string input, output; flags fs;
char const *prog_name = basename(argv[0]); char const *prog_name = basename(argv[0]);
char const *short_opts = "hi:o:V"; char const *short_opts = "hi:o:V";
option const long_opts[] = { option const long_opts[] = {
{ "debug-override-metadata-version", required_argument, NULL, 0 },
{ "help", no_argument, NULL, 'h'}, { "help", no_argument, NULL, 'h'},
{ "input", required_argument, NULL, 'i' }, { "input", required_argument, NULL, 'i' },
{ "output", required_argument, NULL, 'o'}, { "output", required_argument, NULL, 'o'},
@ -62,16 +85,21 @@ int main(int argc, char **argv)
while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
switch(c) { switch(c) {
case 0:
fs.metadata_version = lexical_cast<uint32_t>(optarg);
fs.override_metadata_version = true;
break;
case 'h': case 'h':
usage(cout, prog_name); usage(cout, prog_name);
return 0; return 0;
case 'i': case 'i':
input = optarg; fs.input = optional<string>(string(optarg));
break; break;
case 'o': case 'o':
output = optarg; fs.output = optional<string>(string(optarg));
break; break;
case 'V': case 'V':
@ -89,20 +117,19 @@ int main(int argc, char **argv)
return 1; return 1;
} }
if (input.empty()) { if (!fs.input) {
cerr << "No input file provided." << endl << endl; cerr << "No input file provided." << endl << endl;
usage(cerr, prog_name); usage(cerr, prog_name);
return 1; return 1;
} }
if (output.empty()) { if (!fs.output) {
cerr << "No output file provided." << endl << endl; cerr << "No output file provided." << endl << endl;
usage(cerr, prog_name); usage(cerr, prog_name);
return 1; return 1;
} }
return restore(input, output); return restore(fs);
} }
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -55,7 +55,7 @@ namespace {
uint32_t const SUPERBLOCK_MAGIC = 06142003; uint32_t const SUPERBLOCK_MAGIC = 06142003;
uint32_t const VERSION_BEGIN = 1; uint32_t const VERSION_BEGIN = 1;
uint32_t const VERSION_END = 3; uint32_t const VERSION_END = 2;
} }
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -86,3 +86,9 @@ Feature: cache_check
When I run `cache_check metadata.bin` When I run `cache_check metadata.bin`
Then it should pass Then it should pass
Scenario: Invalid metadata version causes a fail
Given a small xml file
And input file
And I run cache_restore with -i metadata.xml -o input --debug-override-metadata-version 12345
When I run `cache_check input`
Then it should fail

View File

@ -9,7 +9,8 @@ Feature: thin_restore
Scenario: print help (-h) Scenario: print help (-h)
When I run cache_restore with -h When I run cache_restore with -h
Then it should pass with: Then it should pass
And the output should contain exactly:
""" """
Usage: cache_restore [options] Usage: cache_restore [options]
@ -18,11 +19,15 @@ Feature: thin_restore
{-i|--input} <input xml file> {-i|--input} <input xml file>
{-o|--output} <output device or file> {-o|--output} <output device or file>
{-V|--version} {-V|--version}
{--debug-override-metadata-version} <integer>
""" """
Scenario: print help (--help) Scenario: print help (--help)
When I run cache_restore with -h When I run cache_restore with -h
Then it should pass with: Then it should pass
And the output should contain exactly:
""" """
Usage: cache_restore [options] Usage: cache_restore [options]
@ -31,6 +36,9 @@ Feature: thin_restore
{-i|--input} <input xml file> {-i|--input} <input xml file>
{-o|--output} <output device or file> {-o|--output} <output device or file>
{-V|--version} {-V|--version}
{--debug-override-metadata-version} <integer>
""" """
Scenario: missing input file Scenario: missing input file
@ -53,9 +61,14 @@ Feature: thin_restore
No output file provided. No output file provided.
""" """
@announce
Scenario: successfully restores a valid xml file Scenario: successfully restores a valid xml file
Given a small xml file Given a small xml file
And an empty dev file And an empty dev file
When I run cache_restore with -i metadata.xml -o metadata.bin When I run cache_restore with -i metadata.xml -o metadata.bin
Then it should pass Then it should pass
Scenario: accepts --debug-override-metadata-version
Given a small xml file
And an empty dev file
When I run cache_restore with -i metadata.xml -o metadata.bin --debug-override-metadata-version 10298
Then it should pass