Skip to content

Commit

Permalink
Cluster tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmroz-allegro committed Mar 17, 2022
1 parent 21138c3 commit 7684dc2
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 50 deletions.
179 changes: 132 additions & 47 deletions src/common/models/cluster/cluster.mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,136 @@
* limitations under the License.
*/

import { expect, use } from "chai";
import equivalent from "../../../client/utils/test-utils/equivalent";
import { RequestDecorator } from "../../../server/utils/request-decorator/request-decorator";
import { RetryOptions } from "../../../server/utils/retry-options/retry-options";
import { ClusterJS, fromConfig } from "./cluster";

use(equivalent);

describe("Cluster", () => {
// TODO: reimplement this test as simpler cases without immutable-class-tester - it checks too much
// it.skip("is an immutable class", () => {
// testImmutableClass(Cluster, [
// {
// name: "my-druid-cluster"
// },
// {
// name: "my-druid-cluster",
// url: "https://192.168.99.100",
// version: "0.9.1",
// timeout: 30000,
// healthCheckTimeout: 50,
// sourceListScan: "auto",
// sourceListRefreshOnLoad: true,
// sourceListRefreshInterval: 10000,
// sourceReintrospectInterval: 10000,
//
// introspectionStrategy: "segment-metadata-fallback"
// },
// {
// name: "my-mysql-cluster",
// url: "http://192.168.99.100",
// timeout: 30000,
// sourceListScan: "auto"
// },
// {
// name: "my-mysql-cluster",
// url: "https://192.168.99.100",
// timeout: 30000,
// sourceListScan: "auto",
// sourceListRefreshInterval: 0,
// sourceReintrospectInterval: 0
// }
// ]);
// });
//
// describe.skip("backward compatibility", () => {
// it("should read old host and assume http protocol", () => {
// const cluster = fromConfig({
// name: "old-host",
// host: "broker-host.com"
// } as ClusterJS);
//
// expect(cluster.url).to.be.eq("http://broker-host.com");
// });
// });
});
describe("fromConfig", () => {
it("should load defaults", () => {
const cluster = fromConfig({
name: "foobar",
url: "http://bazz"
});

expect(cluster).to.be.deep.equal({
name: "foobar",
guardDataCubes: false,
healthCheckTimeout: 1000,
introspectionStrategy: "segment-metadata-fallback",
requestDecorator: null,
retry: new RetryOptions(),
sourceListRefreshInterval: 0,
sourceListRefreshOnLoad: false,
sourceListScan: "auto",
sourceReintrospectInterval: 0,
sourceReintrospectOnLoad: false,
timeout: undefined,
title: "",
type: "druid",
url: "http://bazz",
version: null
});
});

it("should throw with incorrect name type", () => {
expect(() => fromConfig({ name: 1 } as unknown as ClusterJS)).to.throw("must be a string");
});

it("should throw with incorrect empty name", () => {
expect(() => fromConfig({ name: "" })).to.throw("empty name");
});

it("should throw with not url safe name", () => {
expect(() => fromConfig({ name: "foobar%bazz#" })).to.throw("is not a URL safe name");
});

it("should throw with name equal to native", () => {
expect(() => fromConfig({ name: "native" })).to.throw("name can not be 'native'");
});

it("should read retry options", () => {
const cluster = fromConfig({
name: "foobar", retry: {
maxAttempts: 1,
delay: 42
}
});

expect(cluster.retry).to.be.equivalent(new RetryOptions({ maxAttempts: 1, delay: 42 }));
});

it("should read request decorator", () => {
const cluster = fromConfig({
name: "foobar",
requestDecorator: {
path: "foobar",
options: { bazz: true }
}
});

expect(cluster.requestDecorator).to.be.equivalent(new RequestDecorator("foobar", { bazz: true }));
});

it("should read request decorator old format", () => {
const cluster = fromConfig({
name: "foobar",
requestDecorator: "foobar",
decoratorOptions: { bazz: true }
} as unknown as ClusterJS);

expect(cluster.requestDecorator).to.be.equivalent(new RequestDecorator("foobar", { bazz: true }));
});

it("should read old host and assume http protocol", () => {
const cluster = fromConfig({
name: "old-host",
host: "broker-host.com"
} as ClusterJS);

expect(cluster.url).to.be.eq("http://broker-host.com");
});

it("should override default values", () => {
const cluster = fromConfig({
guardDataCubes: true,
healthCheckTimeout: 42,
introspectionStrategy: "introspection-introspection",
name: "cluster-name",
sourceListRefreshInterval: 1123,
sourceListRefreshOnLoad: true,
sourceListScan: "auto",
sourceReintrospectInterval: 1432,
sourceReintrospectOnLoad: true,
timeout: 581,
title: "foobar-title",
url: "http://url-bazz",
version: "new-version"
});

expect(cluster).to.be.deep.equal({
guardDataCubes: true,
healthCheckTimeout: 42,
introspectionStrategy: "introspection-introspection",
name: "cluster-name",
sourceListRefreshInterval: 1123,
sourceListRefreshOnLoad: true,
sourceListScan: "auto",
sourceReintrospectInterval: 1432,
sourceReintrospectOnLoad: true,
timeout: 581,
title: "foobar-title",
url: "http://url-bazz",
version: "new-version",
type: "druid",
requestDecorator: null,
retry: new RetryOptions()
});
});
});
})
;
13 changes: 10 additions & 3 deletions src/common/models/cluster/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,17 @@ function validateUrl(url: string): void {
const HTTP_PROTOCOL_TEST = /^http(s?):/;

function readUrl(cluster: any): string {
if (isTruthy(cluster.url)) return cluster.url;
if (isTruthy(cluster.url)) {
validateUrl(cluster.url);
return cluster.url;
}
const oldHost = cluster.host || cluster.druidHost || cluster.brokerHost;
return HTTP_PROTOCOL_TEST.test(oldHost) ? oldHost : `http://${oldHost}`;
if (isTruthy(oldHost)) {
const url = HTTP_PROTOCOL_TEST.test(oldHost) ? oldHost : `http://${oldHost}`;
validateUrl(url);
return url;
}
throw new Error("Cluster: missing url field");
}

function readRequestDecorator(cluster: any): RequestDecorator | null {
Expand Down Expand Up @@ -155,7 +163,6 @@ export function fromConfig(params: ClusterJS): Cluster {
const requestDecorator = readRequestDecorator(params);

const url = readUrl(params);
validateUrl(url);

return {
type: "druid",
Expand Down

0 comments on commit 7684dc2

Please sign in to comment.