vitess.io/vitess@v0.16.2/go/vt/logutil/logutil_flaky_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess 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 logutil
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path"
    23  	"path/filepath"
    24  	"testing"
    25  	"time"
    26  )
    27  
    28  func TestParsing(t *testing.T) {
    29  
    30  	path := []string{
    31  		"/tmp/something.foo/zkocc.goedel.szopa.log.INFO.20130806-151006.10530",
    32  		"/tmp/something.foo/zkocc.goedel.szopa.test.log.ERROR.20130806-151006.10530"}
    33  
    34  	for _, filepath := range path {
    35  		ts, err := parseCreatedTimestamp(filepath)
    36  		if err != nil {
    37  			t.Fatalf("parse: %v", err)
    38  		}
    39  
    40  		if want := time.Date(2013, 8, 6, 15, 10, 06, 0, time.Now().Location()); ts != want {
    41  			t.Errorf("timestamp: want %v, got %v", want, ts)
    42  		}
    43  	}
    44  }
    45  
    46  func TestPurgeByCtime(t *testing.T) {
    47  	logDir := path.Join(os.TempDir(), fmt.Sprintf("%v-%v", os.Args[0], os.Getpid()))
    48  	if err := os.MkdirAll(logDir, 0777); err != nil {
    49  		t.Fatalf("os.MkdirAll: %v", err)
    50  	}
    51  	defer os.RemoveAll(logDir)
    52  
    53  	now := time.Date(2013, 8, 6, 15, 10, 06, 0, time.Now().Location())
    54  	files := []string{
    55  		"zkocc.goedel.szopa.log.INFO.20130806-121006.10530",
    56  		"zkocc.goedel.szopa.log.INFO.20130806-131006.10530",
    57  		"zkocc.goedel.szopa.log.INFO.20130806-141006.10530",
    58  		"zkocc.goedel.szopa.log.INFO.20130806-151006.10530",
    59  	}
    60  
    61  	for _, file := range files {
    62  		if _, err := os.Create(path.Join(logDir, file)); err != nil {
    63  			t.Fatalf("os.Create: %v", err)
    64  		}
    65  	}
    66  	if err := os.Symlink(files[1], path.Join(logDir, "zkocc.INFO")); err != nil {
    67  		t.Fatalf("os.Symlink: %v", err)
    68  	}
    69  
    70  	purgeLogsOnce(now, logDir, "zkocc", 30*time.Minute, 0)
    71  
    72  	left, err := filepath.Glob(path.Join(logDir, "zkocc.*"))
    73  	if err != nil {
    74  		t.Fatalf("filepath.Glob: %v", err)
    75  	}
    76  
    77  	if len(left) != 3 {
    78  		// 131006 is current
    79  		// 151006 is within 30 min
    80  		// symlink remains
    81  		// the rest should be removed.
    82  		t.Errorf("wrong number of files remain: want %v, got %v", 3, len(left))
    83  	}
    84  }
    85  
    86  func TestPurgeByMtime(t *testing.T) {
    87  	logDir := path.Join(os.TempDir(), fmt.Sprintf("%v-%v", os.Args[0], os.Getpid()))
    88  	if err := os.MkdirAll(logDir, 0777); err != nil {
    89  		t.Fatalf("os.MkdirAll: %v", err)
    90  	}
    91  	defer os.RemoveAll(logDir)
    92  	createFileWithMtime := func(filename, mtimeStr string) {
    93  		var err error
    94  		var mtime time.Time
    95  		filepath := path.Join(logDir, filename)
    96  		if mtime, err = time.Parse(time.RFC3339, mtimeStr); err != nil {
    97  			t.Fatalf("time.Parse: %v", err)
    98  		}
    99  		if _, err = os.Create(filepath); err != nil {
   100  			t.Fatalf("os.Create: %v", err)
   101  		}
   102  		if err = os.Chtimes(filepath, mtime, mtime); err != nil {
   103  			t.Fatalf("os.Chtimes: %v", err)
   104  		}
   105  	}
   106  	now := time.Date(2020, 1, 1, 12, 0, 0, 0, time.UTC)
   107  	filenameMtimeMap := map[string]string{
   108  		"vtadam.localhost.vitess.log.INFO.20200101-113000.00000": "2020-01-01T11:30:00.000Z",
   109  		"vtadam.localhost.vitess.log.INFO.20200101-100000.00000": "2020-01-01T10:00:00.000Z",
   110  		"vtadam.localhost.vitess.log.INFO.20200101-090000.00000": "2020-01-01T09:00:00.000Z",
   111  		"vtadam.localhost.vitess.log.INFO.20200101-080000.00000": "2020-01-01T08:00:00.000Z",
   112  	}
   113  	for filename, mtimeStr := range filenameMtimeMap {
   114  		createFileWithMtime(filename, mtimeStr)
   115  	}
   116  
   117  	// Create vtadam.INFO symlink to 100000. This is a contrived example in that
   118  	// current log (100000) is not the latest log (113000). This will not happen
   119  	// IRL but it helps us test edge cases of purging by mtime.
   120  	if err := os.Symlink("vtadam.localhost.vitess.log.INFO.20200101-100000.00000", path.Join(logDir, "vtadam.INFO")); err != nil {
   121  		t.Fatalf("os.Symlink: %v", err)
   122  	}
   123  
   124  	purgeLogsOnce(now, logDir, "vtadam", 0, 1*time.Hour)
   125  
   126  	left, err := filepath.Glob(path.Join(logDir, "vtadam.*"))
   127  	if err != nil {
   128  		t.Fatalf("filepath.Glob: %v", err)
   129  	}
   130  
   131  	if len(left) != 3 {
   132  		// 1. 113000 is within 1 hour
   133  		// 2. 100000 is current (vtadam.INFO)
   134  		// 3. vtadam.INFO symlink remains
   135  		// rest are removed
   136  		t.Errorf("wrong number of files remain: want %v, got %v", 3, len(left))
   137  	}
   138  }