MCPcopy Index your code
hub / github.com/docker/docker-agent / TestJsonSchemaWorksForExamples

Function TestJsonSchemaWorksForExamples

pkg/config/schema_test.go:24–57  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

22const schemaFile = "../../agent-schema.json"
23
24func TestJsonSchemaWorksForExamples(t *testing.T) {
25 t.Parallel()
26
27 schemaFile, err := os.ReadFile(schemaFile)
28 require.NoError(t, err)
29
30 schema, err := gojsonschema.NewSchema(gojsonschema.NewBytesLoader(schemaFile))
31 require.NoError(t, err)
32
33 for _, file := range collectExamples(t) {
34 t.Run(file, func(t *testing.T) {
35 t.Parallel()
36
37 buf, err := os.ReadFile(file)
38 require.NoError(t, err)
39
40 // HCL examples are converted to their YAML equivalent before
41 // being validated against the JSON schema, since the schema
42 // describes the YAML/JSON representation only.
43 if filepath.Ext(file) == ".hcl" {
44 buf, err = hclconv.ToYAML(buf, file)
45 require.NoError(t, err)
46 }
47
48 var rawJSON any
49 err = yaml.Unmarshal(buf, &rawJSON)
50 require.NoError(t, err)
51
52 result, err := schema.Validate(gojsonschema.NewRawLoader(rawJSON))
53 require.NoError(t, err)
54 assert.True(t, result.Valid(), "Example %s does not match schema: %v", file, result.Errors())
55 })
56 }
57}
58
59// TestSchemaMatchesGoTypes verifies that every JSON-tagged field in the Go
60// config structs has a corresponding property in agent-schema.json (and

Callers

nothing calls this directly

Calls 4

ReadFileMethod · 0.80
collectExamplesFunction · 0.70
RunMethod · 0.65
ValidateMethod · 0.45

Tested by

no test coverage detected