-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimage_test.go
More file actions
149 lines (128 loc) · 4.6 KB
/
image_test.go
File metadata and controls
149 lines (128 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"net/http"
"net/http/httptest"
"io/ioutil"
)
// Test Scenarios Documentation
//
// TestListImages:
// - Verifies that the ListImages function correctly lists available images.
// - Setup: Creates a temporary directory and a mock image directory.
// - Expected Outcome: The output of ListImages should include the mock image name.
//
// TestDockerHubRegistry_FetchManifest:
// - Verifies the FetchManifest method of DockerHubRegistry using a mock HTTP server.
// - Setup: Creates a mock server to simulate Docker Hub API responses.
// - Expected Outcome: The manifest returned by FetchManifest should match the mock data.
//
// TestDockerHubRegistry_FetchLayer:
// - Verifies the FetchLayer method of DockerHubRegistry using a mock HTTP server.
// - Setup: Creates a mock server to simulate Docker Hub API responses.
// - Expected Outcome: The layer content returned by FetchLayer should match the mock data.
func TestListImages(t *testing.T) {
baseDir := filepath.Join(os.TempDir(), "basic-docker")
imagesDir := filepath.Join(baseDir, "images")
// Setup: Create the images directory
if err := os.MkdirAll(imagesDir, 0755); err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(baseDir) // Cleanup
// Create a mock image directory
imageName := "test-image"
if err := os.MkdirAll(filepath.Join(imagesDir, imageName), 0755); err != nil {
t.Fatalf("Failed to create mock image directory: %v", err)
}
// Capture the output of ListImages
output := captureOutput(ListImages)
// Verify the output contains the mock image name
if !contains(output, imageName) {
t.Errorf("Expected output to contain image name '%s', but got: %s", imageName, output)
}
}
func captureOutput(f func()) string {
// Redirect stdout
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
// Run the function
f()
// Capture the output
w.Close()
os.Stdout = old
var buf [1024]byte
n, _ := r.Read(buf[:])
return string(buf[:n])
}
func contains(output, substring string) bool {
return strings.Contains(output, substring)
}
// TestDockerHubRegistry_FetchManifest tests the FetchManifest method of DockerHubRegistry
func TestDockerHubRegistry_FetchManifest(t *testing.T) {
// Mock server to simulate Docker Hub API
handler := http.NewServeMux()
handler.HandleFunc("/v2/library/busybox/manifests/latest", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{
"config": {"digest": "sha256:configdigest"},
"layers": [
{"digest": "sha256:layer1digest"},
{"digest": "sha256:layer2digest"}
]
}`))
})
server := httptest.NewServer(handler)
defer server.Close()
// Create a DockerHubRegistry instance with the mock server URL
registry := &DockerHubRegistry{BaseURL: server.URL + "/v2/"}
// Call FetchManifest
manifest, err := registry.FetchManifest("library/busybox", "latest")
if err != nil {
t.Fatalf("FetchManifest failed: %v", err)
}
// Verify the manifest content
if manifest.Config.Digest != "sha256:configdigest" {
t.Errorf("Expected config digest 'sha256:configdigest', got '%s'", manifest.Config.Digest)
}
if len(manifest.Layers) != 2 {
t.Errorf("Expected 2 layers, got %d", len(manifest.Layers))
}
if manifest.Layers[0].Digest != "sha256:layer1digest" {
t.Errorf("Expected first layer digest 'sha256:layer1digest', got '%s'", manifest.Layers[0].Digest)
}
if manifest.Layers[1].Digest != "sha256:layer2digest" {
t.Errorf("Expected second layer digest 'sha256:layer2digest', got '%s'", manifest.Layers[1].Digest)
}
}
// TestDockerHubRegistry_FetchLayer tests the FetchLayer method of DockerHubRegistry
func TestDockerHubRegistry_FetchLayer(t *testing.T) {
// Mock server to simulate Docker Hub API
handler := http.NewServeMux()
handler.HandleFunc("/v2/library/busybox/blobs/sha256:layer1digest", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("layer1content"))
})
server := httptest.NewServer(handler)
defer server.Close()
// Create a DockerHubRegistry instance with the mock server URL
registry := &DockerHubRegistry{BaseURL: server.URL + "/v2/"}
// Call FetchLayer
reader, err := registry.FetchLayer("library/busybox", "sha256:layer1digest")
if err != nil {
t.Fatalf("FetchLayer failed: %v", err)
}
defer reader.Close()
// Verify the layer content
content, err := ioutil.ReadAll(reader)
if err != nil {
t.Fatalf("Failed to read layer content: %v", err)
}
if string(content) != "layer1content" {
t.Errorf("Expected layer content 'layer1content', got '%s'", string(content))
}
}