|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// TEST group quorum metrics |
| 9 | +func TestQuoromMetricParsing(t *testing.T) { |
| 10 | + // the data is fake data |
| 11 | + fmt.Println("=== Testing quorum info retrivial") |
| 12 | + quoromStatus := ` |
| 13 | + Quorum information |
| 14 | + ------------------ |
| 15 | + Date: Sun Sep 29 16:10:37 2019 |
| 16 | + Quorum provider: corosync_votequorum |
| 17 | + Nodes: 2 |
| 18 | + Node ID: 1084780051 |
| 19 | + Ring ID: 1084780051/44 |
| 20 | + Quorate: Yes |
| 21 | + |
| 22 | + Votequorum information |
| 23 | + ---------------------- |
| 24 | + Expected votes: 232 |
| 25 | + Highest expected: 22 |
| 26 | + Total votes: 21 |
| 27 | + Quorum: 421 |
| 28 | + Flags: 2Node Quorate WaitForAll |
| 29 | + |
| 30 | + Membership information |
| 31 | + ---------------------- |
| 32 | + Nodeid Votes Name |
| 33 | + 1084780051 1 dma-dog-hana01 (local) |
| 34 | + 1084780052 1 dma-dog-hana02 |
| 35 | + dma-dog-hana01:~ # |
| 36 | + ` |
| 37 | + getQuoromClusterInfo() |
| 38 | + voteQuorumInfo, quorate := parseQuoromStatus([]byte(quoromStatus)) |
| 39 | + |
| 40 | + if voteQuorumInfo["expectedVotes"] != 232 { |
| 41 | + t.Errorf("expectedVotes should be 232 got instead: %d", voteQuorumInfo["expectedVotes"]) |
| 42 | + } |
| 43 | + if voteQuorumInfo["highestExpected"] != 22 { |
| 44 | + t.Errorf("expectedVotes should be 232 got instead: %d", voteQuorumInfo["highestExpected"]) |
| 45 | + } |
| 46 | + |
| 47 | + if voteQuorumInfo["totalVotes"] != 21 { |
| 48 | + t.Errorf("expectedVotes should be 232 got instead: %d", voteQuorumInfo["totalVotes"]) |
| 49 | + } |
| 50 | + |
| 51 | + if voteQuorumInfo["quorum"] != 421 { |
| 52 | + t.Errorf("expectedVotes should be 421 got instead: %d", voteQuorumInfo["quorum"]) |
| 53 | + } |
| 54 | + |
| 55 | + if quorate != "yes" { |
| 56 | + t.Errorf("quorate should be set to Yes, got %s", quorate) |
| 57 | + } |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +// TEST group RING metrics |
| 62 | +// test that we recognize 1 error (for increasing metric later) |
| 63 | +func TestOneRingError(t *testing.T) { |
| 64 | + fmt.Println("=== Test one ring error") |
| 65 | + ringStatusWithOneError := `Printing ring status. |
| 66 | + Local node ID 16777226 |
| 67 | + RING ID 0 |
| 68 | + id = 10.0.0.1 |
| 69 | + status = Marking ringid 0 interface 10.0.0.1 FAULTY |
| 70 | + RING ID 1 |
| 71 | + id = 172.16.0.1 |
| 72 | + status = ring 1 active with no faults |
| 73 | + ` |
| 74 | + |
| 75 | + getCorosyncRingStatus() |
| 76 | + ringErrorsTotal, err := parseRingStatus([]byte(ringStatusWithOneError)) |
| 77 | + RingExpectedErrors := 1 |
| 78 | + if ringErrorsTotal != RingExpectedErrors { |
| 79 | + t.Errorf("ringErrors was incorrect, got: %d, expected: %d.", ringErrorsTotal, RingExpectedErrors) |
| 80 | + } |
| 81 | + if err != nil { |
| 82 | + t.Errorf("error should be nil got instead: %s", err) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestZeroRingErrors(t *testing.T) { |
| 87 | + fmt.Println("=== Test zero Ring errors") |
| 88 | + ringStatusWithOneError := `Printing ring status. |
| 89 | + Local node ID 16777226 |
| 90 | + RING ID 0 |
| 91 | + id = 10.0.0.1 |
| 92 | + status = Marking ringid 0 interface 10.0.0.1 |
| 93 | + RING ID 1 |
| 94 | + id = 172.16.0.1 |
| 95 | + status = ring 1 active with no faults |
| 96 | + ` |
| 97 | + |
| 98 | + getCorosyncRingStatus() |
| 99 | + ringErrorsTotal, err := parseRingStatus([]byte(ringStatusWithOneError)) |
| 100 | + RingExpectedErrors := 0 |
| 101 | + if ringErrorsTotal != RingExpectedErrors { |
| 102 | + t.Errorf("ringErrors was incorrect, got: %d, expected: %d.", ringErrorsTotal, RingExpectedErrors) |
| 103 | + } |
| 104 | + if err != nil { |
| 105 | + t.Errorf("error should be nil got instead: %s", err) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// test that we recognize 3 rings error (for increasing metric later) |
| 110 | +func TestMultipleRingErrors(t *testing.T) { |
| 111 | + fmt.Println("=== Test multiples ring error") |
| 112 | + ringStatusWithOneError := `Printing ring status. |
| 113 | + Local node ID 16777226 |
| 114 | + RING ID 0 |
| 115 | + id = 10.0.0.1 |
| 116 | + status = Marking ringid 0 interface 10.0.0.1 FAULTY |
| 117 | + RING ID 1 |
| 118 | + id = 172.16.0.1 |
| 119 | + status = ring 1 active with no faults |
| 120 | + RING ID 2 |
| 121 | + id = 10.0.0.1 |
| 122 | + status = Marking ringid 1 interface 10.0.0.1 FAULTY |
| 123 | + RING ID 3 |
| 124 | + id = 172.16.0.1 |
| 125 | + status = ring 1 active with no faults |
| 126 | + RING ID 4 |
| 127 | + id = 10.0.0.1 |
| 128 | + status = Marking ringid 1 interface 10.0.0.1 FAULTY |
| 129 | + RING ID 5 |
| 130 | + id = 172.16.0.1 |
| 131 | + status = ring 1 active with no faults |
| 132 | + |
| 133 | + ` |
| 134 | + |
| 135 | + getCorosyncRingStatus() |
| 136 | + ringErrorsTotal, err := parseRingStatus([]byte(ringStatusWithOneError)) |
| 137 | + RingExpectedErrors := 3 |
| 138 | + if ringErrorsTotal != RingExpectedErrors { |
| 139 | + t.Errorf("ringErrors was incorrect, got: %d, expected: %d.", ringErrorsTotal, RingExpectedErrors) |
| 140 | + } |
| 141 | + if err != nil { |
| 142 | + t.Errorf("error should be nil got instead: %s", err) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// test that in case of system unexpected error we detect this |
| 147 | +func TestSystemUnexpectedError(t *testing.T) { |
| 148 | + fmt.Println("=== Test unexpected error") |
| 149 | + // since there is no cluster in a Test env. this will return an error |
| 150 | + ringStatusError := getCorosyncRingStatus() |
| 151 | + parseRingStatus([]byte(ringStatusError)) |
| 152 | + ringErrorsTotal, err := parseRingStatus([]byte(ringStatusError)) |
| 153 | + RingExpectedErrors := 0 |
| 154 | + if ringErrorsTotal != RingExpectedErrors { |
| 155 | + t.Errorf("ringErrors was incorrect, got: %d, expected: %d.", ringErrorsTotal, RingExpectedErrors) |
| 156 | + } |
| 157 | + if err == nil { |
| 158 | + t.Errorf("error should not be nil got !!") |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments