Skip to content

Commit

Permalink
Address Maysun comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mmulholl committed Jan 7, 2021
1 parent ad9fecd commit 9b1f8f7
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 145 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ main
.vscode/

# File created running tests
tests/src/tests/tmp/
tests/tmp/

.DS_Store
4 changes: 2 additions & 2 deletions tests/src/tests/README.md → tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package tests
package api

import (
"fmt"
"strconv"
"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
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)))
}

}
Loading

0 comments on commit 9b1f8f7

Please sign in to comment.