github.com/swiftstack/proxyfs@v0.0.0-20201223034610-5434d919416e/halter/api_test.go (about) 1 package halter 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/swiftstack/ProxyFS/conf" 8 "github.com/swiftstack/ProxyFS/transitions" 9 ) 10 11 var ( 12 testHaltErr error 13 ) 14 15 func TestAPI(t *testing.T) { 16 testConfMapStrings := []string{ 17 "Logging.LogFilePath=/dev/null", 18 "Cluster.WhoAmI=nobody", 19 "FSGlobals.VolumeGroupList=", 20 "FSGlobals.CheckpointHeaderConsensusAttempts=5", 21 "FSGlobals.MountRetryLimit=6", 22 "FSGlobals.MountRetryDelay=1s", 23 "FSGlobals.MountRetryExpBackoff=2", 24 "FSGlobals.LogCheckpointHeaderPosts=true", 25 "FSGlobals.TryLockBackoffMin=10ms", 26 "FSGlobals.TryLockBackoffMax=50ms", 27 "FSGlobals.TryLockSerializationThreshhold=5", 28 "FSGlobals.SymlinkMax=32", 29 "FSGlobals.CoalesceElementChunkSize=16", 30 } 31 32 testConfMap, err := conf.MakeConfMapFromStrings(testConfMapStrings) 33 if nil != err { 34 t.Fatal(err) 35 } 36 37 err = transitions.Up(testConfMap) 38 if nil != err { 39 t.Fatal(err) 40 } 41 42 configureTestModeHaltCB(testHalt) 43 44 availableTriggers := List() 45 apiTestHaltLabel1Found := false 46 apiTestHaltLabel2Found := false 47 for _, s := range availableTriggers { 48 if "halter.testHaltLabel1" == s { 49 apiTestHaltLabel1Found = true 50 } 51 if "halter.testHaltLabel2" == s { 52 apiTestHaltLabel2Found = true 53 } 54 } 55 if !apiTestHaltLabel1Found { 56 t.Fatalf("List() unexpectedly missing 'halter.testHaltLabel1'") 57 } 58 if !apiTestHaltLabel2Found { 59 t.Fatalf("List() unexpectedly missing 'halter.testHaltLabel2'") 60 } 61 62 m1 := Dump() 63 if 0 != len(m1) { 64 t.Fatalf("Dump() unexpectedly returned length %v map at start-up", len(m1)) 65 } 66 67 testHaltErr = nil 68 Arm("halter.testHaltLabel0", 1) 69 if nil == testHaltErr { 70 t.Fatalf("Arm(apiTestHaltLabel0,) unexpectedly left testHaltErr as nil") 71 } 72 if "halter.Arm(haltLabelString='halter.testHaltLabel0',) - label unknown" != testHaltErr.Error() { 73 t.Fatalf("Arm(apiTestHaltLabel0,) unexpectedly set testHaltErr to %v", testHaltErr) 74 } 75 _, err = Stat("halter.testHaltLabel0") 76 if nil == err { 77 t.Fatalf("Stat(\"halter.testHaltLabel0\") unexpectedly succeeded") 78 } 79 80 testHaltErr = nil 81 Arm("halter.testHaltLabel1", 0) 82 if nil == testHaltErr { 83 t.Fatalf("Arm(apiTestHaltLabel1,0) unexpectedly left testHaltErr as nil") 84 } 85 if "halter.Arm(haltLabel==halter.testHaltLabel1,) called with haltAfterCount==0" != testHaltErr.Error() { 86 fmt.Println(testHaltErr.Error()) 87 t.Fatalf("Arm(apiTestHaltLabel0,) unexpectedly set testHaltErr to %v", testHaltErr) 88 } 89 v0, err := Stat("halter.testHaltLabel1") 90 if nil != err { 91 t.Fatalf("Stat(\"halter.testHaltLabel1\") unexpectedly failed: %v", err) 92 } 93 if 0 != v0 { 94 t.Fatalf("Stat(\"halter.testHaltLabel1\") unexpectedly returned haltAfterCount == %v (should have been 0)", v0) 95 } 96 97 Arm("halter.testHaltLabel1", 1) 98 m2 := Dump() 99 if 1 != len(m2) { 100 t.Fatalf("Dump() unexpectedly returned length %v map after Arm(apiTestHaltLabel1,)", len(m2)) 101 } 102 m2v1, ok := m2["halter.testHaltLabel1"] 103 if !ok { 104 t.Fatalf("Dump() unexpectedly missing m2[apiTestHaltLabel1]") 105 } 106 if 1 != m2v1 { 107 t.Fatalf("Dump() unexpectedly returned %v for m2[apiTestHaltLabel1]", m2v1) 108 } 109 v1, err := Stat("halter.testHaltLabel1") 110 if nil != err { 111 t.Fatalf("Stat(\"halter.testHaltLabel1\") unexpectedly failed: %v", err) 112 } 113 if 1 != v1 { 114 t.Fatalf("Stat(\"halter.testHaltLabel1\") unexpectedly returned haltAfterCount == %v (should have been 1)", v1) 115 } 116 117 Arm("halter.testHaltLabel2", 2) 118 m3 := Dump() 119 if 2 != len(m3) { 120 t.Fatalf("Dump() unexpectedly returned length %v map after Arm(apiTestHaltLabel2,)", len(m3)) 121 } 122 m3v1, ok := m3["halter.testHaltLabel1"] 123 if !ok { 124 t.Fatalf("Dump() unexpectedly missing m3[apiTestHaltLabel1]") 125 } 126 if 1 != m3v1 { 127 t.Fatalf("Dump() unexpectedly returned %v for m3[apiTestHaltLabel1]", m3v1) 128 } 129 m3v2, ok := m3["halter.testHaltLabel2"] 130 if !ok { 131 t.Fatalf("Dump() unexpectedly missing m3[apiTestHaltLabel2]") 132 } 133 if 2 != m3v2 { 134 t.Fatalf("Dump() unexpectedly returned %v for m3[apiTestHaltLabel1]", m3v2) 135 } 136 137 testHaltErr = nil 138 Disarm("halter.testHaltLabel0") 139 if nil == testHaltErr { 140 t.Fatalf("Disarm(apiTestHaltLabel) unexpectedly left testHaltErr as nil") 141 } 142 if "halter.Disarm(haltLabelString='halter.testHaltLabel0') - label unknown" != testHaltErr.Error() { 143 t.Fatalf("Disarm(apiTestHaltLabel0) unexpectedly set testHaltErr to %v", testHaltErr) 144 } 145 146 Disarm("halter.testHaltLabel1") 147 m4 := Dump() 148 if 1 != len(m4) { 149 t.Fatalf("Dump() unexpectedly returned length %v map after Disarm(apiTestHaltLabel1)", len(m4)) 150 } 151 m4v2, ok := m4["halter.testHaltLabel2"] 152 if !ok { 153 t.Fatalf("Dump() unexpectedly missing m4[apiTestHaltLabel2]") 154 } 155 if 2 != m4v2 { 156 t.Fatalf("Dump() unexpectedly returned %v for m4[apiTestHaltLabel2]", m4v2) 157 } 158 159 testHaltErr = nil 160 Trigger(apiTestHaltLabel2) 161 if nil != testHaltErr { 162 t.Fatalf("Trigger(apiTestHaltLabel2) [case 1] unexpectedly set testHaltErr to %v", testHaltErr) 163 } 164 m5 := Dump() 165 if 1 != len(m5) { 166 t.Fatalf("Dump() unexpectedly returned length %v map after Trigger(apiTestHaltLabel2)", len(m5)) 167 } 168 m5v2, ok := m5["halter.testHaltLabel2"] 169 if !ok { 170 t.Fatalf("Dump() unexpectedly missing m5[apiTestHaltLabel2]") 171 } 172 if 2 != m4v2 { 173 t.Fatalf("Dump() unexpectedly returned %v for m5[apiTestHaltLabel2]", m5v2) 174 } 175 176 Trigger(apiTestHaltLabel2) 177 if nil == testHaltErr { 178 t.Fatalf("Trigger(apiTestHaltLabel2) [case 2] unexpectedly left testHaltErr as nil") 179 } 180 if "halter.TriggerArm(haltLabelString==halter.testHaltLabel2) triggered HALT" != testHaltErr.Error() { 181 t.Fatalf("Trigger(apiTestHaltLabel2) [case 2] unexpectedly set testHaltErr to %v", testHaltErr) 182 } 183 184 err = transitions.Down(testConfMap) 185 if nil != err { 186 t.Fatal(err) 187 } 188 } 189 190 func testHalt(err error) { 191 testHaltErr = err 192 }