From 25a090279f9c14e4716882d71fca248ecbcf9e07 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Thu, 23 May 2013 13:57:57 +0100 Subject: [PATCH] [thin_rmap] region parsing --- features/step_definitions/thin_steps.rb | 4 ++ features/thin_rmap.feature | 60 +++++++++++++++++++++++++ thin-provisioning/thin_rmap.cc | 25 ++++++++++- 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/features/step_definitions/thin_steps.rb b/features/step_definitions/thin_steps.rb index 3f26a46..37ae174 100644 --- a/features/step_definitions/thin_steps.rb +++ b/features/step_definitions/thin_steps.rb @@ -24,3 +24,7 @@ When(/^I run thin_check with (.*?)$/) do |opts| run_simple("thin_check #{opts} #{dev_file}", false) end +When(/^I run thin_rmap with (.*?)$/) do |opts| + run_simple("thin_rmap #{opts} #{dev_file}", false) +end + diff --git a/features/thin_rmap.feature b/features/thin_rmap.feature index 46b2f48..24ba6a2 100644 --- a/features/thin_rmap.feature +++ b/features/thin_rmap.feature @@ -47,3 +47,63 @@ Feature: thin_rmap Scenario: Unrecognised option should cause failure When I run `thin_rmap --unleash-the-hedeghogs` Then it should fail + + Scenario: Valid region format should pass + Given valid metadata + When I run thin_rmap with --region 23..7890 + Then it should pass + + Scenario: Invalid region format should fail (comma instean of dots) + Given valid metadata + When I run thin_rmap with --region 23,7890 + Then it should fail + + Scenario: Invalid region format should fail (second number a word) + Given valid metadata + When I run thin_rmap with --region 23..six + Then it should fail + + Scenario: Invalid region format should fail (first number a word) + Given valid metadata + When I run thin_rmap with --region four..7890 + Then it should fail + + Scenario: Invalid region format should fail (end is lower than begin) + Given valid metadata + When I run thin_rmap with --region 89..88 + Then it should fail + + Scenario: Invalid region format should fail (end is equal to begin) + Given valid metadata + When I run thin_rmap with --region 89..89 + Then it should fail + + Scenario: Invalid region format should fail (no begin) + Given valid metadata + When I run thin_rmap with --region ..89 + Then it should fail + + Scenario: Invalid region format should fail (no end) + Given valid metadata + When I run thin_rmap with --region 89.. + Then it should fail + + Scenario: Invalid region format should fail (no region at all) + Given valid metadata + When I run thin_rmap with --region + Then it should fail + + Scenario: Invalid region format should fail (three dots) + Given valid metadata + When I run thin_rmap with --region 89...99 + Then it should fail + + Scenario: Multiple regions should pass + Given valid metadata + When I run thin_rmap with --region 1..23 --region 45..78 + Then it should pass + + + + + diff --git a/thin-provisioning/thin_rmap.cc b/thin-provisioning/thin_rmap.cc index 6a59411..53ccc25 100644 --- a/thin-provisioning/thin_rmap.cc +++ b/thin-provisioning/thin_rmap.cc @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "version.h" @@ -19,9 +20,30 @@ namespace { int rmap(string const &path, vector const ®ions) { cerr << "Not implemented" << endl; - return 1; + return 0; } + region parse_region(string const &str) { + istringstream in(str); + + char dots[2] = {'\0', '\0'}; + block_address begin, end; + + in >> begin; + in.read(dots, sizeof(dots)); + if (dots[0] != '.' || dots[1] != '.') + throw runtime_error("badly formed region (no dots)"); + in >> end; + + if (in.fail()) + throw runtime_error("badly formed region (couldn't parse numbers)"); + + if (end <= begin) + throw runtime_error("badly formed region (end <= begin)"); + + return region(begin, end); + }; + void usage(ostream &out, string const &cmd) { out << "Usage: " << cmd << " [options] {device|file}" << endl << "Options:" << endl @@ -60,6 +82,7 @@ int main(int argc, char **argv) case 1: // region + regions.push_back(parse_region(optarg)); break; default: