Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add projects and starter projects to library tests #69

Merged
merged 3 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/devfile/library
go 1.13

require (
github.com/devfile/api/v2 v2.0.0-20210223145532-81859eaef987
github.com/devfile/api/v2 v2.0.0-20210304212617-bfc3f501616b
maysunfaisal marked this conversation as resolved.
Show resolved Hide resolved
github.com/fatih/color v1.7.0
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/gobwas/glob v0.2.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/devfile/api/v2 v2.0.0-20210223145532-81859eaef987 h1:3wclWpWL/+IP6oAMY1M+RECQYa/4ZBkff7jDX1RyLxg=
github.com/devfile/api/v2 v2.0.0-20210223145532-81859eaef987/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
github.com/devfile/api/v2 v2.0.0-20210304212617-bfc3f501616b h1:r6Z2rXRA60eQQTdh1cJBBrj3vp9/c1rNbX42kyw2WpA=
github.com/devfile/api/v2 v2.0.0-20210304212617-bfc3f501616b/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
Expand Down
2 changes: 2 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ The tests which generate devfiles with random content at run time currently cove
* Components
* Container
* Volume
* Projects
* Starter Projects

## Test structure

Expand Down
21 changes: 21 additions & 0 deletions tests/v2/libraryTest/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,33 @@ func Test_MultiComponent(t *testing.T) {
libraryUtils.RunMultiThreadTest(testContent, t)
}

func Test_Projects(t *testing.T) {
testContent := commonUtils.TestContent{}
testContent.ProjectTypes = []schema.ProjectSourceType{schema.GitProjectSourceType, schema.ZipProjectSourceType}
testContent.EditContent = true
testContent.FileName = commonUtils.GetDevFileName()
libraryUtils.RunTest(testContent, t)
libraryUtils.RunMultiThreadTest(testContent, t)
}

func Test_StarterProjects(t *testing.T) {
testContent := commonUtils.TestContent{}
testContent.StarterProjectTypes = []schema.ProjectSourceType{schema.GitProjectSourceType, schema.ZipProjectSourceType}
testContent.EditContent = true
testContent.FileName = commonUtils.GetDevFileName()
libraryUtils.RunTest(testContent, t)
libraryUtils.RunMultiThreadTest(testContent, t)
}

func Test_Everything(t *testing.T) {
testContent := commonUtils.TestContent{}
testContent.CommandTypes = []schema.CommandType{schema.ExecCommandType, schema.CompositeCommandType, schema.ApplyCommandType}
testContent.ComponentTypes = []schema.ComponentType{schema.ContainerComponentType, schema.VolumeComponentType}
testContent.ProjectTypes = []schema.ProjectSourceType{schema.GitProjectSourceType, schema.ZipProjectSourceType}
testContent.StarterProjectTypes = []schema.ProjectSourceType{schema.GitProjectSourceType, schema.ZipProjectSourceType}
testContent.EditContent = true
testContent.FileName = commonUtils.GetDevFileName()
libraryUtils.RunTest(testContent, t)
libraryUtils.RunMultiThreadTest(testContent, t)

}
192 changes: 192 additions & 0 deletions tests/v2/utils/library/project_test_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 {
maysunfaisal marked this conversation as resolved.
Show resolved Hide resolved

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 {
maysunfaisal marked this conversation as resolved.
Show resolved Hide resolved

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
}
Loading