|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// TestReadData_WorkflowStructure tests readData with realistic workflow structure |
| 12 | +func TestReadData_WorkflowStructure(t *testing.T) { |
| 13 | + yamlInput := `version: "1.0" |
| 14 | +workflows: |
| 15 | + deploy-workflow: |
| 16 | + name: "Deploy Application" |
| 17 | + description: "Workflow that triggers on push events" |
| 18 | + enabled: true` |
| 19 | + |
| 20 | + var definition WorkflowDefinitions |
| 21 | + result, err := readData([]byte(yamlInput), definition) |
| 22 | + require.NoError(t, err) |
| 23 | + require.NotNil(t, result) |
| 24 | + |
| 25 | + // Verify the result has the expected structure |
| 26 | + assert.Equal(t, "1.0", result.Version) |
| 27 | + assert.NotNil(t, result.Workflows) |
| 28 | + |
| 29 | + workflow, exists := result.Workflows["deploy-workflow"] |
| 30 | + assert.True(t, exists, "workflow should exist") |
| 31 | + assert.Equal(t, "Deploy Application", workflow.Name) |
| 32 | + assert.Equal(t, "Workflow that triggers on push events", workflow.Description) |
| 33 | + assert.True(t, workflow.Enabled) |
| 34 | +} |
| 35 | + |
| 36 | +// TestReadData_YAMLWithProblematicKeysDirectValidation tests the core YAML parsing fix |
| 37 | +func TestReadData_YAMLWithProblematicKeysDirectValidation(t *testing.T) { |
| 38 | + // Test serverless workflow YAML that uses 'on' in schedule - this would be broken with sigs.k8s.io/yaml |
| 39 | + yamlInput := `version: "1.0" |
| 40 | +workflows: |
| 41 | + scheduled-workflow: |
| 42 | + name: "Scheduled Workflow" |
| 43 | + description: "Workflow with schedule trigger using on event consumption" |
| 44 | + enabled: true |
| 45 | + workflow: |
| 46 | + document: |
| 47 | + dsl: "1.0.1" |
| 48 | + namespace: "test" |
| 49 | + name: "scheduled-workflow" |
| 50 | + version: "1.0.0" |
| 51 | + schedule: |
| 52 | + on: |
| 53 | + any: |
| 54 | + - with: |
| 55 | + type: com.example.cron.trigger |
| 56 | + data: |
| 57 | + schedule: "0 0 * * *" |
| 58 | + - with: |
| 59 | + type: com.example.event.trigger |
| 60 | + data: |
| 61 | + enabled: true |
| 62 | + cron: "0 0 * * *" |
| 63 | + do: |
| 64 | + - log: |
| 65 | + call: http |
| 66 | + with: |
| 67 | + method: POST |
| 68 | + url: "https://api.example.com/log" |
| 69 | + body: |
| 70 | + message: "Scheduled workflow executed"` |
| 71 | + |
| 72 | + var definition WorkflowDefinitions |
| 73 | + result, err := readData([]byte(yamlInput), definition) |
| 74 | + require.NoError(t, err) |
| 75 | + require.NotNil(t, result) |
| 76 | + |
| 77 | + // The key test: marshal back to JSON and verify 'on' is preserved as a key |
| 78 | + jsonBytes, err := json.Marshal(result) |
| 79 | + require.NoError(t, err) |
| 80 | + |
| 81 | + jsonStr := string(jsonBytes) |
| 82 | + t.Logf("Generated JSON: %s", jsonStr) |
| 83 | + |
| 84 | + // Verify that 'on' appears as a key in the JSON, not as boolean conversion |
| 85 | + assert.Contains(t, jsonStr, `"on":`, "JSON should contain 'on' as a key in schedule section") |
| 86 | + |
| 87 | + // Verify that boolean conversions ('true'/'false' as keys) don't appear from 'on' conversion |
| 88 | + // With the old sigs.k8s.io/yaml library, 'on:' would become '"true":' in JSON |
| 89 | + assert.NotContains(t, jsonStr, `"true":{"any"`, "JSON should not contain 'true' as a key from 'on' conversion") |
| 90 | + assert.NotContains(t, jsonStr, `"false":{"any"`, "JSON should not contain 'false' as a key from 'on' conversion") |
| 91 | +} |
| 92 | + |
| 93 | +func TestReadData_JSONInput(t *testing.T) { |
| 94 | + jsonInput := `{ |
| 95 | + "version": "1.0", |
| 96 | + "workflows": { |
| 97 | + "test": { |
| 98 | + "name": "Test", |
| 99 | + "description": "Test workflow", |
| 100 | + "enabled": true |
| 101 | + } |
| 102 | + } |
| 103 | + }` |
| 104 | + |
| 105 | + var definition WorkflowDefinitions |
| 106 | + result, err := readData([]byte(jsonInput), definition) |
| 107 | + require.NoError(t, err) |
| 108 | + require.NotNil(t, result) |
| 109 | + |
| 110 | + assert.Equal(t, "1.0", result.Version) |
| 111 | + assert.NotNil(t, result.Workflows) |
| 112 | + |
| 113 | + workflow, exists := result.Workflows["test"] |
| 114 | + assert.True(t, exists) |
| 115 | + assert.Equal(t, "Test", workflow.Name) |
| 116 | +} |
| 117 | + |
| 118 | +func TestReadData_InvalidYAML(t *testing.T) { |
| 119 | + invalidYAML := `on: |
| 120 | + test: true |
| 121 | + invalid: indentation` |
| 122 | + |
| 123 | + var definition WorkflowDefinitions |
| 124 | + result, err := readData([]byte(invalidYAML), definition) |
| 125 | + assert.Error(t, err, "should return error for invalid YAML") |
| 126 | + assert.Nil(t, result, "result should be nil for invalid YAML") |
| 127 | +} |
| 128 | + |
| 129 | +func TestReadData_EmptyInput(t *testing.T) { |
| 130 | + var definition WorkflowDefinitions |
| 131 | + result, err := readData([]byte(""), definition) |
| 132 | + assert.Error(t, err, "should return error for empty input") |
| 133 | + assert.Nil(t, result, "result should be nil for empty input") |
| 134 | +} |
| 135 | + |
| 136 | +// Benchmark to ensure the YAML parsing performance is reasonable |
| 137 | +func BenchmarkReadData_YAML(b *testing.B) { |
| 138 | + yamlInput := `version: "1.0" |
| 139 | +workflows: |
| 140 | + test: |
| 141 | + name: "Test" |
| 142 | + description: "Benchmark test" |
| 143 | + enabled: true` |
| 144 | + |
| 145 | + var definition WorkflowDefinitions |
| 146 | + |
| 147 | + b.ResetTimer() |
| 148 | + for i := 0; i < b.N; i++ { |
| 149 | + _, err := readData([]byte(yamlInput), definition) |
| 150 | + if err != nil { |
| 151 | + b.Fatal(err) |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments