diff --git a/.gitignore b/.gitignore index 1bd4b352..2e12ef21 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,6 @@ main .vscode/ # File created running tests -tests/src/tests/tmp/ +tests/tmp/ .DS_Store diff --git a/tests/src/tests/README.md b/tests/README.md similarity index 98% rename from tests/src/tests/README.md rename to tests/README.md index 58f4e399..a1198998 100644 --- a/tests/src/tests/README.md +++ b/tests/README.md @@ -4,7 +4,7 @@ The tests use the go language and are intended to test every apsect of the parser for every schema attribute. Some basic aspects of the tests: -* A first test (parser_v200_schema_test.go) feeds pre-created devfiles to the parser to ensure the parser can parse all attribues and return an approproate error when the devfile contains an error. +* A first test (parser_v200_schema_test.go) feeds pre-created devfiles to the parser to ensure the parser can parse all attribues and return an approproate error when the devfile contains an error. This test is not currently available. * A second set of tests (parser_v200_verify_test.go) create devfile content at runtime: * Devfile content is randomly generated and as a result the tests are designed to run multiple times. * Parser functions covered: @@ -18,7 +18,7 @@ The tests use the go language and are intended to test every apsect of the parse ## Current tests: -The tests using pre-created devfiles are complete (but update in progress due to schema changes) +The tests using pre-created devfiles are not currently available (update in progress due to schema changes) The tests which generate devfiles with random content at run time currently cover the following properties and items. diff --git a/tests/src/tests/parser_v200_verify_test.go b/tests/api/parser_v200_verify_test.go similarity index 71% rename from tests/src/tests/parser_v200_verify_test.go rename to tests/api/parser_v200_verify_test.go index 73422531..9275642f 100644 --- a/tests/src/tests/parser_v200_verify_test.go +++ b/tests/api/parser_v200_verify_test.go @@ -1,4 +1,4 @@ -package tests +package api import ( "fmt" @@ -6,13 +6,21 @@ import ( "testing" "time" + "github.com/devfile/library/tests/utils" + schema "github.com/devfile/api/pkg/apis/workspaces/v1alpha2" ) -const numThreads = 5 // Number of threads used by multi-thread tests -const maxCommands = 10 // The maximum number of commands to include in a generated devfile -const maxComponents = 10 // The maximum number of components to include in a generated devfile +const ( + // numThreads : Number of threads used by multi-thread tests + numThreads = 5 + // maxCommands : The maximum number of commands to include in a generated devfile + maxCommands = 10 + // maxComponents : The maximum number of components to include in a generated devfile + maxComponents = 10 +) +// TestContent - structure used by a test to configure the tests to run type TestContent struct { CommandTypes []schema.CommandType ComponentTypes []schema.ComponentType @@ -26,7 +34,7 @@ func Test_ExecCommand(t *testing.T) { testContent.CommandTypes = []schema.CommandType{schema.ExecCommandType} testContent.CreateWithParser = false testContent.EditContent = false - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -35,7 +43,7 @@ func Test_ExecCommandEdit(t *testing.T) { testContent.CommandTypes = []schema.CommandType{schema.ExecCommandType} testContent.CreateWithParser = false testContent.EditContent = true - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -45,7 +53,7 @@ func Test_ExecCommandParserCreate(t *testing.T) { testContent.CommandTypes = []schema.CommandType{schema.ExecCommandType} testContent.CreateWithParser = true testContent.EditContent = false - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -55,7 +63,7 @@ func Test_ExecCommandEditParserCreate(t *testing.T) { testContent.CommandTypes = []schema.CommandType{schema.ExecCommandType} testContent.CreateWithParser = true testContent.EditContent = true - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -65,7 +73,7 @@ func Test_CompositeCommand(t *testing.T) { testContent.CommandTypes = []schema.CommandType{schema.CompositeCommandType} testContent.CreateWithParser = false testContent.EditContent = false - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -74,7 +82,7 @@ func Test_CompositeCommandEdit(t *testing.T) { testContent.CommandTypes = []schema.CommandType{schema.CompositeCommandType} testContent.CreateWithParser = false testContent.EditContent = true - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -84,7 +92,7 @@ func Test_CompositeCommandParserCreate(t *testing.T) { testContent.CommandTypes = []schema.CommandType{schema.CompositeCommandType} testContent.CreateWithParser = true testContent.EditContent = false - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -94,7 +102,7 @@ func Test_CompositeCommandEditParserCreate(t *testing.T) { testContent.CommandTypes = []schema.CommandType{schema.CompositeCommandType} testContent.CreateWithParser = true testContent.EditContent = true - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -104,7 +112,7 @@ func Test_MultiCommand(t *testing.T) { testContent.CommandTypes = []schema.CommandType{schema.ExecCommandType, schema.CompositeCommandType} testContent.CreateWithParser = true testContent.EditContent = true - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -114,7 +122,7 @@ func Test_ContainerComponent(t *testing.T) { testContent.ComponentTypes = []schema.ComponentType{schema.ContainerComponentType} testContent.CreateWithParser = false testContent.EditContent = false - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -124,7 +132,7 @@ func Test_ContainerComponentEdit(t *testing.T) { testContent.ComponentTypes = []schema.ComponentType{schema.ContainerComponentType} testContent.CreateWithParser = false testContent.EditContent = true - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -134,7 +142,7 @@ func Test_ContainerComponentCreateWithParser(t *testing.T) { testContent.ComponentTypes = []schema.ComponentType{schema.ContainerComponentType} testContent.CreateWithParser = true testContent.EditContent = false - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -144,7 +152,7 @@ func Test_ContainerComponentEditCreateWithParser(t *testing.T) { testContent.ComponentTypes = []schema.ComponentType{schema.ContainerComponentType} testContent.CreateWithParser = true testContent.EditContent = true - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -154,7 +162,7 @@ func Test_VolumeComponent(t *testing.T) { testContent.ComponentTypes = []schema.ComponentType{schema.VolumeComponentType} testContent.CreateWithParser = false testContent.EditContent = false - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -164,7 +172,7 @@ func Test_VolumeComponentEdit(t *testing.T) { testContent.ComponentTypes = []schema.ComponentType{schema.VolumeComponentType} testContent.CreateWithParser = false testContent.EditContent = true - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -174,7 +182,7 @@ func Test_VolumeComponentCreateWithParser(t *testing.T) { testContent.ComponentTypes = []schema.ComponentType{schema.VolumeComponentType} testContent.CreateWithParser = true testContent.EditContent = false - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -184,7 +192,7 @@ func Test_VolumeComponentEditCreateWithParser(t *testing.T) { testContent.ComponentTypes = []schema.ComponentType{schema.VolumeComponentType} testContent.CreateWithParser = true testContent.EditContent = true - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -194,7 +202,7 @@ func Test_MultiComponent(t *testing.T) { testContent.ComponentTypes = []schema.ComponentType{schema.ContainerComponentType, schema.VolumeComponentType} testContent.CreateWithParser = true testContent.EditContent = true - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } @@ -205,74 +213,76 @@ func Test_Everything(t *testing.T) { testContent.ComponentTypes = []schema.ComponentType{schema.ContainerComponentType, schema.VolumeComponentType} testContent.CreateWithParser = true testContent.EditContent = true - testContent.FileName = GetDevFileName() + testContent.FileName = utils.GetDevFileName() runTest(testContent, t) runMultiThreadTest(testContent, t) } +// runMultiThreadTest : Runs the same test on multiple threads, the test is based on the content of the specified TestContent func runMultiThreadTest(testContent TestContent, t *testing.T) { - LogMessage(fmt.Sprintf("Start Threaded test for %s", testContent.FileName)) + utils.LogMessage(fmt.Sprintf("Start Threaded test for %s", testContent.FileName)) devfileName := testContent.FileName var i int for i = 1; i < numThreads; i++ { - testContent.FileName = AddSuffixToFileName(devfileName, strconv.Itoa(i)) + testContent.FileName = utils.AddSuffixToFileName(devfileName, strconv.Itoa(i)) go runTest(testContent, t) } - testContent.FileName = AddSuffixToFileName(devfileName, strconv.Itoa(i)) + testContent.FileName = utils.AddSuffixToFileName(devfileName, strconv.Itoa(i)) runTest(testContent, t) - LogMessage(fmt.Sprintf("Sleep 2 seconds to allow all threads to complete : %s", devfileName)) + utils.LogMessage(fmt.Sprintf("Sleep 2 seconds to allow all threads to complete : %s", devfileName)) time.Sleep(2 * time.Second) - LogMessage(fmt.Sprintf("Sleep complete : %s", devfileName)) + utils.LogMessage(fmt.Sprintf("Sleep complete : %s", devfileName)) } +// runTest : Runs a test beased on the content of the specified TestContent func runTest(testContent TestContent, t *testing.T) { - LogMessage(fmt.Sprintf("Start test for %s", testContent.FileName)) - testDevfile := GetDevfile(testContent.FileName) + utils.LogMessage(fmt.Sprintf("Start test for %s", testContent.FileName)) + testDevfile := utils.GetDevfile(testContent.FileName) if len(testContent.CommandTypes) > 0 { - numCommands := GetRandomNumber(maxCommands) + numCommands := utils.GetRandomNumber(maxCommands) for i := 0; i < numCommands; i++ { - commandIndex := GetRandomNumber(len(testContent.CommandTypes)) - testDevfile.addCommand(testContent.CommandTypes[commandIndex-1]) + commandIndex := utils.GetRandomNumber(len(testContent.CommandTypes)) + testDevfile.AddCommand(testContent.CommandTypes[commandIndex-1]) } } if len(testContent.ComponentTypes) > 0 { - numComponents := GetRandomNumber(maxComponents) + numComponents := utils.GetRandomNumber(maxComponents) for i := 0; i < numComponents; i++ { - componentIndex := GetRandomNumber(len(testContent.ComponentTypes)) + componentIndex := utils.GetRandomNumber(len(testContent.ComponentTypes)) testDevfile.AddComponent(testContent.ComponentTypes[componentIndex-1]) } } err := testDevfile.CreateDevfile(testContent.CreateWithParser) if err != nil { - t.Fatalf(LogErrorMessage(fmt.Sprintf("ERROR creating devfile : %s : %v", testContent.FileName, err))) + t.Fatalf(utils.LogErrorMessage(fmt.Sprintf("ERROR creating devfile : %s : %v", testContent.FileName, err))) } if testContent.EditContent { if len(testContent.CommandTypes) > 0 { err = testDevfile.EditCommands() if err != nil { - t.Fatalf(LogErrorMessage(fmt.Sprintf("ERROR editing commands : %s : %v", testContent.FileName, err))) + t.Fatalf(utils.LogErrorMessage(fmt.Sprintf("ERROR editing commands : %s : %v", testContent.FileName, err))) } } if len(testContent.ComponentTypes) > 0 { err = testDevfile.EditComponents() if err != nil { - t.Fatalf(LogErrorMessage(fmt.Sprintf("ERROR editing components : %s : %v", testContent.FileName, err))) + t.Fatalf(utils.LogErrorMessage(fmt.Sprintf("ERROR editing components : %s : %v", testContent.FileName, err))) } } } err = testDevfile.Verify() if err != nil { - t.Fatalf(LogErrorMessage(fmt.Sprintf("ERROR verifying devfile content : %s : %v", testContent.FileName, err))) + t.Fatalf(utils.LogErrorMessage(fmt.Sprintf("ERROR verifying devfile content : %s : %v", testContent.FileName, err))) } } diff --git a/tests/src/tests/command_test_utils.go b/tests/utils/command_test_utils.go similarity index 85% rename from tests/src/tests/command_test_utils.go rename to tests/utils/command_test_utils.go index 60817883..be0c693f 100644 --- a/tests/src/tests/command_test_utils.go +++ b/tests/utils/command_test_utils.go @@ -1,4 +1,4 @@ -package tests +package utils import ( "errors" @@ -11,7 +11,7 @@ import ( schema "github.com/devfile/api/pkg/apis/workspaces/v1alpha2" ) -// Return a specifed number of env attributes in a schema structure +// AddEnv creates and returns a specifed number of env attributes in a schema structure func AddEnv(numEnv int) []schema.EnvVar { commandEnvs := make([]schema.EnvVar, numEnv) for i := 0; i < numEnv; i++ { @@ -22,7 +22,7 @@ func AddEnv(numEnv int) []schema.EnvVar { return commandEnvs } -// Return a specifed number of attributes in a schema structure +// AddAttributes creates returns a specifed number of attributes in a schema structure func AddAttributes(numAtrributes int) map[string]string { attributes := make(map[string]string) for i := 0; i < numAtrributes; i++ { @@ -33,7 +33,7 @@ func AddAttributes(numAtrributes int) map[string]string { return attributes } -// Create and return a group in a schema structure +// addGroup creates and returns a group in a schema structure func addGroup() *schema.CommandGroup { commandGroup := schema.CommandGroup{} @@ -44,14 +44,14 @@ func addGroup() *schema.CommandGroup { return &commandGroup } -// Add a command of the specified type to the schema -func (devfile *TestDevfile) addCommand(commandType schema.CommandType) string { +// AddCommand adds a command of the specified type, with random attributes, to the devfile schema +func (devfile *TestDevfile) AddCommand(commandType schema.CommandType) string { command := generateCommand(commandType) devfile.SchemaDevFile.Commands = append(devfile.SchemaDevFile.Commands, command) return command.Id } -// Create a command of a specified type in a schema structure +// generateCommand creates a command of a specified type in a schema structure func generateCommand(commandType schema.CommandType) schema.Command { command := schema.Command{} command.Id = GetRandomUniqueString(8, true) @@ -65,10 +65,10 @@ func generateCommand(commandType schema.CommandType) schema.Command { return command } -// Update the values of a specified command +// UpdateCommand randomly updates attribute values of a specified command in the devfile schema func (devfile *TestDevfile) UpdateCommand(parserCommand *schema.Command) error { - var errorString []string + var err error testCommand, found := getSchemaCommand(devfile.SchemaDevFile.Commands, parserCommand.Id) if found { LogInfoMessage(fmt.Sprintf("Updating command id: %s", parserCommand.Id)) @@ -79,17 +79,12 @@ func (devfile *TestDevfile) UpdateCommand(parserCommand *schema.Command) error { } devfile.replaceSchemaCommand(*parserCommand) } else { - errorString = append(errorString, LogErrorMessage(fmt.Sprintf("Command not found in test : %s", parserCommand.Id))) - } - - var returnError error - if len(errorString) > 0 { - returnError = errors.New(fmt.Sprint(errorString)) + err = errors.New(LogErrorMessage(fmt.Sprintf("Command not found in test : %s", parserCommand.Id))) } - return returnError + return err } -// Create and return an exec command in a schema structure +// createExecCommand creates and returns an exec command in a schema structure func createExecCommand() *schema.ExecCommand { LogInfoMessage("Create an exec command :") @@ -99,7 +94,7 @@ func createExecCommand() *schema.ExecCommand { } -// Set the attribute values of an exec command +// setExecCommandValues randomly sets exec command attribute to random values func setExecCommandValues(execCommand *schema.ExecCommand) { execCommand.Component = GetRandomString(8, false) @@ -139,7 +134,7 @@ func setExecCommandValues(execCommand *schema.ExecCommand) { } -// Use the specified command to replace the command in the schema structure with the same Id. +// replaceSchemaCommand uses the specified command to replace the command in the schema structure with the same Id. func (devfile TestDevfile) replaceSchemaCommand(command schema.Command) { for i := 0; i < len(devfile.SchemaDevFile.Commands); i++ { if devfile.SchemaDevFile.Commands[i].Id == command.Id { @@ -149,7 +144,7 @@ func (devfile TestDevfile) replaceSchemaCommand(command schema.Command) { } } -// Get a command from the schema structure +// getSchemaCommand get a command from the devfile schema structure func getSchemaCommand(commands []schema.Command, id string) (*schema.Command, bool) { found := false var schemaCommand schema.Command @@ -163,7 +158,7 @@ func getSchemaCommand(commands []schema.Command, id string) (*schema.Command, bo return &schemaCommand, found } -// Create a composite command in a schema structure +// createCompositeCommand creates a composite command in a schema structure func createCompositeCommand() *schema.CompositeCommand { LogInfoMessage("Create a composite command :") @@ -172,7 +167,7 @@ func createCompositeCommand() *schema.CompositeCommand { return &compositeCommand } -// Set the attribute values for a composite command +// setCompositeCommandValues randomly sets composite command attribute to random values func setCompositeCommandValues(compositeCommand *schema.CompositeCommand) { numCommands := GetRandomNumber(3) @@ -197,7 +192,7 @@ func setCompositeCommandValues(compositeCommand *schema.CompositeCommand) { } } -// Verify commands returned by the parser match with those saved in the schema +// VerifyCommands verifies commands returned by the parser are the same as those saved in the devfile schema func (devfile TestDevfile) VerifyCommands(parserCommands []schema.Command) error { LogInfoMessage("Enter VerifyCommands") diff --git a/tests/src/tests/component_test_utils.go b/tests/utils/component_test_utils.go similarity index 89% rename from tests/src/tests/component_test_utils.go rename to tests/utils/component_test_utils.go index 9280e10e..3775751a 100644 --- a/tests/src/tests/component_test_utils.go +++ b/tests/utils/component_test_utils.go @@ -1,4 +1,4 @@ -package tests +package utils import ( "errors" @@ -11,7 +11,7 @@ import ( "sigs.k8s.io/yaml" ) -// Return volumeMounts in a schema structure based on a specified number of volumes +// AddVolume returns volumeMounts in a schema structure based on a specified number of volumes func AddVolume(numVols int) []schema.VolumeMount { commandVols := make([]schema.VolumeMount, numVols) for i := 0; i < numVols; i++ { @@ -22,7 +22,7 @@ func AddVolume(numVols int) []schema.VolumeMount { return commandVols } -// Get a named components from an array of components +// getSchemaComponent returns a named component from an array of components func getSchemaComponent(components []schema.Component, name string) (*schema.Component, bool) { found := false var schemaComponent schema.Component @@ -36,14 +36,14 @@ func getSchemaComponent(components []schema.Component, name string) (*schema.Com return &schemaComponent, found } -// Add a component of the specified type +// AddComponent adds a component of the specified type, with random attributes, to the devfile schema func (devfile *TestDevfile) AddComponent(componentType schema.ComponentType) string { component := generateComponent(componentType) devfile.SchemaDevFile.Components = append(devfile.SchemaDevFile.Components, component) return component.Name } -// Generate a component in a schema structure of the specified type +// generateComponent generates a component in a schema structure of the specified type func generateComponent(componentType schema.ComponentType) schema.Component { component := schema.Component{} @@ -58,7 +58,7 @@ func generateComponent(componentType schema.ComponentType) schema.Component { return component } -// Create a container component and set its attribute values +// createContainerComponent creates a container component and set its attribute values func createContainerComponent() *schema.ContainerComponent { LogInfoMessage("Create a container component :") @@ -70,7 +70,7 @@ func createContainerComponent() *schema.ContainerComponent { } -// Create a volume component and set its attribute values +// createVolumeComponent creates a volume component and set its attribute values func createVolumeComponent() *schema.VolumeComponent { LogInfoMessage("Create a volume component :") @@ -82,7 +82,7 @@ func createVolumeComponent() *schema.VolumeComponent { } -// Set container component attribute values +// setContainerComponentValues randomly sets container component attributes to random values func setContainerComponentValues(containerComponent *schema.ContainerComponent) { containerComponent.Image = GetRandomUniqueString(8+GetRandomNumber(10), false) @@ -142,7 +142,7 @@ func setContainerComponentValues(containerComponent *schema.ContainerComponent) } -// Set volume component attribute values +// setVolumeComponentValues randomly sets volume component attributes to random values func setVolumeComponentValues(volumeComponent *schema.VolumeComponent) { if GetRandomDecision(5, 1) { @@ -152,7 +152,7 @@ func setVolumeComponentValues(volumeComponent *schema.VolumeComponent) { } -// Update the attribute values of a specified component +// UpdateComponent randomly updates the attribute values of a specified component func (devfile *TestDevfile) UpdateComponent(component *schema.Component) error { var errorString []string @@ -174,7 +174,7 @@ func (devfile *TestDevfile) UpdateComponent(component *schema.Component) error { return err } -// Verify components returned by the parser match with those saved in the schema +// VerifyComponents verifies components returned by the parser are the same as those saved in the devfile schema func (devfile TestDevfile) VerifyComponents(parserComponents []schema.Component) error { LogInfoMessage("Enter VerifyComponents") diff --git a/tests/src/tests/endpoint-test-utils.go b/tests/utils/endpoint-test-utils.go similarity index 90% rename from tests/src/tests/endpoint-test-utils.go rename to tests/utils/endpoint-test-utils.go index bcfe1264..52d67d34 100644 --- a/tests/src/tests/endpoint-test-utils.go +++ b/tests/utils/endpoint-test-utils.go @@ -1,4 +1,4 @@ -package tests +package utils import ( "fmt" @@ -8,7 +8,7 @@ import ( var Exposures = [...]schema.EndpointExposure{schema.PublicEndpointExposure, schema.InternalEndpointExposure, schema.NoneEndpointExposure} -// Get a random exposure value +// GetRandomExposure returns a random exposure value func GetRandomExposure() schema.EndpointExposure { return Exposures[GetRandomNumber(len(Exposures))-1] } @@ -16,12 +16,12 @@ func GetRandomExposure() schema.EndpointExposure { //var Protocols = [...]schema.EndpointProtocol{schema.HTTPEndpointProtocol, schema.HTTPSEndpointProtocol, schema.WSEndpointProtocol, schema.WSSEndpointProtocol, schema.TCPEndpointProtocol, schema.UDPEndpointProtocol} var Protocols = [...]schema.EndpointProtocol{schema.HTTPEndpointProtocol, schema.WSEndpointProtocol, schema.TCPEndpointProtocol, schema.UDPEndpointProtocol} -// Get a random protocol value +// GetRandomProtocol returns a random protocol value func GetRandomProtocol() schema.EndpointProtocol { return Protocols[GetRandomNumber(len(Protocols))-1] } -// Create one or more endpoints in a schema structure +// CreateEndpoints createa and returns a randon numebr of endpoints in a schema structure func CreateEndpoints() []schema.Endpoint { numEndpoints := GetRandomNumber(5) diff --git a/tests/src/tests/project_test_utils.go b/tests/utils/project_test_utils.go similarity index 91% rename from tests/src/tests/project_test_utils.go rename to tests/utils/project_test_utils.go index 34beca88..1d7ee70b 100644 --- a/tests/src/tests/project_test_utils.go +++ b/tests/utils/project_test_utils.go @@ -1,4 +1,4 @@ -package tests +package utils import ( schema "github.com/devfile/api/pkg/apis/workspaces/v1alpha2" diff --git a/tests/src/tests/test_utils.go b/tests/utils/test_utils.go similarity index 68% rename from tests/src/tests/test_utils.go rename to tests/utils/test_utils.go index b309d207..749f5734 100644 --- a/tests/src/tests/test_utils.go +++ b/tests/utils/test_utils.go @@ -1,4 +1,4 @@ -package tests +package utils import ( "errors" @@ -22,16 +22,19 @@ import ( "sigs.k8s.io/yaml" ) -const tmpDir = "./tmp/" -const logErrorOnly = false -const logFileName = "test.log" -const logToFileOnly = true // If set to false the log output will also be output to the console +const ( + tmpDir = "../tmp/" + logErrorOnly = false + logFileName = "test.log" + // logToFileOnly - If set to false the log output will also be output to the console + logToFileOnly = true // If set to false the log output will also be output to the console +) var ( testLogger *log.Logger ) -// Creates: +// init creates: // - the temporary directory used by the test to store logs and generated devfiles. // - the log file func init() { @@ -52,26 +55,14 @@ func init() { } testLogger.Println("Test Starting:") } -} -// Called from a test program. -// - determines the test program name -// - creates a temproray directory for the test program -// returns the name of the directory created. -func GetTempDir() string { - _, fn, _, ok := runtime.Caller(1) - if !ok { - return tmpDir - } - testFile := filepath.Base(fn) - testFileExtension := filepath.Ext(testFile) - subdir := testFile[0 : len(testFile)-len(testFileExtension)] - return CreateTempDir(subdir) + testError(true) + testError1(true) } -// Creates a specified sub directory under the temp directory if it does not exists -// Returns the name of the temp directory. -func CreateTempDir(subdir string) string { +// createTempDir creates a specified sub directory under the temp directory if it does not exist. +// Returns the name of the created directory. +func createTempDir(subdir string) string { tempDir := tmpDir + subdir + "/" if _, err := os.Stat(tempDir); os.IsNotExist(err) { os.Mkdir(tempDir, 0755) @@ -79,10 +70,8 @@ func CreateTempDir(subdir string) string { return tempDir } -// Called from a test program. -// - ensure the temp directory for the test program exists -// - generates a devfile name badsed in the calling function name -// Returns the devfile name +// GetDevFileName returns a qualified name of a devfile for use in a test. +// The devfile will be in a temporary directory and is named using the calling function's name. func GetDevFileName() string { pc, fn, _, ok := runtime.Caller(1) if !ok { @@ -92,10 +81,9 @@ func GetDevFileName() string { testFile := filepath.Base(fn) testFileExtension := filepath.Ext(testFile) subdir := testFile[0 : len(testFile)-len(testFileExtension)] - destDir := CreateTempDir(subdir) + destDir := createTempDir(subdir) callerName := runtime.FuncForPC(pc).Name() - - pos1 := strings.LastIndex(callerName, "/tests.") + len("/tests.") + pos1 := strings.LastIndex(callerName, "/tests/api.") + len("/tests/api.") devfileName := destDir + callerName[pos1:len(callerName)] + ".yaml" LogInfoMessage(fmt.Sprintf("GetDevFileName : %s", devfileName)) @@ -103,8 +91,8 @@ func GetDevFileName() string { return devfileName } -// Adds a specified suffix to the name of a specified file. -// For example if the file is devfile.yaml and the suffix 1 the result is devfile1.yaml +// AddSuffixToFileName adds a specified suffix to the name of a specified file. +// For example if the file is devfile.yaml and the suffix is 1, the result is devfile1.yaml func AddSuffixToFileName(fileName string, suffix string) string { pos1 := strings.LastIndex(fileName, ".yaml") newFileName := fileName[0:pos1] + suffix + ".yaml" @@ -112,33 +100,30 @@ func AddSuffixToFileName(fileName string, suffix string) string { return newFileName } -// Log the specified message -// Return the message logged +// LogMessage logs the specified message and returns the message logged func LogMessage(message string) string { testLogger.Println(message) return message } -// Log the specified message as an Error -// Return the message logged var errorPrefix = "..... ERROR : " +var infoPrefix = "INFO :" +// LogErrorMessage logs the specified message as an error message and returns the message logged func LogErrorMessage(message string) string { var errMessage []string errMessage = append(errMessage, errorPrefix, message) return LogMessage(fmt.Sprint(errMessage)) } -// Log the specified message as Info -// Return the message logged -var infoPrefix = "INFO :" - +// LogInfoMessage logs the specified message as an info message and returns the message logged func LogInfoMessage(message string) string { var infoMessage []string infoMessage = append(infoMessage, infoPrefix, message) return LogMessage(fmt.Sprint(infoMessage)) } +// TestDevfile is a structure used to track a test devfile and its contents type TestDevfile struct { SchemaDevFile schema.Devfile FileName string @@ -150,8 +135,7 @@ var StringCount int = 0 var RndSeed int64 = time.Now().UnixNano() -// Return a unique random string which is n characters long. -// An integer is appended to the name to ensure uniqueness +// GetRandomUniqueString returns a unique random string which is n characters long plus an integer to ensure uniqueness // If lower is set to true a lower case string is returned. func GetRandomUniqueString(n int, lower bool) string { StringCount++ @@ -166,7 +150,7 @@ func setRandSeed() { const schemaBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" -// Return a random string which is n characters long. +// GetRandomString returns a random string which is n characters long. // If lower is set to true a lower case string is returned. func GetRandomString(n int, lower bool) string { setRandSeed() @@ -183,30 +167,30 @@ func GetRandomString(n int, lower bool) string { var GroupKinds = [...]schema.CommandGroupKind{schema.BuildCommandGroupKind, schema.RunCommandGroupKind, schema.TestCommandGroupKind, schema.DebugCommandGroupKind} -// Return random group kind. One of "build", "run", "test" or "debug" +// GetRandomGroupKind return random group kind. One of "build", "run", "test" or "debug" func GetRandomGroupKind() schema.CommandGroupKind { return GroupKinds[GetRandomNumber(len(GroupKinds))-1] } -// Randomly returns true or false +// GetBinaryDecision randomly returns true or false func GetBinaryDecision() bool { return GetRandomDecision(1, 1) } -// Randomly returns true or false, but weighted to one or the other. +// GetRandomDecision randomly returns true or false, but weighted to one or the other. // For example if success is set to 2 and failure to 1, true is twice as likely to be returned. func GetRandomDecision(success int, failure int) bool { setRandSeed() return rand.Intn(success+failure) > failure-1 } -// Randomly returns an integer between 1 and the number specified. +// GetRandomNumber randomly returns an integer between 1 and the number specified. func GetRandomNumber(max int) int { setRandSeed() return rand.Intn(max) + 1 } -// Return a structure used to represent a specific devfile in the tests +// GetDevfile returns a structure used to represent a specific devfile in a test func GetDevfile(fileName string) TestDevfile { testDevfile := TestDevfile{} testDevfile.SchemaDevFile = schema.Devfile{} @@ -216,9 +200,8 @@ func GetDevfile(fileName string) TestDevfile { return testDevfile } -// Create a devifle on disk for use in the tests. -// If useParser is true the parser is used to generate the file, otherwise "sigs.k8s.io/yaml" is used. -// The TestDevfile structure specified contains the name of the devfile and its required content. +// CreateDevfile create a devifle on disk for use in a test. +// If useParser is true the parser library is used to generate the file, otherwise "sigs.k8s.io/yaml" is used. func (devfile *TestDevfile) CreateDevfile(useParser bool) error { var err error @@ -229,9 +212,9 @@ func (devfile *TestDevfile) CreateDevfile(useParser bool) error { if useParser { LogInfoMessage(fmt.Sprintf("Use Parser to write devfile %s", fileName)) - newDevfile, err := devfileData.NewDevfileData(devfile.SchemaDevFile.SchemaVersion) - if err != nil { - LogErrorMessage(fmt.Sprintf("Creating new devfile : %v", err)) + newDevfile, createErr := devfileData.NewDevfileData(devfile.SchemaDevFile.SchemaVersion) + if createErr != nil { + err = errors.New(LogErrorMessage(fmt.Sprintf("Creating new devfile : %v", createErr))) } else { newDevfile.SetSchemaVersion(devfile.SchemaDevFile.SchemaVersion) @@ -263,10 +246,10 @@ func (devfile *TestDevfile) CreateDevfile(useParser bool) error { } } else { LogInfoMessage(fmt.Sprintf("Marshall and write devfile %s", devfile.FileName)) - c, err := yaml.Marshal(&(devfile.SchemaDevFile)) + c, marshallErr := yaml.Marshal(&(devfile.SchemaDevFile)) - if err != nil { - LogErrorMessage(fmt.Sprintf("Marshall devfile %s : %v", devfile.FileName, err)) + if marshallErr != nil { + err = errors.New(LogErrorMessage(fmt.Sprintf("Marshall devfile %s : %v", devfile.FileName, marshallErr))) } else { err = ioutil.WriteFile(fileName, c, 0644) if err != nil { @@ -280,7 +263,7 @@ func (devfile *TestDevfile) CreateDevfile(useParser bool) error { } // Use the parser to parse a devfile on disk -func (devfile *TestDevfile) ParseSchema() error { +func (devfile *TestDevfile) parseSchema() error { var err error if !devfile.SchemaParsed { @@ -294,14 +277,14 @@ func (devfile *TestDevfile) ParseSchema() error { return err } -// Verify the contents of the specified devfile match the expected content +// Verify verifies the contents of the specified devfile with the expected content func (devfile TestDevfile) Verify() error { LogInfoMessage(fmt.Sprintf("Verify %s : ", devfile.FileName)) var errorString []string - err := devfile.ParseSchema() + err := devfile.parseSchema() if err != nil { errorString = append(errorString, LogErrorMessage(fmt.Sprintf("parsing schema %s : %v", devfile.FileName, err))) @@ -339,12 +322,12 @@ func (devfile TestDevfile) Verify() error { } -// Edit the commands in the specified devfile. +// EditCommands modifies random attributes for each of the commands in the devfile. func (devfile TestDevfile) EditCommands() error { LogInfoMessage(fmt.Sprintf("Edit %s : ", devfile.FileName)) - err := devfile.ParseSchema() + err := devfile.parseSchema() if err != nil { LogErrorMessage(fmt.Sprintf("From parser : %v", err)) } else { @@ -366,12 +349,12 @@ func (devfile TestDevfile) EditCommands() error { return err } -// Edit the components in the specified devfile. +// EditComponents modifies random attributes for each of the components in the devfile. func (devfile TestDevfile) EditComponents() error { LogInfoMessage(fmt.Sprintf("Edit %s : ", devfile.FileName)) - err := devfile.ParseSchema() + err := devfile.parseSchema() if err != nil { LogErrorMessage(fmt.Sprintf("From parser : %v", err)) } else { @@ -392,3 +375,59 @@ func (devfile TestDevfile) EditComponents() error { } return err } + +func getError(message string) (string, error) { + return message, errors.New(message) +} + +func testError(doit bool) { + + var err error + + message1, err := getError("Error21") + LogInfoMessage(fmt.Sprintf("Message1 : %s", message1)) + LogInfoMessage(fmt.Sprintf("Error : %v", err)) + + if doit { + if err != nil { + message2, err := getError("Error22") + LogInfoMessage(fmt.Sprintf("Message2 : %s", message2)) + LogInfoMessage(fmt.Sprintf("Error : %v", err)) + if err != nil { + message3, err := getError("Error23") + LogInfoMessage(fmt.Sprintf("Message3 : %s", message3)) + LogInfoMessage(fmt.Sprintf("Error : %v", err)) + } + } + } + + LogInfoMessage(fmt.Sprintf("At the end")) + LogInfoMessage(fmt.Sprintf("Error : %v", err)) + +} +func testError1(doit bool) { + + var err error + var message string + + message, err = getError("Message Var Error1") + LogInfoMessage(fmt.Sprintf("Message1 : %s", message)) + LogInfoMessage(fmt.Sprintf("Error : %v", err)) + + if doit { + if err != nil { + message, err = getError("Message Var Error2") + LogInfoMessage(fmt.Sprintf("Message2 : %s", message)) + LogInfoMessage(fmt.Sprintf("Error : %v", err)) + if err != nil { + message, err = getError("Message Var Error3") + LogInfoMessage(fmt.Sprintf("Message1 : %s", message)) + LogInfoMessage(fmt.Sprintf("Error : %v", err)) + } + } + } + + LogInfoMessage(fmt.Sprintf("At the end")) + LogInfoMessage(fmt.Sprintf("Error : %v", err)) + +}