This repository has been archived by the owner on Feb 25, 2025. It is now read-only.
forked from thomaspoignant/go-feature-flag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretriever_gc_storage_test.go
156 lines (142 loc) · 4.06 KB
/
retriever_gc_storage_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
package ffclient
import (
"context"
"crypto/md5" //nolint: gosec
"io"
"io/ioutil"
"testing"
"cloud.google.com/go/storage"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/thomaspoignant/go-feature-flag/testutils"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
)
func TestGCStorageRetriever_Retrieve(t *testing.T) {
type fields struct {
Options []option.ClientOption
Bucket string
Object string
rC io.ReadCloser
}
type args struct {
ctx context.Context
}
mockCtrl := gomock.NewController(t)
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
wantReadDataFromCache bool
}{
{
name: "File on Object Not in Cache",
fields: fields{
Options: []option.ClientOption{option.WithCredentials(&google.Credentials{})},
Bucket: "bucket",
Object: "Object",
rC: &testutils.GCStorageReaderMock{
ShouldFail: false,
FileToRead: "./testdata/flag-config-updated.yaml",
},
},
args: args{
ctx: context.Background(),
},
wantErr: false,
wantReadDataFromCache: false,
},
{
name: "File on Object in Cache",
fields: fields{
Options: []option.ClientOption{option.WithCredentials(&google.Credentials{})},
Bucket: "bucket",
Object: "Object",
rC: &testutils.GCStorageReaderMock{
ShouldFail: true,
},
},
args: args{
ctx: context.Background(),
},
wantErr: false,
wantReadDataFromCache: true,
},
{
name: "File not On Object",
fields: fields{
Options: []option.ClientOption{option.WithCredentials(&google.Credentials{})},
Bucket: "bucket",
Object: "Object",
rC: &testutils.GCStorageReaderMock{
ShouldFail: true,
},
},
args: args{
ctx: context.Background(),
},
wantErr: true,
wantReadDataFromCache: false,
},
{
name: "Option Without Auth",
fields: fields{
Options: []option.ClientOption{option.WithoutAuthentication()},
Bucket: "bucket",
Object: "Object",
rC: &testutils.GCStorageReaderMock{
ShouldFail: false,
FileToRead: "./testdata/flag-config-updated.yaml",
},
},
args: args{
ctx: context.Background(),
},
wantErr: false,
wantReadDataFromCache: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &GCStorageRetriever{
Options: tt.fields.Options,
Bucket: tt.fields.Bucket,
Object: tt.fields.Object,
rC: tt.fields.rC,
md5: make([]byte, 16),
}
// Read default file.
want, err := ioutil.ReadFile("./testdata/flag-config.yaml")
assert.NoError(t, err)
r.cache = want
// Compute Hash of this file data.
md5Hash := md5.Sum(want) //nolint: gosec
wantedMd5 := md5Hash[:]
copy(r.md5, wantedMd5)
obj := testutils.NewMockobject(mockCtrl)
if tt.wantReadDataFromCache {
// If expect the data to be in cache, mock the
// remote hash of the local data.
obj.EXPECT().Attrs(context.Background()).Return(&storage.ObjectAttrs{MD5: r.md5}, nil).Times(1)
} else {
// If expect data not to be in cache, mock the
// remote hash to a different one that the local hash.
want, err = ioutil.ReadFile("./testdata/flag-config-updated.yaml")
assert.NoError(t, err)
md5Hash = md5.Sum(want) //nolint: gosec
wantedMd5 = md5Hash[:]
obj.EXPECT().Attrs(context.Background()).Return(&storage.ObjectAttrs{MD5: wantedMd5}, nil).Times(1)
}
r.obj = obj
got, err := r.Retrieve(tt.args.ctx)
assert.Equal(t, tt.wantErr, err != nil, "Retrieve() error = %v, wantErr %v", err, tt.wantErr)
if err == nil {
assert.Equal(t, want, got, "Retrieve() got = %v, want %v", got, tt.want)
assert.Equal(t, want, r.cache, "Retrieve() got = GCStorageRetriever{cache: %v} want = GCStorageRetriever{cache: %v}", r.cache, want)
assert.Equal(t, wantedMd5, r.md5, "Retrieve() got = GCStorageRetriever{md5: %v} want = GCStorageRetriever{md5: %v}", r.md5, wantedMd5)
}
})
}
}