Shoulder.dev Logo Shoulder.dev

How do I add tests to this codebase? - chainguard-dev/apko

Apko is a powerful tool for building and managing Alpine Linux packages. Chainguard's apko project provides various examples and utilities to help you get started. In this documentation, we will guide you through the process of adding tests to the apko codebase.

Prerequisites

Before we dive into adding tests, make sure you have the following prerequisites in place:

  1. Familiarity with Alpine Linux and apko.
  2. Basic understanding of shell scripting and YAML configuration files.
  3. A text editor or IDE of your choice.

Testing Strategy

Apko uses a combination of shell scripts, Go packages, and YAML configuration files to build and manage packages. To test these components, we will create unit tests for Go packages, shell scripts, and YAML files.

Adding Unit Tests for Go Packages

Apko includes several Go packages, such as vcs, sbom, and publish. To add unit tests for these packages, follow these steps:

  1. Navigate to the package directory:

    cd path/to/apko/go/packages/<package-name>
    
  2. Create a new file named <package-name>_test.go in the same directory.

  3. Write your tests using the testing package and any required third-party packages. For example:

    package vcs
    
    import (
        "testing"
        "github.com/stretchr/testify/require"
        // Add any other required packages here
    )
    
    func TestResolveGitRevision(t *testing.T) {
        // Write your test case here
    }
    
    func TestAnotherFunction(t *testing.T) {
        // Write another test case here
    }
    
  4. Run your tests using the go test command:

    go test
    

Adding Unit Tests for Shell Scripts

To test shell scripts, you can write Bash scripts or use a testing framework like expect or test-lint. Here, we will demonstrate using expect.

  1. Install expect:

    apk add --no-cache expect
    
  2. Create a new file named <script-name>.test.sh in the same directory as your shell script.

  3. Write your tests using the expect framework:

    #!/usr/bin/env expect
    
    set -o noclobber
    set -o pipefail
    set -o errtrace
    
    # Test your script here
    run "./<script-name>"
    
    # Assert expected output
    expect {
        "expected output" {
            sentinel "expected output"
            exp_continue
        }
        "error message" {
            error "Unexpected error: $"
        }
        "other output" {
            fail "Unexpected output: $"
        }
    }
    
  4. Run your tests using the expect command:

    expect <script-name>.test.sh
    

Adding Unit Tests for YAML Files

To test YAML files, you can use a YAML parsing library like go-yaml or ruamel.yaml. Here, we will demonstrate using go-yaml.

  1. Install go-yaml:

    go get gopkg.in/yaml.v3
    
  2. Create a new file named <yaml-file>.test.go in the same directory as your YAML file.

  3. Write your tests using the go-yaml library:

    package <package-name>
    
    import (
        "testing"
        "io/ioutil"
        "os"
        "github.com/go-yaml/yaml"
    )
    
    func TestReadYAMLFile(t *testing.T) {
        // Read your YAML file
        data, err := ioutil.ReadFile("path/to/<yaml-file>")
        if err != nil {
            t.Fatalf("Failed to read YAML file: %v", err)
        }
    
        // Unmarshal the YAML data
        var config <config-type>
        err = yaml.Unmarshal(data, &config)
        if err != nil {
            t.Fatalf("Failed to unmarshal YAML: %v", err)
        }
    
        // Assert the values of the YAML data
        require.Equal(t, expectedValue, config.Field)
    }
    
  4. Run your tests using the go test command:

    go test
    

Integrating Tests with CI

To run your tests as part of your CI pipeline, you can use a continuous integration (CI) system like GitHub Actions, Jenkins, or Travis CI. Configure your CI system to run your tests whenever a change is pushed to your repository.

Conclusion

In this documentation, we have covered the process of adding tests to the apko codebase. By following these steps, you can write unit tests for Go packages, shell scripts, and YAML files, and integrate them with your CI pipeline. This will help ensure the reliability and maintainability of your apko-based projects.