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