Skip to content

Commit 9e54a97

Browse files
committed
feat: add test-code for s3 registry store
1 parent bb17fbd commit 9e54a97

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package registry
2+
3+
import (
4+
"context"
5+
"errors"
6+
"io/ioutil"
7+
"net/url"
8+
"strings"
9+
"testing"
10+
"time"
11+
12+
"github.com/aws/aws-sdk-go-v2/service/s3"
13+
)
14+
15+
func TestGetOnlineFeaturesS3Registry(t *testing.T) {
16+
mockS3Client := &MockS3Client{
17+
GetObjectFn: func(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error) {
18+
return &s3.GetObjectOutput{
19+
Body: ioutil.NopCloser(strings.NewReader("mock data")),
20+
}, nil
21+
},
22+
DeleteObjectFn: func(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) {
23+
return &s3.DeleteObjectOutput{}, nil
24+
},
25+
}
26+
27+
tests := []struct {
28+
name string
29+
config *RepoConfig
30+
}{
31+
{
32+
name: "redis with simple features",
33+
config: &RepoConfig{
34+
Project: "feature_repo",
35+
Registry: map[string]interface{}{
36+
"path": "s3://test-bucket/path/to/registry.db",
37+
},
38+
Provider: "aws",
39+
},
40+
},
41+
}
42+
for _, test := range tests {
43+
registryConfig, err := test.config.GetRegistryConfig()
44+
if err != nil {
45+
t.Errorf("Error getting registry config. msg: %s", err.Error())
46+
}
47+
r := &Registry{
48+
project: test.config.Project,
49+
cachedRegistryProtoTtl: time.Duration(registryConfig.CacheTtlSeconds) * time.Second,
50+
}
51+
_ = registryConfig.RegistryStoreType
52+
registryPath := registryConfig.Path
53+
uri, err := url.Parse(registryPath)
54+
if err != nil {
55+
t.Errorf("Error parsing registry path. msg: %s", err.Error())
56+
}
57+
if registryStoreType, ok := REGISTRY_STORE_CLASS_FOR_SCHEME[uri.Scheme]; ok {
58+
switch registryStoreType {
59+
case "S3RegistryStore":
60+
registryStore := &S3RegistryStore{
61+
filePath: registryConfig.Path,
62+
s3Client: mockS3Client,
63+
}
64+
r.registryStore = registryStore
65+
err := r.InitializeRegistry()
66+
if err != nil {
67+
t.Errorf("Error initializing registry. msg: %s. registry path=%q", err.Error(), registryPath)
68+
}
69+
default:
70+
t.Errorf("Only S3RegistryStore is supported on this testing. got=%s", registryStoreType)
71+
}
72+
}
73+
}
74+
}
75+
76+
// MockS3Client is mock client for testing s3 registry store
77+
type MockS3Client struct {
78+
GetObjectFn func(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error)
79+
DeleteObjectFn func(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error)
80+
}
81+
82+
func (m *MockS3Client) GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error) {
83+
if m.GetObjectFn != nil {
84+
return m.GetObjectFn(ctx, params)
85+
}
86+
return nil, errors.New("not implemented")
87+
}
88+
89+
func (m *MockS3Client) DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) {
90+
if m.DeleteObjectFn != nil {
91+
return m.DeleteObjectFn(ctx, params)
92+
}
93+
return nil, errors.New("not implemented")
94+
}

go/internal/feast/registry/s3.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@ import (
1515
"google.golang.org/protobuf/proto"
1616
)
1717

18+
// S3ClientInterface define interface of s3.Client for making mocking s3 client and testing it
19+
type S3ClientInterface interface {
20+
GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error)
21+
DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error)
22+
}
23+
1824
// A S3RegistryStore is a S3 object storage-based implementation of the RegistryStore interface
1925
type S3RegistryStore struct {
2026
filePath string
21-
s3Client *s3.Client
27+
s3Client S3ClientInterface
2228
}
2329

2430
// NewS3RegistryStore creates a S3RegistryStore with the given configuration
@@ -55,7 +61,7 @@ func (r *S3RegistryStore) GetRegistryProto() (*core.Registry, error) {
5561
Key: aws.String(key),
5662
})
5763
if err != nil {
58-
return nil, err
64+
panic(err)
5965
}
6066
defer output.Body.Close()
6167

@@ -88,7 +94,7 @@ func (r *S3RegistryStore) Teardown() error {
8894
Key: aws.String(key),
8995
})
9096
if err != nil {
91-
return err
97+
panic(err)
9298
}
9399
return nil
94100
}

0 commit comments

Comments
 (0)