k8s.io/kubernetes@v1.29.3/pkg/routes/logs_test.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes 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 routes
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  )
    25  
    26  func TestPreCheckLogFileNameLength(t *testing.T) {
    27  	// In windows, with long file name support enabled, file names can have up to 32,767 characters.
    28  	oversizeFileName := fmt.Sprintf("%032768s", "a")
    29  	normalFileName := fmt.Sprintf("%0255s", "a")
    30  
    31  	// check file with oversize name.
    32  	if !logFileNameIsTooLong(oversizeFileName) {
    33  		t.Error("failed to check oversize filename")
    34  	}
    35  
    36  	// check file with normal name which doesn't exist.
    37  	if logFileNameIsTooLong(normalFileName) {
    38  		t.Error("failed to check normal filename")
    39  	}
    40  
    41  	// check file with normal name which does exist.
    42  	dir, err := os.MkdirTemp("", "logs")
    43  	if err != nil {
    44  		t.Fatal("failed to create temp dir")
    45  	}
    46  	defer os.RemoveAll(dir)
    47  
    48  	normalFileName = filepath.Join(dir, normalFileName)
    49  	f, err := os.Create(normalFileName)
    50  	if err != nil {
    51  		t.Error("failed to create test file")
    52  	}
    53  	defer os.Remove(normalFileName)
    54  	defer f.Close()
    55  	if logFileNameIsTooLong(normalFileName) {
    56  		t.Error("failed to check normal filename")
    57  	}
    58  }