| 54 | const tempDir = "./tmp/" |
| 55 | |
| 56 | func Test_Schema(t *testing.T) { |
| 57 | |
| 58 | // Clear the temp directory if it exists |
| 59 | if _, err := os.Stat(tempDir); !os.IsNotExist(err) { |
| 60 | os.RemoveAll(tempDir) |
| 61 | } |
| 62 | os.Mkdir(tempDir, 0755) |
| 63 | |
| 64 | // Read the content of the jso directory to find test files |
| 65 | files, err := ioutil.ReadDir(jsonDir) |
| 66 | if err != nil { |
| 67 | t.Fatalf("Error finding test json files in : %s : %v", jsonDir, err) |
| 68 | } |
| 69 | |
| 70 | combinedTests := 0 |
| 71 | combinedPasses := 0 |
| 72 | for _, testJsonFile := range files { |
| 73 | |
| 74 | // if the file begins with test- and ends .json it can be processed |
| 75 | if strings.HasPrefix(testJsonFile.Name(), "test-") && strings.HasSuffix(testJsonFile.Name(), ".json") { |
| 76 | |
| 77 | // Open the json file which defines the tests to run |
| 78 | testJson, err := os.Open(filepath.Join(jsonDir, testJsonFile.Name())) |
| 79 | if err != nil { |
| 80 | t.Errorf(" FAIL : Failed to open %s : %s", testJsonFile.Name(), err) |
| 81 | continue |
| 82 | } |
| 83 | |
| 84 | // Read contents of the json file which defines the tests to run |
| 85 | byteValue, err := ioutil.ReadAll(testJson) |
| 86 | if err != nil { |
| 87 | t.Errorf("FAIL : failed to read : %s : %v", testJsonFile.Name(), err) |
| 88 | } |
| 89 | |
| 90 | var testJsonContent TestJson |
| 91 | |
| 92 | // Unmarshall the contents of the json file which defines the tests to run for each test |
| 93 | err = json.Unmarshal(byteValue, &testJsonContent) |
| 94 | if err != nil { |
| 95 | t.Errorf("FAIL : failed to unmarshal : %s : %v", testJsonFile.Name(), err) |
| 96 | continue |
| 97 | } |
| 98 | |
| 99 | testJson.Close() |
| 100 | |
| 101 | t.Logf("INFO : File %s : SchemaFile : %s , SchemaVersion : %s", testJsonFile.Name(), testJsonContent.SchemaFile, testJsonContent.SchemaVersion) |
| 102 | |
| 103 | // Prepare the schema file |
| 104 | compiler := jsonschema.NewCompiler() |
| 105 | compiler.Draft = jsonschema.Draft7 |
| 106 | schema, err := compiler.Compile(filepath.Join(schemaDir, testJsonContent.SchemaFile)) |
| 107 | if err != nil { |
| 108 | t.Fatalf(" FAIL : Schema compile failed : %s: %v", testJsonContent.SchemaFile, err) |
| 109 | } |
| 110 | |
| 111 | // create the tempdirectoy to hold the generated yaml files |
| 112 | testTempDir := tempDir |
| 113 | testTempDir += strings.Split(testJsonFile.Name(), ".")[0] |