github.com/stakater/IngressMonitorController@v1.0.103/pkg/monitors/uptime/uptime-monitor_test.go (about)

     1  package uptime
     2  
     3  import (
     4  	log "github.com/sirupsen/logrus"
     5  	"testing"
     6  
     7  	"github.com/stakater/IngressMonitorController/pkg/config"
     8  	"github.com/stakater/IngressMonitorController/pkg/models"
     9  	"github.com/stakater/IngressMonitorController/pkg/util"
    10  )
    11  
    12  func TestGetAllMonitors(t *testing.T) {
    13  	config := config.GetControllerConfig()
    14  
    15  	service := UpTimeMonitorService{}
    16  	provider := util.GetProviderWithName(config, "Uptime")
    17  	if provider == nil {
    18  		panic("Failed to find provider")
    19  	}
    20  	// If test Config is passed skip the test
    21  	if provider.ApiKey == "API_KEY" {
    22  		return
    23  	}
    24  	service.Setup(*provider)
    25  	monitors := service.GetAll()
    26  	log.Println(monitors)
    27  
    28  	if len(monitors) == 0 {
    29  		t.Log("No Monitors Exist")
    30  	}
    31  	if nil == monitors {
    32  		t.Error("Error: " + "GetAll request Failed")
    33  	}
    34  }
    35  
    36  func TestAddMonitorWithCorrectValues(t *testing.T) {
    37  	config := config.GetControllerConfig()
    38  
    39  	service := UpTimeMonitorService{}
    40  	provider := util.GetProviderWithName(config, "Uptime")
    41  	if nil == provider {
    42  		panic("Failed to find provider")
    43  	}
    44  	// If test Config is passed skip the test
    45  	if provider.ApiKey == "API_KEY" {
    46  		return
    47  	}
    48  	service.Setup(*provider)
    49  
    50  	annotations := make(map[string]string)
    51  	annotations["uptime.monitor.stakater.com/locations"] = "US-Central"
    52  	annotations["uptime.monitor.stakater.com/contacts"] = "Default"
    53  	annotations["uptime.monitor.stakater.com/interval"] = "5"
    54  
    55  	m := models.Monitor{Name: "google-test", URL: "https://google.com", Annotations: annotations}
    56  	service.Add(m)
    57  
    58  	mRes, err := service.GetByName("google-test")
    59  
    60  	if err != nil {
    61  		t.Error("Error: " + err.Error())
    62  	}
    63  	if mRes.Name != m.Name {
    64  		t.Error("The name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name)
    65  	}
    66  	if mRes.URL != m.URL {
    67  		t.Error("The URL is incorrect, expected: " + m.URL + ", but was: " + mRes.URL)
    68  	}
    69  	service.Remove(*mRes)
    70  }
    71  
    72  func TestUpdateMonitorWithCorrectValues(t *testing.T) {
    73  	config := config.GetControllerConfig()
    74  
    75  	service := UpTimeMonitorService{}
    76  	provider := util.GetProviderWithName(config, "Uptime")
    77  	if provider == nil {
    78  		panic("Failed to find provider")
    79  	}
    80  	// If test Config is passed skip the test
    81  	if provider.ApiKey == "API_KEY" {
    82  		return
    83  	}
    84  	service.Setup(*provider)
    85  	annotations := make(map[string]string)
    86  	annotations["uptime.monitor.stakater.com/locations"] = "US-Central"
    87  	annotations["uptime.monitor.stakater.com/contacts"] = "Default"
    88  	annotations["uptime.monitor.stakater.com/interval"] = "5"
    89  
    90  	m := models.Monitor{Name: "google-test", URL: "https://google.com", Annotations: annotations}
    91  
    92  	service.Add(m)
    93  
    94  	mRes, err := service.GetByName("google-test")
    95  
    96  	if err != nil {
    97  		t.Error("Error: " + err.Error())
    98  	}
    99  	if mRes.Name != m.Name {
   100  		t.Error("The initial name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name)
   101  	}
   102  	if mRes.URL != m.URL {
   103  		t.Error("The initial URL is incorrect, expected: " + m.URL + ", but was: " + mRes.URL)
   104  	}
   105  
   106  	mRes.Name = "google-test-update"
   107  	mRes.URL = "https://facebook.com"
   108  	mRes.Annotations["uptime.monitor.stakater.com/locations"] = "US-East"
   109  	mRes.Annotations["uptime.monitor.stakater.com/contacts"] = "Default"
   110  	mRes.Annotations["uptime.monitor.stakater.com/interval"] = "10"
   111  
   112  	service.Update(*mRes)
   113  
   114  	mRes, err = service.GetByName("google-test-update")
   115  
   116  	if err != nil {
   117  		t.Error("Error: " + err.Error())
   118  	}
   119  	if mRes.URL != "https://facebook.com" {
   120  		t.Error("The URL should have been updated, expected: https://facebook.com, but was: " + mRes.URL)
   121  	}
   122  	if mRes.Name != "google-test-update" {
   123  		t.Error("The URL should have been updated, expected: google-test-update, but was: " + mRes.Name)
   124  	}
   125  	if mRes.Annotations["uptime.monitor.stakater.com/locations"] != "US-East" {
   126  		t.Error("The URL should have been updated, expected: US-East, but was: " + mRes.Annotations["uptime.monitor.stakater.com/locations"])
   127  	}
   128  	if mRes.Annotations["uptime.monitor.stakater.com/interval"] != "10" {
   129  		t.Error("The URL should have been updated, expected: 10, but was: " + mRes.Annotations["uptime.monitor.stakater.com/interval"])
   130  	}
   131  
   132  	service.Remove(*mRes)
   133  }
   134  
   135  func TestAddMonitorWithIncorrectValues(t *testing.T) {
   136  	config := config.GetControllerConfig()
   137  
   138  	service := UpTimeMonitorService{}
   139  
   140  	provider := util.GetProviderWithName(config, "Uptime")
   141  	if provider == nil {
   142  		panic("Failed to find provider")
   143  	}
   144  	// If test Config is passed skip the test
   145  	if provider.ApiKey == "API_KEY" {
   146  		return
   147  	}
   148  	service.Setup(*provider)
   149  	annotations := make(map[string]string)
   150  	annotations["uptime.monitor.stakater.com/locations"] = "US-Central"
   151  	annotations["uptime.monitor.stakater.com/contacts"] = "Default"
   152  	annotations["uptime.monitor.stakater.com/interval"] = "900"
   153  
   154  	m := models.Monitor{Name: "google-test", URL: "https://google.com", Annotations: annotations}
   155  
   156  	service.Add(m)
   157  
   158  	_, err := service.GetByName("google-test")
   159  
   160  	if err == nil {
   161  		t.Error("google-test should not have existed")
   162  	}
   163  }