|
| 1 | +// Copyright 2026 Google LLC |
| 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 job |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | +) |
| 25 | + |
| 26 | +func TestIsStateStale(t *testing.T) { |
| 27 | + now := time.Now() |
| 28 | + freshTime := now.Add(-1 * time.Hour) |
| 29 | + staleTime := now.Add(-48 * time.Hour) |
| 30 | + |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + state PrereqState |
| 34 | + currentProjectID string |
| 35 | + wantStale bool |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "Fresh state, same project", |
| 39 | + state: PrereqState{ |
| 40 | + LastCheckedTimestamp: freshTime, |
| 41 | + LastCheckedProjectID: "test-project", |
| 42 | + }, |
| 43 | + currentProjectID: "test-project", |
| 44 | + wantStale: false, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "Stale state (time), same project", |
| 48 | + state: PrereqState{ |
| 49 | + LastCheckedTimestamp: staleTime, |
| 50 | + LastCheckedProjectID: "test-project", |
| 51 | + }, |
| 52 | + currentProjectID: "test-project", |
| 53 | + wantStale: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Fresh state, different project", |
| 57 | + state: PrereqState{ |
| 58 | + LastCheckedTimestamp: freshTime, |
| 59 | + LastCheckedProjectID: "old-project", |
| 60 | + }, |
| 61 | + currentProjectID: "new-project", |
| 62 | + wantStale: true, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + got := isStateStale(tt.state, tt.currentProjectID) |
| 69 | + if got != tt.wantStale { |
| 70 | + t.Errorf("isStateStale() = %v, want %v", got, tt.wantStale) |
| 71 | + } |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestStateFilePath(t *testing.T) { |
| 77 | + tempDir, err := os.MkdirTemp("", "prereq-test-home") |
| 78 | + if err != nil { |
| 79 | + t.Fatal(err) |
| 80 | + } |
| 81 | + defer os.RemoveAll(tempDir) |
| 82 | + |
| 83 | + origHome := os.Getenv("HOME") |
| 84 | + os.Setenv("HOME", tempDir) |
| 85 | + defer os.Setenv("HOME", origHome) |
| 86 | + |
| 87 | + path, err := stateFilePath() |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("stateFilePath() error = %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + expectedPrefix := filepath.Join(tempDir, stateDirName) |
| 93 | + if !strings.HasPrefix(path, expectedPrefix) { |
| 94 | + t.Errorf("expected path to start with %s, got %s", expectedPrefix, path) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestLoadPrereqState_Success(t *testing.T) { |
| 99 | + tempDir, err := os.MkdirTemp("", "prereq-load-test") |
| 100 | + if err != nil { |
| 101 | + t.Fatal(err) |
| 102 | + } |
| 103 | + defer os.RemoveAll(tempDir) |
| 104 | + |
| 105 | + origHome := os.Getenv("HOME") |
| 106 | + os.Setenv("HOME", tempDir) |
| 107 | + defer os.Setenv("HOME", origHome) |
| 108 | + |
| 109 | + stateDir := filepath.Join(tempDir, stateDirName) |
| 110 | + if err := os.MkdirAll(stateDir, 0755); err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + statePath := filepath.Join(stateDir, stateFileName) |
| 114 | + |
| 115 | + state := PrereqState{ |
| 116 | + GCloudSDKInstalled: true, |
| 117 | + LastCheckedProjectID: "test-project", |
| 118 | + LastCheckedTimestamp: time.Now(), |
| 119 | + } |
| 120 | + data, _ := json.Marshal(state) |
| 121 | + if err := os.WriteFile(statePath, data, 0644); err != nil { |
| 122 | + t.Fatal(err) |
| 123 | + } |
| 124 | + |
| 125 | + loaded := loadPrereqState() |
| 126 | + if !loaded.GCloudSDKInstalled { |
| 127 | + t.Errorf("expected loaded state to be true for GCloudSDKInstalled, got false") |
| 128 | + } |
| 129 | + if loaded.LastCheckedProjectID != "test-project" { |
| 130 | + t.Errorf("expected loaded state project ID to be 'test-project', got %s", loaded.LastCheckedProjectID) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func TestSavePrereqState_Success(t *testing.T) { |
| 135 | + tempDir, err := os.MkdirTemp("", "prereq-save-test") |
| 136 | + if err != nil { |
| 137 | + t.Fatal(err) |
| 138 | + } |
| 139 | + defer os.RemoveAll(tempDir) |
| 140 | + |
| 141 | + origHome := os.Getenv("HOME") |
| 142 | + os.Setenv("HOME", tempDir) |
| 143 | + defer os.Setenv("HOME", origHome) |
| 144 | + |
| 145 | + state := PrereqState{ |
| 146 | + GCloudAuthenticated: true, |
| 147 | + LastCheckedProjectID: "test-project", |
| 148 | + LastCheckedTimestamp: time.Now(), |
| 149 | + } |
| 150 | + |
| 151 | + savePrereqState(state) |
| 152 | + |
| 153 | + stateDir := filepath.Join(tempDir, stateDirName) |
| 154 | + statePath := filepath.Join(stateDir, stateFileName) |
| 155 | + |
| 156 | + if _, err := os.Stat(statePath); os.IsNotExist(err) { |
| 157 | + t.Fatalf("state file was not created at %s", statePath) |
| 158 | + } |
| 159 | + |
| 160 | + data, err := os.ReadFile(statePath) |
| 161 | + if err != nil { |
| 162 | + t.Fatalf("failed to read created state file: %v", err) |
| 163 | + } |
| 164 | + |
| 165 | + var savedState PrereqState |
| 166 | + if err := json.Unmarshal(data, &savedState); err != nil { |
| 167 | + t.Fatalf("failed to unmarshal saved state: %v", err) |
| 168 | + } |
| 169 | + |
| 170 | + if !savedState.GCloudAuthenticated { |
| 171 | + t.Errorf("expected saved state to be true for GCloudAuthenticated, got false") |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func TestAskForConfirmation_Yes(t *testing.T) { |
| 176 | + pipeRead, pipeWrite, err := os.Pipe() |
| 177 | + if err != nil { |
| 178 | + t.Fatal(err) |
| 179 | + } |
| 180 | + defer pipeRead.Close() |
| 181 | + defer pipeWrite.Close() |
| 182 | + |
| 183 | + origStdin := os.Stdin |
| 184 | + os.Stdin = pipeRead |
| 185 | + defer func() { os.Stdin = origStdin }() |
| 186 | + |
| 187 | + if _, err := pipeWrite.Write([]byte("y\n")); err != nil { |
| 188 | + t.Fatal(err) |
| 189 | + } |
| 190 | + |
| 191 | + got := askForConfirmation("Test prompt") |
| 192 | + if !got { |
| 193 | + t.Error("expected true for 'y', got false") |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +func TestAskForConfirmation_No(t *testing.T) { |
| 198 | + pipeRead, pipeWrite, err := os.Pipe() |
| 199 | + if err != nil { |
| 200 | + t.Fatal(err) |
| 201 | + } |
| 202 | + defer pipeRead.Close() |
| 203 | + defer pipeWrite.Close() |
| 204 | + |
| 205 | + origStdin := os.Stdin |
| 206 | + os.Stdin = pipeRead |
| 207 | + defer func() { os.Stdin = origStdin }() |
| 208 | + |
| 209 | + if _, err := pipeWrite.Write([]byte("n\n")); err != nil { |
| 210 | + t.Fatal(err) |
| 211 | + } |
| 212 | + |
| 213 | + got := askForConfirmation("Test prompt") |
| 214 | + if got { |
| 215 | + t.Error("expected false for 'n', got true") |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +func TestLoadPrereqState_CorruptedFile(t *testing.T) { |
| 220 | + tempDir, err := os.MkdirTemp("", "prereq-corrupt-test") |
| 221 | + if err != nil { |
| 222 | + t.Fatal(err) |
| 223 | + } |
| 224 | + defer os.RemoveAll(tempDir) |
| 225 | + |
| 226 | + origHome := os.Getenv("HOME") |
| 227 | + os.Setenv("HOME", tempDir) |
| 228 | + defer os.Setenv("HOME", origHome) |
| 229 | + |
| 230 | + stateDir := filepath.Join(tempDir, stateDirName) |
| 231 | + if err := os.MkdirAll(stateDir, 0755); err != nil { |
| 232 | + t.Fatal(err) |
| 233 | + } |
| 234 | + statePath := filepath.Join(stateDir, stateFileName) |
| 235 | + |
| 236 | + if err := os.WriteFile(statePath, []byte("invalid-json"), 0644); err != nil { |
| 237 | + t.Fatal(err) |
| 238 | + } |
| 239 | + |
| 240 | + loaded := loadPrereqState() |
| 241 | + if loaded.GCloudSDKInstalled { |
| 242 | + t.Errorf("expected empty state for corrupted file, got GCloudSDKInstalled=true") |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | +func TestSavePrereqState_WriteError(t *testing.T) { |
| 247 | + tempDir, err := os.MkdirTemp("", "prereq-write-error-test") |
| 248 | + if err != nil { |
| 249 | + t.Fatal(err) |
| 250 | + } |
| 251 | + defer os.RemoveAll(tempDir) |
| 252 | + |
| 253 | + origHome := os.Getenv("HOME") |
| 254 | + os.Setenv("HOME", tempDir) |
| 255 | + defer os.Setenv("HOME", origHome) |
| 256 | + |
| 257 | + stateDir := filepath.Join(tempDir, stateDirName) |
| 258 | + if err := os.MkdirAll(stateDir, 0755); err != nil { |
| 259 | + t.Fatal(err) |
| 260 | + } |
| 261 | + |
| 262 | + // Make directory read-only to force write error |
| 263 | + if err := os.Chmod(stateDir, 0555); err != nil { |
| 264 | + t.Fatal(err) |
| 265 | + } |
| 266 | + defer os.Chmod(stateDir, 0755) // Restore to allow cleanup |
| 267 | + |
| 268 | + state := PrereqState{ |
| 269 | + GCloudAuthenticated: true, |
| 270 | + } |
| 271 | + |
| 272 | + // This should log an error but not panic |
| 273 | + savePrereqState(state) |
| 274 | +} |
| 275 | + |
| 276 | + |
| 277 | + |
| 278 | + |
0 commit comments