| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | package dockerimg |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 5 | "os" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 6 | "testing" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 7 | ) |
| 8 | |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 9 | // TestDockerHashIsPushed tests that the published image hash is available |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 10 | func TestDockerHashIsPushed(t *testing.T) { |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 11 | // Skip this test if we can't reach the internet |
| 12 | if os.Getenv("CI") == "" { |
| 13 | t.Skip("Skipping test that requires internet access") |
| 14 | } |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 15 | |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 16 | if err := checkTagExists(dockerfileBaseHash()); err != nil { |
| 17 | t.Errorf("Docker image tag %s not found: %v", dockerfileBaseHash(), err) |
| 18 | } |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 19 | |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 20 | // Test that the default image components are reasonable |
| 21 | name, dockerfile, tag := DefaultImage() |
| 22 | if name == "" { |
| 23 | t.Error("DefaultImage name is empty") |
| 24 | } |
| 25 | if dockerfile == "" { |
| 26 | t.Error("DefaultImage dockerfile is empty") |
| 27 | } |
| 28 | if tag == "" { |
| 29 | t.Error("DefaultImage tag is empty") |
| 30 | } |
| 31 | if len(tag) < 10 { |
| 32 | t.Errorf("DefaultImage tag suspiciously short: %s", tag) |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 33 | } |
| 34 | } |
| Josh Bleecher Snyder | 3e6a4c4 | 2025-05-23 17:29:57 +0000 | [diff] [blame] | 35 | |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 36 | // TestGetHostGoCacheDirs tests that we can get the host Go cache directories |
| Josh Bleecher Snyder | 3e6a4c4 | 2025-05-23 17:29:57 +0000 | [diff] [blame] | 37 | func TestGetHostGoCacheDirs(t *testing.T) { |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 38 | if !RaceEnabled() { |
| 39 | t.Skip("Race detector not enabled, skipping test") |
| 40 | } |
| 41 | |
| Josh Bleecher Snyder | 3e6a4c4 | 2025-05-23 17:29:57 +0000 | [diff] [blame] | 42 | ctx := context.Background() |
| 43 | |
| Josh Bleecher Snyder | 3e6a4c4 | 2025-05-23 17:29:57 +0000 | [diff] [blame] | 44 | goCacheDir, err := getHostGoCacheDir(ctx) |
| 45 | if err != nil { |
| 46 | t.Fatalf("getHostGoCacheDir failed: %v", err) |
| 47 | } |
| 48 | if goCacheDir == "" { |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 49 | t.Error("GOCACHE is empty") |
| Josh Bleecher Snyder | 3e6a4c4 | 2025-05-23 17:29:57 +0000 | [diff] [blame] | 50 | } |
| Josh Bleecher Snyder | 3e6a4c4 | 2025-05-23 17:29:57 +0000 | [diff] [blame] | 51 | |
| Josh Bleecher Snyder | 3e6a4c4 | 2025-05-23 17:29:57 +0000 | [diff] [blame] | 52 | goModCacheDir, err := getHostGoModCacheDir(ctx) |
| 53 | if err != nil { |
| 54 | t.Fatalf("getHostGoModCacheDir failed: %v", err) |
| 55 | } |
| 56 | if goModCacheDir == "" { |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 57 | t.Error("GOMODCACHE is empty") |
| Josh Bleecher Snyder | 3e6a4c4 | 2025-05-23 17:29:57 +0000 | [diff] [blame] | 58 | } |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 59 | |
| 60 | t.Logf("GOCACHE: %s", goCacheDir) |
| Josh Bleecher Snyder | 3e6a4c4 | 2025-05-23 17:29:57 +0000 | [diff] [blame] | 61 | t.Logf("GOMODCACHE: %s", goModCacheDir) |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 62 | } |
| Josh Bleecher Snyder | 3e6a4c4 | 2025-05-23 17:29:57 +0000 | [diff] [blame] | 63 | |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 64 | // TestCreateCacheKey tests the cache key generation |
| 65 | func TestCreateCacheKey(t *testing.T) { |
| 66 | key1 := createCacheKey("image1", "/path1") |
| 67 | key2 := createCacheKey("image2", "/path1") |
| 68 | key3 := createCacheKey("image1", "/path2") |
| 69 | key4 := createCacheKey("image1", "/path1") |
| 70 | |
| 71 | // Different inputs should produce different keys |
| 72 | if key1 == key2 { |
| 73 | t.Error("Different base images should produce different cache keys") |
| Josh Bleecher Snyder | 3e6a4c4 | 2025-05-23 17:29:57 +0000 | [diff] [blame] | 74 | } |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 75 | if key1 == key3 { |
| 76 | t.Error("Different paths should produce different cache keys") |
| 77 | } |
| 78 | |
| 79 | // Same inputs should produce same key |
| 80 | if key1 != key4 { |
| 81 | t.Error("Same inputs should produce same cache key") |
| 82 | } |
| 83 | |
| 84 | // Keys should be reasonably short |
| 85 | if len(key1) != 12 { |
| 86 | t.Errorf("Cache key length should be 12, got %d", len(key1)) |
| Josh Bleecher Snyder | 3e6a4c4 | 2025-05-23 17:29:57 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| Jon Friesen | d27921f | 2025-06-05 13:15:56 +0000 | [diff] [blame] | 89 | |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 90 | // TestEnsureBaseImageExists tests the base image existence check and pull logic |
| 91 | func TestEnsureBaseImageExists(t *testing.T) { |
| 92 | // This test would require Docker to be running and would make network calls |
| 93 | // So we'll skip it unless we're in an integration test environment |
| 94 | if testing.Short() { |
| 95 | t.Skip("Skipping integration test that requires Docker") |
| Jon Friesen | d27921f | 2025-06-05 13:15:56 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 98 | ctx := context.Background() |
| Jon Friesen | d27921f | 2025-06-05 13:15:56 +0000 | [diff] [blame] | 99 | |
| Philip Zeyliger | 983b58a | 2025-07-02 19:42:08 -0700 | [diff] [blame] | 100 | // Test with a non-existent image (should fail gracefully) |
| 101 | err := ensureBaseImageExists(ctx, "nonexistent/image:tag") |
| 102 | if err == nil { |
| 103 | t.Error("Expected error for nonexistent image, got nil") |
| Philip Zeyliger | 2343f8a | 2025-06-17 06:16:19 -0700 | [diff] [blame] | 104 | } |
| 105 | } |