|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + commonUtils "github.com/devfile/api/v2/test/v200/utils/common" |
| 13 | + "github.com/santhosh-tekuri/jsonschema" |
| 14 | + "sigs.k8s.io/yaml" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + // numDevfiles : the number of devfiles to create for each test |
| 19 | + numDevfiles = 5 |
| 20 | + |
| 21 | + schemaFileName = "../../../schemas/latest/ide-targeted/devfile.json" |
| 22 | +) |
| 23 | + |
| 24 | +var schemas = make(map[string]SchemaFile) |
| 25 | + |
| 26 | +// SchemaFile - represents the schema stucture |
| 27 | +type SchemaFile struct { |
| 28 | + Schema *jsonschema.Schema |
| 29 | +} |
| 30 | + |
| 31 | +// DevfileValidator struct for DevfileValidator interface defined in common utils. |
| 32 | +type DevfileValidator struct{} |
| 33 | + |
| 34 | +// WriteAndValidate implements Saved.DevfileValidator interface. |
| 35 | +// writes to disk and validates the specified devfile |
| 36 | +func (devfileValidator DevfileValidator) WriteAndValidate(devfile *commonUtils.TestDevfile) error { |
| 37 | + err := writeDevfile(devfile) |
| 38 | + if err != nil { |
| 39 | + commonUtils.LogErrorMessage(fmt.Sprintf("Error writing file : %s : %v", devfile.FileName, err)) |
| 40 | + } else { |
| 41 | + err = validateDevfile(devfile) |
| 42 | + if err != nil { |
| 43 | + commonUtils.LogErrorMessage(fmt.Sprintf("Error vaidating file : %s : %v", devfile.FileName, err)) |
| 44 | + } |
| 45 | + } |
| 46 | + return err |
| 47 | +} |
| 48 | + |
| 49 | +// checkWithSchema checks the validity of a devfile against the schema. |
| 50 | +func (schemaFile *SchemaFile) checkWithSchema(devfile string, expectedMessage string) error { |
| 51 | + |
| 52 | + // Read the created yaml file, ready for converison to json |
| 53 | + devfileData, err := ioutil.ReadFile(devfile) |
| 54 | + if err != nil { |
| 55 | + commonUtils.LogErrorMessage(fmt.Sprintf(" FAIL: schema : unable to read %s: %v", devfile, err)) |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + // Convert the yaml file to json |
| 60 | + devfileDataAsJSON, err := yaml.YAMLToJSON(devfileData) |
| 61 | + if err != nil { |
| 62 | + commonUtils.LogErrorMessage(fmt.Sprintf(" FAIL : %s : schema : failed to convert to json : %v", devfile, err)) |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + validationErr := schemaFile.Schema.Validate(bytes.NewReader(devfileDataAsJSON)) |
| 67 | + if validationErr != nil { |
| 68 | + if len(expectedMessage) > 0 { |
| 69 | + if !strings.Contains(validationErr.Error(), expectedMessage) { |
| 70 | + err = errors.New(commonUtils.LogErrorMessage(fmt.Sprintf(" FAIL : schema : %s : Did not fail as expected : %s got : %v", devfile, expectedMessage, validationErr))) |
| 71 | + } else { |
| 72 | + commonUtils.LogInfoMessage(fmt.Sprintf("PASS: schema : Expected Error received : %s", expectedMessage)) |
| 73 | + } |
| 74 | + } else { |
| 75 | + err = errors.New(commonUtils.LogErrorMessage(fmt.Sprintf(" FAIL : schema : %s : Did not pass as expected, got : %v", devfile, validationErr))) |
| 76 | + } |
| 77 | + } else { |
| 78 | + if len(expectedMessage) > 0 { |
| 79 | + err = errors.New(commonUtils.LogErrorMessage(fmt.Sprintf(" FAIL : schema : %s : was valid - Expected Error not found : %v", devfile, validationErr))) |
| 80 | + } else { |
| 81 | + commonUtils.LogInfoMessage(fmt.Sprintf(" PASS : schema : %s : devfile was valid.", devfile)) |
| 82 | + } |
| 83 | + } |
| 84 | + return err |
| 85 | +} |
| 86 | + |
| 87 | +// getSchema downloads and saves a schema from the provided url |
| 88 | +func getSchema(schemaFileName string) (SchemaFile, error) { |
| 89 | + |
| 90 | + var err error |
| 91 | + schemaFile, found := schemas[schemaFileName] |
| 92 | + if !found { |
| 93 | + |
| 94 | + schemaFile = SchemaFile{} |
| 95 | + |
| 96 | + // Prepare the schema file |
| 97 | + compiler := jsonschema.NewCompiler() |
| 98 | + // Use Draft 7, github.com/santhosh-tekuri/jsonschema provides 4,6 an 7 so use the latest |
| 99 | + compiler.Draft = jsonschema.Draft7 |
| 100 | + schemaFile.Schema, err = compiler.Compile(schemaFileName) |
| 101 | + if err != nil { |
| 102 | + commonUtils.LogErrorMessage(fmt.Sprintf("FAIL : Failed to compile schema %v", err)) |
| 103 | + } else { |
| 104 | + commonUtils.LogInfoMessage(fmt.Sprintf("Schema compiled from file: %s)", schemaFileName)) |
| 105 | + schemas[schemaFileName] = schemaFile |
| 106 | + } |
| 107 | + } |
| 108 | + return schemaFile, err |
| 109 | +} |
| 110 | + |
| 111 | +// writeDevfile creates a devfile on disk for use in a test. |
| 112 | +func writeDevfile(devfile *commonUtils.TestDevfile) error { |
| 113 | + var err error |
| 114 | + |
| 115 | + fileName := devfile.FileName |
| 116 | + if !strings.HasSuffix(fileName, ".yaml") { |
| 117 | + fileName += ".yaml" |
| 118 | + } |
| 119 | + |
| 120 | + commonUtils.LogInfoMessage(fmt.Sprintf("Marshall and write devfile %s", devfile.FileName)) |
| 121 | + |
| 122 | + c, marshallErr := yaml.Marshal(&(devfile.SchemaDevFile)) |
| 123 | + |
| 124 | + if marshallErr != nil { |
| 125 | + err = errors.New(commonUtils.LogErrorMessage(fmt.Sprintf("Marshall devfile %s : %v", devfile.FileName, marshallErr))) |
| 126 | + } else { |
| 127 | + err = ioutil.WriteFile(fileName, c, 0644) |
| 128 | + if err != nil { |
| 129 | + commonUtils.LogErrorMessage(fmt.Sprintf("Write devfile %s : %v", devfile.FileName, err)) |
| 130 | + } |
| 131 | + } |
| 132 | + return err |
| 133 | +} |
| 134 | + |
| 135 | +// validateDevfile check the provided defile against the schema |
| 136 | +func validateDevfile(devfile *commonUtils.TestDevfile) error { |
| 137 | + |
| 138 | + var err error |
| 139 | + var schemaFile SchemaFile |
| 140 | + |
| 141 | + schemaFile, err = getSchema(schemaFileName) |
| 142 | + if err != nil { |
| 143 | + commonUtils.LogErrorMessage(fmt.Sprintf("Failed to get devfile schema : %v", err)) |
| 144 | + } else { |
| 145 | + err = schemaFile.checkWithSchema(devfile.FileName, "") |
| 146 | + if err != nil { |
| 147 | + commonUtils.LogErrorMessage(fmt.Sprintf("Verification with devfile schema failed : %v", err)) |
| 148 | + } else { |
| 149 | + commonUtils.LogInfoMessage(fmt.Sprintf("Devfile validated using JSONSchema schema : %s", devfile.FileName)) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return err |
| 154 | +} |
| 155 | + |
| 156 | +// RunTest : Runs a test to create and verify a devfile based on the content of the specified TestContent |
| 157 | +func RunTest(testContent commonUtils.TestContent, t *testing.T) { |
| 158 | + |
| 159 | + commonUtils.LogMessage(fmt.Sprintf("Start test for %s", testContent.FileName)) |
| 160 | + |
| 161 | + validator := DevfileValidator{} |
| 162 | + |
| 163 | + devfileName := testContent.FileName |
| 164 | + for i := 1; i <= numDevfiles; i++ { |
| 165 | + |
| 166 | + testContent.FileName = commonUtils.AddSuffixToFileName(devfileName, strconv.Itoa(i)) |
| 167 | + |
| 168 | + testDevfile, err := commonUtils.GetDevfile(testContent.FileName, nil, validator) |
| 169 | + if err != nil { |
| 170 | + t.Fatalf(commonUtils.LogMessage(fmt.Sprintf("Error creating devfile : %v", err))) |
| 171 | + } |
| 172 | + |
| 173 | + testDevfile.RunTest(testContent, t) |
| 174 | + } |
| 175 | +} |
0 commit comments