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

parent tests #564

Merged
merged 3 commits into from
Aug 4, 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
41 changes: 15 additions & 26 deletions test/v200/apiTest/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,21 @@ func Test_VolumeComponent(t *testing.T) {

func Test_MultiComponent(t *testing.T) {
testContent := commonUtils.TestContent{}
testContent.ComponentTypes = []schema.ComponentType{
schema.ContainerComponentType,
schema.KubernetesComponentType,
schema.OpenshiftComponentType,
schema.VolumeComponentType}
testContent.ComponentTypes = commonUtils.ComponentTypes
testContent.FileName = commonUtils.GetDevFileName()
apiUtils.RunTest(testContent, t)
}

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

func Test_StarterProjects(t *testing.T) {
testContent := commonUtils.TestContent{}
testContent.StarterProjectTypes = []schema.ProjectSourceType{
schema.GitProjectSourceType,
schema.ZipProjectSourceType}
testContent.StarterProjectTypes = commonUtils.ProjectSourceTypes
testContent.FileName = commonUtils.GetDevFileName()
apiUtils.RunTest(testContent, t)
}
Expand All @@ -116,25 +108,22 @@ func Test_MetaData(t *testing.T) {
apiUtils.RunTest(testContent, t)
}

func Test_Parent(t *testing.T) {
testContent := commonUtils.TestContent{}
testContent.AddParent = true
testContent.FileName = commonUtils.GetDevFileName()
apiUtils.RunTest(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.KubernetesComponentType,
schema.OpenshiftComponentType,
schema.VolumeComponentType}
testContent.ProjectTypes = []schema.ProjectSourceType{
schema.GitProjectSourceType,
schema.ZipProjectSourceType}
testContent.StarterProjectTypes = []schema.ProjectSourceType{
schema.GitProjectSourceType,
schema.ZipProjectSourceType}
testContent.CommandTypes = commonUtils.CommandTypes
testContent.ComponentTypes = commonUtils.ComponentTypes
testContent.ProjectTypes = commonUtils.ProjectSourceTypes
testContent.StarterProjectTypes = commonUtils.ProjectSourceTypes
testContent.AddMetaData = true
testContent.AddEvents = true
testContent.AddParent = true
testContent.FileName = commonUtils.GetDevFileName()
apiUtils.RunTest(testContent, t)
}
2 changes: 1 addition & 1 deletion test/v200/utils/common/command_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func addAttributes(numAtrributes int) map[string]string {
func (testDevFile *TestDevfile) addGroup() *schema.CommandGroup {

commandGroup := schema.CommandGroup{}
commandGroup.Kind = GetRandomGroupKind()
commandGroup.Kind = schema.CommandGroupKind(GetRandomValue(GroupKinds).String())
LogInfoMessage(fmt.Sprintf("group Kind: %s, default already set %t", commandGroup.Kind, testDevFile.GroupDefaults[commandGroup.Kind]))
// Ensure only one and at least one of each type are labelled as default
if !testDevFile.GroupDefaults[commandGroup.Kind] {
Expand Down
76 changes: 15 additions & 61 deletions test/v200/utils/common/component_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,74 +38,27 @@ func (devfile *TestDevfile) addVolume(numVols int) []schema.VolumeMount {

// AddComponent adds a component of the specified type, with random attributes, to the devfile schema
func (devfile *TestDevfile) AddComponent(componentType schema.ComponentType) schema.Component {

var component schema.Component
if componentType == schema.ContainerComponentType {
component = devfile.createContainerComponent()
kim-tsao marked this conversation as resolved.
Show resolved Hide resolved
LogInfoMessage(fmt.Sprintf("Create a %v component :", componentType))
component := schema.Component{}
component.Name = GetRandomUniqueString(8, true)
LogInfoMessage(fmt.Sprintf("....... Name: %s", component.Name))
switch componentType {
case schema.ContainerComponentType:
component.Container = &schema.ContainerComponent{}
devfile.SetContainerComponentValues(&component)
} else if componentType == schema.KubernetesComponentType {
component = devfile.createKubernetesComponent()
case schema.KubernetesComponentType:
component.Kubernetes = &schema.KubernetesComponent{}
devfile.SetK8sComponentValues(&component)
} else if componentType == schema.OpenshiftComponentType {
component = devfile.createOpenshiftComponent()
case schema.OpenshiftComponentType:
component.Openshift = &schema.OpenshiftComponent{}
devfile.SetK8sComponentValues(&component)
} else if componentType == schema.VolumeComponentType {
component = devfile.createVolumeComponent()
case schema.VolumeComponentType:
component.Volume = &schema.VolumeComponent{}
devfile.SetVolumeComponentValues(&component)
}
return component
}

// createContainerComponent creates a container component, ready for attribute setting
func (devfile *TestDevfile) createContainerComponent() schema.Component {

LogInfoMessage("Create a container component :")
component := schema.Component{}
component.Name = GetRandomUniqueString(8, true)
LogInfoMessage(fmt.Sprintf("....... Name: %s", component.Name))
component.Container = &schema.ContainerComponent{}
devfile.componentAdded(component)
return component

}

// createKubernetesComponent creates a kubernetes component, ready for attribute setting
func (devfile *TestDevfile) createKubernetesComponent() schema.Component {

LogInfoMessage("Create a kubernetes component :")
component := schema.Component{}
component.Name = GetRandomUniqueString(8, true)
LogInfoMessage(fmt.Sprintf("....... Name: %s", component.Name))
component.Kubernetes = &schema.KubernetesComponent{}
devfile.componentAdded(component)
return component

}

// createOpenshiftComponent creates an openshift component, ready for attribute setting
func (devfile *TestDevfile) createOpenshiftComponent() schema.Component {

LogInfoMessage("Create an openshift component :")
component := schema.Component{}
component.Name = GetRandomUniqueString(8, true)
LogInfoMessage(fmt.Sprintf("....... Name: %s", component.Name))
component.Openshift = &schema.OpenshiftComponent{}
devfile.componentAdded(component)
return component

}

// createVolumeComponent creates a volume component , ready for attribute setting
func (devfile *TestDevfile) createVolumeComponent() schema.Component {

LogInfoMessage("Create a volume component :")
component := schema.Component{}
component.Name = GetRandomUniqueString(8, true)
LogInfoMessage(fmt.Sprintf("....... Name: %s", component.Name))
component.Volume = &schema.VolumeComponent{}
devfile.componentAdded(component)
return component

}

// GetContainer returns the name of an existing, or newly created, container.
Expand All @@ -121,7 +74,7 @@ func (devfile *TestDevfile) GetContainerName() string {
}

if componentName == "" {
component := devfile.createContainerComponent()
component := devfile.AddComponent(schema.ContainerComponentType)
component.Container.Image = GetRandomUniqueString(GetRandomNumber(8, 18), false)
componentName = component.Name
LogInfoMessage(fmt.Sprintf("retrun new container from GetContainerName : %s", componentName))
Expand Down Expand Up @@ -194,6 +147,7 @@ func (devfile *TestDevfile) SetContainerComponentValues(component *schema.Compon

}

//SetK8sComponentValues randomly sets the required properties of a Kubernetes or Openshift component
func (devfile *TestDevfile) SetK8sComponentValues(component *schema.Component) {
var k8type *schema.K8sLikeComponent = &schema.K8sLikeComponent{}

Expand Down
192 changes: 192 additions & 0 deletions test/v200/utils/common/parent_command_test_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package common

import (
"fmt"

schema "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
)

// parentCommandAdded adds a new command to the test schema data
func (testDevFile *TestDevfile) parentCommandAdded(command schema.CommandParentOverride) {
LogInfoMessage(fmt.Sprintf("parent command added Id: %s", command.Id))
testDevFile.SchemaDevFile.Parent.Commands = append(testDevFile.SchemaDevFile.Parent.Commands, command)
}

// addParentEnv creates and returns a specifed number of env attributes in a schema structure
func addParentEnv(numEnv int) []schema.EnvVarParentOverride {
commandEnvs := make([]schema.EnvVarParentOverride, numEnv)
for i := 0; i < numEnv; i++ {
commandEnvs[i].Name = "Name_" + GetRandomString(5, false)
commandEnvs[i].Value = "Value_" + GetRandomString(5, false)
LogInfoMessage(fmt.Sprintf("Add Parent Env: %s", commandEnvs[i]))
}
return commandEnvs
}

// addParentGroup creates and returns a group in a schema structure
func (testDevFile *TestDevfile) addParentGroup() *schema.CommandGroupParentOverride {

commandGroup := schema.CommandGroupParentOverride{}
commandGroup.Kind = schema.CommandGroupKindParentOverride(GetRandomValue(GroupKinds).String())
kind := schema.CommandGroupKind(commandGroup.Kind)
LogInfoMessage(fmt.Sprintf("parent group Kind: %s, default already set %t", commandGroup.Kind, testDevFile.GroupDefaults[kind]))
// Ensure only one and at least one of each type are labelled as default
if !testDevFile.GroupDefaults[schema.CommandGroupKind(kind)] {
testDevFile.GroupDefaults[schema.CommandGroupKind(kind)] = true
commandGroup.IsDefault = true
} else {
commandGroup.IsDefault = false
}
LogInfoMessage(fmt.Sprintf("parent group isDefault: %t", commandGroup.IsDefault))
return &commandGroup
}

// AddParentCommand creates a command of a specified type in a schema structure and populates it with random values
func (testDevFile *TestDevfile) AddParentCommand(commandType schema.CommandType) schema.CommandParentOverride {

var command *schema.CommandParentOverride
switch commandType {
case schema.ExecCommandType:
command = testDevFile.createParentExecCommand()
testDevFile.SetParentExecCommandValues(command)
case schema.CompositeCommandType:
command = testDevFile.createParentCompositeCommand()
testDevFile.SetParentCompositeCommandValues(command)
case schema.ApplyCommandType:
command = testDevFile.createParentApplyCommand()
testDevFile.SetParentApplyCommandValues(command)
}
return *command
}

// createParentExecCommand creates and returns an empty exec command in a schema structure
func (testDevFile *TestDevfile) createParentExecCommand() *schema.CommandParentOverride {

LogInfoMessage("Create a parent exec command :")
command := schema.CommandParentOverride{}
command.Id = GetRandomUniqueString(8, true)
LogInfoMessage(fmt.Sprintf("command Id: %s", command.Id))
command.Exec = &schema.ExecCommandParentOverride{}
testDevFile.parentCommandAdded(command)
return &command

}

// SetParentExecCommandValues randomly sets/updates exec command attributes to random values
func (testDevFile *TestDevfile) SetParentExecCommandValues(command *schema.CommandParentOverride) {

execCommand := command.Exec

// exec command must be mentioned by a container component
execCommand.Component = testDevFile.GetParentContainerName()

execCommand.CommandLine = GetRandomString(4, false) + " " + GetRandomString(4, false)
LogInfoMessage(fmt.Sprintf("....... commandLine: %s", execCommand.CommandLine))

// If group already leave it to make sure defaults are not deleted or added
if execCommand.Group == nil {
if GetRandomDecision(2, 1) {
execCommand.Group = testDevFile.addParentGroup()
}
}

if GetBinaryDecision() {
execCommand.Label = GetRandomString(12, false)
LogInfoMessage(fmt.Sprintf("....... label: %s", execCommand.Label))
} else {
execCommand.Label = ""
}

if GetBinaryDecision() {
execCommand.WorkingDir = "./tmp"
LogInfoMessage(fmt.Sprintf("....... WorkingDir: %s", execCommand.WorkingDir))
} else {
execCommand.WorkingDir = ""
}

execCommand.HotReloadCapable = GetBinaryDecision()
LogInfoMessage(fmt.Sprintf("....... HotReloadCapable: %t", execCommand.HotReloadCapable))

if GetBinaryDecision() {
execCommand.Env = addParentEnv(GetRandomNumber(1, 4))
} else {
execCommand.Env = nil
}
LogInfoMessage(fmt.Sprintf("parent command updated Id: %s", command.Id))

}

// createParentCompositeCommand creates an empty composite command in a schema structure
func (testDevFile *TestDevfile) createParentCompositeCommand() *schema.CommandParentOverride {

LogInfoMessage("Create a parent composite command :")
command := schema.CommandParentOverride{}
command.Id = GetRandomUniqueString(8, true)
LogInfoMessage(fmt.Sprintf("command Id: %s", command.Id))
command.Composite = &schema.CompositeCommandParentOverride{}
testDevFile.parentCommandAdded(command)

return &command
}

// SetParentCompositeCommandValues randomly sets/updates composite command attributes to random values
func (testDevFile *TestDevfile) SetParentCompositeCommandValues(command *schema.CommandParentOverride) {

compositeCommand := command.Composite
numCommands := GetRandomNumber(1, 3)

for i := 0; i < numCommands; i++ {
execCommand := testDevFile.AddParentCommand(schema.ExecCommandType)
compositeCommand.Commands = append(compositeCommand.Commands, execCommand.Id)
LogInfoMessage(fmt.Sprintf("....... command %d of %d : %s", i, numCommands, execCommand.Id))
}
kim-tsao marked this conversation as resolved.
Show resolved Hide resolved

// If group already exists - leave it to make sure defaults are not deleted or added
if compositeCommand.Group == nil {
if GetRandomDecision(2, 1) {
compositeCommand.Group = testDevFile.addParentGroup()
}
}

if GetBinaryDecision() {
compositeCommand.Label = GetRandomString(12, false)
LogInfoMessage(fmt.Sprintf("....... label: %s", compositeCommand.Label))
}

if GetBinaryDecision() {
compositeCommand.Parallel = true
LogInfoMessage(fmt.Sprintf("....... Parallel: %t", compositeCommand.Parallel))
}

LogInfoMessage(fmt.Sprintf("parent command updated Id: %s", command.Id))
}

// createParentApplyCommand creates an apply command in a schema structure
func (testDevFile *TestDevfile) createParentApplyCommand() *schema.CommandParentOverride {

LogInfoMessage("Create a parent apply command :")
command := schema.CommandParentOverride{}
command.Id = GetRandomUniqueString(8, true)
LogInfoMessage(fmt.Sprintf("command Id: %s", command.Id))
command.Apply = &schema.ApplyCommandParentOverride{}
testDevFile.parentCommandAdded(command)
return &command
}

// SetApplyCommandValues randomly sets/updates apply command attributes to random values
func (testDevFile *TestDevfile) SetParentApplyCommandValues(command *schema.CommandParentOverride) {
applyCommand := command.Apply

applyCommand.Component = testDevFile.GetParentContainerName()

if GetRandomDecision(2, 1) {
applyCommand.Group = testDevFile.addParentGroup()
}

if GetBinaryDecision() {
applyCommand.Label = GetRandomString(63, false)
LogInfoMessage(fmt.Sprintf("....... label: %s", applyCommand.Label))
}

LogInfoMessage(fmt.Sprintf("parent command updated Id: %s", command.Id))
}
Loading