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

feat: add cypress trigger type #3439

Merged
merged 3 commits into from
Dec 14, 2023
Merged
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
4 changes: 2 additions & 2 deletions api/triggers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ components:
properties:
type:
type: string
enum: ["http", "grpc", "traceid", "kafka"]
enum: ["http", "grpc", "traceid", "kafka", "cypress"]
httpRequest:
$ref: "./http.yaml#/components/schemas/HTTPRequest"
grpc:
Expand All @@ -21,7 +21,7 @@ components:
properties:
type:
type: string
enum: ["http", "grpc", "traceid", "kafka"]
enum: ["http", "grpc", "traceid", "kafka", "cypress"]
triggerResult:
type: object
properties:
Expand Down
8 changes: 7 additions & 1 deletion server/executor/trigger/traceid.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/kubeshop/tracetest/server/test/trigger"
)

const TRACEID_EXPRESSION = "${env:TRACE_ID}"

func TRACEID() Triggerer {
return &traceidTriggerer{}
}
Expand All @@ -17,7 +19,7 @@ type traceidTriggerer struct{}
func (t *traceidTriggerer) Trigger(ctx context.Context, test test.Test, opts *TriggerOptions) (Response, error) {
response := Response{
Result: trigger.TriggerResult{
Type: t.Type(),
Type: test.Trigger.Type,
TraceID: &trigger.TraceIDResponse{ID: test.Trigger.TraceID.ID},
},
}
Expand All @@ -30,6 +32,10 @@ func (t *traceidTriggerer) Type() trigger.TriggerType {
}

func (t *traceidTriggerer) Resolve(ctx context.Context, test test.Test, opts *ResolveOptions) (test.Test, error) {
if test.Trigger.Type == trigger.TriggerTypeCypress {
jorgeepc marked this conversation as resolved.
Show resolved Hide resolved
test.Trigger.TraceID = &trigger.TraceIDRequest{ID: TRACEID_EXPRESSION}
}

traceid := test.Trigger.TraceID
if traceid == nil {
return test, fmt.Errorf("no settings provided for TRACEID triggerer")
Expand Down
4 changes: 4 additions & 0 deletions server/executor/trigger/triggerer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (r *Registry) Get(triggererType trigger.TriggerType) (Triggerer, error) {
r.Lock()
defer r.Unlock()

if trigger.IsTraceIDBasedTrigger(triggererType) {
triggererType = trigger.TriggerTypeTraceID
}

t, found := r.reg[triggererType]
if !found {
return nil, fmt.Errorf(`cannot get trigger type "%s": %w`, triggererType, ErrTriggererTypeNotRegistered)
Expand Down
4 changes: 2 additions & 2 deletions server/executor/trigger_resolver_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func (r triggerResolverWorker) ProcessItem(ctx context.Context, job Job) {
r.handleError(job.Run, err)
}

if job.Test.Trigger.Type == trigger.TriggerTypeTraceID {
traceIDFromParam, err := trace.TraceIDFromHex(job.Test.Trigger.TraceID.ID)
if trigger.IsTraceIDBasedTrigger(job.Test.Trigger.Type) {
traceIDFromParam, err := trace.TraceIDFromHex(resolvedTest.Trigger.TraceID.ID)
if err == nil {
run.TraceID = traceIDFromParam
}
Expand Down
8 changes: 6 additions & 2 deletions server/http/mappings/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,17 @@ func (m Model) TriggerResult(in openapi.TriggerResult) trigger.TriggerResult {
Type: trigger.TriggerType(in.Type),
Error: m.TriggerError(in.TriggerResult.Error),
}

if trigger.IsTraceIDBasedTrigger(tr.Type) {
tr.TraceID = m.TraceIDResponse(in.TriggerResult.Traceid)
return tr
}

switch in.Type {
case "http":
tr.HTTP = m.HTTPResponse(in.TriggerResult.Http)
case "grpc":
tr.GRPC = m.GRPCResponse(in.TriggerResult.Grpc)
case "traceid":
tr.TraceID = m.TraceIDResponse(in.TriggerResult.Traceid)
case "kafka":
tr.Kafka = m.KafkaResponse(in.TriggerResult.Kafka)
}
Expand Down
9 changes: 9 additions & 0 deletions server/test/trigger/traceid.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package trigger

import "golang.org/x/exp/slices"

const TriggerTypeTraceID TriggerType = "traceid"
const TriggerTypeCypress TriggerType = "cypress"

var traceIDBasedTriggers = []TriggerType{TriggerTypeTraceID, TriggerTypeCypress}

func IsTraceIDBasedTrigger(triggerType TriggerType) bool {
return slices.Contains(traceIDBasedTriggers, triggerType)
}

type TraceIDRequest struct {
ID string `json:"id,omitempty" expr_enabled:"true"`
Expand Down
4 changes: 2 additions & 2 deletions server/test/trigger/trigger_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ type triggerJSONV3 struct {

func (v3 triggerJSONV3) valid() bool {
// has a valid type and at least one not nil trigger type settings
return v3.Type != "" &&
return (v3.Type != "" &&
(v3.HTTP != nil ||
v3.GRPC != nil ||
v3.TraceID != nil ||
v3.Kafka != nil)
v3.Kafka != nil)) || v3.Type == TriggerTypeCypress
}

type triggerJSONV2 struct {
Expand Down
27 changes: 15 additions & 12 deletions web/src/components/CreateTest/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import AllowButton, {Operation} from 'components/AllowButton';
import CreateButton from 'components/CreateButton';
import {TriggerTypes} from 'constants/Test.constants';
import EntryPointFactory from 'components/TestPlugins/EntryPointFactory';
import Test from 'models/Test.model';
import * as S from './CreateTest.styled';

interface IProps {
Expand All @@ -21,18 +22,20 @@ const Header = ({isLoading, isValid, triggerType}: IProps) => {
</S.HeaderLeft>

<S.HeaderRight>
<AllowButton
block
ButtonComponent={CreateButton}
data-cy="run-test-submit"
disabled={!isValid}
loading={isLoading}
onClick={() => form.submit()}
operation={Operation.Edit}
type="primary"
>
Run
</AllowButton>
{Test.shouldAllowRun(triggerType) && (
<AllowButton
block
ButtonComponent={CreateButton}
data-cy="run-test-submit"
disabled={!isValid}
loading={isLoading}
onClick={() => form.submit()}
operation={Operation.Edit}
type="primary"
>
Run
</AllowButton>
)}
</S.HeaderRight>
</S.Header>
);
Expand Down
24 changes: 13 additions & 11 deletions web/src/components/ResourceCard/TestCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ const TestCard = ({onEdit, onDelete, onDuplicate, onRun, onViewAll, test}: IProp
<ResourceCardSummary summary={test.summary} />

<S.Row $gap={12}>
<CreateButton
data-cy={`test-run-button-${test.id}`}
ghost
onClick={event => {
event.stopPropagation();
onRun(test, ResourceType.Test);
}}
type="primary"
>
Run
</CreateButton>
{Test.shouldAllowRun(test.trigger.type) && (
<CreateButton
data-cy={`test-run-button-${test.id}`}
ghost
onClick={event => {
event.stopPropagation();
onRun(test, ResourceType.Test);
}}
type="primary"
>
Run
</CreateButton>
)}
<ResourceCardActions
id={test.id}
shouldEdit={shouldEdit}
Expand Down
7 changes: 5 additions & 2 deletions web/src/components/RunDetailLayout/HeaderRight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import CreateButton from 'components/CreateButton';
import RunActionsMenu from 'components/RunActionsMenu';
import TestActions from 'components/TestActions';
import TestState from 'components/TestState';
import {TriggerTypes} from 'constants/Test.constants';
import {TestState as TestStateEnum} from 'constants/TestRun.constants';
import Test from 'models/Test.model';
import {isRunPollingState, isRunStateFinished, isRunStateStopped, isRunStateSucceeded} from 'models/TestRun.model';
import {useTest} from 'providers/Test/Test.provider';
import {useTestRun} from 'providers/TestRun/TestRun.provider';
Expand All @@ -17,9 +19,10 @@ import useSkipPolling from './hooks/useSkipPolling';

interface IProps {
testId: string;
triggerType: TriggerTypes;
}

const HeaderRight = ({testId}: IProps) => {
const HeaderRight = ({testId, triggerType}: IProps) => {
const {isDraftMode: isTestSpecsDraftMode} = useTestSpecs();
const {isDraftMode: isTestOutputsDraftMode} = useTestOutput();
const isDraftMode = isTestSpecsDraftMode || isTestOutputsDraftMode;
Expand Down Expand Up @@ -48,7 +51,7 @@ const HeaderRight = ({testId}: IProps) => {
<RunStatusIcon state={state} requiredGatesResult={requiredGatesResult} />
)}
<VariableSetSelector />
{!isDraftMode && state && isRunStateFinished(state) && (
{!isDraftMode && state && isRunStateFinished(state) && Test.shouldAllowRun(triggerType) && (
<CreateButton data-cy="run-test-button" ghost onClick={() => onRun()} type="primary">
Run Test
</CreateButton>
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/RunDetailLayout/RunDetailLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const RunDetailLayout = ({test: {id, name, trigger, skipTraceCollection}, test}:
const tabBarExtraContent = useMemo(
() => ({
left: <HeaderLeft name={name} triggerType={trigger.type.toUpperCase()} origin={runOriginPath} />,
right: <HeaderRight testId={id} />,
right: <HeaderRight testId={id} triggerType={trigger.type} />,
}),
[id, name, trigger.type, runOriginPath]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export interface IPropsComponent {
const ComponentMap: Record<TriggerTypes, (props: IPropsComponent) => React.ReactElement> = {
[TriggerTypes.http]: RunDetailTriggerResponse,
[TriggerTypes.grpc]: RunDetailTriggerResponse,
[TriggerTypes.traceid]: RunDetailTriggerData,
[TriggerTypes.kafka]: RunDetailTriggerResponse,
[TriggerTypes.traceid]: RunDetailTriggerData,
[TriggerTypes.cypress]: RunDetailTriggerData,
};

interface IProps extends IPropsComponent {
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/TestHeader/TestHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface IProps {
onDelete(): void;
onDuplicate(): void;
title: string;
runButton: React.ReactElement;
runButton: React.ReactNode;
}

const TestHeader = ({description, id, shouldEdit, onEdit, onDelete, onDuplicate, title, runButton}: IProps) => {
Expand Down
3 changes: 2 additions & 1 deletion web/src/components/TestPlugins/EntryPointFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import TriggerHeaderBarKafka from './EntryPoint/Kafka';
const EntryPointFactoryMap = {
[TriggerTypes.http]: TriggerHeaderBarHttp,
[TriggerTypes.grpc]: TriggerHeaderBarGrpc,
[TriggerTypes.traceid]: TriggerHeaderBarTraceID,
[TriggerTypes.kafka]: TriggerHeaderBarKafka,
[TriggerTypes.traceid]: TriggerHeaderBarTraceID,
[TriggerTypes.cypress]: () => null,
};

interface IProps {
Expand Down
3 changes: 2 additions & 1 deletion web/src/components/TestPlugins/FormFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {TDraftTestForm} from 'types/Test.types';
const FormFactoryMap = {
[TriggerTypes.http]: Rest,
[TriggerTypes.grpc]: Grpc,
[TriggerTypes.traceid]: () => null,
[TriggerTypes.kafka]: Kafka,
[TriggerTypes.traceid]: () => null,
[TriggerTypes.cypress]: () => null,
};

export interface IFormProps {
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/TriggerTypeModal/TriggerTypeModal.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {TriggerTypeToPlugin} from 'constants/Plugins.constants';
import {CreateTriggerTypeToPlugin} from 'constants/Plugins.constants';
import {useDashboard} from 'providers/Dashboard/Dashboard.provider';
import CreateTestAnalyticsService from 'services/Analytics/CreateTestAnalytics.service';
import TriggerTypeCard from './TriggerTypeCard';
import * as S from './TriggerTypeModal.styled';

const pluginList = Object.values(TriggerTypeToPlugin);
const pluginList = Object.values(CreateTriggerTypeToPlugin);

interface IProps {
isOpen: boolean;
Expand Down
3 changes: 2 additions & 1 deletion web/src/constants/Common.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export enum RouterSearchFields {
export enum SupportedPlugins {
REST = 'REST',
GRPC = 'GRPC',
TraceID = 'TraceID',
Kafka = 'Kafka',
TraceID = 'TraceID',
Cypress = 'Cypress',
}
1 change: 1 addition & 0 deletions web/src/constants/Demo.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export function getDemoByPluginMap(demos: Demo[]) {
...((otelDemoMap && otelDemoMap[SupportedPlugins.GRPC]) || []),
],
[SupportedPlugins.TraceID]: [],
[SupportedPlugins.Cypress]: [],
[SupportedPlugins.Kafka]: (pokeshopDemoMap && pokeshopDemoMap[SupportedPlugins.Kafka]) || [],
};
}
19 changes: 19 additions & 0 deletions web/src/constants/Plugins.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,35 @@ const TraceID: IPlugin = {
type: TriggerTypes.traceid,
};

const Cypress: IPlugin = {
name: SupportedPlugins.Cypress,
title: 'Cypress',
description: 'Define your test via Cypress',
isActive: true,
demoList: [],
type: TriggerTypes.cypress,
requestType: TriggerTypes.traceid,
};

export const Plugins = {
[SupportedPlugins.REST]: Rest,
[SupportedPlugins.GRPC]: GRPC,
[SupportedPlugins.Kafka]: Kafka,
[SupportedPlugins.TraceID]: TraceID,
[SupportedPlugins.Cypress]: Cypress,
} as const;

export const TriggerTypeToPlugin = {
[TriggerTypes.http]: Plugins.REST,
[TriggerTypes.grpc]: Plugins.GRPC,
[TriggerTypes.kafka]: Plugins.Kafka,
[TriggerTypes.traceid]: Plugins.TraceID,
[TriggerTypes.cypress]: Plugins.Cypress,
} as const;

export const CreateTriggerTypeToPlugin = {
[TriggerTypes.http]: Plugins.REST,
[TriggerTypes.grpc]: Plugins.GRPC,
[TriggerTypes.kafka]: Plugins.Kafka,
[TriggerTypes.traceid]: Plugins.TraceID,
} as const;
1 change: 1 addition & 0 deletions web/src/constants/Test.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum TriggerTypes {
grpc = 'grpc',
traceid = 'traceid',
kafka = 'kafka',
cypress = 'cypress',
}

export enum ImportTypes {
Expand Down
5 changes: 5 additions & 0 deletions web/src/models/Test.model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {TriggerTypes} from 'constants/Test.constants';
import {load} from 'js-yaml';
import {Model, TTestSchemas} from 'types/Common.types';
import TestOutput from './TestOutput.model';
Expand Down Expand Up @@ -54,4 +55,8 @@ Test.FromDefinition = (definition: string): Test => {
return Test(raw);
};

Test.shouldAllowRun = (triggerType: TriggerTypes): boolean => {
return triggerType !== TriggerTypes.cypress;
};

export default Test;
Loading
Loading