-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathe2e_test.go
257 lines (242 loc) · 9.03 KB
/
e2e_test.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package pkg
import (
"context"
"fmt"
"os"
"testing"
azstorage "github.com/Azure/azure-sdk-for-go/storage"
"github.com/containers/image/v5/copy"
"github.com/containers/image/v5/docker"
"github.com/containers/image/v5/image"
"github.com/containers/image/v5/signature"
"github.com/containers/image/v5/transports"
"github.com/containers/image/v5/types"
"github.com/distribution/distribution/v3/uuid"
"github.com/migtools/udistribution/pkg/image/udistribution"
"github.com/migtools/udistribution/pkg/internal/image/imagesource"
"github.com/pkg/errors"
"golang.org/x/sync/semaphore"
)
/*
To test e2e, fill s3 credentials below and run:
export UDISTRIBUTION_TEST_E2E_ENABLE=true
export REGISTRY_STORAGE=s3
export REGISTRY_STORAGE_S3_BUCKET=<your-bucket>
export REGISTRY_STORAGE_S3_ACCESSKEY=<your-access-key>
export REGISTRY_STORAGE_S3_SECRETKEY=<your-secret-key>
export REGISTRY_STORAGE_S3_REGION=<your-region>
export REGISTRY_STORAGE_DELETE_ENABLED=true
go test -v ./pkg/e2e_test.go
Note: This test will likely fail on macos due to lack of darwin container images.
udistribution/pkg/e2e_test.go:63: failed to copy image: choosing an image from manifest list docker://alpine:latest: no image found in manifest list for architecture amd64, variant "", OS darwin
*/
func TestE2e(t *testing.T) {
t.Logf("TestE2e called")
// Set test environment variables when running in IDE.
// only test if found key in env
if os.Getenv("UDISTRIBUTION_TEST_E2E_ENABLE") == "" {
t.Skip("UDISTRIBUTION_TEST_E2E_ENABLE not set, skipping e2e test")
}
if os.Getenv("REGISTRY_STORAGE") == "" {
t.Skip("REGISTRY_STORAGE not set, skipping e2e test")
}
ut, err := udistribution.NewTransportFromNewConfig("", os.Environ())
defer ut.Deregister()
if err != nil {
t.Errorf("failed to create transport with environment variables: %v", err)
}
srcRef, err := docker.ParseReference("//quay.io/konveyor/openshift-velero-plugin:latest")
if err != nil {
t.Errorf("failed to parse reference: %v", err)
}
randomRepoName := "udistribution-e2e-test" + uuid.Generate().String()
destRef, err := ut.ParseReference(fmt.Sprintf("//%s/openshift-velero-plugin:latest", randomRepoName))
if err != nil {
t.Errorf("failed to parse reference: %v", err)
}
// TODO: uncomment
// Remove existing if any
// storageParam := ut.GetApp().Config.Storage.Parameters()
// err = destRef.DeleteImage(context.Background(), nil)
// if err != nil {
// // ignore unable to delete before copy.
// if errors.Cause(err) != getUnableToDeleteError(udistribution.GetRef(destRef)) {
// log.Printf("error isn't due to unable to delete: %v", getUnableToDeleteError(udistribution.GetRef(destRef)))
// if storageParam["delete"] != nil {
// deleteParam := storageParam["delete"].(map[string]bool)
// if deleteParam["enabled"] == true {
// t.Errorf("failed to delete image: %v", err)
// } else {
// t.Logf("delete disabled, skipping delete test")
// }
// }
// }
// }
pc, err := getPolicyContext()
if err != nil {
t.Errorf("failed to get policy context: %v", err)
}
ctx, err := getDefaultContext()
if err != nil {
t.Errorf("failed to get default context: %v", err)
}
options := copy.Options{
SourceCtx: ctx,
DestinationCtx: ctx,
}
_, err = copy.Image(context.Background(), pc, destRef, srcRef, &options)
if err != nil {
t.Errorf("%v", errors.Wrapf(err, "failed to copy image"))
}
// try trigger reusing blob
_, err = copy.Image(context.Background(), pc, destRef, srcRef, &options)
if err != nil {
t.Errorf("%v", errors.Wrapf(err, "failed to copy image"))
}
// t.Errorf("fail here")
// Cleanup
// err = destRef.DeleteImage(context.Background(), nil)
// if err != nil {
// t.Errorf("failed to delete image: %v", err)
// }
// test that udistributionReference when used as source can call ConfigBlob() which is used during velero restore
publicRawSource, err := destRef.NewImageSource(context.Background(), options.SourceCtx)
if err != nil {
t.Error(errors.Wrapf(err, "initializing source %s", transports.ImageName(destRef)))
}
rawSource := imagesource.FromPublic(publicRawSource)
defer func() {
if err := rawSource.Close(); err != nil {
t.Errorf(" (src: %v)", err)
}
}()
unparsedToplevel := image.UnparsedInstance(rawSource, nil)
// targetInstance := unparsedToplevel // inside copyOneImage(ctx, policyContext, options, unparsedToplevel, unparsedToplevel, nil)
unparsedImage := unparsedToplevel
src, err := image.FromUnparsedImage(context.Background(), options.SourceCtx, unparsedImage)
if err != nil {
t.Error(errors.Wrapf(err, "initializing image from source %s", transports.ImageName(rawSource.Reference())))
}
// instanceDigest := targetInstance // inside copyUpdatedConfigAndManifest, and copyUpdatedConfigAndManifests
// pendingImage := src // inside copyConfig(ctx, pendingImage)
// pendingImage is now src again
srcInfo := src.ConfigInfo()
if srcInfo.Digest != "" {
maxParallelDownloads := uint(6) // from var
max := options.MaxParallelDownloads
if max == 0 {
max = maxParallelDownloads
}
concurrentBlobCopiesSemaphore := semaphore.NewWeighted(int64(max))
if err := concurrentBlobCopiesSemaphore.Acquire(context.Background(), 1); err != nil {
// This can only fail with ctx.Err(), so no need to blame acquiring the semaphore.
_ = fmt.Errorf("copying config: %w", err)
}
defer concurrentBlobCopiesSemaphore.Release(1)
func() { // A scope for defer
// we don't care about progress bar
// progressPool := c.newProgressPool()
// defer progressPool.Wait()
// bar := c.createProgressBar(progressPool, false, srcInfo, "config", "done")
// defer bar.Abort(false)
configBlob, err := src.ConfigBlob(context.Background())
if err != nil {
t.Error(errors.Wrapf(err, "reading config blob %s", srcInfo.Digest))
}
fmt.Printf("configBlob: %v", configBlob)
// destInfo, err := c.copyBlobFromStream(ctx, bytes.NewReader(configBlob), srcInfo, nil, false, true, false, bar, -1, false)
// if err != nil {
// return types.BlobInfo{}, err
// }
// bar.mark100PercentComplete()
// return destInfo, nil
}()
}
}
func getPolicyContext() (*signature.PolicyContext, error) {
policy := &signature.Policy{Default: []signature.PolicyRequirement{signature.NewPRInsecureAcceptAnything()}}
return signature.NewPolicyContext(policy)
}
func getDefaultContext() (*types.SystemContext, error) {
ctx := &types.SystemContext{
DockerDaemonInsecureSkipTLSVerify: true,
DockerInsecureSkipTLSVerify: types.OptionalBoolTrue,
DockerDisableDestSchema1MIMETypes: true,
OSChoice: "linux",
}
return ctx, nil
}
// TODO:
// // https://github.com/containers/image/blob/857a813795f6a5dc8116912a86ba7956a315cd81/docker/docker_image_src.go#L638
// func getUnableToDeleteError(ref reference.Named) error {
// return errors.Errorf("Unable to delete %v. Image may not exist or is not stored with a v2 Schema in a v2 registry", ref)
// }
func TestS3Store(t *testing.T) {
// test s3 driver
validRegions := []string{
"us-east-1",
"eu-north-1",
"af-south-1",
}
for _, region := range validRegions {
ut, err := udistribution.NewTransportFromNewConfig("", []string{
"REGISTRY_STORAGE=s3",
"REGISTRY_STORAGE_S3_BUCKET=udistribution-test-e2e",
"REGISTRY_STORAGE_S3_ACCESSKEY=<your-access-key>",
"REGISTRY_STORAGE_S3_SECRETKEY=<your-secret-key>",
"REGISTRY_STORAGE_S3_REGION=" + region,
})
if err != nil {
t.Fatal(err)
}
ut.Deregister()
}
}
func TestAzureStore(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Logf("TestE2e recovered from panic: %v", r)
if r.(azstorage.AzureStorageServiceError).Code == "AuthenticationFailed" {
t.Log("azure storage driver initialized and authentication failed as expected")
} else {
t.Fatalf("TestE2e unexpected error: %v", r)
}
}
}()
ut, err := udistribution.NewTransportFromNewConfig("", []string{
"REGISTRY_STORAGE=azure",
"REGISTRY_STORAGE_AZURE_ACCOUNTNAME=accountname",
"REGISTRY_STORAGE_AZURE_ACCOUNTKEY=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==", //sample key
"REGISTRY_STORAGE_AZURE_CONTAINER=udistribution-test-e2e",
})
if err != nil {
t.Fatal(err)
}
ut.Deregister()
}
func TestGCSStore(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Logf("TestE2e recovered from panic: %v", r)
if fmt.Sprint(r) == "The credentials were not specified in the correct format" {
t.Log("google cloud storage driver initialized and authentication failed as expected")
} else {
t.Fatalf("TestE2e unexpected error: %v", r)
}
}
}()
ut, err := udistribution.NewTransportFromNewConfig("", []string{
"REGISTRY_STORAGE=gcs",
"REGISTRY_STORAGE_GCS_BUCKET=udistribution-test-e2e",
"REGISTRY_STORAGE_GCS_PROJECT=udistribution-test-e2e",
"REGISTRY_STORAGE_GCS_CREDENTIALS_TYPE=service_account",
"REGISTRY_STORAGE_GCS_CREDENTIALS_PROJECT_ID=udistribution-test-e2e",
"REGISTRY_STORAGE_GCS_CREDENTIALS_PRIVATE_KEY_ID=udistribution-test-e2e",
"REGISTRY_STORAGE_GCS_CREDENTIALS_PRIVATE_KEY=",
})
if err != nil {
t.Fatal(err)
}
ut.Deregister()
}