...

Source file src/github.com/dsoprea/go-logging/v2/log_test.go

Documentation: github.com/dsoprea/go-logging/v2

     1  package log
     2  
     3  import (
     4  	e "errors"
     5  	"testing"
     6  
     7  	"math/rand"
     8  )
     9  
    10  // Extends the default environment configuration-provider to set the level to
    11  // "debug" (so none of our messages get filtered).
    12  type testConfigurationProvider struct {
    13  	levelName LogLevelName
    14  }
    15  
    16  func newTestConfigurationProvider(levelName LogLevelName) ConfigurationProvider {
    17  	if levelName == "" {
    18  		levelName = levelNameError
    19  	}
    20  
    21  	return &testConfigurationProvider{
    22  		levelName: levelName,
    23  	}
    24  }
    25  
    26  func (ec *testConfigurationProvider) Format() string {
    27  	return ""
    28  }
    29  
    30  func (ec *testConfigurationProvider) DefaultAdapterName() string {
    31  	return ""
    32  }
    33  
    34  func (ec *testConfigurationProvider) LevelName() LogLevelName {
    35  	return ec.levelName
    36  }
    37  
    38  func (ec *testConfigurationProvider) IncludeNouns() string {
    39  	return ""
    40  }
    41  
    42  func (ec *testConfigurationProvider) ExcludeNouns() string {
    43  	return ""
    44  }
    45  
    46  func (ec *testConfigurationProvider) ExcludeBypassLevelName() LogLevelName {
    47  	return ""
    48  }
    49  
    50  // A test logging-adapter that sets flags as certain messages are received.
    51  type testLogAdapter struct {
    52  	id int
    53  
    54  	debugTriggered   bool
    55  	infoTriggered    bool
    56  	warningTriggered bool
    57  	errorTriggered   bool
    58  }
    59  
    60  func newTestLogAdapter() LogAdapter {
    61  	return &testLogAdapter{
    62  		id: rand.Int(),
    63  	}
    64  }
    65  
    66  func (tla *testLogAdapter) Debugf(lc *LogContext, message *string) error {
    67  	tla.debugTriggered = true
    68  
    69  	return nil
    70  }
    71  
    72  func (tla *testLogAdapter) Infof(lc *LogContext, message *string) error {
    73  	tla.infoTriggered = true
    74  
    75  	return nil
    76  }
    77  
    78  func (tla *testLogAdapter) Warningf(lc *LogContext, message *string) error {
    79  	tla.warningTriggered = true
    80  
    81  	return nil
    82  }
    83  
    84  func (tla *testLogAdapter) Errorf(lc *LogContext, message *string) error {
    85  	tla.errorTriggered = true
    86  
    87  	return nil
    88  }
    89  
    90  // Tests
    91  
    92  func TestConfigurationOverride(t *testing.T) {
    93  	cs := getConfigState()
    94  	defer func() {
    95  		setConfigState(cs)
    96  	}()
    97  
    98  	levelName = "xyz"
    99  
   100  	// Overwrite configuration, first thing.
   101  	tcp := newTestConfigurationProvider(levelNameDebug)
   102  	LoadConfiguration(tcp)
   103  
   104  	if levelName != levelNameDebug {
   105  		t.Fatalf("The test configuration-provider didn't override the level properly: [%s]", levelName)
   106  	}
   107  }
   108  
   109  func TestConfigurationLevelDirectOverride(t *testing.T) {
   110  	// Overwrite configuration, first thing.
   111  	tcp := newTestConfigurationProvider("")
   112  	LoadConfiguration(tcp)
   113  
   114  	ClearAdapters()
   115  
   116  	tla1 := newTestLogAdapter()
   117  	AddAdapter("test", tla1)
   118  
   119  	l := NewLoggerWithAdapterName("logTest", "test")
   120  
   121  	// Usually we don't configure until the first message. Force it.
   122  	l.doConfigure(false)
   123  	tla2 := l.Adapter().(*testLogAdapter)
   124  
   125  	if tla2.debugTriggered != false {
   126  		t.Fatalf("Debug flag should've been FALSE initially but wasn't.")
   127  	}
   128  
   129  	// Set the level high to prevent logging, first.
   130  	levelName = levelNameError
   131  
   132  	// Force a reconfig (which will bring in the new level).
   133  	l.doConfigure(true)
   134  
   135  	// Re-retrieve. This is reconstructed during reconfiguration.
   136  	tla3 := l.Adapter().(*testLogAdapter)
   137  
   138  	l.Debugf(nil, "Debug message")
   139  
   140  	if tla3.debugTriggered != false {
   141  		t.Fatalf("Debug message not through but wasn't supposed to.")
   142  	}
   143  
   144  	// Now, set the level low to allow logging.
   145  	levelName = levelNameDebug
   146  
   147  	// Force a reconfig (which will bring in the new level).
   148  	l.doConfigure(true)
   149  
   150  	// Re-retrieve. This is reconstructed during reconfiguration.
   151  	tla4 := l.Adapter().(*testLogAdapter)
   152  
   153  	l.Debugf(nil, "Debug message")
   154  
   155  	if tla4.debugTriggered == false {
   156  		t.Fatalf("Debug message not getting through.")
   157  	}
   158  }
   159  
   160  func TestConfigurationLevelProviderOverride(t *testing.T) {
   161  	cs := getConfigState()
   162  	defer func() {
   163  		setConfigState(cs)
   164  	}()
   165  
   166  	// Overwrite configuration, first thing.
   167  	tcp := newTestConfigurationProvider("")
   168  	LoadConfiguration(tcp)
   169  
   170  	ClearAdapters()
   171  
   172  	tla1 := newTestLogAdapter()
   173  	AddAdapter("test", tla1)
   174  
   175  	l := NewLoggerWithAdapterName("logTest", "test")
   176  
   177  	// Usually we don't configure until the first message. Force it.
   178  	l.doConfigure(false)
   179  	tla2 := l.Adapter().(*testLogAdapter)
   180  
   181  	if tla2.debugTriggered != false {
   182  		t.Fatalf("Debug flag should've been FALSE initially but wasn't.")
   183  	}
   184  
   185  	// Set the level high to prevent logging, first.
   186  	tcp = newTestConfigurationProvider(levelNameError)
   187  	LoadConfiguration(tcp)
   188  
   189  	// Force a reconfig (which will bring in the new level).
   190  	l.doConfigure(true)
   191  
   192  	// Re-retrieve. This is reconstructed during reconfiguration.
   193  	tla3 := l.Adapter().(*testLogAdapter)
   194  
   195  	l.Debugf(nil, "Debug message")
   196  
   197  	if tla3.debugTriggered != false {
   198  		t.Fatalf("Debug message not through but wasn't supposed to.")
   199  	}
   200  
   201  	// Now, set the level low to allow logging.
   202  	tcp = newTestConfigurationProvider(levelNameDebug)
   203  	LoadConfiguration(tcp)
   204  
   205  	// Force a reconfig (which will bring in the new level).
   206  	l.doConfigure(true)
   207  
   208  	// Re-retrieve. This is reconstructed during reconfiguration.
   209  	tla4 := l.Adapter().(*testLogAdapter)
   210  
   211  	l.Debugf(nil, "Debug message")
   212  
   213  	if tla4.debugTriggered == false {
   214  		t.Fatalf("Debug message not getting through.")
   215  	}
   216  }
   217  
   218  func TestDefaultAdapterAssignment(t *testing.T) {
   219  	SetDefaultAdapterName("")
   220  
   221  	ClearAdapters()
   222  
   223  	tla := newTestLogAdapter()
   224  	AddAdapter("test1", tla)
   225  
   226  	an := GetDefaultAdapterName()
   227  	if an == "" {
   228  		t.Fatalf("Default adapter not set after registration.")
   229  	}
   230  
   231  	an = GetDefaultAdapterName()
   232  	if an != "test1" {
   233  		t.Fatalf("Default adapter not set to our adapter after registration: [%v]", an)
   234  	}
   235  
   236  	SetDefaultAdapterName("test2")
   237  	an = GetDefaultAdapterName()
   238  	if an != "test2" {
   239  		t.Fatalf("SetDefaultAdapterName() did not set default adapter correctly: [%v]", an)
   240  	}
   241  }
   242  
   243  func TestAdapter(t *testing.T) {
   244  	cs := getConfigState()
   245  	defer func() {
   246  		setConfigState(cs)
   247  	}()
   248  
   249  	// Overwrite configuration, first thing.
   250  	tcp := newTestConfigurationProvider(levelNameDebug)
   251  	LoadConfiguration(tcp)
   252  
   253  	ClearAdapters()
   254  
   255  	tla1 := newTestLogAdapter()
   256  	AddAdapter("test", tla1)
   257  
   258  	l := NewLoggerWithAdapterName("logTest", "test")
   259  
   260  	l.doConfigure(false)
   261  
   262  	tla2 := l.Adapter().(*testLogAdapter)
   263  
   264  	l.Debugf(nil, "Debug message")
   265  	if tla2.debugTriggered == false {
   266  		t.Fatalf("Debug message not getting through.")
   267  	}
   268  
   269  	l.Infof(nil, "Info message")
   270  	if tla2.infoTriggered == false {
   271  		t.Fatalf("Info message not getting through.")
   272  	}
   273  
   274  	l.Warningf(nil, "Warning message")
   275  	if tla2.warningTriggered == false {
   276  		t.Fatalf("Warning message not getting through.")
   277  	}
   278  
   279  	err := e.New("an error happened")
   280  	l.Errorf(nil, err, "Error message")
   281  	if tla2.errorTriggered == false {
   282  		t.Fatalf("Error message not getting through.")
   283  	}
   284  }
   285  
   286  func TestStaticConfiguration(t *testing.T) {
   287  	scp := NewStaticConfigurationProvider()
   288  
   289  	cs := getConfigState()
   290  	defer func() {
   291  		setConfigState(cs)
   292  	}()
   293  
   294  	scp.SetFormat("aa")
   295  	scp.SetDefaultAdapterName("bb")
   296  	scp.SetLevelName("cc")
   297  	scp.SetIncludeNouns("dd")
   298  	scp.SetExcludeNouns("ee")
   299  	scp.SetExcludeBypassLevelName("ff")
   300  
   301  	LoadConfiguration(scp)
   302  
   303  	if format != "aa" {
   304  		t.Fatalf("Static configuration provider was not set correctly: format")
   305  	}
   306  
   307  	if defaultAdapterName != "bb" {
   308  		t.Fatalf("Static configuration provider was not set correctly: defaultAdapterName")
   309  	}
   310  
   311  	if levelName != "cc" {
   312  		t.Fatalf("Static configuration provider was not set correctly: levelName")
   313  	}
   314  
   315  	if includeNouns != "dd" {
   316  		t.Fatalf("Static configuration provider was not set correctly: includeNouns")
   317  	}
   318  
   319  	if excludeNouns != "ee" {
   320  		t.Fatalf("Static configuration provider was not set correctly: excludeNouns")
   321  	}
   322  
   323  	if excludeBypassLevelName != "ff" {
   324  		t.Fatalf("Static configuration provider was not set correctly: excludeBypassLevelName")
   325  	}
   326  }
   327  
   328  func TestNoAdapter(t *testing.T) {
   329  	ClearAdapters()
   330  
   331  	l := NewLogger("logTest")
   332  
   333  	if l.Adapter() != nil {
   334  		t.Fatalf("Logger has an adapter at init when no adapters were available.")
   335  	}
   336  
   337  	l.doConfigure(false)
   338  
   339  	if l.Adapter() != nil {
   340  		t.Fatalf("Logger has an adapter after configuration no adapters were available.")
   341  	}
   342  
   343  	// Should execute, but nothing will happen.
   344  	err := e.New("an error happened")
   345  	l.Errorf(nil, err, "Error message")
   346  }
   347  
   348  func TestNewLogger(t *testing.T) {
   349  	noun := "logTest"
   350  
   351  	l := NewLogger(noun)
   352  	if l.noun != noun {
   353  		t.Fatalf("Noun not correct: [%s]", l.noun)
   354  	}
   355  }
   356  
   357  func TestNewLoggerWithAdapterName(t *testing.T) {
   358  	noun := "logTest"
   359  
   360  	originalDefaultAdapterName := GetDefaultAdapterName()
   361  
   362  	adapterName := "abcdef"
   363  
   364  	cla := NewConsoleLogAdapter()
   365  	AddAdapter(adapterName, cla)
   366  
   367  	SetDefaultAdapterName(adapterName)
   368  
   369  	defer func() {
   370  		SetDefaultAdapterName(originalDefaultAdapterName)
   371  		delete(adapters, adapterName)
   372  	}()
   373  
   374  	l := NewLoggerWithAdapterName(noun, adapterName)
   375  	if l.noun != noun {
   376  		t.Fatalf("Noun not correct: [%s]", l.noun)
   377  	} else if l.an != adapterName {
   378  		t.Fatalf("Adapter-name not correct: [%s]", l.an)
   379  	}
   380  }
   381  
   382  func TestIs__unwrapped__hit(t *testing.T) {
   383  	e1 := e.New("test error")
   384  	if Is(e1, e1) != true {
   385  		t.Fatalf("Is() should be true for an unwrapped success")
   386  	}
   387  }
   388  
   389  func TestIs__unwrapped__miss(t *testing.T) {
   390  	e1 := e.New("test error")
   391  	e2 := e.New("test error 2")
   392  
   393  	if Is(e1, e2) != false {
   394  		t.Fatalf("Is() should be false for an unwrapped failure")
   395  	}
   396  }
   397  
   398  func TestIs__wrapped__hit(t *testing.T) {
   399  	e2 := e.New("test error")
   400  	e1 := Wrap(e2)
   401  	if Is(e1, e2) != true {
   402  		t.Fatalf("Is() should be true for a wrapped success")
   403  	}
   404  }
   405  
   406  func TestIs__wrapped__miss(t *testing.T) {
   407  	e1 := Errorf("test error")
   408  	e2 := e.New("test error 2")
   409  
   410  	if Is(e1, e2) != false {
   411  		t.Fatalf("Is() should be false for a wrapped failure")
   412  	}
   413  }
   414  

View as plain text