github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/event/v2/logger_test.go (about)

     1  /*
     2  Copyright 2021 The Skaffold 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 v2
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/sirupsen/logrus"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    25  	"github.com/GoogleContainerTools/skaffold/proto/enums"
    26  	proto "github.com/GoogleContainerTools/skaffold/proto/v2"
    27  	"github.com/GoogleContainerTools/skaffold/testutil"
    28  )
    29  
    30  func TestHandleSkaffoldLogEvent(t *testing.T) {
    31  	testHandler := newHandler()
    32  	testHandler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))
    33  
    34  	messages := []string{
    35  		"hi!",
    36  		"how's it going",
    37  		"hope you're well",
    38  		"this is a skaffold test",
    39  	}
    40  
    41  	// ensure that messages sent through the SkaffoldLog function are populating the event log
    42  	for _, message := range messages {
    43  		testHandler.handleSkaffoldLogEvent(&proto.SkaffoldLogEvent{
    44  			TaskId:    "Test-0",
    45  			SubtaskId: "1",
    46  			Level:     enums.LogLevel_INFO,
    47  			Message:   message,
    48  		})
    49  	}
    50  	wait(t, func() bool {
    51  		testHandler.logLock.Lock()
    52  		logLen := len(testHandler.eventLog)
    53  		testHandler.logLock.Unlock()
    54  		return logLen == len(messages)
    55  	})
    56  }
    57  
    58  func TestLevelFromEntry(t *testing.T) {
    59  	tests := []struct {
    60  		name      string
    61  		logrusLvl logrus.Level
    62  		enumLvl   enums.LogLevel
    63  	}{
    64  		{
    65  			name:      "panic",
    66  			logrusLvl: logrus.PanicLevel,
    67  			enumLvl:   enums.LogLevel_PANIC,
    68  		},
    69  		{
    70  			name:      "fatal",
    71  			logrusLvl: logrus.FatalLevel,
    72  			enumLvl:   enums.LogLevel_FATAL,
    73  		},
    74  		{
    75  			name:      "error",
    76  			logrusLvl: logrus.ErrorLevel,
    77  			enumLvl:   enums.LogLevel_ERROR,
    78  		},
    79  		{
    80  			name:      "warn",
    81  			logrusLvl: logrus.WarnLevel,
    82  			enumLvl:   enums.LogLevel_WARN,
    83  		},
    84  		{
    85  			name:      "info",
    86  			logrusLvl: logrus.InfoLevel,
    87  			enumLvl:   enums.LogLevel_INFO,
    88  		},
    89  		{
    90  			name:      "debug",
    91  			logrusLvl: logrus.DebugLevel,
    92  			enumLvl:   enums.LogLevel_DEBUG,
    93  		},
    94  		{
    95  			name:      "trace",
    96  			logrusLvl: logrus.TraceLevel,
    97  			enumLvl:   enums.LogLevel_TRACE,
    98  		},
    99  	}
   100  
   101  	for _, test := range tests {
   102  		testutil.Run(t, test.name, func(t *testutil.T) {
   103  			got := levelFromEntry(&logrus.Entry{Level: test.logrusLvl})
   104  			t.CheckDeepEqual(test.enumLvl, got)
   105  		})
   106  	}
   107  }