Browse code

Merge "Add vercmp function"

Jenkins authored on 2016/01/29 14:21:09
Showing 3 changed files
... ...
@@ -529,12 +529,58 @@ function vercmp_numbers {
529 529
     typeset v1=$1 v2=$2 sep
530 530
     typeset -a ver1 ver2
531 531
 
532
+    deprecated "vercmp_numbers is deprecated for more generic vercmp"
533
+
532 534
     IFS=. read -ra ver1 <<< "$v1"
533 535
     IFS=. read -ra ver2 <<< "$v2"
534 536
 
535 537
     _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
536 538
 }
537 539
 
540
+# vercmp ver1 op ver2
541
+#  Compare VER1 to VER2
542
+#   - op is one of < <= == >= >
543
+#   - returns true if satisified
544
+#  e.g.
545
+#  if vercmp 1.0 "<" 2.0; then
546
+#    ...
547
+#  fi
548
+function vercmp {
549
+    local v1=$1
550
+    local op=$2
551
+    local v2=$3
552
+    local result
553
+
554
+    # sort the two numbers with sort's "-V" argument.  Based on if v2
555
+    # swapped places with v1, we can determine ordering.
556
+    result=$(echo -e "$v1\n$v2" | sort -V | head -1)
557
+
558
+    case $op in
559
+        "==")
560
+            [ "$v1" = "$v2" ]
561
+            return
562
+            ;;
563
+        ">")
564
+            [ "$v1" != "$v2" ] && [ "$result" = "$v2" ]
565
+            return
566
+            ;;
567
+        "<")
568
+            [ "$v1" != "$v2" ] && [ "$result" = "$v1" ]
569
+            return
570
+            ;;
571
+        ">=")
572
+            [ "$result" = "$v2" ]
573
+            return
574
+            ;;
575
+        "<=")
576
+            [ "$result" = "$v1" ]
577
+            return
578
+            ;;
579
+        *)
580
+            die $LINENO "unrecognised op: $op"
581
+            ;;
582
+    esac
583
+}
538 584
 
539 585
 # This function sets log formatting options for colorizing log
540 586
 # output to stdout. It is meant to be called by lib modules.
541 587
new file mode 100755
... ...
@@ -0,0 +1,47 @@
0
+#!/usr/bin/env bash
1
+
2
+# Tests for DevStack vercmp functionality
3
+
4
+TOP=$(cd $(dirname "$0")/.. && pwd)
5
+
6
+# Import common functions
7
+source $TOP/functions
8
+source $TOP/tests/unittest.sh
9
+
10
+assert_true "numeric gt"  vercmp 2.0 ">" 1.0
11
+assert_true "numeric gte" vercmp 2.0 ">=" 1.0
12
+assert_true "numeric gt"  vercmp 1.0.1 ">" 1.0
13
+assert_true "numeric gte" vercmp 1.0.1 ">=" 1.0
14
+assert_true "alpha gt"    vercmp 1.0.1b ">" 1.0.1a
15
+assert_true "alpha gte"   vercmp 1.0.1b ">=" 1.0.1a
16
+assert_true "alpha gt"    vercmp b ">" a
17
+assert_true "alpha gte"   vercmp b ">=" a
18
+assert_true "alpha gt"    vercmp 2.0-rc3 ">" 2.0-rc1
19
+assert_true "alpha gte"   vercmp 2.0-rc3 ">=" 2.0-rc1
20
+
21
+assert_false "numeric gt fail"  vercmp 1.0 ">" 1.0
22
+assert_true  "numeric gte"      vercmp 1.0 ">=" 1.0
23
+assert_false "numeric gt fail"  vercmp 0.9 ">" 1.0
24
+assert_false "numeric gte fail" vercmp 0.9 ">=" 1.0
25
+assert_false "numeric gt fail"  vercmp 0.9.9 ">" 1.0
26
+assert_false "numeric gte fail" vercmp 0.9.9 ">=" 1.0
27
+assert_false "numeric gt fail"  vercmp 0.9a.9 ">" 1.0.1
28
+assert_false "numeric gte fail" vercmp 0.9a.9 ">=" 1.0.1
29
+
30
+assert_false "numeric lt"  vercmp 1.0 "<" 1.0
31
+assert_true  "numeric lte" vercmp 1.0 "<=" 1.0
32
+assert_true "numeric lt"   vercmp 1.0 "<" 1.0.1
33
+assert_true "numeric lte"  vercmp 1.0 "<=" 1.0.1
34
+assert_true "alpha lt"     vercmp 1.0.1a "<" 1.0.1b
35
+assert_true "alpha lte"    vercmp 1.0.1a "<=" 1.0.1b
36
+assert_true "alpha lt"     vercmp a "<" b
37
+assert_true "alpha lte"    vercmp a "<=" b
38
+assert_true "alpha lt"     vercmp 2.0-rc1 "<" 2.0-rc3
39
+assert_true "alpha lte"    vercmp 2.0-rc1 "<=" 2.0-rc3
40
+
41
+assert_true "eq"       vercmp 1.0 "==" 1.0
42
+assert_true "eq"       vercmp 1.0.1 "==" 1.0.1
43
+assert_false "eq fail" vercmp 1.0.1 "==" 1.0.2
44
+assert_false "eq fail" vercmp 2.0-rc1 "==" 2.0-rc2
45
+
46
+report_results
... ...
@@ -92,6 +92,51 @@ function assert_empty {
92 92
     fi
93 93
 }
94 94
 
95
+# assert the arguments evaluate to true
96
+#  assert_true "message" arg1 arg2
97
+function assert_true {
98
+    local lineno
99
+    lineno=`caller 0 | awk '{print $1}'`
100
+    local function
101
+    function=`caller 0 | awk '{print $2}'`
102
+    local msg=$1
103
+    shift
104
+
105
+    $@
106
+    if [ $? -eq 0 ]; then
107
+        PASS=$((PASS+1))
108
+        echo "PASS: $function:L$lineno - $msg"
109
+    else
110
+        FAILED_FUNCS+="$function:L$lineno\n"
111
+        echo "ERROR: test failed in $function:L$lineno!"
112
+        echo "  $msg"
113
+        ERROR=$((ERROR+1))
114
+    fi
115
+}
116
+
117
+# assert the arguments evaluate to false
118
+#  assert_false "message" arg1 arg2
119
+function assert_false {
120
+    local lineno
121
+    lineno=`caller 0 | awk '{print $1}'`
122
+    local function
123
+    function=`caller 0 | awk '{print $2}'`
124
+    local msg=$1
125
+    shift
126
+
127
+    $@
128
+    if [ $? -eq 0 ]; then
129
+        FAILED_FUNCS+="$function:L$lineno\n"
130
+        echo "ERROR: test failed in $function:L$lineno!"
131
+        echo "  $msg"
132
+        ERROR=$((ERROR+1))
133
+    else
134
+        PASS=$((PASS+1))
135
+        echo "PASS: $function:L$lineno - $msg"
136
+    fi
137
+}
138
+
139
+
95 140
 # Print a summary of passing and failing tests and exit
96 141
 # (with an error if we have failed tests)
97 142
 #  usage: report_results