func GenerateSteps()

in pkg/testing/buildkite/buildkite.go [204:313]


func GenerateSteps(cfg common.Config, batches ...define.Batch) (string, error) {
	stackSteps := map[string]Step{}
	stackTeardown := map[string][]string{}
	var steps []Step

	// create the supported batches first
	platforms, err := cfg.GetPlatforms()
	if err != nil {
		return "", err
	}
	osBatches, err := supported.CreateBatches(batches, platforms, cfg.Groups, cfg.Matrix, cfg.SingleTest)
	if err != nil {
		return "", err
	}

	// create the stack steps first
	for _, lb := range osBatches {
		if !lb.Skip && lb.Batch.Stack != nil {
			if lb.Batch.Stack.Version == "" {
				// no version defined on the stack; set it to the defined stack version
				lb.Batch.Stack.Version = cfg.StackVersion
			}
			_, ok := stackSteps[lb.Batch.Stack.Version]
			if !ok {
				// add a step for creating the stack
				stackKey := getStackKey(lb.Batch.Stack)
				stackStep := Step{
					Label:   fmt.Sprintf("Integration Stack: %s", lb.Batch.Stack.Version),
					Key:     stackKey,
					Command: "false",
					Agents:  []StepAgent{bkStackAgent},
				}
				steps = append(steps, stackStep)
				stackSteps[lb.Batch.Stack.Version] = stackStep
				stackTeardown[stackKey] = append(stackTeardown[stackKey], stackKey)
			}
		}
	}

	// generate the steps for the tests
	for _, lb := range osBatches {
		if lb.Skip {
			continue
		}
		agentStep, err := getAgent(lb.OS)
		if err != nil {
			return "", fmt.Errorf("unable to get machine and image: %w", err)
		}
		if len(lb.Batch.Tests) > 0 {
			var step Step
			step.Label = fmt.Sprintf("Integration Test (non-sudo): %s", lb.ID)
			step.Key = fmt.Sprintf("integration-non-sudo-%s", lb.ID)
			if lb.Batch.Stack != nil {
				stackKey := getStackKey(lb.Batch.Stack)
				step.DependsOn = append(step.DependsOn, stackKey)
				stackTeardown[stackKey] = append(stackTeardown[stackKey], step.Key)
			}
			step.ArtifactPaths = []string{"build/**"}
			step.Agents = []StepAgent{agentStep}
			step.Env = map[string]string{
				"AGENT_VERSION":      cfg.AgentVersion,
				"TEST_DEFINE_PREFIX": step.Key,
				"TEST_DEFINE_TESTS":  strings.Join(getTestNames(lb.Batch.Tests), ","),
			}
			step.Command = getCommand(lb)
			step.Skip = shouldSkip(lb.OS)
			steps = append(steps, step)
		}
		if len(lb.Batch.SudoTests) > 0 {
			var step Step
			step.Label = fmt.Sprintf("Integration Test (sudo): %s", lb.ID)
			step.Key = fmt.Sprintf("integration-sudo-%s", lb.ID)
			if lb.Batch.Stack != nil {
				stackKey := getStackKey(lb.Batch.Stack)
				step.DependsOn = append(step.DependsOn, stackKey)
				stackTeardown[stackKey] = append(stackTeardown[stackKey], step.Key)
			}
			step.ArtifactPaths = []string{"build/**"}
			step.Agents = []StepAgent{agentStep}
			step.Env = map[string]string{
				"AGENT_VERSION":      cfg.AgentVersion,
				"TEST_DEFINE_PREFIX": step.Key,
				"TEST_DEFINE_TESTS":  strings.Join(getTestNames(lb.Batch.SudoTests), ","),
			}
			step.Command = getCommand(lb)
			step.Skip = shouldSkip(lb.OS)
			steps = append(steps, step)
		}
	}

	// add the teardown steps for the stacks
	for _, step := range stackSteps {
		steps = append(steps, Step{
			Label:                  fmt.Sprintf("Teardown: %s", step.Label),
			Key:                    fmt.Sprintf("teardown-%s", step.Key),
			DependsOn:              stackTeardown[step.Key],
			AllowDependencyFailure: true,
			Command:                "false",
			Agents:                 []StepAgent{bkStackAgent},
		})
	}

	yamlOutput, err := yaml.Marshal(Step{
		Steps: steps,
	})
	if err != nil {
		return "", fmt.Errorf("unable to marshal yaml: %w", err)
	}
	return string(yamlOutput), nil
}