From 4270caa721f2e32041d6cf4567f5e911c8170a97 Mon Sep 17 00:00:00 2001 From: Lucas Holmquist Date: Thu, 21 May 2020 14:42:45 -0400 Subject: [PATCH] squash: warn if the client can't load the remote spec. load the default spec instead --- lib/openshift-rest-client.js | 12 +++++-- test/openshift-client-test.js | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/lib/openshift-rest-client.js b/lib/openshift-rest-client.js index 402339b..90fdf36 100644 --- a/lib/openshift-rest-client.js +++ b/lib/openshift-rest-client.js @@ -150,9 +150,17 @@ async function openshiftClient (settings = {}) { clientConfig.backend = new Request({ kubeconfig }); } - const client = new Client(clientConfig); + let client = new Client(clientConfig); if (settings.loadSpecFromCluster) { - await client.loadSpec(); + try { + await client.loadSpec(); + } catch (err) { + // Warn the user there was an error and loading the other spec + console.warn('Warning: Remote client spec unable to load', err.message); + console.warn('Warning: Loading default spec instead'); + clientConfig.spec = spec; + client = new Client(clientConfig); + } } // CRD with the service instance stuff, but only to this client, not the cluster diff --git a/test/openshift-client-test.js b/test/openshift-client-test.js index e7552df..5275697 100644 --- a/test/openshift-client-test.js +++ b/test/openshift-client-test.js @@ -212,3 +212,63 @@ test('openshift client tests - loadSpecFromCluster', async (t) => { t.ok(osClient.kubeconfig, 'client should have the kubeconfig object'); t.end(); }); + +test('openshift client tests - loadSpecFromCluster - Fail to load remote and load default spec', async (t) => { + const openshiftRestClient = require('../'); + + openshiftRestClient.config.loadFromString(JSON.stringify(userDefinedConfig)); + const settings = { + loadSpecFromCluster: true, + config: openshiftRestClient.config + }; + + nock('https://192.168.99.100:8443') + .matchHeader('authorization', 'Bearer zVBd1ZFeJqEAILJgimm4-gZJauaw3PW4EVqV_peEZ3U') + .get('/openapi/v2') + .reply(404, { message: 'Nope' }) + .get('/swagger.json') + .reply(404, { message: 'Nope' }); + + const osClient = await openshiftRestClient.OpenshiftClient(settings); + + t.pass('Failing client load should load default spec'); + + t.ok(osClient.apis['build.openshift.io'], 'client object should have a build object'); + t.ok(osClient.apis.build, 'build object is aliased'); + + t.ok(osClient.apis['apps.openshift.io'], 'client object should have a apps object'); + t.ok(osClient.apis.app, 'apps object is aliased to app'); + + t.ok(osClient.apis['authorization.openshift.io'], 'client object should have a authorization object'); + t.ok(osClient.apis.authorization, 'authorization object is aliased to authorization'); + + t.ok(osClient.apis['image.openshift.io'], 'client object should have a image object'); + t.ok(osClient.apis.image, 'image object is aliased to image'); + + t.ok(osClient.apis['network.openshift.io'], 'osClient object should have a network object'); + t.ok(osClient.apis.network, 'network object is aliased to network'); + + t.ok(osClient.apis['oauth.openshift.io'], 'osClient object should have a oauth object'); + t.ok(osClient.apis.oauth, 'oauth object is aliased to oauth'); + + t.ok(osClient.apis['project.openshift.io'], 'osClient object should have a project object'); + t.ok(osClient.apis.project, 'project object is aliased to project'); + + t.ok(osClient.apis['quota.openshift.io'], 'osClient object should have a quota object'); + t.ok(osClient.apis.quota, 'quota object is aliased to quota'); + + t.ok(osClient.apis['route.openshift.io'], 'osClient object should have a route object'); + t.ok(osClient.apis.route, 'route object is aliased to route'); + + t.ok(osClient.apis['security.openshift.io'], 'osClient object should have a security object'); + t.ok(osClient.apis.security, 'security object is aliased to security'); + + t.ok(osClient.apis['template.openshift.io'], 'osClient object should have a template object'); + t.ok(osClient.apis.template, 'template object is aliased to template'); + + t.ok(osClient.apis['user.openshift.io'], 'osClient object should have a user object'); + t.ok(osClient.apis.user, 'user object is aliased to user'); + + t.ok(osClient.kubeconfig, 'client should have the kubeconfig object'); + t.end(); +});