github.com/containerd/nerdctl@v1.7.7/pkg/logging/fluentd_logger_test.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package logging
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/fluent/fluent-logger-golang/fluent"
    24  )
    25  
    26  func TestParseAddress(t *testing.T) {
    27  	type args struct {
    28  		address string
    29  	}
    30  	tests := []struct {
    31  		name    string
    32  		args    args
    33  		want    *fluentdLocation
    34  		wantErr bool
    35  	}{
    36  		{name: "empty", args: args{address: ""}, want: &fluentdLocation{protocol: "tcp", host: "127.0.0.1", port: 24224}, wantErr: false},
    37  		{name: "unix", args: args{address: "unix:///var/run/fluentd/fluentd.sock"}, want: &fluentdLocation{protocol: "unix", path: "/var/run/fluentd/fluentd.sock"}, wantErr: false},
    38  		{name: "tcp", args: args{address: "tcp://127.0.0.1:24224"}, want: &fluentdLocation{protocol: "tcp", host: "127.0.0.1", port: 24224}, wantErr: false},
    39  		{name: "tcpWithPath", args: args{address: "tcp://127.0.0.1:24224/1234"}, want: nil, wantErr: true},
    40  		{name: "unixWithEmpty", args: args{address: "unix://"}, want: nil, wantErr: true},
    41  		{name: "invalidPath", args: args{address: "://asd123"}, want: nil, wantErr: true},
    42  	}
    43  	for _, tt := range tests {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			got, err := parseAddress(tt.args.address)
    46  			if (err != nil) != tt.wantErr {
    47  				t.Errorf("parseAddress() error = %v, wantErr %v", err, tt.wantErr)
    48  				return
    49  			}
    50  			if !reflect.DeepEqual(got, tt.want) {
    51  				t.Errorf("parseAddress() got = %v, want %v", got, tt.want)
    52  			}
    53  		})
    54  	}
    55  }
    56  
    57  func TestValidateFluentdLoggerOpts(t *testing.T) {
    58  	type args struct {
    59  		config map[string]string
    60  	}
    61  	tests := []struct {
    62  		name    string
    63  		args    args
    64  		wantErr bool
    65  	}{
    66  		// TODO: Add test cases.
    67  		{name: "empty", args: args{config: map[string]string{}}, wantErr: false},
    68  		{name: "invalid", args: args{config: map[string]string{"foo": "bar"}}, wantErr: true},
    69  	}
    70  	for _, tt := range tests {
    71  		t.Run(tt.name, func(t *testing.T) {
    72  			if err := ValidateFluentdLoggerOpts(tt.args.config); (err != nil) != tt.wantErr {
    73  				t.Errorf("ValidateFluentdLoggerOpts() error = %v, wantErr %v", err, tt.wantErr)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func TestParseFluentdConfig(t *testing.T) {
    80  	type args struct {
    81  		config map[string]string
    82  	}
    83  	tests := []struct {
    84  		name    string
    85  		args    args
    86  		want    fluent.Config
    87  		wantErr bool
    88  	}{
    89  		{"DefaultLocation", args{
    90  			config: map[string]string{}},
    91  			fluent.Config{
    92  				FluentPort:             defaultPort,
    93  				FluentHost:             defaultHost,
    94  				FluentNetwork:          defaultProtocol,
    95  				FluentSocketPath:       "",
    96  				BufferLimit:            defaultBufferLimit,
    97  				RetryWait:              int(defaultRetryWait),
    98  				MaxRetry:               defaultMaxRetries,
    99  				Async:                  false,
   100  				AsyncReconnectInterval: 0,
   101  				SubSecondPrecision:     false,
   102  				RequestAck:             false}, false},
   103  		{"InputLocation", args{
   104  			config: map[string]string{
   105  				fluentAddress: "tcp://127.0.0.1:123",
   106  			}},
   107  			fluent.Config{
   108  				FluentPort:             123,
   109  				FluentHost:             "127.0.0.1",
   110  				FluentNetwork:          defaultProtocol,
   111  				FluentSocketPath:       "",
   112  				BufferLimit:            defaultBufferLimit,
   113  				RetryWait:              int(defaultRetryWait),
   114  				MaxRetry:               defaultMaxRetries,
   115  				Async:                  false,
   116  				AsyncReconnectInterval: 0,
   117  				SubSecondPrecision:     false,
   118  				RequestAck:             false}, false},
   119  		{"InvalidLocation", args{config: map[string]string{fluentAddress: "://asd123"}}, fluent.Config{}, true},
   120  		{"InputAsyncOption", args{
   121  			config: map[string]string{
   122  				fluentdAsync: "true",
   123  			}},
   124  			fluent.Config{
   125  				FluentPort:             defaultPort,
   126  				FluentHost:             defaultHost,
   127  				FluentNetwork:          defaultProtocol,
   128  				FluentSocketPath:       "",
   129  				BufferLimit:            defaultBufferLimit,
   130  				RetryWait:              int(defaultRetryWait),
   131  				MaxRetry:               defaultMaxRetries,
   132  				Async:                  true,
   133  				AsyncReconnectInterval: 0,
   134  				SubSecondPrecision:     false,
   135  				RequestAck:             false}, false},
   136  		{"InputAsyncReconnectOption", args{
   137  			config: map[string]string{
   138  				fluentdAsyncReconnectInterval: "100ms",
   139  			}},
   140  			fluent.Config{
   141  				FluentPort:             defaultPort,
   142  				FluentHost:             defaultHost,
   143  				FluentNetwork:          defaultProtocol,
   144  				FluentSocketPath:       "",
   145  				BufferLimit:            defaultBufferLimit,
   146  				RetryWait:              int(defaultRetryWait),
   147  				MaxRetry:               defaultMaxRetries,
   148  				Async:                  false,
   149  				AsyncReconnectInterval: 100,
   150  				SubSecondPrecision:     false,
   151  				RequestAck:             false}, false},
   152  		{"InputBufferLimitOption", args{
   153  			config: map[string]string{
   154  				fluentdBufferLimit: "1000",
   155  			}},
   156  			fluent.Config{
   157  				FluentPort:             defaultPort,
   158  				FluentHost:             defaultHost,
   159  				FluentNetwork:          defaultProtocol,
   160  				FluentSocketPath:       "",
   161  				BufferLimit:            1000,
   162  				RetryWait:              int(defaultRetryWait),
   163  				MaxRetry:               defaultMaxRetries,
   164  				Async:                  false,
   165  				AsyncReconnectInterval: 0,
   166  				SubSecondPrecision:     false,
   167  				RequestAck:             false}, false},
   168  		{"InputRetryWaitOption", args{
   169  			config: map[string]string{
   170  				fluentdRetryWait: "10s",
   171  			}},
   172  			fluent.Config{
   173  				FluentPort:             defaultPort,
   174  				FluentHost:             defaultHost,
   175  				FluentNetwork:          defaultProtocol,
   176  				FluentSocketPath:       "",
   177  				BufferLimit:            defaultBufferLimit,
   178  				RetryWait:              10000,
   179  				MaxRetry:               defaultMaxRetries,
   180  				Async:                  false,
   181  				AsyncReconnectInterval: 0,
   182  				SubSecondPrecision:     false,
   183  				RequestAck:             false}, false},
   184  		{"InputMaxRetriesOption", args{
   185  			config: map[string]string{
   186  				fluentdMaxRetries: "100",
   187  			}},
   188  			fluent.Config{
   189  				FluentPort:             defaultPort,
   190  				FluentHost:             defaultHost,
   191  				FluentNetwork:          defaultProtocol,
   192  				FluentSocketPath:       "",
   193  				BufferLimit:            defaultBufferLimit,
   194  				RetryWait:              int(defaultRetryWait),
   195  				MaxRetry:               100,
   196  				Async:                  false,
   197  				AsyncReconnectInterval: 0,
   198  				SubSecondPrecision:     false,
   199  				RequestAck:             false}, false},
   200  		{"InputSubSecondPrecision", args{
   201  			config: map[string]string{
   202  				fluentdSubSecondPrecision: "true",
   203  			}},
   204  			fluent.Config{
   205  				FluentPort:             defaultPort,
   206  				FluentHost:             defaultHost,
   207  				FluentNetwork:          defaultProtocol,
   208  				FluentSocketPath:       "",
   209  				BufferLimit:            defaultBufferLimit,
   210  				RetryWait:              int(defaultRetryWait),
   211  				MaxRetry:               defaultMaxRetries,
   212  				Async:                  false,
   213  				AsyncReconnectInterval: 0,
   214  				SubSecondPrecision:     true,
   215  				RequestAck:             false}, false},
   216  		{"InputRequestAck", args{
   217  			config: map[string]string{
   218  				fluentRequestAck: "true",
   219  			}},
   220  			fluent.Config{
   221  				FluentPort:             defaultPort,
   222  				FluentHost:             defaultHost,
   223  				FluentNetwork:          defaultProtocol,
   224  				FluentSocketPath:       "",
   225  				BufferLimit:            defaultBufferLimit,
   226  				RetryWait:              int(defaultRetryWait),
   227  				MaxRetry:               defaultMaxRetries,
   228  				Async:                  false,
   229  				AsyncReconnectInterval: 0,
   230  				SubSecondPrecision:     false,
   231  				RequestAck:             true}, false},
   232  	}
   233  	for _, tt := range tests {
   234  		t.Run(tt.name, func(t *testing.T) {
   235  			got, err := parseFluentdConfig(tt.args.config)
   236  			if (err != nil) != tt.wantErr {
   237  				t.Errorf("parseFluentdConfig() error = %v, wantErr %v", err, tt.wantErr)
   238  				return
   239  			}
   240  			if !reflect.DeepEqual(got, tt.want) {
   241  				t.Errorf("parseFluentdConfig() got = %v, want %v", got, tt.want)
   242  			}
   243  		})
   244  	}
   245  }