github.com/1and1/oneandone-cloudserver-sdk-go@v1.4.1/logs_test.go (about)

     1  package oneandone
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  // /logs tests
    11  
    12  func TestListLogs(t *testing.T) {
    13  	fmt.Println("Listing all logs...")
    14  
    15  	res, err := api.ListLogs("LAST_24H", nil, nil)
    16  	if err != nil {
    17  		t.Errorf("ListLogs failed. Error: " + err.Error())
    18  	}
    19  	if len(res) == 0 {
    20  		t.Errorf("No log found.")
    21  	}
    22  
    23  	res, err = api.ListLogs("LAST_24H", nil, nil, 1, 7, "action", "", "action,start_date,end_date")
    24  
    25  	if err != nil {
    26  		t.Errorf("ListLogs with parameter options failed. Error: " + err.Error())
    27  		return
    28  	}
    29  	if len(res) == 0 {
    30  		t.Errorf("No log found.")
    31  	}
    32  	if len(res) != 7 {
    33  		t.Errorf("Wrong number of objects per page.")
    34  	}
    35  	for index := 0; index < len(res); index += 1 {
    36  		if res[index].Status != nil || res[index].Resource != nil || res[index].User != nil {
    37  			t.Errorf("Filtering a list of logs failed.")
    38  		}
    39  		if index < len(res)-1 {
    40  			if res[index].Action > res[index+1].Action {
    41  				t.Errorf("Sorting a list of logs failed.")
    42  			}
    43  		}
    44  	}
    45  
    46  	sd, _ := time.Parse(time.RFC3339, res[len(res)/2].StartDate)
    47  	ed := sd.Add(time.Hour)
    48  	res, err = api.ListLogs("CUSTOM", &sd, &ed, 0, 0, "start_date", "", "action,start_date,end_date")
    49  
    50  	if err != nil {
    51  		t.Errorf("Getting logs in custom date range failed. Error: " + err.Error())
    52  		return
    53  	}
    54  	if len(res) > 0 {
    55  		sd1, _ := time.Parse(time.RFC3339, res[0].StartDate)
    56  		ed1, _ := time.Parse(time.RFC3339, res[len(res)-1].EndDate)
    57  		if sd1.Before(sd) || ed1.After(ed) {
    58  			t.Errorf("Getting logs in custom date range failed.")
    59  		}
    60  	}
    61  
    62  	res, err = api.ListLogs("LAST_7D", nil, nil, 0, 0, "", "CREATE", "action")
    63  
    64  	if err != nil {
    65  		t.Errorf("ListLogs with parameter options failed. Error: " + err.Error())
    66  		return
    67  	}
    68  	for _, log := range res {
    69  		if !strings.Contains(strings.ToUpper(log.Action), "CREATE") {
    70  			t.Errorf("Search parameter failed.")
    71  		}
    72  	}
    73  	// Test for error response
    74  	res, err = api.ListLogs("LAST_24H", nil, nil, 2, 5, 5)
    75  	if res != nil || err == nil {
    76  		t.Errorf("ListLogs failed to handle incorrect argument type.")
    77  	}
    78  }
    79  
    80  func TestGetLog(t *testing.T) {
    81  	logs, _ := api.ListLogs("LAST_24H", nil, nil, 0, 0, "", "", "id")
    82  	fmt.Printf("Getting log '%s'...\n", logs[0].Id)
    83  	log, err := api.GetLog(logs[0].Id)
    84  
    85  	if err != nil {
    86  		t.Errorf("GetLog failed. Error: " + err.Error())
    87  		return
    88  	}
    89  	if log.Id != logs[0].Id {
    90  		t.Errorf("Wrong server ID.")
    91  	}
    92  }