github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/clients/pkg/promtail/targets/windows/win_eventlog/win_eventlog_test.go (about)

     1  // The MIT License (MIT)
     2  
     3  // Copyright (c) 2015-2020 InfluxData Inc.
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in all
    13  // copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    21  // SOFTWARE.
    22  //go:build windows
    23  // +build windows
    24  
    25  //revive:disable-next-line:var-naming
    26  // Package win_eventlog Input plugin to collect Windows Event Log messages
    27  package win_eventlog
    28  
    29  import (
    30  	"testing"
    31  )
    32  
    33  func TestWinEventLog_shouldExcludeEmptyField(t *testing.T) {
    34  	type args struct {
    35  		field      string
    36  		fieldType  string
    37  		fieldValue interface{}
    38  	}
    39  	tests := []struct {
    40  		name       string
    41  		w          *WinEventLog
    42  		args       args
    43  		wantShould bool
    44  	}{
    45  		{
    46  			name:       "Not in list",
    47  			args:       args{field: "qq", fieldType: "string", fieldValue: ""},
    48  			wantShould: false,
    49  			w:          &WinEventLog{ExcludeEmpty: []string{"te*"}},
    50  		},
    51  		{
    52  			name:       "Empty string",
    53  			args:       args{field: "test", fieldType: "string", fieldValue: ""},
    54  			wantShould: true,
    55  			w:          &WinEventLog{ExcludeEmpty: []string{"te*"}},
    56  		},
    57  		{
    58  			name:       "Non-empty string",
    59  			args:       args{field: "test", fieldType: "string", fieldValue: "qq"},
    60  			wantShould: false,
    61  			w:          &WinEventLog{ExcludeEmpty: []string{"te*"}},
    62  		},
    63  		{
    64  			name:       "Zero int",
    65  			args:       args{field: "test", fieldType: "int", fieldValue: int(0)},
    66  			wantShould: true,
    67  			w:          &WinEventLog{ExcludeEmpty: []string{"te*"}},
    68  		},
    69  		{
    70  			name:       "Non-zero int",
    71  			args:       args{field: "test", fieldType: "int", fieldValue: int(-1)},
    72  			wantShould: false,
    73  			w:          &WinEventLog{ExcludeEmpty: []string{"te*"}},
    74  		},
    75  		{
    76  			name:       "Zero uint32",
    77  			args:       args{field: "test", fieldType: "uint32", fieldValue: uint32(0)},
    78  			wantShould: true,
    79  			w:          &WinEventLog{ExcludeEmpty: []string{"te*"}},
    80  		},
    81  		{
    82  			name:       "Non-zero uint32",
    83  			args:       args{field: "test", fieldType: "uint32", fieldValue: uint32(0xc0fefeed)},
    84  			wantShould: false,
    85  			w:          &WinEventLog{ExcludeEmpty: []string{"te*"}},
    86  		},
    87  	}
    88  	for _, tt := range tests {
    89  		t.Run(tt.name, func(t *testing.T) {
    90  			if gotShould := tt.w.shouldExcludeEmptyField(tt.args.field, tt.args.fieldType, tt.args.fieldValue); gotShould != tt.wantShould {
    91  				t.Errorf("WinEventLog.shouldExcludeEmptyField() = %v, want %v", gotShould, tt.wantShould)
    92  			}
    93  		})
    94  	}
    95  }
    96  
    97  func TestWinEventLog_shouldProcessField(t *testing.T) {
    98  	tags := []string{"Source", "Level*"}
    99  	fields := []string{"EventID", "Message*"}
   100  	excluded := []string{"Message*"}
   101  	type args struct {
   102  		field string
   103  	}
   104  	tests := []struct {
   105  		name       string
   106  		w          *WinEventLog
   107  		args       args
   108  		wantShould bool
   109  		wantList   string
   110  	}{
   111  		{
   112  			name:       "Not in tags",
   113  			args:       args{field: "test"},
   114  			wantShould: false,
   115  			wantList:   "excluded",
   116  			w:          &WinEventLog{EventTags: tags, EventFields: fields, ExcludeFields: excluded},
   117  		},
   118  		{
   119  			name:       "In Tags",
   120  			args:       args{field: "LevelText"},
   121  			wantShould: true,
   122  			wantList:   "tags",
   123  			w:          &WinEventLog{EventTags: tags, EventFields: fields, ExcludeFields: excluded},
   124  		},
   125  		{
   126  			name:       "Not in Fields",
   127  			args:       args{field: "EventId"},
   128  			wantShould: false,
   129  			wantList:   "excluded",
   130  			w:          &WinEventLog{EventTags: tags, EventFields: fields, ExcludeFields: excluded},
   131  		},
   132  		{
   133  			name:       "In Fields",
   134  			args:       args{field: "EventID"},
   135  			wantShould: true,
   136  			wantList:   "fields",
   137  			w:          &WinEventLog{EventTags: tags, EventFields: fields, ExcludeFields: excluded},
   138  		},
   139  		{
   140  			name:       "In Fields and Excluded",
   141  			args:       args{field: "Messages"},
   142  			wantShould: false,
   143  			wantList:   "excluded",
   144  			w:          &WinEventLog{EventTags: tags, EventFields: fields, ExcludeFields: excluded},
   145  		},
   146  	}
   147  	for _, tt := range tests {
   148  		t.Run(tt.name, func(t *testing.T) {
   149  			gotShould, gotList := tt.w.shouldProcessField(tt.args.field)
   150  			if gotShould != tt.wantShould {
   151  				t.Errorf("WinEventLog.shouldProcessField() gotShould = %v, want %v", gotShould, tt.wantShould)
   152  			}
   153  			if gotList != tt.wantList {
   154  				t.Errorf("WinEventLog.shouldProcessField() gotList = %v, want %v", gotList, tt.wantList)
   155  			}
   156  		})
   157  	}
   158  }