Skip to content

Commit

Permalink
Test step bundle in json logs
Browse files Browse the repository at this point in the history
  • Loading branch information
godrei committed Aug 9, 2024
1 parent 742bb67 commit 8d36dfb
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 65 deletions.
122 changes: 61 additions & 61 deletions _tests/integration/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,6 @@ import (
"github.com/stretchr/testify/require"
)

func Test_Docker_JSON_Logs(t *testing.T) {
testCases := map[string]struct {
workflowName string
configPath string
inventoryPath string
requiredContainerImage string
requiredServiceImages []string
}{
"With group with step execution and service containers": {
workflowName: "docker-login-multiple-containers",
configPath: "docker_multiple_containers_bitrise.yml",
inventoryPath: "docker_multiple_containers_secrets.yml",
requiredContainerImage: "localhost:5001/healthy-image",
requiredServiceImages: []string{
"localhost:5002/healthy-image",
"localhost:5003/healthy-image",
},
},
}
for testName, testCase := range testCases {
t.Run(testName, func(t *testing.T) {
cmd := command.New(binPath(), "run", testCase.workflowName, "--config", testCase.configPath, "--inventory", testCase.inventoryPath, "--output-format", "json")
out, _ := cmd.RunAndReturnTrimmedCombinedOutput()
//require.NoError(t, err, out)
checkRequiredContainers(t, out, testCase.requiredContainerImage, testCase.requiredServiceImages)
})
}
}

func checkRequiredContainers(t *testing.T, log string, requiredContainerImage string, requiredServiceImages []string) {
lines := strings.Split(log, "\n")
require.True(t, len(lines) > 0)

var bitriseStartedEventLog struct {
BitriseStartedEvent models.WorkflowRunPlan `json:"content"`
}
bitriseStartedLog := lines[0]
require.NoError(t, json.Unmarshal([]byte(bitriseStartedLog), &bitriseStartedEventLog))
bitriseStartedEvent := bitriseStartedEventLog.BitriseStartedEvent

var usedContainerImages []string
var usedServiceImages []string

for _, workflowPlans := range bitriseStartedEvent.ExecutionPlan {
for _, stepPlans := range workflowPlans.Steps {
if stepPlans.WithGroupUUID != "" {
withGroupPlan := bitriseStartedEvent.WithGroupPlans[stepPlans.WithGroupUUID]

usedContainerImages = append(usedContainerImages, withGroupPlan.Container.Image)
for _, servicePlan := range withGroupPlan.Services {
usedServiceImages = append(usedServiceImages, servicePlan.Image)
}
}
}
}

require.Equal(t, 1, len(usedContainerImages), bitriseStartedLog)
require.EqualValues(t, requiredContainerImage, usedContainerImages[0], bitriseStartedLog)
require.EqualValues(t, requiredServiceImages, usedServiceImages, bitriseStartedLog)
}

func Test_Docker(t *testing.T) {
testCases := map[string]struct {
configPath string
Expand Down Expand Up @@ -222,3 +161,64 @@ func Test_Docker(t *testing.T) {
})
}
}

func Test_Docker_JSON_Logs(t *testing.T) {
testCases := map[string]struct {
workflowName string
configPath string
inventoryPath string
requiredContainerImage string
requiredServiceImages []string
}{
"With group with step execution and service containers": {
workflowName: "docker-login-multiple-containers",
configPath: "docker_multiple_containers_bitrise.yml",
inventoryPath: "docker_multiple_containers_secrets.yml",
requiredContainerImage: "localhost:5001/healthy-image",
requiredServiceImages: []string{
"localhost:5002/healthy-image",
"localhost:5003/healthy-image",
},
},
}
for testName, testCase := range testCases {
t.Run(testName, func(t *testing.T) {
cmd := command.New(binPath(), "run", testCase.workflowName, "--config", testCase.configPath, "--inventory", testCase.inventoryPath, "--output-format", "json")
out, _ := cmd.RunAndReturnTrimmedCombinedOutput()
//require.NoError(t, err, out)
checkRequiredContainers(t, out, testCase.requiredContainerImage, testCase.requiredServiceImages)
})
}
}

