kubernetes-backend: refactor tests to avoid mock-fs

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-10-02 19:16:00 +02:00
parent 527142b06b
commit 3247b26d63
4 changed files with 48 additions and 24 deletions
-1
View File
@@ -89,7 +89,6 @@
"@backstage/backend-test-utils": "workspace:^",
"@backstage/cli": "workspace:^",
"@types/aws4": "^1.5.1",
"mock-fs": "^5.2.0",
"msw": "^1.0.0",
"supertest": "^6.1.3",
"ws": "^8.13.0"
@@ -13,8 +13,37 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createMockDirectory } from '@backstage/backend-test-utils';
import { ServiceAccountStrategy } from './ServiceAccountStrategy';
import mockFs from 'mock-fs';
const mockDir = createMockDirectory({
content: {
'token.txt': 'in-cluster-token',
},
});
jest.mock('@kubernetes/client-node', () => ({
KubeConfig: class {
#loaded = false;
loadFromCluster() {
this.#loaded = true;
}
getCurrentUser() {
if (!this.#loaded) {
throw new Error('loadFromCluster not called');
}
return {
authProvider: {
config: {
get tokenFile() {
return mockDir.resolve('token.txt');
},
},
},
};
}
},
}));
describe('ServiceAccountStrategy', () => {
describe('#getCredential', () => {
@@ -32,16 +61,9 @@ describe('ServiceAccountStrategy', () => {
token: 'from config',
});
});
describe('when serviceAccountToken is absent from config', () => {
afterEach(() => {
mockFs.restore();
});
describe('when serviceAccountToken is absent from config', () => {
it('reads in-cluster token', async () => {
mockFs({
'/var/run/secrets/kubernetes.io/serviceaccount/token':
'in-cluster-token',
});
const strategy = new ServiceAccountStrategy();
const credential = await strategy.getCredential({
@@ -26,8 +26,17 @@ import {
rest,
} from 'msw';
import { setupServer } from 'msw/node';
import { setupRequestMockHandlers } from '@backstage/backend-test-utils';
import mockFs from 'mock-fs';
import {
createMockDirectory,
setupRequestMockHandlers,
} from '@backstage/backend-test-utils';
import { Config } from '@kubernetes/client-node';
const mockCertDir = createMockDirectory({
content: {
'ca.crt': 'MOCKCA',
},
});
const OBJECTS_TO_FETCH = new Set<ObjectToFetch>([
{
@@ -728,13 +737,7 @@ describe('KubernetesFetcher', () => {
expect(agent.options.ca).toBeUndefined();
});
describe('with a CA file on disk', () => {
afterEach(() => {
mockFs.restore();
});
it('should trust contents of specified caFile', async () => {
mockFs({
'/path/to/ca.crt': 'MOCKCA',
});
worker.use(
rest.get('https://localhost:9999/api/v1/pods', (req, res, ctx) =>
res(
@@ -752,7 +755,7 @@ describe('KubernetesFetcher', () => {
name: 'cluster1',
url: 'https://localhost:9999',
authMetadata: {},
caFile: '/path/to/ca.crt',
caFile: mockCertDir.resolve('ca.crt'),
},
credential: { type: 'bearer token', token: 'token' },
objectTypesToFetch: new Set<ObjectToFetch>([
@@ -899,17 +902,18 @@ describe('KubernetesFetcher', () => {
describe('Backstage running on k8s', () => {
const initialHost = process.env.KUBERNETES_SERVICE_HOST;
const initialPort = process.env.KUBERNETES_SERVICE_PORT;
const initialCaPath = Config.SERVICEACCOUNT_CA_PATH;
afterEach(() => {
process.env.KUBERNETES_SERVICE_HOST = initialHost;
process.env.KUBERNETES_SERVICE_PORT = initialPort;
mockFs.restore();
Config.SERVICEACCOUNT_CA_PATH = initialCaPath;
});
it('makes in-cluster requests when cluster details has no token', async () => {
process.env.KUBERNETES_SERVICE_HOST = '10.10.10.10';
process.env.KUBERNETES_SERVICE_PORT = '443';
mockFs({
'/var/run/secrets/kubernetes.io/serviceaccount/ca.crt': '',
});
Config.SERVICEACCOUNT_CA_PATH = mockCertDir.resolve('ca.crt');
worker.use(
rest.get('https://10.10.10.10/api/v1/pods', (req, res, ctx) =>
res(