cyberark / secretless-broker

Secure your apps by making them Secretless
Apache License 2.0
234 stars 40 forks source link

Adds code coverage reporting for docker-compose based integ tests #1455

Closed diverdane closed 2 years ago

diverdane commented 2 years ago

Desired Outcome

Current Cobertura coverage results only report coverage for Go unit tests. Our automated tests include integration tests, but the coverage for these integration tests are not being included in coverage reports.

The integration tests use Docker Compose, and have the general form:

                                Conjur
                                    ^
                                    |
                                    V
Test Client Container   <==>  Secretless Broker  <==>   Backend Database
    (client binaries)

We should have either:

Or both.

Implemented Changes

The comment that is added to the Secretless Broker Go coverage test file might explain how things are implemented:

// TestCoverage runs the Secretless Broker executable wrapped in a
// Go test so that the Go test infrastructure can collect code coverage stats
// for the Secretless Broker e.g. while running Docker-Compose-based
// integration tests. This Go test and the Secretless Broker code can be
// compiled into a special Secretless Broker test binary that includes
// code coverage instrumentation using the following Go test compile command:
//
//        go test -c -coverpkg="./..." ./cmd/secretless-broker
//
// rather than the usual `go build ...` compile command. (See
// 'Dockerfile.coverage' in the root of this GitHub repository to see how this
// compilation is done and built into a Docker image).
//
// Using this specially-instrumented Secretless Broker test binary, the
// Go test with its embedded Secretless Broker functionality would be invoked
// e.g. using the following command (note that standard Go test parameters
// must be prefixed with '-test.' for images compiled via 'go test -c ...'):
//
//        /usr/local/bin/secretless-broker \
//            -test.v \
//            -test.run ^TestRunMain$$ \
//            -test.coverprofile=/test-coverage/cover.out"
//
// Normally, the Secretless Broker binary can be passed optional command line
// parameters, for example:
//
//        secretless-broker -f /secretless-test.yml -debug
//
// However, in the case of the Secretless Broker test binary, command line
// parameters are processed by the Go test infrastructure, and therefore any
// of the usual Secretless Broker command line parameters will be rejected
// and cause a test failure.
//
// To get around this limitation, the TestCoverage test will
// look for equivalent environment variable settings for the usual Secretless
// Broker command line parameters, and pass those settings to the Secretless
// Broker code. These environment variable settings include:
//
//       Environment Variable      Equivalent Command Line Parameter
//       --------------------      ---------------------------------
//       SB_CONFIG_FILE            -f <config-file>
//       SB_PROFILING_MODE         -profile [cpu|memory]
//       SB_DEBUG_ENABLED          -debug
//       SB_CONFIG_MANAGER         -config-mgr <config-mgr-spec>
//       SB_FS_WATCH_ENABLED       -watch
//       SB_PLUGIN_DIR             -p <plugin-directory>
//       SB_PLUGIN_CHECKSUM_FILE   -s <plugin-checksum-file>

func TestCoverage(t *testing.T) {
    params, err := getSecretlessOptions()
    assert.NoError(t, err)
    entrypoint.StartSecretless(params)
}

Connected Issue/Story

Resolves #[relevant GitHub issue(s), e.g. 76]

CyberArk internal issue link: [insert issue ID]()

Definition of Done

At least 1 todo must be completed in the sections below for the PR to be merged.

Changelog

Test coverage

Documentation

Behavior

Security

codeclimate[bot] commented 2 years ago

Code Climate has analyzed commit ebf4fea1 and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 0.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 34.8% (-0.1% change).

View more on Code Climate.