Skip to content

Commit bfc3f50

Browse files
authored
add tests for projects and starter projects (#360)
* add tests for projects and starter projects * Limit starter projects to 1 remote * Remove GitHub Project Type from tests * Fix createStarterProject function name
1 parent 81859ea commit bfc3f50

File tree

3 files changed

+266
-6
lines changed

3 files changed

+266
-6
lines changed

test/v200/apiTest/api_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ func Test_MultiComponent(t *testing.T) {
6666
apiUtils.RunTest(testContent, t)
6767
}
6868

69+
func Test_Projects(t *testing.T) {
70+
testContent := commonUtils.TestContent{}
71+
testContent.ProjectTypes = []schema.ProjectSourceType{
72+
schema.GitProjectSourceType,
73+
schema.ZipProjectSourceType}
74+
testContent.FileName = commonUtils.GetDevFileName()
75+
apiUtils.RunTest(testContent, t)
76+
}
77+
78+
func Test_StarterProjects(t *testing.T) {
79+
testContent := commonUtils.TestContent{}
80+
testContent.StarterProjectTypes = []schema.ProjectSourceType{
81+
schema.GitProjectSourceType,
82+
schema.ZipProjectSourceType}
83+
testContent.FileName = commonUtils.GetDevFileName()
84+
apiUtils.RunTest(testContent, t)
85+
}
86+
6987
func Test_Everything(t *testing.T) {
7088
testContent := commonUtils.TestContent{}
7189
testContent.CommandTypes = []schema.CommandType{
@@ -75,6 +93,12 @@ func Test_Everything(t *testing.T) {
7593
testContent.ComponentTypes = []schema.ComponentType{
7694
schema.ContainerComponentType,
7795
schema.VolumeComponentType}
96+
testContent.ProjectTypes = []schema.ProjectSourceType{
97+
schema.GitProjectSourceType,
98+
schema.ZipProjectSourceType}
99+
testContent.StarterProjectTypes = []schema.ProjectSourceType{
100+
schema.GitProjectSourceType,
101+
schema.ZipProjectSourceType}
78102
testContent.FileName = commonUtils.GetDevFileName()
79103
apiUtils.RunTest(testContent, t)
80104
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
package common
2+
3+
import (
4+
"fmt"
5+
schema "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
6+
)
7+
8+
// projectAdded adds a new project to the test schema data and notifies the follower
9+
func (testDevfile *TestDevfile) projectAdded(project schema.Project) {
10+
LogInfoMessage(fmt.Sprintf("project added Name: %s", project.Name))
11+
testDevfile.SchemaDevFile.Projects = append(testDevfile.SchemaDevFile.Projects, project)
12+
if testDevfile.Follower != nil {
13+
testDevfile.Follower.AddProject(project)
14+
}
15+
}
16+
17+
// projectUpdated notifies the follower of the project which has been updated
18+
func (testDevfile *TestDevfile) projectUpdated(project schema.Project) {
19+
LogInfoMessage(fmt.Sprintf("project updated Name: %s", project.Name))
20+
testDevfile.replaceSchemaProject(project)
21+
if testDevfile.Follower != nil {
22+
testDevfile.Follower.UpdateProject(project)
23+
}
24+
}
25+
26+
// starterProjectAdded adds a new starter project to the test schema data and notifies the follower
27+
func (testDevfile *TestDevfile) starterProjectAdded(starterProject schema.StarterProject) {
28+
LogInfoMessage(fmt.Sprintf("starter project added Name: %s", starterProject.Name))
29+
testDevfile.SchemaDevFile.StarterProjects = append(testDevfile.SchemaDevFile.StarterProjects, starterProject)
30+
if testDevfile.Follower != nil {
31+
testDevfile.Follower.AddStarterProject(starterProject)
32+
}
33+
}
34+
35+
// starterProjectUpdated notifies the follower of the starter project which has been updated
36+
func (testDevfile *TestDevfile) starterProjectUpdated(starterProject schema.StarterProject) {
37+
LogInfoMessage(fmt.Sprintf("starter project updated Name: %s", starterProject.Name))
38+
testDevfile.replaceSchemaStarterProject(starterProject)
39+
if testDevfile.Follower != nil {
40+
testDevfile.Follower.UpdateStarterProject(starterProject)
41+
}
42+
}
43+
44+
// replaceSchemaProject replaces a Project in the saved devfile schema structure
45+
func (testDevfile *TestDevfile) replaceSchemaProject(project schema.Project) {
46+
for i := 0; i < len(testDevfile.SchemaDevFile.Projects); i++ {
47+
if testDevfile.SchemaDevFile.Projects[i].Name == project.Name {
48+
testDevfile.SchemaDevFile.Projects[i] = project
49+
break
50+
}
51+
}
52+
}
53+
54+
// replaceSchemaStarterProject replaces a Starter Project in the saved devfile schema structure
55+
func (testDevfile *TestDevfile) replaceSchemaStarterProject(starterProject schema.StarterProject) {
56+
for i := 0; i < len(testDevfile.SchemaDevFile.StarterProjects); i++ {
57+
if testDevfile.SchemaDevFile.StarterProjects[i].Name == starterProject.Name {
58+
testDevfile.SchemaDevFile.StarterProjects[i] = starterProject
59+
break
60+
}
61+
}
62+
}
63+
64+
// getRemotes creates and returns a map of remotes
65+
func getRemotes(numRemotes int) map[string]string {
66+
remotes := make(map[string]string)
67+
for i := 0; i < numRemotes; i++ {
68+
key := GetRandomUniqueString(GetRandomNumber(6, 12), false)
69+
remotes[key] = GetRandomUniqueString(GetRandomNumber(6, 12), false)
70+
LogInfoMessage(fmt.Sprintf("Set remote key= %s, value= %s", key, remotes[key]))
71+
}
72+
return remotes
73+
}
74+
75+
// AddProject adds a project of the specified type, with random attributes, to the devfile schema
76+
func (testDevfile *TestDevfile) AddProject(projectType schema.ProjectSourceType) string {
77+
project := testDevfile.createProject(projectType)
78+
testDevfile.SetProjectValues(&project)
79+
return project.Name
80+
}
81+
82+
// AddStarterProject adds a starter project of the specified type, with random attributes, to the devfile schema
83+
func (testDevfile *TestDevfile) AddStarterProject(projectType schema.ProjectSourceType) string {
84+
starterProject := testDevfile.createStarterProject(projectType)
85+
testDevfile.SetStarterProjectValues(&starterProject)
86+
return starterProject.Name
87+
}
88+
89+
// createProject creates a project of a specified type with only required attributes set
90+
func (testDevfile *TestDevfile) createProject(projectType schema.ProjectSourceType) schema.Project {
91+
project := schema.Project{}
92+
project.Name = GetRandomUniqueString(GetRandomNumber(8, 63), true)
93+
LogInfoMessage(fmt.Sprintf("Create Project Name: %s", project.Name))
94+
95+
if projectType == schema.GitProjectSourceType {
96+
project.Git = createGitProject(GetRandomNumber(1, 5))
97+
} else if projectType == schema.ZipProjectSourceType {
98+
project.Zip = createZipProject()
99+
}
100+
testDevfile.projectAdded(project)
101+
return project
102+
}
103+
104+
// createStarterProject creates a starter project of a specified type with only required attributes set
105+
func (testDevfile *TestDevfile) createStarterProject(projectType schema.ProjectSourceType) schema.StarterProject {
106+
starterProject := schema.StarterProject{}
107+
starterProject.Name = GetRandomUniqueString(GetRandomNumber(8, 63), true)
108+
LogInfoMessage(fmt.Sprintf("Create StarterProject Name: %s", starterProject.Name))
109+
110+
if projectType == schema.GitProjectSourceType {
111+
starterProject.Git = createGitProject(1)
112+
} else if projectType == schema.ZipProjectSourceType {
113+
starterProject.Zip = createZipProject()
114+
}
115+
testDevfile.starterProjectAdded(starterProject)
116+
return starterProject
117+
118+
}
119+
120+
// createGitProject creates a git project structure with mandatory attributes set
121+
func createGitProject(numRemotes int) *schema.GitProjectSource {
122+
project := schema.GitProjectSource{}
123+
project.Remotes = getRemotes(numRemotes)
124+
return &project
125+
}
126+
127+
// createZipProject creates a zip project structure
128+
func createZipProject() *schema.ZipProjectSource {
129+
project := schema.ZipProjectSource{}
130+
return &project
131+
}
132+
133+
// SetProjectValues sets project attributes, common to all projects, to random values.
134+
func (testDevfile *TestDevfile) SetProjectValues(project *schema.Project) {
135+
136+
if GetBinaryDecision() {
137+
project.ClonePath = "./" + GetRandomString(GetRandomNumber(4, 12), false)
138+
LogInfoMessage(fmt.Sprintf("Set ClonePath : %s", project.ClonePath))
139+
}
140+
141+
if GetBinaryDecision() {
142+
var sparseCheckoutDirs []string
143+
numDirs := GetRandomNumber(1, 6)
144+
for i := 0; i < numDirs; i++ {
145+
sparseCheckoutDirs = append(sparseCheckoutDirs, GetRandomString(8, false))
146+
LogInfoMessage(fmt.Sprintf("Set sparseCheckoutDir : %s", sparseCheckoutDirs[i]))
147+
}
148+
project.SparseCheckoutDirs = sparseCheckoutDirs
149+
}
150+
151+
if project.Git != nil {
152+
setGitProjectValues(project.Git)
153+
} else if project.Zip != nil {
154+
setZipProjectValues(project.Zip)
155+
}
156+
157+
testDevfile.projectUpdated(*project)
158+
}
159+
160+
// SetStarterProjectValues sets starter project attributes, common to all starter projects, to random values.
161+
func (testDevfile *TestDevfile) SetStarterProjectValues(starterProject *schema.StarterProject) {
162+
163+
if GetBinaryDecision() {
164+
numWords := GetRandomNumber(2, 6)
165+
for i := 0; i < numWords; i++ {
166+
if i > 0 {
167+
starterProject.Description += " "
168+
}
169+
starterProject.Description += GetRandomString(8, false)
170+
}
171+
LogInfoMessage(fmt.Sprintf("Set Description : %s", starterProject.Description))
172+
}
173+
174+
if GetBinaryDecision() {
175+
starterProject.SubDir = GetRandomString(12, false)
176+
LogInfoMessage(fmt.Sprintf("Set SubDir : %s", starterProject.SubDir))
177+
}
178+
179+
if starterProject.Git != nil {
180+
setGitProjectValues(starterProject.Git)
181+
} else if starterProject.Zip != nil {
182+
setZipProjectValues(starterProject.Zip)
183+
}
184+
185+
testDevfile.starterProjectUpdated(*starterProject)
186+
187+
}
188+
189+
// setGitProjectValues randomly sets attributes for a Git project
190+
func setGitProjectValues(gitProject *schema.GitProjectSource) {
191+
192+
if len(gitProject.Remotes) > 1 {
193+
numKey := GetRandomNumber(1, len(gitProject.Remotes))
194+
for key, _ := range gitProject.Remotes {
195+
numKey--
196+
if numKey <= 0 {
197+
gitProject.CheckoutFrom = &schema.CheckoutFrom{}
198+
gitProject.CheckoutFrom.Remote = key
199+
gitProject.CheckoutFrom.Revision = GetRandomString(8, false)
200+
LogInfoMessage(fmt.Sprintf("set CheckoutFrom remote = %s, and revision = %s", gitProject.CheckoutFrom.Remote, gitProject.CheckoutFrom.Revision))
201+
break
202+
}
203+
}
204+
}
205+
}
206+
207+
// setZipProjectValues randomly sets attributes for a Zip Project
208+
func setZipProjectValues(zipProject *schema.ZipProjectSource) {
209+
zipProject.Location = GetRandomString(GetRandomNumber(8, 16), false)
210+
}

test/v200/utils/common/test_utils.go

+32-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const (
2121
maxCommands = 10
2222
// maxComponents : The maximum number of components to include in a generated devfile
2323
maxComponents = 10
24+
// maxProjects : The maximum number of projects to include in a generated devfile
25+
maxProjects = 10
26+
// maxStarterProjects : the number of starterProjects to create for each test
27+
maxStarterProjects = 10
2428

2529
defaultTempDir = "./tmp/"
2630
logFileName = "test.log"
@@ -61,10 +65,12 @@ type DevfileValidator interface {
6165

6266
// TestContent - structure used by a test to configure the tests to run
6367
type TestContent struct {
64-
CommandTypes []schema.CommandType
65-
ComponentTypes []schema.ComponentType
66-
FileName string
67-
EditContent bool
68+
CommandTypes []schema.CommandType
69+
ComponentTypes []schema.ComponentType
70+
ProjectTypes []schema.ProjectSourceType
71+
StarterProjectTypes []schema.ProjectSourceType
72+
FileName string
73+
EditContent bool
6874
}
6975

7076
// init creates:
@@ -152,7 +158,7 @@ func LogMessage(message string) string {
152158
return message
153159
}
154160

155-
var errorPrefix = "..... ERROR : "
161+
var errorPrefix = "..... ERROR :"
156162
var infoPrefix = "INFO :"
157163

158164
// LogErrorMessage logs the specified message as an error message and returns the message logged
@@ -185,7 +191,11 @@ var StringCount int = 0
185191
// If lower is set to true a lower case string is returned.
186192
func GetRandomUniqueString(n int, lower bool) string {
187193
StringCount++
188-
return fmt.Sprintf("%s%04d", GetRandomString(n, lower), StringCount)
194+
countAsString := fmt.Sprintf("%05d", StringCount)
195+
if n < len(countAsString) {
196+
n += len(countAsString)
197+
}
198+
return fmt.Sprintf("%s%s", GetRandomString(n-len(countAsString), lower), countAsString)
189199
}
190200

191201
const schemaBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
@@ -270,6 +280,22 @@ func (testDevfile *TestDevfile) RunTest(testContent TestContent, t *testing.T) {
270280
}
271281
}
272282

283+
if len(testContent.ProjectTypes) > 0 {
284+
numProjects := GetRandomNumber(1, maxProjects)
285+
for i := 0; i < numProjects; i++ {
286+
projectIndex := GetRandomNumber(1, len(testContent.ProjectTypes))
287+
testDevfile.AddProject(testContent.ProjectTypes[projectIndex-1])
288+
}
289+
}
290+
291+
if len(testContent.StarterProjectTypes) > 0 {
292+
numStarterProjects := GetRandomNumber(1, maxStarterProjects)
293+
for i := 0; i < numStarterProjects; i++ {
294+
starterProjectIndex := GetRandomNumber(1, len(testContent.StarterProjectTypes))
295+
testDevfile.AddStarterProject(testContent.StarterProjectTypes[starterProjectIndex-1])
296+
}
297+
}
298+
273299
err := testDevfile.Validator.WriteAndValidate(testDevfile)
274300
if err != nil {
275301
t.Fatalf(LogErrorMessage(fmt.Sprintf("ERROR verifying devfile : %s : %v", testContent.FileName, err)))

0 commit comments

Comments
 (0)