legacy_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kuberuntime
  14. import (
  15. "fmt"
  16. "math/rand"
  17. "path"
  18. "testing"
  19. "github.com/stretchr/testify/assert"
  20. )
  21. const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  22. func randStringBytes(n int) string {
  23. b := make([]byte, n)
  24. for i := range b {
  25. b[i] = letterBytes[rand.Intn(len(letterBytes))]
  26. }
  27. return string(b)
  28. }
  29. func TestLogSymLink(t *testing.T) {
  30. as := assert.New(t)
  31. containerLogsDir := "/foo/bar"
  32. podFullName := randStringBytes(128)
  33. containerName := randStringBytes(70)
  34. dockerID := randStringBytes(80)
  35. // The file name cannot exceed 255 characters. Since .log suffix is required, the prefix cannot exceed 251 characters.
  36. expectedPath := path.Join(containerLogsDir, fmt.Sprintf("%s_%s-%s", podFullName, containerName, dockerID)[:251]+".log")
  37. as.Equal(expectedPath, logSymlink(containerLogsDir, podFullName, containerName, dockerID))
  38. }
  39. func TestLegacyLogSymLink(t *testing.T) {
  40. as := assert.New(t)
  41. containerID := randStringBytes(80)
  42. containerName := randStringBytes(70)
  43. podName := randStringBytes(128)
  44. podNamespace := randStringBytes(10)
  45. // The file name cannot exceed 255 characters. Since .log suffix is required, the prefix cannot exceed 251 characters.
  46. expectedPath := path.Join(legacyContainerLogsDir, fmt.Sprintf("%s_%s_%s-%s", podName, podNamespace, containerName, containerID)[:251]+".log")
  47. as.Equal(expectedPath, legacyLogSymlink(containerID, containerName, podName, podNamespace))
  48. }