Skip to content

Commit def6889

Browse files
committed
refactor: update test cases to use YAML content instead of filenames for better clarity
1 parent 2ccabd8 commit def6889

File tree

1 file changed

+100
-18
lines changed

1 file changed

+100
-18
lines changed

internal/orchestrator/app/validator_test.go

Lines changed: 100 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package app
22

33
import (
44
"errors"
5+
"os"
6+
"path/filepath"
7+
"strings"
58
"testing"
69

710
"github.com/stretchr/testify/assert"
@@ -19,59 +22,120 @@ func TestValidateAppDescriptorBricks(t *testing.T) {
1922

2023
testCases := []struct {
2124
name string
22-
filename string
25+
yamlContent string
2326
expectedError error
2427
}{
2528
{
26-
name: "valid with all required filled",
27-
filename: "all-required-app.yaml",
29+
name: "valid with all required filled",
30+
yamlContent: `
31+
name: App ok
32+
description: App ok
33+
bricks:
34+
- arduino:arduino_cloud:
35+
variables:
36+
ARDUINO_DEVICE_ID: "my-device-id"
37+
ARDUINO_SECRET: "my-secret"
38+
`,
2839
expectedError: nil,
2940
},
3041
{
31-
name: "valid with missing bricks",
32-
filename: "no-bricks-app.yaml",
42+
name: "valid with missing bricks",
43+
yamlContent: `
44+
name: App with no bricks
45+
description: App with no bricks description
46+
`,
3347
expectedError: nil,
3448
},
3549
{
36-
name: "valid with empty list of bricks",
37-
filename: "empty-bricks-app.yaml",
50+
name: "valid with empty list of bricks",
51+
yamlContent: `
52+
name: App with empty bricks
53+
description: App with empty bricks
54+
55+
bricks: []
56+
`,
3857
expectedError: nil,
3958
},
4059
{
41-
name: "valid if required variable is empty string",
42-
filename: "empty-required-app.yaml",
60+
name: "valid if required variable is empty string",
61+
yamlContent: `
62+
name: App with an empty variable
63+
description: App with an empty variable
64+
bricks:
65+
- arduino:arduino_cloud:
66+
variables:
67+
ARDUINO_DEVICE_ID: "my-device-id"
68+
ARDUINO_SECRET:
69+
`,
4370
expectedError: nil,
4471
},
4572
{
46-
name: "invalid if required variable is omitted",
47-
filename: "omitted-required-app.yaml",
73+
name: "invalid if required variable is omitted",
74+
yamlContent: `
75+
name: App with no required variables
76+
description: App with no required variables
77+
bricks:
78+
- arduino:arduino_cloud
79+
`,
4880
expectedError: errors.Join(
4981
errors.New("variable \"ARDUINO_DEVICE_ID\" is required by brick \"arduino:arduino_cloud\""),
5082
errors.New("variable \"ARDUINO_SECRET\" is required by brick \"arduino:arduino_cloud\""),
5183
),
5284
},
5385
{
54-
name: "invalid if a required variable among two is omitted",
55-
filename: "omitted-mixed-required-app.yaml",
86+
name: "invalid if a required variable among two is omitted",
87+
yamlContent: `
88+
name: App only one required variable filled
89+
description: App only one required variable filled
90+
bricks:
91+
- arduino:arduino_cloud:
92+
variables:
93+
ARDUINO_DEVICE_ID: "my-device-id"
94+
`,
5695
expectedError: errors.New("variable \"ARDUINO_SECRET\" is required by brick \"arduino:arduino_cloud\""),
5796
},
5897
{
59-
name: "invalid if brick id not found",
60-
filename: "not-found-brick-app.yaml",
98+
name: "invalid if brick id not found",
99+
yamlContent: `
100+
name: App no existing brick
101+
description: App no existing brick
102+
bricks:
103+
- arduino:not_existing_brick:
104+
variables:
105+
ARDUINO_DEVICE_ID: "my-device-id"
106+
ARDUINO_SECRET: "LAKDJ"
107+
`,
61108
expectedError: errors.New("brick \"arduino:not_existing_brick\" not found"),
62109
},
63110
{
64-
name: "log a warning if variable does not exist in the brick",
65-
filename: "not-found-variable-app.yaml",
111+
name: "log a warning if variable does not exist in the brick",
112+
yamlContent: `
113+
name: App with non existing variable
114+
description: App with non existing variable
115+
bricks:
116+
- arduino:arduino_cloud:
117+
variables:
118+
NOT_EXISTING_VARIABLE: "this-is-a-not-existing-variable-for-the-brick"
119+
ARDUINO_DEVICE_ID: "my-device-id"
120+
ARDUINO_SECRET: "my-secret"
121+
`,
66122
expectedError: nil,
67123
},
68124
}
69125

70126
for _, tc := range testCases {
71127
t.Run(tc.name, func(t *testing.T) {
72-
appDescriptor, err := ParseDescriptorFile(paths.New("testdata/validator/" + tc.filename))
128+
tempDir := t.TempDir()
129+
err := paths.New(tempDir).MkdirAll()
130+
require.NoError(t, err)
131+
appYaml := paths.New(tempDir, "app.yaml")
132+
err = os.WriteFile(appYaml.String(), []byte(tc.yamlContent), 0600)
133+
require.NoError(t, err)
134+
135+
appDescriptor, err := ParseDescriptorFile(appYaml)
73136
require.NoError(t, err)
74137

138+
// Validate the bricks
75139
err = ValidateBricks(appDescriptor, bricksIndex)
76140
if tc.expectedError == nil {
77141
assert.NoError(t, err, "Expected no validation errors")
@@ -82,3 +146,21 @@ func TestValidateAppDescriptorBricks(t *testing.T) {
82146
})
83147
}
84148
}
149+
150+
func writeYAMLToTempFile(t *testing.T, yamlContent string) *paths.Path {
151+
// Create a temporary directory
152+
tempDir, err := os.MkdirTemp("", "validator_test_*")
153+
require.NoError(t, err)
154+
155+
// Clean up the temporary directory after the test
156+
t.Cleanup(func() {
157+
os.RemoveAll(tempDir)
158+
})
159+
160+
// Write YAML content to a temporary file
161+
yamlFile := filepath.Join(tempDir, "app.yaml")
162+
err = os.WriteFile(yamlFile, []byte(strings.TrimSpace(yamlContent)), 0644)
163+
require.NoError(t, err)
164+
165+
return paths.New(yamlFile)
166+
}

0 commit comments

Comments
 (0)