- 
        Couldn't load subscription status. 
- Fork 25
VC-35738: Assert the logs in TestNoneCache #619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            maelvls
  wants to merge
  1
  commit into
  master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
VC-35738/improve-test
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package testutil | ||
|  | ||
| import ( | ||
| "regexp" | ||
| ) | ||
|  | ||
| // Replaces the klog and JSON timestamps with a static timestamp to make it | ||
| // easier to assert the logs. It also replaces the line number with 000 as it | ||
| // often changes. | ||
| // | ||
| // From: I1018 15:12:57.953433 22183 logs.go:000] log | ||
| // To: I0000 00:00:00.000000 00000 logs.go:000] log | ||
| // | ||
| // From: I1018 15:12:57.953433] log | ||
| // To: I0000 00:00:00.000000] log | ||
| // | ||
| // From: {"ts":1729258473588.828,"caller":"log/log.go:000","msg":"log Print","v":0} | ||
| // To: {"ts":0000000000000.000,"caller":"log/log.go:000","msg":"log Print","v":0} | ||
| // | ||
| // From: 2024/10/18 15:40:50 log Print | ||
| // To: 0000/00/00 00:00:00 log Print | ||
| func ReplaceWithStaticTimestamps(input string) string { | ||
| input = timestampRegexpKlog.ReplaceAllString(input, "0000 00:00:00.000000 00000") | ||
| input = timestampRegexpKlogAlt.ReplaceAllString(input, "0000 00:00:00.000000") | ||
| input = timestampRegexpJSON.ReplaceAllString(input, `"ts":0000000000000.000`) | ||
| input = timestampRegexpStdLog.ReplaceAllString(input, "0000/00/00 00:00:00") | ||
| input = fileAndLineRegexpJSON.ReplaceAllString(input, `"caller":"$1.go:000"`) | ||
| input = fileAndLineRegexpKlog.ReplaceAllString(input, " $1.go:000") | ||
| return input | ||
| } | ||
|  | ||
| var ( | ||
| timestampRegexpStdLog = regexp.MustCompile(`\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}`) | ||
|  | ||
| timestampRegexpKlog = regexp.MustCompile(`\d{4} \d{2}:\d{2}:\d{2}\.\d{6} +\d+`) | ||
| timestampRegexpKlogAlt = regexp.MustCompile(`\d{4} \d{2}:\d{2}:\d{2}\.\d{6}`) | ||
| fileAndLineRegexpKlog = regexp.MustCompile(` ([^:]+).go:\d+`) | ||
|  | ||
| timestampRegexpJSON = regexp.MustCompile(`"ts":\d+\.?\d*`) | ||
| fileAndLineRegexpJSON = regexp.MustCompile(`"caller":"([^"]+).go:\d+"`) | ||
| ) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package testutil | ||
|  | ||
| import ( | ||
| "testing" | ||
|  | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|  | ||
| func TestReplaceWithStaticTimestamps(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "klog", | ||
| input: `I1018 15:20:42.861239 2386 logs_test.go:13] "Contextual Info Level 3" logger="foo" key="value"`, | ||
| expected: `I0000 00:00:00.000000 00000 logs_test.go:000] "Contextual Info Level 3" logger="foo" key="value"`, | ||
| }, | ||
| { | ||
| name: "klog without process ID and without file name", | ||
| input: `E1114 11:15:39.455086] Cache update failure err="not a cacheResource type: *k8s.notCachable missing metadata/uid field" operation="add"`, | ||
| expected: `E0000 00:00:00.000000] Cache update failure err="not a cacheResource type: *k8s.notCachable missing metadata/uid field" operation="add"`, | ||
| }, | ||
| { | ||
| name: "json-with-nanoseconds", | ||
| input: `{"ts":1729270111728.125,"caller":"logs/logs_test.go:000","msg":"slog Warn","v":0}`, | ||
| expected: `{"ts":0000000000000.000,"caller":"logs/logs_test.go:000","msg":"slog Warn","v":0}`, | ||
| }, | ||
| { | ||
| name: "json-might-not-have-nanoseconds", | ||
| input: `{"ts":1729270111728,"caller":"logs/logs_test.go:000","msg":"slog Info","v":0}`, | ||
| expected: `{"ts":0000000000000.000,"caller":"logs/logs_test.go:000","msg":"slog Info","v":0}`, | ||
| }, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| assert.Equal(t, test.expected, ReplaceWithStaticTimestamps(test.input)) | ||
| }) | ||
| } | ||
| } | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In config_test I used ktesting like this:
jetstack-secure/pkg/agent/config_test.go
Lines 998 to 1003 in 1a567ef
The advantage is that you don't have to worry about timestamps in the logs; you only assert that the logged messages have the expected severity and that the message and additional structured fields are visible.
An additional advantage, and the real reason I created this test, was to be able to see and verify that the correct line numbers were being logged.
I had forgotten to use the
WithHelpermodifier in the logging helper function and thego test -voutput helped me get that right.jetstack-secure/pkg/datagatherer/k8s/cache.go
Lines 34 to 40 in 1a567ef