...

Source file src/k8s.io/klog/v2/internal/verbosity/verbosity_test.go

Documentation: k8s.io/klog/v2/internal/verbosity

     1  /*
     2  Copyright 2013 Google Inc. All Rights Reserved.
     3  Copyright 2022 The Kubernetes Authors.
     4  
     5  Licensed under the Apache License, Version 2.0 (the "License");
     6  you may not use this file except in compliance with the License.
     7  You may obtain a copy of the License at
     8  
     9      http://www.apache.org/licenses/LICENSE-2.0
    10  
    11  Unless required by applicable law or agreed to in writing, software
    12  distributed under the License is distributed on an "AS IS" BASIS,
    13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  See the License for the specific language governing permissions and
    15  limitations under the License.
    16  */
    17  
    18  package verbosity
    19  
    20  import (
    21  	"testing"
    22  
    23  	"k8s.io/klog/v2/internal/test/require"
    24  )
    25  
    26  func TestV(t *testing.T) {
    27  	vs := New()
    28  	require.NoError(t, vs.verbosity.Set("2"))
    29  	depth := 0
    30  	if !vs.Enabled(1, depth) {
    31  		t.Error("not enabled for 1")
    32  	}
    33  	if !vs.Enabled(2, depth) {
    34  		t.Error("not enabled for 2")
    35  	}
    36  	if vs.Enabled(3, depth) {
    37  		t.Error("enabled for 3")
    38  	}
    39  }
    40  
    41  func TestVmoduleOn(t *testing.T) {
    42  	vs := New()
    43  	require.NoError(t, vs.vmodule.Set("verbosity_test=2"))
    44  	depth := 0
    45  	if !vs.Enabled(1, depth) {
    46  		t.Error("not enabled for 1")
    47  	}
    48  	if !vs.Enabled(2, depth) {
    49  		t.Error("not enabled for 2")
    50  	}
    51  	if vs.Enabled(3, depth) {
    52  		t.Error("enabled for 3")
    53  	}
    54  	if enabledInHelper(vs, 1) {
    55  		t.Error("enabled for helper at 1")
    56  	}
    57  	if enabledInHelper(vs, 2) {
    58  		t.Error("enabled for helper at 2")
    59  	}
    60  	if enabledInHelper(vs, 3) {
    61  		t.Error("enabled for helper at 3")
    62  	}
    63  }
    64  
    65  // vGlobs are patterns that match/don't match this file at V=2.
    66  var vGlobs = map[string]bool{
    67  	// Easy to test the numeric match here.
    68  	"verbosity_test=1": false, // If -vmodule sets V to 1, V(2) will fail.
    69  	"verbosity_test=2": true,
    70  	"verbosity_test=3": true, // If -vmodule sets V to 1, V(3) will succeed.
    71  	// These all use 2 and check the patterns. All are true.
    72  	"*=2":                true,
    73  	"?e*=2":              true,
    74  	"?????????_*=2":      true,
    75  	"??[arx]??????_*t=2": true,
    76  	// These all use 2 and check the patterns. All are false.
    77  	"*x=2":         false,
    78  	"m*=2":         false,
    79  	"??_*=2":       false,
    80  	"?[abc]?_*t=2": false,
    81  }
    82  
    83  // Test that vmodule globbing works as advertised.
    84  func testVmoduleGlob(pat string, match bool, t *testing.T) {
    85  	vs := New()
    86  	require.NoError(t, vs.vmodule.Set(pat))
    87  	depth := 0
    88  	actual := vs.Enabled(2, depth)
    89  	if actual != match {
    90  		t.Errorf("incorrect match for %q: got %#v expected %#v", pat, actual, match)
    91  	}
    92  }
    93  
    94  // Test that a vmodule globbing works as advertised.
    95  func TestVmoduleGlob(t *testing.T) {
    96  	for glob, match := range vGlobs {
    97  		testVmoduleGlob(glob, match, t)
    98  	}
    99  }
   100  

View as plain text