Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge template management to index-operation-common #511

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions cypress/integration/templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { PLUGIN_NAME } from "../support/constants";

const SAMPLE_TEMPLATE_PREFIX = "index-for-alias-test";
const MAX_TEMPLATE_NUMBER = 30;

describe("Templates", () => {
before(() => {
// Set welcome screen tracking to false
localStorage.setItem("home:welcome:show", "false");
cy.deleteTemplate(`${SAMPLE_TEMPLATE_PREFIX}-${MAX_TEMPLATE_NUMBER}`);
for (let i = 0; i < MAX_TEMPLATE_NUMBER; i++) {
cy.deleteTemplate(`${SAMPLE_TEMPLATE_PREFIX}-${i}`);
cy.createIndexTemplate(`${SAMPLE_TEMPLATE_PREFIX}-${i}`, {
index_patterns: ["template-test-*"],
priority: i,
template: {
aliases: {},
settings: {
number_of_shards: 2,
number_of_replicas: 1,
},
},
});
}
});

beforeEach(() => {
// Visit ISM OSD
cy.visit(`${Cypress.env("opensearch_dashboards")}/app/${PLUGIN_NAME}#/templates`);

// Common text to wait for to confirm page loaded, give up to 60 seconds for initial load
cy.contains("Rows per page", { timeout: 60000 });
});

describe("can be searched / sorted / paginated", () => {
it("successfully", () => {
cy.get('[data-test-subj="pagination-button-1"]').should("exist");
cy.get('[placeholder="Search..."]').type(`${SAMPLE_TEMPLATE_PREFIX}-0`);
cy.contains(`${SAMPLE_TEMPLATE_PREFIX}-0`);
cy.get(".euiTableRow").should("have.length", 1);
});
});

describe("can create a template", () => {
it("successfully", () => {
cy.get('[data-test-subj="Create templateButton"]').click();
cy.contains("Define template");

cy.get('[data-test-subj="form-row-name"] input').type(`${SAMPLE_TEMPLATE_PREFIX}-${MAX_TEMPLATE_NUMBER}`);
cy.get('[data-test-subj="form-row-index_patterns"] [data-test-subj="comboBoxSearchInput"]').type("test{enter}");
cy.get('[data-test-subj="CreateIndexTemplateCreateButton"]').click();

cy.contains(`${SAMPLE_TEMPLATE_PREFIX}-${MAX_TEMPLATE_NUMBER} has been successfully created.`);

cy.get('[placeholder="Search..."]').type(`${SAMPLE_TEMPLATE_PREFIX}-${MAX_TEMPLATE_NUMBER}`);
cy.contains(`${SAMPLE_TEMPLATE_PREFIX}-${MAX_TEMPLATE_NUMBER}`);
cy.get(".euiTableRow").should("have.length", 1);
});
});

describe("can delete a template", () => {
it("successfully", () => {
cy.get('[placeholder="Search..."]').type(`${SAMPLE_TEMPLATE_PREFIX}-0`);
cy.contains(`${SAMPLE_TEMPLATE_PREFIX}-0`);
cy.get(`#_selection_column_${SAMPLE_TEMPLATE_PREFIX}-0-checkbox`).click();

cy.get('[data-test-subj="moreAction"] button').click().get('[data-test-subj="deleteAction"]').click();
// The confirm button should be disabled
cy.get('[data-test-subj="deleteConfirmButton"]').should("be.disabled");
// type delete
cy.wait(500).get('[data-test-subj="deleteInput"]').type("delete");
cy.get('[data-test-subj="deleteConfirmButton"]').should("not.be.disabled");
// click to delete
cy.get('[data-test-subj="deleteConfirmButton"]').click();
// the alias should not exist
cy.wait(500);
cy.get(`#_selection_column_${SAMPLE_TEMPLATE_PREFIX}-0-checkbox`).should("not.exist");
});
});

after(() => {
cy.deleteTemplate(`${SAMPLE_TEMPLATE_PREFIX}-${MAX_TEMPLATE_NUMBER}`);
for (let i = 0; i < MAX_TEMPLATE_NUMBER; i++) {
cy.deleteTemplate(`${SAMPLE_TEMPLATE_PREFIX}-${i}`);
}
});
});
92 changes: 84 additions & 8 deletions models/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,70 @@ export interface ManagedIndexMetaData {
info?: object;
}

export type MappingsPropertiesObject = Record<
string,
{
type: string;
properties?: MappingsPropertiesObject;
}
>;

export type MappingsProperties = {
fieldName: string;
type: string;
path?: string;
analyzer?: string;
properties?: MappingsProperties;
}[];

export interface IndexItem {
index: string;
indexUuid?: string;
data_stream: string | null;
settings?: {
index?: {
number_of_shards?: number;
number_of_replicas?: number;
creation_date?: string;
[key: string]: any;
};
"index.number_of_shards"?: number;
"index.number_of_replicas"?: number;
"index.refresh_interval"?: string;
[key: string]: any;
};
aliases?: Record<string, {}>;
mappings?: {
properties?: MappingsProperties;
[key: string]: any;
};
}

