cloudflare/cfssl_trust
Publicmirrored from https://github.com/cloudflare/cfssl_trustAvailable
release/release_test.go
161lines · modecode
| 1 | package release |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | type parseTest struct { |
| 9 | s string |
| 10 | r Release |
| 11 | } |
| 12 | |
| 13 | var parseTests = []parseTest{ |
| 14 | {s: "2016.2.2", r: Release{Year: 2016, Month: 2, Iteration: 2}}, |
| 15 | {s: "2016.3.0", r: Release{Year: 2016, Month: 3, Iteration: 0}}, |
| 16 | {s: "2016.3.1", r: Release{Year: 2016, Month: 3, Iteration: 1}}, |
| 17 | {s: "2016.3.2", r: Release{Year: 2016, Month: 3, Iteration: 2}}, |
| 18 | {s: "2016.4.0", r: Release{Year: 2016, Month: 4, Iteration: 0}}, |
| 19 | {s: "2016.4.1", r: Release{Year: 2016, Month: 4, Iteration: 1}}, |
| 20 | {s: "2016.4.2", r: Release{Year: 2016, Month: 4, Iteration: 2}}, |
| 21 | {s: "2017.1.0", r: Release{Year: 2017, Month: 1, Iteration: 0}}, |
| 22 | {s: "2017.1.1-g7fdd9a3-dev", r: Release{Year: 2017, Month: 1, Iteration: 1, Extra: "g7fdd9a3-dev"}}, |
| 23 | } |
| 24 | |
| 25 | var badReleases = []string{ |
| 26 | "v0.5", |
| 27 | "v0.5.1", |
| 28 | } |
| 29 | |
| 30 | // TestString validates the Stringer and Parse functions. |
| 31 | func TestString(t *testing.T) { |
| 32 | for _, tc := range parseTests { |
| 33 | rel, err := Parse(tc.s) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | |
| 38 | s := rel.String() |
| 39 | if s != tc.s { |
| 40 | t.Fatalf("release: String() returned %s, but should have returned %s", |
| 41 | s, tc.s) |
| 42 | } |
| 43 | |
| 44 | cmp := rel.Cmp(tc.r) |
| 45 | if cmp != 0 { |
| 46 | t.Logf("release: Cmp() should return 0, but returned %d", cmp) |
| 47 | t.Logf("release: test case was '%#v', parsed was '%#v'", tc.r, rel) |
| 48 | t.Error() |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | for _, br := range badReleases { |
| 53 | if _, err := Parse(br); err == nil { |
| 54 | t.Fatalf("release: %s should be a bad release") |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | const ( |
| 60 | // The indicies are named for clarity. |
| 61 | relFirst = iota |
| 62 | relSecond |
| 63 | relThird |
| 64 | relNewMonth |
| 65 | relNewYear |
| 66 | ) |
| 67 | |
| 68 | type bumpTest struct { |
| 69 | a, b Release // a is the initial version, b is the bumped version |
| 70 | t time.Time // the timestamp to use for the bump |
| 71 | } |
| 72 | |
| 73 | var bumpTests = map[int]bumpTest{ |
| 74 | // The first release happens at the beginning of the month. |
| 75 | relFirst: bumpTest{ |
| 76 | a: Release{Year: 2016, Month: 11, Iteration: 0}, |
| 77 | b: Release{Year: 2016, Month: 11, Iteration: 1}, |
| 78 | t: time.Date(2016, time.November, 1, 13, 15, 0, 0, time.Local), |
| 79 | }, |
| 80 | |
| 81 | // The second release happens an hour later. |
| 82 | relSecond: bumpTest{ |
| 83 | a: Release{Year: 2016, Month: 11, Iteration: 1}, |
| 84 | b: Release{Year: 2016, Month: 11, Iteration: 2}, |
| 85 | t: time.Date(2016, time.November, 1, 14, 12, 58, 0, time.Local), |
| 86 | }, |
| 87 | |
| 88 | // The third release happens a few weeks later. |
| 89 | relThird: bumpTest{ |
| 90 | a: Release{Year: 2016, Month: 11, Iteration: 2}, |
| 91 | b: Release{Year: 2016, Month: 11, Iteration: 3}, |
| 92 | t: time.Date(2016, time.November, 18, 15, 34, 0, 0, time.Local), |
| 93 | }, |
| 94 | |
| 95 | // The fourth release happens the next month (a few weeks |
| 96 | // after the third release). |
| 97 | relNewMonth: bumpTest{ |
| 98 | a: Release{Year: 2016, Month: 11, Iteration: 3}, |
| 99 | b: Release{Year: 2016, Month: 12, Iteration: 0}, |
| 100 | t: time.Date(2016, time.December, 6, 11, 41, 19, 0, time.Local), |
| 101 | }, |
| 102 | |
| 103 | // The last release happens the next year. |
| 104 | relNewYear: bumpTest{ |
| 105 | a: Release{Year: 2016, Month: 12, Iteration: 0}, |
| 106 | b: Release{Year: 2017, Month: 1, Iteration: 0}, |
| 107 | t: time.Date(2017, time.January, 6, 11, 41, 19, 0, time.Local), |
| 108 | }, |
| 109 | } |
| 110 | |
| 111 | // TestIncrement verifies that incrementing a release works as |
| 112 | // expected. |
| 113 | func TestIncrement(t *testing.T) { |
| 114 | // This release is older than this package. It should always |
| 115 | // fail, unless the clock on the testing machine is months out |
| 116 | // of date. |
| 117 | old := Release{Year: 2017, Month: 1, Iteration: 0} |
| 118 | rel, err := old.Inc() |
| 119 | if err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | |
| 123 | if rel.Cmp(old) != 1 { |
| 124 | t.Fatal("release: incremented release is not newer than old release") |
| 125 | } |
| 126 | |
| 127 | // Verify the test cases: incrementing the 'a' release at the |
| 128 | // timestamp in the test case should result in the 'b' |
| 129 | // release. The 'b' release should also be newer than the 'a' |
| 130 | // release, at least as seen by Cmp(). |
| 131 | for _, tc := range bumpTests { |
| 132 | rel, err := tc.a.IncAt(tc.t) |
| 133 | if err != nil { |
| 134 | t.Log(tc.a) |
| 135 | t.Log(tc.b) |
| 136 | t.Log(rel) |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | |
| 140 | if rel.Cmp(tc.b) != 0 { |
| 141 | t.Fatalf("release: increment should have returned %s, but returned %s", |
| 142 | tc.b, rel) |
| 143 | } |
| 144 | |
| 145 | cmp := rel.Cmp(tc.a) |
| 146 | if cmp != 1 { |
| 147 | t.Log("release: Cmp() returned %d, but it should have returned 1", cmp) |
| 148 | t.Error("release: this indicates that the incremented release does not represent a later release.") |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func TestRegression(t *testing.T) { |
| 154 | rel := Release{Year: 2017, Month: 1, Iteration: 0} |
| 155 | at := time.Date(2016, time.January, 6, 11, 41, 19, 0, time.Local) |
| 156 | |
| 157 | _, err := rel.IncAt(at) |
| 158 | if err == nil { |
| 159 | t.Fatal("release: inc() should have signaled a regression") |
| 160 | } |
| 161 | } |