|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "slices" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// mockRegistryHandler implements a minimal Docker Registry V2 API that captures User-Agent headers |
| 15 | +type mockRegistryHandler struct { |
| 16 | + mu sync.Mutex |
| 17 | + userAgents []string |
| 18 | +} |
| 19 | + |
| 20 | +func (h *mockRegistryHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 21 | + // Capture the User-Agent header |
| 22 | + h.mu.Lock() |
| 23 | + h.userAgents = append(h.userAgents, r.Header.Get("User-Agent")) |
| 24 | + h.mu.Unlock() |
| 25 | + |
| 26 | + // Implement minimal Docker Registry V2 API endpoints for inspect --raw |
| 27 | + switch { |
| 28 | + case r.URL.Path == "/v2/": |
| 29 | + // Registry version check endpoint |
| 30 | + w.Header().Set("Docker-Distribution-API-Version", "registry/2.0") |
| 31 | + w.WriteHeader(http.StatusOK) |
| 32 | + |
| 33 | + case strings.HasSuffix(r.URL.Path, "/manifests/latest"): |
| 34 | + // Return a minimal OCI manifest as raw string |
| 35 | + // The digest matches this exact content |
| 36 | + manifest := `{"schemaVersion":2,"mediaType":"application/vnd.oci.image.manifest.v1+json","config":{"mediaType":"application/vnd.oci.image.config.v1+json","digest":"sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a","size":2},"layers":[{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855","size":0}]}` |
| 37 | + w.Header().Set("Content-Type", "application/vnd.oci.image.manifest.v1+json") |
| 38 | + w.WriteHeader(http.StatusOK) |
| 39 | + if _, err := w.Write([]byte(manifest)); err != nil { |
| 40 | + panic(err) |
| 41 | + } |
| 42 | + |
| 43 | + default: |
| 44 | + w.WriteHeader(http.StatusNotFound) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func (h *mockRegistryHandler) getUserAgents() []string { |
| 49 | + h.mu.Lock() |
| 50 | + defer h.mu.Unlock() |
| 51 | + return slices.Clone(h.userAgents) |
| 52 | +} |
| 53 | + |
| 54 | +func TestUserAgent(t *testing.T) { |
| 55 | + testCases := []struct { |
| 56 | + name string |
| 57 | + extraArgs []string |
| 58 | + userAgentValidator func(string) bool |
| 59 | + description string |
| 60 | + }{ |
| 61 | + { |
| 62 | + name: "default user agent", |
| 63 | + extraArgs: []string{}, |
| 64 | + userAgentValidator: func(ua string) bool { |
| 65 | + return strings.HasPrefix(ua, "skopeo/") |
| 66 | + }, |
| 67 | + description: "Default user agent should start with 'skopeo/'", |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "custom user agent prefix", |
| 71 | + extraArgs: []string{"--user-agent-prefix", "bootc/1.0"}, |
| 72 | + userAgentValidator: func(ua string) bool { |
| 73 | + return strings.HasPrefix(ua, "bootc/1.0 skopeo/") |
| 74 | + }, |
| 75 | + description: "Custom user agent should be in format 'prefix skopeo/version'", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "prefix with spaces", |
| 79 | + extraArgs: []string{"--user-agent-prefix", "my cool app"}, |
| 80 | + userAgentValidator: func(ua string) bool { |
| 81 | + return strings.HasPrefix(ua, "my cool app skopeo/") |
| 82 | + }, |
| 83 | + description: "User agent with spaces should work correctly", |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + for _, tc := range testCases { |
| 88 | + t.Run(tc.name, func(t *testing.T) { |
| 89 | + handler := &mockRegistryHandler{} |
| 90 | + server := httptest.NewServer(handler) |
| 91 | + defer server.Close() |
| 92 | + |
| 93 | + // Extract host:port from the test server URL |
| 94 | + registryAddr := strings.TrimPrefix(server.URL, "http://") |
| 95 | + imageRef := "docker://" + registryAddr + "/test/image:latest" |
| 96 | + |
| 97 | + // Build arguments: base args + test-specific args + image ref |
| 98 | + args := append([]string{"--tls-verify=false"}, tc.extraArgs...) |
| 99 | + args = append(args, "inspect", "--raw", imageRef) |
| 100 | + |
| 101 | + // Run skopeo inspect --raw |
| 102 | + assertSkopeoSucceeds(t, "", args...) |
| 103 | + |
| 104 | + // Verify that at least one request was made with the expected User-Agent |
| 105 | + userAgents := handler.getUserAgents() |
| 106 | + require.NotEmpty(t, userAgents, "Expected at least one request to be made") |
| 107 | + |
| 108 | + // Check that at least one User-Agent matches the validator |
| 109 | + require.True(t, |
| 110 | + slices.ContainsFunc(userAgents, tc.userAgentValidator), |
| 111 | + "%s, got: %v", tc.description, userAgents) |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
0 commit comments