|
| 1 | +/* |
| 2 | +Copyright 2021 The OpenYurt 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 components |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +func TestGetYurthubYaml(t *testing.T) { |
| 26 | + testCases := []struct { |
| 27 | + name string |
| 28 | + podManifestPath string |
| 29 | + expected string |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "standard path", |
| 33 | + podManifestPath: "/etc/kubernetes/manifests", |
| 34 | + expected: "/etc/kubernetes/manifests/yurthub.yaml", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "custom path", |
| 38 | + podManifestPath: "/tmp/manifests", |
| 39 | + expected: "/tmp/manifests/yurthub.yaml", |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + for _, tc := range testCases { |
| 44 | + t.Run(tc.name, func(t *testing.T) { |
| 45 | + result := getYurthubYaml(tc.podManifestPath) |
| 46 | + if result != tc.expected { |
| 47 | + t.Errorf("getYurthubYaml() = %v, want %v", result, tc.expected) |
| 48 | + } |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestGetYurthubConf(t *testing.T) { |
| 54 | + result := getYurthubConf() |
| 55 | + if result == "" { |
| 56 | + t.Error("getYurthubConf() should not return empty string") |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestGetYurthubCacheDir(t *testing.T) { |
| 61 | + result := getYurthubCacheDir() |
| 62 | + if result == "" { |
| 63 | + t.Error("getYurthubCacheDir() should not return empty string") |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestNewYurthubOperator(t *testing.T) { |
| 68 | + op := NewYurthubOperator("https://127.0.0.1:6443", "test-token", "test-pool", 60) |
| 69 | + if op == nil { |
| 70 | + t.Fatal("NewYurthubOperator() returned nil") |
| 71 | + } |
| 72 | + if op.apiServerAddr != "https://127.0.0.1:6443" { |
| 73 | + t.Errorf("apiServerAddr = %v, want %v", op.apiServerAddr, "https://127.0.0.1:6443") |
| 74 | + } |
| 75 | + if op.joinToken != "test-token" { |
| 76 | + t.Errorf("joinToken = %v, want %v", op.joinToken, "test-token") |
| 77 | + } |
| 78 | + if op.nodePoolName != "test-pool" { |
| 79 | + t.Errorf("nodePoolName = %v, want %v", op.nodePoolName, "test-pool") |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestYurthubYamlCleanup_FileNotExists(t *testing.T) { |
| 84 | + tmpDir, err := os.MkdirTemp("", "yurthub-test") |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 87 | + } |
| 88 | + defer os.RemoveAll(tmpDir) |
| 89 | + |
| 90 | + nonExistentPath := filepath.Join(tmpDir, "nonexistent", "yurt-hub.yaml") |
| 91 | + |
| 92 | + // Verify file doesn't exist |
| 93 | + if _, err := os.Stat(nonExistentPath); !os.IsNotExist(err) { |
| 94 | + t.Fatal("Test setup error: file should not exist") |
| 95 | + } |
| 96 | + |
| 97 | + // Simulating the logic from UnInstall: when file doesn't exist, no error |
| 98 | + if _, err := os.Stat(nonExistentPath); os.IsNotExist(err) { |
| 99 | + // This is the expected path - file doesn't exist, skip delete |
| 100 | + } else { |
| 101 | + t.Error("Expected file to not exist") |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestYurthubYamlCleanup_FileExists(t *testing.T) { |
| 106 | + tmpDir, err := os.MkdirTemp("", "yurthub-test") |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 109 | + } |
| 110 | + defer os.RemoveAll(tmpDir) |
| 111 | + |
| 112 | + testFile := filepath.Join(tmpDir, "yurt-hub.yaml") |
| 113 | + if err := os.WriteFile(testFile, []byte("test content"), 0644); err != nil { |
| 114 | + t.Fatalf("Failed to create test file: %v", err) |
| 115 | + } |
| 116 | + |
| 117 | + // Verify file exists |
| 118 | + if _, err := os.Stat(testFile); os.IsNotExist(err) { |
| 119 | + t.Fatal("Test setup error: file should exist") |
| 120 | + } |
| 121 | + |
| 122 | + // Remove file (simulating UnInstall cleanup) |
| 123 | + if err := os.Remove(testFile); err != nil { |
| 124 | + t.Errorf("Failed to remove file: %v", err) |
| 125 | + } |
| 126 | + |
| 127 | + // Verify file is removed |
| 128 | + if _, err := os.Stat(testFile); !os.IsNotExist(err) { |
| 129 | + t.Error("File should be removed") |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestYurthubConfDirCleanup_DirNotExists(t *testing.T) { |
| 134 | + tmpDir, err := os.MkdirTemp("", "yurthub-test") |
| 135 | + if err != nil { |
| 136 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 137 | + } |
| 138 | + defer os.RemoveAll(tmpDir) |
| 139 | + |
| 140 | + nonExistentDir := filepath.Join(tmpDir, "nonexistent-conf") |
| 141 | + |
| 142 | + // Verify directory doesn't exist |
| 143 | + if _, err := os.Stat(nonExistentDir); !os.IsNotExist(err) { |
| 144 | + t.Fatal("Test setup error: directory should not exist") |
| 145 | + } |
| 146 | + |
| 147 | + // Simulating the updated UnInstall logic: when directory doesn't exist, |
| 148 | + // it should continue to clean up cache directory instead of returning early |
| 149 | + confDirExists := true |
| 150 | + if _, err := os.Stat(nonExistentDir); os.IsNotExist(err) { |
| 151 | + confDirExists = false |
| 152 | + } |
| 153 | + |
| 154 | + if confDirExists { |
| 155 | + t.Error("Expected directory to not exist") |
| 156 | + } |
| 157 | + |
| 158 | + // The key change in the PR: after checking conf dir, we continue to cache cleanup |
| 159 | + // regardless of whether conf dir existed |
| 160 | + cacheDir := filepath.Join(tmpDir, "cache") |
| 161 | + if err := os.MkdirAll(cacheDir, 0755); err != nil { |
| 162 | + t.Fatalf("Failed to create cache dir: %v", err) |
| 163 | + } |
| 164 | + |
| 165 | + // Cache cleanup should proceed even when conf dir didn't exist |
| 166 | + if err := os.RemoveAll(cacheDir); err != nil { |
| 167 | + t.Errorf("Failed to remove cache dir: %v", err) |
| 168 | + } |
| 169 | + |
| 170 | + if _, err := os.Stat(cacheDir); !os.IsNotExist(err) { |
| 171 | + t.Error("Cache directory should be removed") |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func TestYurthubConfDirCleanup_DirExists(t *testing.T) { |
| 176 | + tmpDir, err := os.MkdirTemp("", "yurthub-test") |
| 177 | + if err != nil { |
| 178 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 179 | + } |
| 180 | + defer os.RemoveAll(tmpDir) |
| 181 | + |
| 182 | + confDir := filepath.Join(tmpDir, "yurthub-conf") |
| 183 | + if err := os.MkdirAll(confDir, 0755); err != nil { |
| 184 | + t.Fatalf("Failed to create conf dir: %v", err) |
| 185 | + } |
| 186 | + |
| 187 | + // Create a file inside conf dir |
| 188 | + testFile := filepath.Join(confDir, "test.conf") |
| 189 | + if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil { |
| 190 | + t.Fatalf("Failed to create test file: %v", err) |
| 191 | + } |
| 192 | + |
| 193 | + // Verify directory exists |
| 194 | + if _, err := os.Stat(confDir); os.IsNotExist(err) { |
| 195 | + t.Fatal("Test setup error: directory should exist") |
| 196 | + } |
| 197 | + |
| 198 | + // Remove entire directory (simulating UnInstall cleanup) |
| 199 | + if err := os.RemoveAll(confDir); err != nil { |
| 200 | + t.Errorf("Failed to remove conf dir: %v", err) |
| 201 | + } |
| 202 | + |
| 203 | + // Verify directory is removed |
| 204 | + if _, err := os.Stat(confDir); !os.IsNotExist(err) { |
| 205 | + t.Error("Conf directory should be removed") |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +func TestCacheDirCleanup(t *testing.T) { |
| 210 | + tmpDir, err := os.MkdirTemp("", "yurthub-test") |
| 211 | + if err != nil { |
| 212 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 213 | + } |
| 214 | + defer os.RemoveAll(tmpDir) |
| 215 | + |
| 216 | + cacheDir := filepath.Join(tmpDir, "cache") |
| 217 | + if err := os.MkdirAll(cacheDir, 0755); err != nil { |
| 218 | + t.Fatalf("Failed to create cache dir: %v", err) |
| 219 | + } |
| 220 | + |
| 221 | + // Create nested structure |
| 222 | + nestedDir := filepath.Join(cacheDir, "kubelet", "pods") |
| 223 | + if err := os.MkdirAll(nestedDir, 0755); err != nil { |
| 224 | + t.Fatalf("Failed to create nested dir: %v", err) |
| 225 | + } |
| 226 | + |
| 227 | + testFile := filepath.Join(nestedDir, "pod.yaml") |
| 228 | + if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil { |
| 229 | + t.Fatalf("Failed to create test file: %v", err) |
| 230 | + } |
| 231 | + |
| 232 | + // Remove entire cache directory |
| 233 | + if err := os.RemoveAll(cacheDir); err != nil { |
| 234 | + t.Errorf("Failed to remove cache dir: %v", err) |
| 235 | + } |
| 236 | + |
| 237 | + // Verify directory is fully removed |
| 238 | + if _, err := os.Stat(cacheDir); !os.IsNotExist(err) { |
| 239 | + t.Error("Cache directory should be completely removed") |
| 240 | + } |
| 241 | +} |
0 commit comments