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