-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
GoogleGcsUrlReader.test.ts
160 lines (144 loc) · 4.89 KB
/
GoogleGcsUrlReader.test.ts
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
/*
* Copyright 2020 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ConfigReader } from '@backstage/config';
import { JsonObject } from '@backstage/types';
import { getVoidLogger } from '../logging';
import { DefaultReadTreeResponseFactory } from './tree';
import { GoogleGcsUrlReader } from './GoogleGcsUrlReader';
import { UrlReaderPredicateTuple } from './types';
const bucketGetFilesMock = jest.fn();
jest.mock('@google-cloud/storage', () => {
class Bucket {
getFiles(query: any) {
return bucketGetFilesMock(query);
}
}
class Storage {
bucket() {
return new Bucket();
}
}
return {
__esModule: true,
Storage,
};
});
describe('GcsUrlReader', () => {
const createReader = (config: JsonObject): UrlReaderPredicateTuple[] => {
return GoogleGcsUrlReader.factory({
config: new ConfigReader(config),
logger: getVoidLogger(),
treeResponseFactory: DefaultReadTreeResponseFactory.create({
config: new ConfigReader({}),
}),
});
};
it('does not create a reader without the googleGcs field', () => {
const entries = createReader({
integrations: {},
});
expect(entries).toHaveLength(0);
});
it('creates a reader with credentials correctly configured', () => {
const entries = createReader({
integrations: {
googleGcs: {
privateKey: '--- BEGIN KEY ---- fakekey --- END KEY ---',
clientEmail: 'someone@example.com',
},
},
});
expect(entries).toHaveLength(1);
});
it('creates a reader with default credentials provider', () => {
const entries = createReader({
integrations: {
googleGcs: {},
},
});
expect(entries).toHaveLength(1);
});
describe('predicates', () => {
const readers = createReader({
integrations: {
googleGcs: {},
},
});
const predicate = readers[0].predicate;
it('returns true for the correct google cloud storage host', () => {
expect(predicate(new URL('https://storage.cloud.google.com'))).toBe(true);
});
it('returns true for a url with the full path and the correct host', () => {
expect(
predicate(
new URL(
'https://storage.cloud.google.com/team1/service1/catalog-info.yaml',
),
),
).toBe(true);
});
it('returns false for the wrong hostname under cloud.google.com', () => {
expect(predicate(new URL('https://storage2.cloud.google.com'))).toBe(
false,
);
});
it('returns false for a partially correct host', () => {
expect(predicate(new URL('https://cloud.google.com'))).toBe(false);
});
it('returns false for a completely different host', () => {
expect(predicate(new URL('https://a.example.com/test'))).toBe(false);
});
});
describe('search', () => {
const { reader } = createReader({ integrations: { googleGcs: {} } })[0];
beforeEach(() => {
jest.clearAllMocks();
});
it('throws if search url does not end with *', async () => {
const glob = 'https://storage.cloud.google.com/bucket/no-asterisk';
await expect(() => reader.search(glob)).rejects.toThrow(
'GcsUrlReader only supports prefix-based searches',
);
});
it('throws if search url looks truly glob-y', async () => {
const glob = 'https://storage.cloud.google.com/bucket/**/path*';
await expect(() => reader.search(glob)).rejects.toThrow(
'GcsUrlReader only supports prefix-based searches',
);
});
it('searches with expected prefix and pagination', () => {
bucketGetFilesMock.mockResolvedValue([[]]);
const glob = 'https://storage.cloud.google.com/bucket/path/some-prefix-*';
reader.search(glob);
expect(bucketGetFilesMock).toHaveBeenCalledWith(
expect.objectContaining({
autoPaginate: true,
prefix: 'path/some-prefix-',
}),
);
});
it('returns valid SearchResponse object', async () => {
const expectedFile = { name: 'path/some-prefix-1.txt' };
bucketGetFilesMock.mockResolvedValue([[expectedFile]]);
const glob = 'https://storage.cloud.google.com/bucket/path/some-prefix-*';
const result = await reader.search(glob);
expect(result.files).toHaveLength(1);
expect(result.files[0].url).toEqual(
'https://storage.cloud.google.com/bucket/path/some-prefix-1.txt',
);
});
});
});