sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/log/logger_test.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes 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 log
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	"github.com/pkg/errors"
    24  	"k8s.io/utils/ptr"
    25  )
    26  
    27  func TestFlatten(t *testing.T) {
    28  	type args struct {
    29  		prefix string
    30  		kvList []interface{}
    31  	}
    32  	tests := []struct {
    33  		name string
    34  		args args
    35  		want string
    36  	}{
    37  		{
    38  			name: "message without values",
    39  			args: args{
    40  				prefix: "",
    41  				kvList: []interface{}{
    42  					"msg", "this is a message",
    43  				},
    44  			},
    45  			want: "this is a message",
    46  		},
    47  		{
    48  			name: "message with values",
    49  			args: args{
    50  				prefix: "",
    51  				kvList: []interface{}{
    52  					"msg", "this is a message",
    53  					"val1", 123,
    54  					"val2", "string",
    55  					"val3", "string with spaces",
    56  				},
    57  			},
    58  			want: "this is a message val1=123 val2=\"string\" val3=\"string with spaces\"",
    59  		},
    60  		{
    61  			name: "error without values",
    62  			args: args{
    63  				prefix: "",
    64  				kvList: []interface{}{
    65  					"msg", "this is a message",
    66  					"error", errors.New("this is an error"),
    67  				},
    68  			},
    69  			want: "this is a message: this is an error",
    70  		},
    71  		{
    72  			name: "error with values",
    73  			args: args{
    74  				prefix: "",
    75  				kvList: []interface{}{
    76  					"msg", "this is a message",
    77  					"error", errors.New("this is an error"),
    78  					"val1", 123,
    79  				},
    80  			},
    81  			want: "this is a message: this is an error val1=123",
    82  		},
    83  		{
    84  			name: "message with prefix",
    85  			args: args{
    86  				prefix: "a\\b",
    87  				kvList: []interface{}{
    88  					"msg", "this is a message",
    89  				},
    90  			},
    91  			want: "[a\\b] this is a message",
    92  		},
    93  	}
    94  	for _, tt := range tests {
    95  		t.Run(tt.name, func(t *testing.T) {
    96  			g := NewWithT(t)
    97  
    98  			got, err := flatten(logEntry{
    99  				Prefix: tt.args.prefix,
   100  				Level:  0,
   101  				Values: tt.args.kvList,
   102  			})
   103  			g.Expect(err).ToNot(HaveOccurred())
   104  			g.Expect(got).To(Equal(tt.want))
   105  		})
   106  	}
   107  }
   108  
   109  func TestLoggerEnabled(t *testing.T) {
   110  	tests := []struct {
   111  		name      string
   112  		threshold *int
   113  		level     int
   114  		want      bool
   115  	}{
   116  		{
   117  			name:      "Return true when level is set below the threshold",
   118  			threshold: ptr.To(5),
   119  			level:     1,
   120  			want:      true,
   121  		},
   122  		{
   123  			name:  "Return true when no threshold set",
   124  			level: 7,
   125  			want:  true,
   126  		},
   127  		{
   128  			name:      "Return false when level is set above the threshold",
   129  			threshold: ptr.To(5),
   130  			level:     7,
   131  			want:      false,
   132  		},
   133  	}
   134  	for _, tt := range tests {
   135  		t.Run(tt.name, func(t *testing.T) {
   136  			l := &logger{
   137  				threshold: tt.threshold,
   138  			}
   139  			g := NewWithT(t)
   140  			got := l.Enabled(tt.level)
   141  			g.Expect(got).To(Equal(tt.want))
   142  		})
   143  	}
   144  }