|
| 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 | +} |
0 commit comments