|
| 1 | +// Copyright © 2026 Kube logging authors |
| 2 | +// |
| 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 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package common |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "path/filepath" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +func TestClusterKubeconfigPath(t *testing.T) { |
| 26 | + alpha, err := clusterKubeconfigPath("alpha") |
| 27 | + require.NoError(t, err) |
| 28 | + beta, err := clusterKubeconfigPath("beta") |
| 29 | + require.NoError(t, err) |
| 30 | + |
| 31 | + require.NotEqual(t, alpha, beta) |
| 32 | + require.Equal(t, filepath.Dir(alpha), filepath.Dir(beta)) |
| 33 | + |
| 34 | + again, err := clusterKubeconfigPath("alpha") |
| 35 | + require.NoError(t, err) |
| 36 | + require.Equal(t, alpha, again, "create and delete have to name the same file") |
| 37 | + |
| 38 | + dir := filepath.Dir(alpha) |
| 39 | + require.NotEqual(t, os.TempDir(), dir, "a path in the shared temp directory is guessable") |
| 40 | + |
| 41 | + info, err := os.Stat(dir) |
| 42 | + require.NoError(t, err) |
| 43 | + require.Equal(t, os.FileMode(0o700), info.Mode().Perm()) |
| 44 | +} |
| 45 | + |
| 46 | +func TestRemoveClusterKubeconfig(t *testing.T) { |
| 47 | + path := stubKubeconfig(t, "gamma") |
| 48 | + lock := path + ".lock" |
| 49 | + require.NoError(t, os.WriteFile(lock, nil, 0o600)) |
| 50 | + |
| 51 | + require.NoError(t, removeClusterKubeconfig("gamma")) |
| 52 | + |
| 53 | + require.NoFileExists(t, path) |
| 54 | + require.NoFileExists(t, lock) |
| 55 | +} |
| 56 | + |
| 57 | +func TestRemoveClusterKubeconfigToleratesMissingFiles(t *testing.T) { |
| 58 | + require.NoError(t, removeClusterKubeconfig("never-created")) |
| 59 | +} |
| 60 | + |
| 61 | +// kind recreates a missing parent directory itself, at 0755, so a lookup after |
| 62 | +// anything has taken the directory away has to put the 0700 back. Putting a |
| 63 | +// 0755 directory in place is the state that needs fixing: removing it instead |
| 64 | +// would only prove that MkdirAll creates a fresh one at the mode it is given. |
| 65 | +func TestClusterKubeconfigPathRestoresTheDirectory(t *testing.T) { |
| 66 | + dir := filepath.Dir(mustPath(t, "first")) |
| 67 | + require.NoError(t, os.RemoveAll(dir)) |
| 68 | + require.NoError(t, os.MkdirAll(dir, 0o755)) |
| 69 | + |
| 70 | + path := stubKubeconfig(t, "second") |
| 71 | + require.FileExists(t, path) |
| 72 | + |
| 73 | + info, err := os.Stat(filepath.Dir(path)) |
| 74 | + require.NoError(t, err) |
| 75 | + require.Equal(t, os.FileMode(0o700), info.Mode().Perm()) |
| 76 | +} |
| 77 | + |
| 78 | +func mustPath(t *testing.T, name string) string { |
| 79 | + t.Helper() |
| 80 | + |
| 81 | + path, err := clusterKubeconfigPath(name) |
| 82 | + require.NoError(t, err) |
| 83 | + return path |
| 84 | +} |
| 85 | + |
| 86 | +func stubKubeconfig(t *testing.T, name string) string { |
| 87 | + t.Helper() |
| 88 | + |
| 89 | + path, err := clusterKubeconfigPath(name) |
| 90 | + require.NoError(t, err) |
| 91 | + require.NoError(t, os.WriteFile(path, nil, 0o600)) |
| 92 | + return path |
| 93 | +} |
0 commit comments