Skip to content

Commit

Permalink
Fix #983 - Add CTK workflow features to CI/CD validation
Browse files Browse the repository at this point in the history
Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com>
  • Loading branch information
ricardozanini committed Sep 17, 2024
1 parent 581ba09 commit 896b7e2
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 11 deletions.
27 changes: 16 additions & 11 deletions .ci/validation/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions .ci/validation/src/ctk.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2023-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { SWSchemaValidator } from "./index";
import fs from "node:fs";
import path from "node:path";

SWSchemaValidator.prepareSchemas();

const ctkDir = path.join(__dirname, "..", "..", "..", "ctk", "features");

function extractYamlBlocks(content: string): string[] {
const yamlBlockRegex = /"""yaml\s([\s\S]*?)\s"""/gm; // Match YAML blocks
let match;
const yamlBlocks: string[] = [];

while ((match = yamlBlockRegex.exec(content)) !== null) {
yamlBlocks.push(match[1]);
}

return yamlBlocks;
}

const workflows = fs.readdirSync(ctkDir)
.filter((file) => file.endsWith(".feature"))
.flatMap((file) => {
const filePath = path.join(ctkDir, file);
const fileContent = fs.readFileSync(filePath, SWSchemaValidator.defaultEncoding);

const yamlBlocks = extractYamlBlocks(fileContent);

return yamlBlocks
.map((yamlText) => SWSchemaValidator.yamlToJSON(yamlText))
.filter((workflow) => typeof workflow === "object")
.filter((workflow) => "document" in workflow)
.filter((workflow) => "dsl" in workflow.document)
.map((workflow) => ({ workflow, file }));
});

describe(`Validate workflows from .feature files`, () => {
test.each(workflows)('$workflow.document.name (from $file)', ({ workflow, file }) => {
const results = SWSchemaValidator.validateSchema(workflow);

if (results?.errors) {
console.warn(
`Schema validation failed for workflow "${workflow.document.name}" in file "${file}" with:`,
JSON.stringify(results.errors, null, 2)
);
}

expect(results?.valid).toBeTruthy();
});
});
1 change: 1 addition & 0 deletions ctk/features/branch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Feature: Composite Task
dsl: '1.0.0'
namespace: default
name: fork
version: '1.0.0'
do:
- branchWithCompete:
fork:
Expand Down
5 changes: 5 additions & 0 deletions ctk/features/call.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Feature: Call Task
dsl: '1.0.0'
namespace: default
name: http-call-with-content-output
version: '1.0.0'
do:
- findPet:
call: http
Expand Down Expand Up @@ -42,6 +43,7 @@ Feature: Call Task
dsl: '1.0.0'
namespace: default
name: http-call-with-response-output
version: '1.0.0'
do:
- getPet:
call: http
Expand Down Expand Up @@ -69,6 +71,7 @@ Feature: Call Task
dsl: '1.0.0'
namespace: default
name: http-call-with-basic-auth
version: '1.0.0'
do:
- login:
call: http
Expand Down Expand Up @@ -98,6 +101,7 @@ Feature: Call Task
dsl: '1.0.0'
namespace: default
name: openapi-call-with-content-output
version: '1.0.0'
do:
- findPet:
call: openapi
Expand Down Expand Up @@ -126,6 +130,7 @@ Feature: Call Task
dsl: '1.0.0'
namespace: default
name: openapi-call-with-response-output
version: '1.0.0'
do:
- getPet:
call: openapi
Expand Down
3 changes: 3 additions & 0 deletions ctk/features/data-flow.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Feature: Data Flow
dsl: '1.0.0'
namespace: default
name: output-filtering
version: '1.0.0'
do:
- setPlayerId:
input:
Expand Down Expand Up @@ -38,6 +39,7 @@ Feature: Data Flow
dsl: '1.0.0'
namespace: default
name: output-filtering
version: '1.0.0'
do:
- getPet:
call: http
Expand Down Expand Up @@ -66,6 +68,7 @@ Feature: Data Flow
dsl: '1.0.0'
namespace: default
name: non-object-output
version: '1.0.0'
do:
- getPetById1:
call: http
Expand Down
1 change: 1 addition & 0 deletions ctk/features/do.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Feature: Composite Task
dsl: '1.0.0'
namespace: default
name: do
version: '1.0.0'
do:
- compositeExample:
do:
Expand Down
1 change: 1 addition & 0 deletions ctk/features/emit.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Feature: Emit Task
dsl: '1.0.0'
namespace: default
name: emit
version: '1.0.0'
do:
- emitEvent:
emit:
Expand Down
2 changes: 2 additions & 0 deletions ctk/features/flow.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Feature: Flow Directive
dsl: '1.0.0'
namespace: default
name: implicit-sequence
version: '1.0.0'
do:
- setRed:
set:
Expand Down Expand Up @@ -37,6 +38,7 @@ Feature: Flow Directive
dsl: '1.0.0'
namespace: default
name: explicit-sequence
version: '1.0.0'
do:
- setRed:
set:
Expand Down
1 change: 1 addition & 0 deletions ctk/features/for.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Feature: For Task
dsl: '1.0.0'
namespace: default
name: for
version: '1.0.0'
do:
- loopColors:
for:
Expand Down
1 change: 1 addition & 0 deletions ctk/features/raise.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Feature: Raise Task
dsl: '1.0.0'
namespace: default
name: raise-custom-error
version: '1.0.0'
do:
- raiseError:
raise:
Expand Down
1 change: 1 addition & 0 deletions ctk/features/set.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Feature: Set Task
dsl: '1.0.0'
namespace: default
name: set
version: '1.0.0'
do:
- setShape:
set:
Expand Down
3 changes: 3 additions & 0 deletions ctk/features/switch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Feature: Switch Task
dsl: '1.0.0'
namespace: default
name: switch-match
version: '1.0.0'
do:
- switchColor:
switch:
Expand Down Expand Up @@ -54,6 +55,7 @@ Feature: Switch Task
dsl: '1.0.0'
namespace: default
name: switch-default-implicit
version: '1.0.0'
do:
- switchColor:
switch:
Expand Down Expand Up @@ -96,6 +98,7 @@ Feature: Switch Task
dsl: '1.0.0'
namespace: default
name: switch-default-implicit
version: '1.0.0'
do:
- switchColor:
switch:
Expand Down
2 changes: 2 additions & 0 deletions ctk/features/try.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Feature: Try Task
dsl: '1.0.0'
namespace: default
name: try-catch-404
version: '1.0.0'
do:
- tryGetPet:
try:
Expand Down Expand Up @@ -57,6 +58,7 @@ Feature: Try Task
dsl: '1.0.0'
namespace: default
name: try-catch-503
version: '1.0.0'
do:
- tryGetPet:
try:
Expand Down

0 comments on commit 896b7e2

Please sign in to comment.