-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathregistry_test.go
More file actions
140 lines (128 loc) · 4.14 KB
/
registry_test.go
File metadata and controls
140 lines (128 loc) · 4.14 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
package registry
import (
"context"
"errors"
"io"
"net/url"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func TestCloudRegistryStores(t *testing.T) {
mockS3Client := &MockS3Client{
GetObjectFn: func(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error) {
return &s3.GetObjectOutput{
Body: io.NopCloser(strings.NewReader("mock data")),
}, nil
},
DeleteObjectFn: func(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) {
return &s3.DeleteObjectOutput{}, nil
},
}
mockGCSClient := &MockGCSClient{
GetObjectFn: func(ctx context.Context, bucket string, object string) (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader("mock data")), nil
},
DeleteObjectFn: func(ctx context.Context, bucket string, object string) error {
return nil
},
}
tests := []struct {
name string
config *RepoConfig
}{
{
name: "s3 registry store",
config: &RepoConfig{
Project: "feature_repo",
Registry: map[string]any{
"path": "s3://test-bucket/path/to/registry.db",
},
Provider: "aws",
},
},
{
name: "gcs registry store",
config: &RepoConfig{
Project: "feature_repo",
Registry: map[string]any{
"path": "gs://test-bucket/path/to/registry.db",
},
Provider: "gcp",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
registryConfig, err := test.config.GetRegistryConfig()
if err != nil {
t.Errorf("Error getting registry config. msg: %s", err.Error())
}
r := &Registry{
project: test.config.Project,
cachedRegistryProtoTtl: time.Duration(registryConfig.CacheTtlSeconds) * time.Second,
}
registryPath := registryConfig.Path
uri, err := url.Parse(registryPath)
if err != nil {
t.Errorf("Error parsing registry path. msg: %s", err.Error())
}
if registryStoreType, ok := REGISTRY_STORE_CLASS_FOR_SCHEME[uri.Scheme]; ok {
switch registryStoreType {
case "S3RegistryStore":
r.registryStore = &S3RegistryStore{
filePath: registryConfig.Path,
s3Client: mockS3Client,
}
case "GCSRegistryStore":
r.registryStore = &GCSRegistryStore{
registryPath: registryConfig.Path,
client: mockGCSClient,
}
default:
t.Errorf("Unsupported registry store type: %s", registryStoreType)
return
}
err := r.InitializeRegistry()
if err != nil {
t.Errorf("Error initializing registry. msg: %s. registry path=%q", err.Error(), registryPath)
}
}
})
}
}
// MockS3Client is mock client for testing S3 registry store
type MockS3Client struct {
GetObjectFn func(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error)
DeleteObjectFn func(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error)
}
func (m *MockS3Client) GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error) {
if m.GetObjectFn != nil {
return m.GetObjectFn(ctx, params)
}
return nil, errors.New("not implemented")
}
func (m *MockS3Client) DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) {
if m.DeleteObjectFn != nil {
return m.DeleteObjectFn(ctx, params)
}
return nil, errors.New("not implemented")
}
// MockGCSClient is mock client for testing GCS registry store
type MockGCSClient struct {
GetObjectFn func(ctx context.Context, bucket string, object string) (io.ReadCloser, error)
DeleteObjectFn func(ctx context.Context, bucket string, object string) error
}
func (m *MockGCSClient) GetObject(ctx context.Context, bucket string, object string) (io.ReadCloser, error) {
if m.GetObjectFn != nil {
return m.GetObjectFn(ctx, bucket, object)
}
return nil, errors.New("not implemented")
}
func (m *MockGCSClient) DeleteObject(ctx context.Context, bucket string, object string) error {
if m.DeleteObjectFn != nil {
return m.DeleteObjectFn(ctx, bucket, object)
}
return errors.New("not implemented")
}