export interface IndexItemRemote extends Omit<IndexItem, "mappings"> {
mappings?: {
properties?: MappingsPropertiesObject;
};
}

interface ITemplateExtras {
name: string;
data_stream?: {};
version: number;
priority: number;
index_patterns: string[];
}

export interface TemplateItem extends ITemplateExtras {
template: Pick<IndexItem, "aliases" | "mappings" | "settings">;
}
export interface TemplateItemRemote extends ITemplateExtras {
template: Pick<IndexItemRemote, "aliases" | "mappings" | "settings">;
}

/**
* ManagedIndex item shown in the Managed Indices table
*/
export interface ManagedIndexItem {
index: string;
indexUuid: string;
export interface ManagedIndexItem extends IndexItem {
dataStream: string | null;
policyId: string;
policySeqNo: number;
Expand All @@ -38,10 +96,6 @@ export interface ManagedIndexItem {
managedIndexMetaData: ManagedIndexMetaData | null;
}

export interface IndexItem {
index: string;
}

/**
* Interface what the Policy Opensearch Document
*/
Expand Down Expand Up @@ -168,7 +222,7 @@ export interface SMDeleteCondition {
export interface ErrorNotification {
destination?: Destination;
channel?: Channel;
message_template: MessageTemplate;
message_template?: MessageTemplate;
}

export interface Notification {
Expand Down Expand Up @@ -564,3 +618,25 @@ export enum TRANSFORM_AGG_TYPE {
histogram = "histogram",
date_histogram = "date_histogram",
}
export interface IAPICaller {
endpoint: string;
method?: string;
data?: any;
}

export interface IRecoveryItem {
index: string;
stage: "done" | "translog";
}

export interface ITaskItem {
action: string;
description: string;
}

export interface IReindexItem extends ITaskItem {
fromIndex: string;
toIndex: string;
}

export type IAliasAction = Record<string, { index: string; alias: string }>;
117 changes: 117 additions & 0 deletions public/pages/CreateIndex/components/AliasSelect/AliasSelect.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useState } from "react";
import { render, waitFor } from "@testing-library/react";
import AliasSelect, { AliasSelectProps } from "./index";
import userEvent from "@testing-library/user-event";

const onChangeMock = jest.fn();

const AliasSelectWithOnchange = (props: AliasSelectProps) => {
const [tempValue, setTempValue] = useState(props.value);
return (
<AliasSelect
{...props}
value={tempValue}
onChange={(val) => {
onChangeMock(val);
setTempValue(val);
}}
/>
);
};

describe("<AliasSelect /> spec", () => {
it("renders the component and remove duplicate aliases", async () => {
const onOptionsChange = jest.fn();
const { container } = render(
<AliasSelect
refreshOptions={() =>
Promise.resolve({
ok: true,
response: [
{
alias: "a",
index: "a",
},
{
alias: "a",
index: "b",
},
],
})
}
onChange={() => {}}
onOptionsChange={onOptionsChange}
/>
);
await waitFor(
() => {
expect(onOptionsChange).toBeCalledWith([
{
label: "a",
},
]);
expect(container.firstChild).toMatchSnapshot();
},
{
timeout: 3000,
}
);
});

it("renders with error", async () => {
const onOptionsChange = jest.fn();
const { container } = render(
<AliasSelect
refreshOptions={() =>
Promise.resolve({
ok: false,
error: "Some error",
})
}
onChange={() => {}}
onOptionsChange={onOptionsChange}
/>
);
await waitFor(() => {});
expect(container).toMatchSnapshot();
});

it("it should choose options or create one", async () => {
const { getByTestId } = render(
<AliasSelectWithOnchange
refreshOptions={() => Promise.resolve({ ok: true, response: [{ alias: "test", index: "123", query: "test" }] })}
/>
);
await waitFor(() => {
expect(getByTestId("comboBoxInput")).toBeInTheDocument();
});
await userEvent.click(getByTestId("comboBoxInput"));
await waitFor(() => {
expect(document.querySelector('button[title="test"]')).toBeInTheDocument();
});
await userEvent.click(document.querySelector('button[title="test"]') as Element);
await waitFor(() => {
expect(onChangeMock).toBeCalledTimes(1);
expect(onChangeMock).toBeCalledWith({
test: {},
});
});
await userEvent.type(getByTestId("comboBoxInput"), "test2{enter}");
await waitFor(() => {
expect(onChangeMock).toBeCalledTimes(2);
expect(onChangeMock).toBeCalledWith({
test: {},
test2: {},
});
});
await userEvent.type(getByTestId("comboBoxInput"), " {enter}");
await waitFor(() => {
expect(onChangeMock).toBeCalledTimes(2);
});
});
});
Loading