github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/daemon/logger/gelf/gelf_test.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package gelf // import "github.com/docker/docker/daemon/logger/gelf"
     5  
     6  import (
     7  	"net"
     8  	"testing"
     9  
    10  	"github.com/docker/docker/daemon/logger"
    11  )
    12  
    13  // Validate parseAddress
    14  func TestParseAddress(t *testing.T) {
    15  	url, err := parseAddress("udp://127.0.0.1:12201")
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	if url.String() != "udp://127.0.0.1:12201" {
    20  		t.Fatalf("Expected address udp://127.0.0.1:12201, got %s", url.String())
    21  	}
    22  
    23  	_, err = parseAddress("127.0.0.1:12201")
    24  	if err == nil {
    25  		t.Fatal("Expected error requiring protocol")
    26  	}
    27  
    28  	_, err = parseAddress("http://127.0.0.1:12201")
    29  	if err == nil {
    30  		t.Fatal("Expected error restricting protocol")
    31  	}
    32  }
    33  
    34  // Validate TCP options
    35  func TestTCPValidateLogOpt(t *testing.T) {
    36  	err := ValidateLogOpt(map[string]string{
    37  		"gelf-address": "tcp://127.0.0.1:12201",
    38  	})
    39  	if err != nil {
    40  		t.Fatal("Expected TCP to be supported")
    41  	}
    42  
    43  	err = ValidateLogOpt(map[string]string{
    44  		"gelf-address":           "tcp://127.0.0.1:12201",
    45  		"gelf-compression-level": "9",
    46  	})
    47  	if err == nil {
    48  		t.Fatal("Expected TCP to reject compression level")
    49  	}
    50  
    51  	err = ValidateLogOpt(map[string]string{
    52  		"gelf-address":          "tcp://127.0.0.1:12201",
    53  		"gelf-compression-type": "gzip",
    54  	})
    55  	if err == nil {
    56  		t.Fatal("Expected TCP to reject compression type")
    57  	}
    58  
    59  	err = ValidateLogOpt(map[string]string{
    60  		"gelf-address":             "tcp://127.0.0.1:12201",
    61  		"gelf-tcp-max-reconnect":   "5",
    62  		"gelf-tcp-reconnect-delay": "10",
    63  	})
    64  	if err != nil {
    65  		t.Fatal("Expected TCP reconnect to be a valid parameters")
    66  	}
    67  
    68  	err = ValidateLogOpt(map[string]string{
    69  		"gelf-address":             "tcp://127.0.0.1:12201",
    70  		"gelf-tcp-max-reconnect":   "-1",
    71  		"gelf-tcp-reconnect-delay": "-3",
    72  	})
    73  	if err == nil {
    74  		t.Fatal("Expected negative TCP reconnect to be rejected")
    75  	}
    76  
    77  	err = ValidateLogOpt(map[string]string{
    78  		"gelf-address":             "tcp://127.0.0.1:12201",
    79  		"gelf-tcp-max-reconnect":   "invalid",
    80  		"gelf-tcp-reconnect-delay": "invalid",
    81  	})
    82  	if err == nil {
    83  		t.Fatal("Expected TCP reconnect to be required to be an int")
    84  	}
    85  
    86  	err = ValidateLogOpt(map[string]string{
    87  		"gelf-address":             "udp://127.0.0.1:12201",
    88  		"gelf-tcp-max-reconnect":   "1",
    89  		"gelf-tcp-reconnect-delay": "3",
    90  	})
    91  	if err == nil {
    92  		t.Fatal("Expected TCP reconnect to be invalid for UDP")
    93  	}
    94  }
    95  
    96  // Validate UDP options
    97  func TestUDPValidateLogOpt(t *testing.T) {
    98  	err := ValidateLogOpt(map[string]string{
    99  		"gelf-address":           "udp://127.0.0.1:12201",
   100  		"tag":                    "testtag",
   101  		"labels":                 "testlabel",
   102  		"labels-regex":           "testlabel-regex",
   103  		"env":                    "testenv",
   104  		"env-regex":              "testenv-regex",
   105  		"gelf-compression-level": "9",
   106  		"gelf-compression-type":  "gzip",
   107  	})
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  
   112  	err = ValidateLogOpt(map[string]string{
   113  		"gelf-address":           "udp://127.0.0.1:12201",
   114  		"gelf-compression-level": "ultra",
   115  		"gelf-compression-type":  "zlib",
   116  	})
   117  	if err == nil {
   118  		t.Fatal("Expected compression level error")
   119  	}
   120  
   121  	err = ValidateLogOpt(map[string]string{
   122  		"gelf-address":          "udp://127.0.0.1:12201",
   123  		"gelf-compression-type": "rar",
   124  	})
   125  	if err == nil {
   126  		t.Fatal("Expected compression type error")
   127  	}
   128  
   129  	err = ValidateLogOpt(map[string]string{
   130  		"invalid": "invalid",
   131  	})
   132  	if err == nil {
   133  		t.Fatal("Expected unknown option error")
   134  	}
   135  
   136  	err = ValidateLogOpt(map[string]string{})
   137  	if err == nil {
   138  		t.Fatal("Expected required parameter error")
   139  	}
   140  }
   141  
   142  // Validate newGELFTCPWriter
   143  func TestNewGELFTCPWriter(t *testing.T) {
   144  	address := "127.0.0.1:0"
   145  	tcpAddr, err := net.ResolveTCPAddr("tcp", address)
   146  	if err != nil {
   147  		t.Fatal(err)
   148  	}
   149  
   150  	listener, err := net.ListenTCP("tcp", tcpAddr)
   151  	if err != nil {
   152  		t.Fatal(err)
   153  	}
   154  
   155  	url := "tcp://" + listener.Addr().String()
   156  	info := logger.Info{
   157  		Config: map[string]string{
   158  			"gelf-address":             url,
   159  			"gelf-tcp-max-reconnect":   "0",
   160  			"gelf-tcp-reconnect-delay": "0",
   161  			"tag":                      "{{.ID}}",
   162  		},
   163  		ContainerID: "12345678901234567890",
   164  	}
   165  
   166  	writer, err := newGELFTCPWriter(listener.Addr().String(), info)
   167  	if err != nil {
   168  		t.Fatal(err)
   169  	}
   170  
   171  	err = writer.Close()
   172  	if err != nil {
   173  		t.Fatal(err)
   174  	}
   175  
   176  	err = listener.Close()
   177  	if err != nil {
   178  		t.Fatal(err)
   179  	}
   180  }
   181  
   182  // Validate newGELFUDPWriter
   183  func TestNewGELFUDPWriter(t *testing.T) {
   184  	address := "127.0.0.1:0"
   185  	info := logger.Info{
   186  		Config: map[string]string{
   187  			"gelf-address":           "udp://127.0.0.1:0",
   188  			"gelf-compression-level": "5",
   189  			"gelf-compression-type":  "gzip",
   190  		},
   191  	}
   192  
   193  	writer, err := newGELFUDPWriter(address, info)
   194  	if err != nil {
   195  		t.Fatal(err)
   196  	}
   197  	writer.Close()
   198  	if err != nil {
   199  		t.Fatal(err)
   200  	}
   201  }
   202  
   203  // Validate New for TCP
   204  func TestNewTCP(t *testing.T) {
   205  	address := "127.0.0.1:0"
   206  	tcpAddr, err := net.ResolveTCPAddr("tcp", address)
   207  	if err != nil {
   208  		t.Fatal(err)
   209  	}
   210  
   211  	listener, err := net.ListenTCP("tcp", tcpAddr)
   212  	if err != nil {
   213  		t.Fatal(err)
   214  	}
   215  
   216  	url := "tcp://" + listener.Addr().String()
   217  	info := logger.Info{
   218  		Config: map[string]string{
   219  			"gelf-address":             url,
   220  			"gelf-tcp-max-reconnect":   "0",
   221  			"gelf-tcp-reconnect-delay": "0",
   222  		},
   223  		ContainerID: "12345678901234567890",
   224  	}
   225  
   226  	gelfLogger, err := New(info)
   227  	if err != nil {
   228  		t.Fatal(err)
   229  	}
   230  
   231  	err = gelfLogger.Close()
   232  	if err != nil {
   233  		t.Fatal(err)
   234  	}
   235  
   236  	err = listener.Close()
   237  	if err != nil {
   238  		t.Fatal(err)
   239  	}
   240  }
   241  
   242  // Validate New for UDP
   243  func TestNewUDP(t *testing.T) {
   244  	info := logger.Info{
   245  		Config: map[string]string{
   246  			"gelf-address":           "udp://127.0.0.1:0",
   247  			"gelf-compression-level": "5",
   248  			"gelf-compression-type":  "gzip",
   249  		},
   250  		ContainerID: "12345678901234567890",
   251  	}
   252  
   253  	gelfLogger, err := New(info)
   254  	if err != nil {
   255  		t.Fatal(err)
   256  	}
   257  
   258  	err = gelfLogger.Close()
   259  	if err != nil {
   260  		t.Fatal(err)
   261  	}
   262  }