...

Source file src/github.com/prometheus/common/promlog/log_test.go

Documentation: github.com/prometheus/common/promlog

     1  // Copyright 2020 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package promlog
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  
    20  	"github.com/go-kit/log/level"
    21  	"gopkg.in/yaml.v2"
    22  )
    23  
    24  // Make sure creating and using a logger with an empty configuration doesn't
    25  // result in a panic.
    26  func TestDefaultConfig(t *testing.T) {
    27  	logger := New(&Config{})
    28  
    29  	if err := logger.Log("hello", "world"); err != nil {
    30  		t.Fatal(err)
    31  	}
    32  }
    33  
    34  func TestUnmarshallLevel(t *testing.T) {
    35  	l := &AllowedLevel{}
    36  	err := yaml.Unmarshal([]byte(`debug`), l)
    37  	if err != nil {
    38  		t.Error(err)
    39  	}
    40  	if l.s != "debug" {
    41  		t.Errorf("expected %s, got %s", "debug", l.s)
    42  	}
    43  }
    44  
    45  func TestUnmarshallEmptyLevel(t *testing.T) {
    46  	l := &AllowedLevel{}
    47  	err := yaml.Unmarshal([]byte(``), l)
    48  	if err != nil {
    49  		t.Error(err)
    50  	}
    51  	if l.s != "" {
    52  		t.Errorf("expected empty level, got %s", l.s)
    53  	}
    54  }
    55  
    56  func TestUnmarshallBadLevel(t *testing.T) {
    57  	l := &AllowedLevel{}
    58  	err := yaml.Unmarshal([]byte(`debugg`), l)
    59  	if err == nil {
    60  		t.Error("expected error")
    61  	}
    62  	expErr := `unrecognized log level "debugg"`
    63  	if err.Error() != expErr {
    64  		t.Errorf("expected error %s, got %s", expErr, err.Error())
    65  	}
    66  	if l.s != "" {
    67  		t.Errorf("expected empty level, got %s", l.s)
    68  	}
    69  }
    70  
    71  type recordKeyvalLogger struct {
    72  	count int
    73  }
    74  
    75  func (r *recordKeyvalLogger) Log(keyvals ...interface{}) error {
    76  	for _, v := range keyvals {
    77  		if fmt.Sprintf("%v", v) == "Log level changed" {
    78  			return nil
    79  		}
    80  	}
    81  	r.count++
    82  	return nil
    83  }
    84  
    85  func TestDynamic(t *testing.T) {
    86  	logger := NewDynamic(&Config{})
    87  
    88  	debugLevel := &AllowedLevel{}
    89  	if err := debugLevel.Set("debug"); err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	infoLevel := &AllowedLevel{}
    93  	if err := infoLevel.Set("info"); err != nil {
    94  		t.Fatal(err)
    95  	}
    96  
    97  	recorder := &recordKeyvalLogger{}
    98  	logger.base = recorder
    99  	logger.SetLevel(debugLevel)
   100  	if err := level.Debug(logger).Log("hello", "world"); err != nil {
   101  		t.Fatal(err)
   102  	}
   103  	if recorder.count != 1 {
   104  		t.Fatal("log not found")
   105  	}
   106  
   107  	recorder.count = 0
   108  	logger.SetLevel(infoLevel)
   109  	if err := level.Debug(logger).Log("hello", "world"); err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	if recorder.count != 0 {
   113  		t.Fatal("log found")
   114  	}
   115  	if err := level.Info(logger).Log("hello", "world"); err != nil {
   116  		t.Fatal(err)
   117  	}
   118  	if recorder.count != 1 {
   119  		t.Fatal("log not found")
   120  	}
   121  	if err := level.Debug(logger).Log("hello", "world"); err != nil {
   122  		t.Fatal(err)
   123  	}
   124  	if recorder.count != 1 {
   125  		t.Fatal("extra log found")
   126  	}
   127  }
   128  

View as plain text