Merge pull request #6 from marineam/tests

Tests and bugfixes for cgpt next
diff --git a/cgpt/cgpt_next.c b/cgpt/cgpt_next.c
index 5840829..d121ca4 100644
--- a/cgpt/cgpt_next.c
+++ b/cgpt/cgpt_next.c
@@ -14,14 +14,14 @@
 char next_file_name[BUFSIZE];
 int next_priority, next_index;
 
-static int do_search(CgptNextParams *params, char *fileName) {
+static int do_search(CgptNextParams *params) {
   struct drive drive;
   uint32_t max_part;
   int gpt_retval;
   int priority, tries, successful;
   int i;
 
-  if (CGPT_OK != DriveOpen(fileName, &drive, O_RDONLY))
+  if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDONLY))
     return CGPT_FAILED;
 
   if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive.gpt))) {
@@ -41,8 +41,12 @@
     successful = GetSuccessful(&drive, PRIMARY, i);
 
     if (next_index == -1 || ((priority > next_priority) && (successful || tries))) {
-      strncpy(next_file_name, fileName, BUFSIZE);
-      next_priority = priority;
+      strncpy(next_file_name, params->drive_name, BUFSIZE);
+      if (successful || tries) {
+        next_priority = priority;
+      } else {
+        next_priority = -1;
+      }
       next_index = i;
     }
   }
@@ -75,7 +79,8 @@
       continue;
 
     if ((pathname = IsWholeDev(partname))) {
-      do_search(params, pathname);
+      params->drive_name = pathname;
+      do_search(params);
     }
   }
 
@@ -94,7 +99,11 @@
   if (params == NULL)
     return CGPT_FAILED;
 
-  scan_real_devs(params);
+  if (params->drive_name) {
+    do_search(params);
+  } else {
+    scan_real_devs(params);
+  }
 
   if (strlen(next_file_name) < 0) {
     return CGPT_FAILED;
diff --git a/cgpt/cmd_next.c b/cgpt/cmd_next.c
index 9b34e15..6c4306e 100644
--- a/cgpt/cmd_next.c
+++ b/cgpt/cmd_next.c
@@ -34,7 +34,7 @@
   int errorcnt = 0;
 
   opterr = 0;                     // quiet, you
-  while ((c=getopt(argc, argv, ":hi:b:p")) != -1)
+  while ((c=getopt(argc, argv, ":h")) != -1)
   {
     switch (c)
     {
@@ -60,6 +60,10 @@
     return CGPT_FAILED;
   }
 
+  if (optind < argc) {
+    params.drive_name = argv[optind];
+  }
+
   // TODO: handle drive types to make this generic
 
   return CgptNext(&params);
diff --git a/host/include/cgpt_params.h b/host/include/cgpt_params.h
index 4db80d5..a320b35 100644
--- a/host/include/cgpt_params.h
+++ b/host/include/cgpt_params.h
@@ -72,6 +72,7 @@
 } CgptPrioritizeParams;
 
 typedef struct CgptNextParams {
+  char *drive_name;
   char *drive_type;
 } CgptNextParams;
 
diff --git a/tests/run_cgpt_tests.sh b/tests/run_cgpt_tests.sh
index b9029bc..ca815be 100755
--- a/tests/run_cgpt_tests.sh
+++ b/tests/run_cgpt_tests.sh
@@ -139,6 +139,42 @@
 [ "$X" = "$Y" ] || error
 
 
+echo "Test the cgpt next command..."
+ROOT_A=562DE070-1539-4EDF-AC33-B1028227D525
+ROOT_B=839C1172-5036-4EFE-9926-7074340D5772
+expect_next() {
+  local root=$($CGPT next $DEV)
+  [ "$root" == "$1" ] || error 1 "expected next to be $1 but got $root"
+}
+
+# Basic state, one good rootfs
+$CGPT create $DEV || error
+$CGPT add -i 1 -t coreos-rootfs -u $ROOT_A -b 100 -s 1 -P 1 -S 1 $DEV || error
+$CGPT add -i 2 -t coreos-rootfs -u $ROOT_B -b 101 -s 1 -P 0 -S 0 $DEV || error
+expect_next $ROOT_A
+expect_next $ROOT_A
+
+# Try the other order
+$CGPT add -i 1 -P 0 -S 0 $DEV || error
+$CGPT add -i 2 -P 1 -S 1 $DEV || error
+expect_next $ROOT_B
+expect_next $ROOT_B
+
+# Try B, fall back to A
+$CGPT add -i 1 -P 0 -S 1 -T 0 $DEV || error
+$CGPT add -i 2 -P 1 -S 0 -T 1 $DEV || error
+expect_next $ROOT_B
+expect_next $ROOT_A
+expect_next $ROOT_A
+
+# Try A, fall back to B
+$CGPT add -i 1 -P 1 -S 0 -T 1 $DEV || error
+$CGPT add -i 2 -P 0 -S 1 -T 0 $DEV || error
+expect_next $ROOT_A
+expect_next $ROOT_B
+expect_next $ROOT_B
+
+
 echo "Test the cgpt prioritize command..."
 
 # Input: sequence of priorities