|
| 1 | +/* |
| 2 | +Copyright 2021. |
| 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +func TestGetContainerLogPath(t *testing.T) { |
| 26 | + tests := []struct { |
| 27 | + name string |
| 28 | + envValue string |
| 29 | + setupEnvFile bool |
| 30 | + envFileContent string |
| 31 | + expectedPath string |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "returns path from CONTAINER_LOG_PATH environment variable", |
| 35 | + envValue: "/custom/log/path", |
| 36 | + setupEnvFile: false, |
| 37 | + expectedPath: "/custom/log/path", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "returns path from fluent-bit.env file when env var not set", |
| 41 | + envValue: "", |
| 42 | + setupEnvFile: true, |
| 43 | + envFileContent: "CONTAINER_ROOT_DIR=/var/lib/docker", |
| 44 | + expectedPath: "/var/lib/docker/containers", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "returns path with different CONTAINER_ROOT_DIR", |
| 48 | + envValue: "", |
| 49 | + setupEnvFile: true, |
| 50 | + envFileContent: "CONTAINER_ROOT_DIR=/custom/container/root", |
| 51 | + expectedPath: "/custom/container/root/containers", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "returns default path when neither env var nor file is available", |
| 55 | + envValue: "", |
| 56 | + setupEnvFile: false, |
| 57 | + expectedPath: "/var/log/containers", |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "prefers env var over file when both are set", |
| 61 | + envValue: "/priority/path", |
| 62 | + setupEnvFile: true, |
| 63 | + envFileContent: "CONTAINER_ROOT_DIR=/var/lib/docker", |
| 64 | + expectedPath: "/priority/path", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "handles env file with multiple variables", |
| 68 | + envValue: "", |
| 69 | + setupEnvFile: true, |
| 70 | + envFileContent: "OTHER_VAR=value\nCONTAINER_ROOT_DIR=/multi/var/test\nANOTHER_VAR=other", |
| 71 | + expectedPath: "/multi/var/test/containers", |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + for _, tt := range tests { |
| 76 | + t.Run(tt.name, func(t *testing.T) { |
| 77 | + originalEnv := os.Getenv("CONTAINER_LOG_PATH") |
| 78 | + defer func() { |
| 79 | + if originalEnv != "" { |
| 80 | + if err := os.Setenv("CONTAINER_LOG_PATH", originalEnv); err != nil { |
| 81 | + t.Errorf("Failed to restore CONTAINER_LOG_PATH: %v", err) |
| 82 | + } |
| 83 | + } else { |
| 84 | + if err := os.Unsetenv("CONTAINER_LOG_PATH"); err != nil { |
| 85 | + t.Errorf("Failed to unset CONTAINER_LOG_PATH: %v", err) |
| 86 | + } |
| 87 | + } |
| 88 | + }() |
| 89 | + |
| 90 | + if tt.envValue != "" { |
| 91 | + if err := os.Setenv("CONTAINER_LOG_PATH", tt.envValue); err != nil { |
| 92 | + t.Fatalf("Failed to set CONTAINER_LOG_PATH: %v", err) |
| 93 | + } |
| 94 | + } else { |
| 95 | + if err := os.Unsetenv("CONTAINER_LOG_PATH"); err != nil { |
| 96 | + t.Fatalf("Failed to unset CONTAINER_LOG_PATH: %v", err) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + var envFilePath string |
| 101 | + if tt.setupEnvFile { |
| 102 | + tempDir := t.TempDir() |
| 103 | + envFilePath = filepath.Join(tempDir, "fluent-bit.env") |
| 104 | + |
| 105 | + err := os.WriteFile(envFilePath, []byte(tt.envFileContent), 0644) |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("Failed to create test env file: %v", err) |
| 108 | + } |
| 109 | + } else { |
| 110 | + envFilePath = "/non/existent/path/fluent-bit.env" |
| 111 | + } |
| 112 | + |
| 113 | + result := getContainerLogPath(envFilePath) |
| 114 | + if result != tt.expectedPath { |
| 115 | + t.Errorf("getContainerLogPath() = %q, want %q", result, tt.expectedPath) |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
0 commit comments