blob: 563d33bc6ab4ef89c06d67c98ea24245954c081d [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001package dockerimg
2
3import (
4 "context"
Earl Lee2e463fb2025-04-17 11:22:22 -07005 "os"
Earl Lee2e463fb2025-04-17 11:22:22 -07006 "testing"
Earl Lee2e463fb2025-04-17 11:22:22 -07007)
8
Philip Zeyliger983b58a2025-07-02 19:42:08 -07009// TestDockerHashIsPushed tests that the published image hash is available
David Crawshaw11129492025-04-25 20:41:53 -070010func TestDockerHashIsPushed(t *testing.T) {
Philip Zeyliger983b58a2025-07-02 19:42:08 -070011 // 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 Crawshaw11129492025-04-25 20:41:53 -070015
Philip Zeyliger983b58a2025-07-02 19:42:08 -070016 if err := checkTagExists(dockerfileBaseHash()); err != nil {
17 t.Errorf("Docker image tag %s not found: %v", dockerfileBaseHash(), err)
18 }
David Crawshaw11129492025-04-25 20:41:53 -070019
Philip Zeyliger983b58a2025-07-02 19:42:08 -070020 // 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 Crawshaw11129492025-04-25 20:41:53 -070033 }
34}
Josh Bleecher Snyder3e6a4c42025-05-23 17:29:57 +000035
Philip Zeyliger983b58a2025-07-02 19:42:08 -070036// TestGetHostGoCacheDirs tests that we can get the host Go cache directories
Josh Bleecher Snyder3e6a4c42025-05-23 17:29:57 +000037func TestGetHostGoCacheDirs(t *testing.T) {
Philip Zeyliger983b58a2025-07-02 19:42:08 -070038 if !RaceEnabled() {
39 t.Skip("Race detector not enabled, skipping test")
40 }
41
Josh Bleecher Snyder3e6a4c42025-05-23 17:29:57 +000042 ctx := context.Background()
43
Josh Bleecher Snyder3e6a4c42025-05-23 17:29:57 +000044 goCacheDir, err := getHostGoCacheDir(ctx)
45 if err != nil {
46 t.Fatalf("getHostGoCacheDir failed: %v", err)
47 }
48 if goCacheDir == "" {
Philip Zeyliger983b58a2025-07-02 19:42:08 -070049 t.Error("GOCACHE is empty")
Josh Bleecher Snyder3e6a4c42025-05-23 17:29:57 +000050 }
Josh Bleecher Snyder3e6a4c42025-05-23 17:29:57 +000051
Josh Bleecher Snyder3e6a4c42025-05-23 17:29:57 +000052 goModCacheDir, err := getHostGoModCacheDir(ctx)
53 if err != nil {
54 t.Fatalf("getHostGoModCacheDir failed: %v", err)
55 }
56 if goModCacheDir == "" {
Philip Zeyliger983b58a2025-07-02 19:42:08 -070057 t.Error("GOMODCACHE is empty")
Josh Bleecher Snyder3e6a4c42025-05-23 17:29:57 +000058 }
Philip Zeyliger983b58a2025-07-02 19:42:08 -070059
60 t.Logf("GOCACHE: %s", goCacheDir)
Josh Bleecher Snyder3e6a4c42025-05-23 17:29:57 +000061 t.Logf("GOMODCACHE: %s", goModCacheDir)
Philip Zeyliger983b58a2025-07-02 19:42:08 -070062}
Josh Bleecher Snyder3e6a4c42025-05-23 17:29:57 +000063
Philip Zeyliger983b58a2025-07-02 19:42:08 -070064// TestCreateCacheKey tests the cache key generation
65func 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 Snyder3e6a4c42025-05-23 17:29:57 +000074 }
Philip Zeyliger983b58a2025-07-02 19:42:08 -070075 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 Snyder3e6a4c42025-05-23 17:29:57 +000087 }
88}
Jon Friesend27921f2025-06-05 13:15:56 +000089
Philip Zeyliger983b58a2025-07-02 19:42:08 -070090// TestEnsureBaseImageExists tests the base image existence check and pull logic
91func 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 Friesend27921f2025-06-05 13:15:56 +000096 }
97
Philip Zeyliger983b58a2025-07-02 19:42:08 -070098 ctx := context.Background()
Jon Friesend27921f2025-06-05 13:15:56 +000099
Philip Zeyliger983b58a2025-07-02 19:42:08 -0700100 // 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 Zeyliger2343f8a2025-06-17 06:16:19 -0700104 }
105}