forked from devfile/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_test_utils.go
192 lines (174 loc) · 8.45 KB
/
project_test_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package utils
import (
"errors"
"fmt"
"io/ioutil"
"github.com/google/go-cmp/cmp"
"sigs.k8s.io/yaml"
schema "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
commonUtils "github.com/devfile/api/v2/test/v200/utils/common"
)
// getSchemaProject gets a named Project from the saved devfile schema structure
func getSchemaProject(projects []schema.Project, name string) (*schema.Project, bool) {
found := false
var schemaProject schema.Project
for _, project := range projects {
if project.Name == name {
schemaProject = project
found = true
break
}
}
return &schemaProject, found
}
// getSchemaStarterProject gets a named Starter Project from the saved devfile schema structure
func getSchemaStarterProject(starterProjects []schema.StarterProject, name string) (*schema.StarterProject, bool) {
found := false
var schemaStarterProject schema.StarterProject
for _, starterProject := range starterProjects {
if starterProject.Name == name {
schemaStarterProject = starterProject
found = true
break
}
}
return &schemaStarterProject, found
}
// UpdateProject randomly modifies an existing project
func UpdateProject(devfile *commonUtils.TestDevfile, projectName string) error {
var err error
testProject, found := getSchemaProject(devfile.SchemaDevFile.Projects, projectName)
if found {
commonUtils.LogInfoMessage(fmt.Sprintf("Updating Project : %s", projectName))
devfile.SetProjectValues(testProject)
} else {
err = errors.New(commonUtils.LogErrorMessage(fmt.Sprintf("Project not found in test : %s", projectName)))
}
return err
}
// UpdateStarterProject randomly modifies an existing starter project
func UpdateStarterProject(devfile *commonUtils.TestDevfile, projectName string) error {
var err error
testStarterProject, found := getSchemaStarterProject(devfile.SchemaDevFile.StarterProjects, projectName)
if found {
commonUtils.LogInfoMessage(fmt.Sprintf("Updating Starter Project : %s", projectName))
devfile.SetStarterProjectValues(testStarterProject)
} else {
err = errors.New(commonUtils.LogErrorMessage(fmt.Sprintf("Starter Project not found in test : %s", projectName)))
}
return err
}
// VerifyProjects verifies projects returned by the parser are the same as those saved in the devfile schema
func VerifyProjects(devfile *commonUtils.TestDevfile, parserProjects []schema.Project) error {
commonUtils.LogInfoMessage("Enter VerifyProjects")
var errorString []string
// Compare entire array of projects
if !cmp.Equal(parserProjects, devfile.SchemaDevFile.Projects) {
// Compare failed so compare each project to find which one(s) don't compare
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf("Project array compare failed.")))
for _, project := range parserProjects {
if testProject, found := getSchemaProject(devfile.SchemaDevFile.Projects, project.Name); found {
if !cmp.Equal(project, *testProject) {
// Write out the failing project to a file, once as expected by the test, and a second as returned by the parser
parserFilename := commonUtils.AddSuffixToFileName(devfile.FileName, "_"+project.Name+"_Parser")
testFilename := commonUtils.AddSuffixToFileName(devfile.FileName, "_"+project.Name+"_Test")
commonUtils.LogInfoMessage(fmt.Sprintf(".......marshall and write devfile %s", parserFilename))
c, err := yaml.Marshal(project)
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", parserFilename)))
} else {
err = ioutil.WriteFile(parserFilename, c, 0644)
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", parserFilename)))
}
}
commonUtils.LogInfoMessage(fmt.Sprintf(".......marshall and write devfile %s", testFilename))
c, err = yaml.Marshal(testProject)
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", testFilename)))
} else {
err = ioutil.WriteFile(testFilename, c, 0644)
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", testFilename)))
}
}
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf("Project %s did not match, see files : %s and %s", project.Name, parserFilename, testFilename)))
} else {
commonUtils.LogInfoMessage(fmt.Sprintf(" --> Project matched : %s", project.Name))
}
} else {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf("Project from parser not known to test - id : %s ", project.Name)))
}
}
// Check test does not include projects which the parser did not return
for _, project := range devfile.SchemaDevFile.Projects {
if _, found := getSchemaProject(parserProjects, project.Name); !found {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf("Project from test not returned by parser : %s ", project.Name)))
}
}
} else {
commonUtils.LogInfoMessage(fmt.Sprintf("Project structures matched"))
}
var err error
if len(errorString) > 0 {
err = errors.New(fmt.Sprint(errorString))
}
return err
}
// VerifyStarterProjects verifies starter projects returned by the parser are the same as those saved in the devfile schema
func VerifyStarterProjects(devfile *commonUtils.TestDevfile, parserStarterProjects []schema.StarterProject) error {
commonUtils.LogInfoMessage("Enter VerifyStarterProjects")
var errorString []string
// Compare entire array of projects
if !cmp.Equal(parserStarterProjects, devfile.SchemaDevFile.StarterProjects) {
// Compare failed so compare each project to find which one(s) don't compare
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf("Starter Project array compare failed.")))
for _, starterProject := range parserStarterProjects {
if testStarterProject, found := getSchemaStarterProject(devfile.SchemaDevFile.StarterProjects, starterProject.Name); found {
if !cmp.Equal(starterProject, *testStarterProject) {
// Write out the failing starter project to a file, once as expected by the test, and a second as returned by the parser
parserFilename := commonUtils.AddSuffixToFileName(devfile.FileName, "_"+starterProject.Name+"_Parser")
testFilename := commonUtils.AddSuffixToFileName(devfile.FileName, "_"+starterProject.Name+"_Test")
commonUtils.LogInfoMessage(fmt.Sprintf(".......marshall and write devfile %s", parserFilename))
c, err := yaml.Marshal(starterProject)
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", parserFilename)))
} else {
err = ioutil.WriteFile(parserFilename, c, 0644)
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", parserFilename)))
}
}
commonUtils.LogInfoMessage(fmt.Sprintf(".......marshall and write devfile %s", testFilename))
c, err = yaml.Marshal(testStarterProject)
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", testFilename)))
} else {
err = ioutil.WriteFile(testFilename, c, 0644)
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", testFilename)))
}
}
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf("Starter Project %s did not match, see files : %s and %s", starterProject.Name, parserFilename, testFilename)))
} else {
commonUtils.LogInfoMessage(fmt.Sprintf(" --> Starter Project matched : %s", starterProject.Name))
}
} else {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf("Starter Project from parser not known to test - id : %s ", starterProject.Name)))
}
}
// Check test does not include projects which the parser did not return
for _, starterProject := range devfile.SchemaDevFile.StarterProjects {
if _, found := getSchemaStarterProject(parserStarterProjects, starterProject.Name); !found {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf("Starter Project from test not returned by parser : %s ", starterProject.Name)))
}
}
} else {
commonUtils.LogInfoMessage(fmt.Sprintf("Starter Project structures matched"))
}
var err error
if len(errorString) > 0 {
err = errors.New(fmt.Sprint(errorString))
}
return err
}