From 8d36dfb985c4518264ffc5f4fc363f4a7ee1751f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krisztia=CC=81n=20Go=CC=88drei?= Date: Fri, 9 Aug 2024 13:22:36 +0200 Subject: [PATCH] Test step bundle in json logs --- _tests/integration/docker_test.go | 122 +++++++++---------- _tests/integration/modular_config_module.yml | 11 +- _tests/integration/modular_config_test.go | 41 ++++++- 3 files changed, 109 insertions(+), 65 deletions(-) diff --git a/_tests/integration/docker_test.go b/_tests/integration/docker_test.go index bbcf65cd..998ff772 100644 --- a/_tests/integration/docker_test.go +++ b/_tests/integration/docker_test.go @@ -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 @@ -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) +} diff --git a/_tests/integration/modular_config_module.yml b/_tests/integration/modular_config_module.yml index d79f299d..d54dadfb 100644 --- a/_tests/integration/modular_config_module.yml +++ b/_tests/integration/modular_config_module.yml @@ -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: {} diff --git a/_tests/integration/modular_config_test.go b/_tests/integration/modular_config_test.go index 78655db0..b403bd94 100644 --- a/_tests/integration/modular_config_test.go +++ b/_tests/integration/modular_config_test.go @@ -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") @@ -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) +}