Skip to content
Prev Previous commit
test: Adds several new test helpers.
  • Loading branch information
cognifloyd committed Feb 7, 2023
commit 2d560a5253370ec6da10681de178b720f6053e58
9 changes: 7 additions & 2 deletions executor/linux/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"testing"
"time"

v1 "k8s.io/api/core/v1"

"github.com/gin-gonic/gin"
"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/server/compiler/native"
Expand Down Expand Up @@ -1451,11 +1453,14 @@ func TestLinux_ExecBuild(t *testing.T) {
// Docker uses _ while Kubernetes uses -
_pipeline = _pipeline.Sanitize(test.runtime)

var _runtime runtime.Engine
var (
_runtime runtime.Engine
_pod *v1.Pod
)
Comment on lines +1456 to +1459
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike the other tests, this test will need access to _pod after initialize the k8s runtime.


switch test.runtime {
case constants.DriverKubernetes:
_pod := testPodFor(_pipeline)
_pod = testPodFor(_pipeline)
_runtime, err = kubernetes.NewMock(_pod)
if err != nil {
t.Errorf("unable to create kubernetes runtime engine: %v", err)
Expand Down
185 changes: 156 additions & 29 deletions executor/linux/linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package linux

import (
"math"
"net/http/httptest"
"testing"

Expand Down Expand Up @@ -457,9 +458,8 @@ func testPod(useStages bool) *v1.Pod {
// using container names from a test pipeline.
func testPodFor(build *pipeline.Build) *v1.Pod {
var (
pod *v1.Pod
containers []v1.Container
containerStatuses []v1.ContainerStatus
pod *v1.Pod
containers []v1.Container
)

workingDir := "/vela/src/github.com/octocat/helloworld"
Expand All @@ -483,35 +483,13 @@ func testPodFor(build *pipeline.Build) *v1.Pod {
WorkingDir: workingDir,
ImagePullPolicy: v1.PullAlways,
})

containerStatuses = append(containerStatuses, v1.ContainerStatus{
Name: "step-github-octocat-1-clone-clone",
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Reason: "Completed",
ExitCode: 0,
},
},
Image: "target/vela-git:v0.6.0",
})
} else { // steps
containers = append(containers, v1.Container{
Name: "step-github-octocat-1-clone",
Image: "target/vela-git:v0.6.0",
WorkingDir: workingDir,
ImagePullPolicy: v1.PullAlways,
})

containerStatuses = append(containerStatuses, v1.ContainerStatus{
Name: "step-github-octocat-1-clone",
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Reason: "Completed",
ExitCode: 0,
},
},
Image: "target/vela-git:v0.6.0",
})
}

for _, stage := range build.Stages {
Expand Down Expand Up @@ -556,6 +534,158 @@ func testPodFor(build *pipeline.Build) *v1.Pod {
// secret.Origin.Pull should be one of: Always, Never, IfNotPresent
ImagePullPolicy: v1.PullPolicy(secret.Origin.Pull),
})
}

pod.Spec.Containers = containers
pod.Status.ContainerStatuses = testContainerStatuses(build, false, 0, 0)

return pod
}

// countBuildSteps counts the steps in the build.
func countBuildSteps(build *pipeline.Build) int {
steps := 0

for _, stage := range build.Stages {
for _, step := range stage.Steps {
if step.Name == "init" {
continue
}

steps++
}
}

for _, step := range build.Steps {
if step.Name == "init" {
continue
}

steps++
}

return steps
}

// testContainerStatuses is a test helper function to create a ContainerStatuses list.
func testContainerStatuses(build *pipeline.Build, servicesRunning bool, stepsRunningCount, stepsCompletedPercent int) []v1.ContainerStatus {
var containerStatuses []v1.ContainerStatus

useStages := len(build.Stages) > 0
stepsCompletedCount := 0

if stepsCompletedPercent > 0 {
stepsCompletedCount = int(math.Round(float64(stepsCompletedPercent) / 100 * float64(countBuildSteps(build))))
}

if servicesRunning {
for _, service := range build.Services {
containerStatuses = append(containerStatuses, v1.ContainerStatus{
Name: service.ID,
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
Image: service.Image,
})
}
}

if useStages {
containerStatuses = append(containerStatuses, v1.ContainerStatus{
Name: "step-github-octocat-1-clone-clone",
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Reason: "Completed",
ExitCode: 0,
},
},
Image: "target/vela-git:v0.6.0",
})
} else { // steps
containerStatuses = append(containerStatuses, v1.ContainerStatus{
Name: "step-github-octocat-1-clone",
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Reason: "Completed",
ExitCode: 0,
},
},
Image: "target/vela-git:v0.6.0",
})
}

steps := 0

for _, stage := range build.Stages {
for _, step := range stage.Steps {
if step.Name == "init" {
continue
}

steps++
if steps > stepsCompletedCount+stepsRunningCount {
break
}

if stepsRunningCount > 0 && steps > stepsCompletedCount {
containerStatuses = append(containerStatuses, v1.ContainerStatus{
Name: step.ID,
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
Image: step.Image,
})
} else if steps <= stepsCompletedCount {
containerStatuses = append(containerStatuses, v1.ContainerStatus{
Name: step.ID,
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Reason: "Completed",
ExitCode: 0,
},
},
Image: step.Image,
})
}
}
}

for _, step := range build.Steps {
if step.Name == "init" {
continue
}

steps++
if steps > stepsCompletedCount+stepsRunningCount {
break
}

if stepsRunningCount > 0 && steps > stepsCompletedCount {
containerStatuses = append(containerStatuses, v1.ContainerStatus{
Name: step.ID,
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
Image: step.Image,
})
} else if steps <= stepsCompletedCount {
containerStatuses = append(containerStatuses, v1.ContainerStatus{
Name: step.ID,
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Reason: "Completed",
ExitCode: 0,
},
},
Image: step.Image,
})
}
}

for _, secret := range build.Secrets {
if secret.Origin.Empty() {
continue
}

containerStatuses = append(containerStatuses, v1.ContainerStatus{
Name: secret.Origin.ID,
Expand All @@ -569,8 +699,5 @@ func testPodFor(build *pipeline.Build) *v1.Pod {
})
}

pod.Spec.Containers = containers
pod.Status.ContainerStatuses = containerStatuses

return pod
return containerStatuses
}