github.com/stakater/IngressMonitorController@v1.0.103/pkg/monitors/updown/updown-monitor_test.go (about) 1 package updown 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stakater/IngressMonitorController/pkg/config" 8 "github.com/stakater/IngressMonitorController/pkg/models" 9 "github.com/stakater/IngressMonitorController/pkg/util" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 type Block struct { 14 Try func() 15 Catch func(Exception) 16 Finally func() 17 } 18 19 type Exception interface{} 20 21 func Throw(up Exception) { 22 panic(up) 23 } 24 25 func (tcf Block) Do() { 26 if tcf.Finally != nil { 27 28 defer tcf.Finally() 29 } 30 if tcf.Catch != nil { 31 defer func() { 32 if r := recover(); r != nil { 33 tcf.Catch(r) 34 } 35 }() 36 } 37 tcf.Try() 38 } 39 40 const ( 41 CheckURL = "https://updown.io" 42 CheckName = "Updown-site-check" 43 UpdatedCheckName = "Update-Updown-site-check" 44 ) 45 46 func TestSetupMonitorWithCorrectValues(t *testing.T) { 47 config := config.GetControllerConfig() 48 UpdownService := UpdownMonitorService{} 49 50 provider := util.GetProviderWithName(config, "Updown") 51 52 Block{ 53 Try: func() { 54 UpdownService.Setup(*provider) 55 }, 56 Catch: func(e Exception) {}, 57 }.Do() 58 59 } 60 61 func TestSetupMonitorWithIncorrectValues(t *testing.T) { 62 config := config.GetControllerConfig() 63 UpdownService := UpdownMonitorService{} 64 65 provider := util.GetProviderWithName(config, "InvalidProviderName") 66 Block{ 67 Try: func() { 68 UpdownService.Setup(*provider) 69 }, 70 Catch: func(e Exception) {}, 71 }.Do() 72 73 } 74 75 // TestRemoveCleanUp it will remove all the checks before any test executes 76 func TestRemoveCleanUp(t *testing.T) { 77 config := config.GetControllerConfig() 78 UpdownService := UpdownMonitorService{} 79 provider := util.GetProviderWithName(config, "Updown") 80 UpdownService.Setup(*provider) 81 82 monitorSlice := UpdownService.GetAll() 83 for _, monitor := range monitorSlice { 84 85 monitorObj := models.Monitor{ 86 ID: monitor.ID, 87 Name: monitor.Name} 88 UpdownService.Remove(monitorObj) 89 } 90 time.Sleep(10 * time.Second) 91 monitorSlice = UpdownService.GetAll() 92 93 assert.Equal(t, 0, len(monitorSlice)) 94 95 } 96 97 func TestGetAllMonitorWhileNoCheckExists(t *testing.T) { 98 config := config.GetControllerConfig() 99 UpdownService := UpdownMonitorService{} 100 provider := util.GetProviderWithName(config, "Updown") 101 UpdownService.Setup(*provider) 102 103 monitorSlice := UpdownService.GetAll() 104 105 assert.Equal(t, 0, len(monitorSlice)) 106 107 } 108 109 func TestGetByNameMonitorWhileNoCheckExists(t *testing.T) { 110 config := config.GetControllerConfig() 111 UpdownService := UpdownMonitorService{} 112 provider := util.GetProviderWithName(config, "Updown") 113 UpdownService.Setup(*provider) 114 115 var nilMonitorModelObj *models.Monitor 116 monitorObject, _ := UpdownService.GetByName("NoExistingCheck") 117 118 assert.Equal(t, monitorObject, nilMonitorModelObj) 119 120 } 121 122 func TestAddMonitorWhileNoCheckExists(t *testing.T) { 123 config := config.GetControllerConfig() 124 UpdownService := UpdownMonitorService{} 125 provider := util.GetProviderWithName(config, "Updown") 126 UpdownService.Setup(*provider) 127 128 annotations := map[string]string{ 129 "updown.monitor.stakater.com/publish-page": "false", 130 "updown.monitor.stakater.com/enable": "false", 131 "updown.monitor.stakater.com/period": "120"} 132 133 newMonitor := models.Monitor{ 134 URL: CheckURL, 135 Name: CheckName, 136 Annotations: annotations} 137 138 UpdownService.Add(newMonitor) 139 } 140 141 func TestAddMonitorWhileCheckExists(t *testing.T) { 142 config := config.GetControllerConfig() 143 UpdownService := UpdownMonitorService{} 144 provider := util.GetProviderWithName(config, "Updown") 145 UpdownService.Setup(*provider) 146 147 newMonitor := models.Monitor{ 148 URL: CheckURL, 149 Name: CheckName} 150 151 UpdownService.Add(newMonitor) 152 } 153 154 func TestGetAllMonitorWhileCheckExists(t *testing.T) { 155 config := config.GetControllerConfig() 156 UpdownService := UpdownMonitorService{} 157 provider := util.GetProviderWithName(config, "Updown") 158 UpdownService.Setup(*provider) 159 160 time.Sleep(30 * time.Second) 161 monitorSlice := UpdownService.GetAll() 162 firstElement := 0 163 oneElement := 1 164 165 assert.Equal(t, oneElement, len(monitorSlice)) 166 assert.Equal(t, monitorSlice[firstElement].Name, CheckName) 167 assert.Equal(t, monitorSlice[firstElement].URL, CheckURL) 168 169 } 170 171 func TestGetByNameMonitorWhileCheckExists(t *testing.T) { 172 config := config.GetControllerConfig() 173 UpdownService := UpdownMonitorService{} 174 provider := util.GetProviderWithName(config, "Updown") 175 UpdownService.Setup(*provider) 176 177 firstElement := 0 178 var nilMonitorModelObj *models.Monitor 179 monitorSlice := UpdownService.GetAll() 180 monitorObject, _ := UpdownService.GetByName(monitorSlice[firstElement].ID) 181 182 assert.NotEqual(t, &monitorObject, nilMonitorModelObj) 183 184 } 185 186 func TestUpdateMonitorWhileCheckExists(t *testing.T) { 187 config := config.GetControllerConfig() 188 UpdownService := UpdownMonitorService{} 189 provider := util.GetProviderWithName(config, "Updown") 190 UpdownService.Setup(*provider) 191 192 firstElement := 0 193 monitorSlice := UpdownService.GetAll() 194 195 annotations := map[string]string{ 196 "updown.monitor.stakater.com/publish-page": "true", 197 "updown.monitor.stakater.com/enable": "false", 198 "updown.monitor.stakater.com/period": "60"} 199 200 updatedMonitor := models.Monitor{ 201 URL: CheckURL, 202 Name: UpdatedCheckName, 203 ID: monitorSlice[firstElement].ID, 204 Annotations: annotations} 205 206 UpdownService.Update(updatedMonitor) 207 208 } 209 210 func TestGetAllMonitorWhileCheckUpdated(t *testing.T) { 211 config := config.GetControllerConfig() 212 UpdownService := UpdownMonitorService{} 213 provider := util.GetProviderWithName(config, "Updown") 214 UpdownService.Setup(*provider) 215 216 time.Sleep(10 * time.Second) 217 monitorSlice := UpdownService.GetAll() 218 firstElement := 0 219 220 assert.NotEqual(t, monitorSlice[firstElement].Name, UpdatedCheckName) 221 222 } 223 224 func TestRemoveMonitorWhileCheckExists(t *testing.T) { 225 config := config.GetControllerConfig() 226 UpdownService := UpdownMonitorService{} 227 provider := util.GetProviderWithName(config, "Updown") 228 UpdownService.Setup(*provider) 229 230 firstElement := 0 231 monitorSlice := UpdownService.GetAll() 232 updatedMonitor := models.Monitor{ 233 URL: monitorSlice[firstElement].URL, 234 Name: monitorSlice[firstElement].Name, 235 ID: monitorSlice[firstElement].ID} 236 237 UpdownService.Remove(updatedMonitor) 238 239 } 240 241 func TestGetAllMonitorWhenCheckAreRemoved(t *testing.T) { 242 config := config.GetControllerConfig() 243 UpdownService := UpdownMonitorService{} 244 provider := util.GetProviderWithName(config, "Updown") 245 UpdownService.Setup(*provider) 246 247 time.Sleep(30 * time.Second) 248 monitorSlice1 := UpdownService.GetAll() 249 250 assert.Equal(t, 0, len(monitorSlice1)) 251 252 }