func checkRequiredContainers(t *testing.T, log string, requiredContainerImage string, requiredServiceImages []string) {
lines := strings.Split(log, "\n")
require.True(t, len(lines) > 0)

var bitriseStartedEventLog struct {
BitriseStartedEvent models.WorkflowRunPlan `json:"content"`
}
bitriseStartedLog := lines[0]
require.NoError(t, json.Unmarshal([]byte(bitriseStartedLog), &bitriseStartedEventLog))
bitriseStartedEvent := bitriseStartedEventLog.BitriseStartedEvent

var usedContainerImages []string
var usedServiceImages []string

for _, workflowPlans := range bitriseStartedEvent.ExecutionPlan {
for _, stepPlans := range workflowPlans.Steps {
if stepPlans.WithGroupUUID != "" {
withGroupPlan := bitriseStartedEvent.WithGroupPlans[stepPlans.WithGroupUUID]

usedContainerImages = append(usedContainerImages, withGroupPlan.Container.Image)
for _, servicePlan := range withGroupPlan.Services {
usedServiceImages = append(usedServiceImages, servicePlan.Image)
}
}
}
}

require.Equal(t, 1, len(usedContainerImages), bitriseStartedLog)
require.EqualValues(t, requiredContainerImage, usedContainerImages[0], bitriseStartedLog)
require.EqualValues(t, requiredServiceImages, usedServiceImages, bitriseStartedLog)
}
11 changes: 8 additions & 3 deletions _tests/integration/modular_config_module.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
step_bundles:
print:
steps:
- script:
inputs:
- content: echo "Hello $NAME!"

workflows:
print_hello_world:
envs:
- NAME: World
steps:
- script:
inputs:
- content: echo "Hello $NAME!"
- bundle::print: {}
41 changes: 40 additions & 1 deletion _tests/integration/modular_config_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package integration

import (
"encoding/json"
"os"
"strings"
"testing"

"github.com/bitrise-io/bitrise/models"
"github.com/bitrise-io/go-utils/command"
"github.com/stretchr/testify/require"
)

func Test_ModularConfig_Run(t *testing.T) {
func Test_ModularConfig(t *testing.T) {
configPth := "modular_config_main.yml"
deployDir := os.Getenv("BITRISE_DEPLOY_DIR")

Expand Down Expand Up @@ -41,3 +44,39 @@ func Test_ModularConfig_Run(t *testing.T) {
require.NoError(t, err, out)
require.Contains(t, out, "Hello World!")
}

func Test_ModularConfig_Run_JSON_Logs(t *testing.T) {
configPth := "modular_config_main.yml"

cmd := command.New(binPath(), "run", "print_hello_world", "--config", configPth, "--output-format", "json")
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
require.NoError(t, err, out)
require.Contains(t, out, "Hello World!")
checkRequiredStepBundle(t, out, "print")
}

func checkRequiredStepBundle(t *testing.T, log string, requiredStepBundle string) {
lines := strings.Split(log, "\n")
require.True(t, len(lines) > 0)

var bitriseStartedEventLog struct {
BitriseStartedEvent models.WorkflowRunPlan `json:"content"`
}
bitriseStartedLog := lines[0]
require.NoError(t, json.Unmarshal([]byte(bitriseStartedLog), &bitriseStartedEventLog))
bitriseStartedEvent := bitriseStartedEventLog.BitriseStartedEvent

var usedStepBundles []string

for _, workflowPlans := range bitriseStartedEvent.ExecutionPlan {
for _, stepPlans := range workflowPlans.Steps {
if stepPlans.StepBundleUUID != "" {
stepBundlePlan := bitriseStartedEvent.StepBundlePlans[stepPlans.StepBundleUUID]
usedStepBundles = append(usedStepBundles, stepBundlePlan.ID)
}
}
}

require.Equal(t, 1, len(usedStepBundles), bitriseStartedLog)
require.EqualValues(t, requiredStepBundle, usedStepBundles[0], bitriseStartedLog)
}

0 comments on commit 8d36dfb

Please sign in to comment.