|
| 1 | +// |
| 2 | +// Copyright 2026 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package crafter |
| 17 | + |
| 18 | +import ( |
| 19 | + "bytes" |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + schemaapi "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1" |
| 24 | + api "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/api/attestation/v1" |
| 25 | + "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/runners" |
| 26 | + "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/runners/commitverification" |
| 27 | + "github.com/rs/zerolog" |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
| 31 | + |
| 32 | +// Environments that make the CheckEnv of a runner match |
| 33 | +var ( |
| 34 | + gitlabEnv = map[string]string{"GITLAB_CI": "true", "CI_JOB_URL": "https://gitlab.example.com/group/project/-/jobs/1"} |
| 35 | + githubEnv = map[string]string{"CI": "true", "GITHUB_REPOSITORY": "owner/repo", "GITHUB_RUN_ID": "1"} |
| 36 | + daggerEnv = map[string]string{"CHAINLOOP_DAGGER_CLIENT": "v1.0.0"} |
| 37 | + // otherRunnerEnvVars are read by the CheckEnv of the remaining runners |
| 38 | + otherRunnerEnvVars = []string{"TF_BUILD", "BUILD_BUILDURI", "JENKINS_HOME", "BUILD_URL", "CIRCLECI", "TEAMCITY_PROJECT_NAME", "CHAINLOOP_SANDBOX"} |
| 39 | +) |
| 40 | + |
| 41 | +func TestDiscoverRunner(t *testing.T) { |
| 42 | + testCases := []struct { |
| 43 | + name string |
| 44 | + envs []map[string]string |
| 45 | + want schemaapi.CraftingSchema_Runner_RunnerType |
| 46 | + }{ |
| 47 | + { |
| 48 | + name: "no runner environment", |
| 49 | + want: schemaapi.CraftingSchema_Runner_RUNNER_TYPE_UNSPECIFIED, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "gitlab pipeline", |
| 53 | + envs: []map[string]string{gitlabEnv}, |
| 54 | + want: schemaapi.CraftingSchema_Runner_GITLAB_PIPELINE, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "github action", |
| 58 | + envs: []map[string]string{githubEnv}, |
| 59 | + want: schemaapi.CraftingSchema_Runner_GITHUB_ACTION, |
| 60 | + }, |
| 61 | + { |
| 62 | + // The Chainloop Dagger module passes the parent GitLab CI context to the CLI container |
| 63 | + name: "dagger pipeline with gitlab context", |
| 64 | + envs: []map[string]string{daggerEnv, gitlabEnv}, |
| 65 | + want: schemaapi.CraftingSchema_Runner_DAGGER_PIPELINE, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "dagger pipeline with github context", |
| 69 | + envs: []map[string]string{daggerEnv, githubEnv}, |
| 70 | + want: schemaapi.CraftingSchema_Runner_DAGGER_PIPELINE, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + for _, tc := range testCases { |
| 75 | + t.Run(tc.name, func(t *testing.T) { |
| 76 | + // Clear the runner variables of the environment the test runs in, for example a CI job |
| 77 | + for _, env := range []map[string]string{gitlabEnv, githubEnv, daggerEnv} { |
| 78 | + for name := range env { |
| 79 | + t.Setenv(name, "") |
| 80 | + } |
| 81 | + } |
| 82 | + for _, name := range otherRunnerEnvVars { |
| 83 | + t.Setenv(name, "") |
| 84 | + } |
| 85 | + for _, env := range tc.envs { |
| 86 | + for name, value := range env { |
| 87 | + t.Setenv(name, value) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Discover several times: map iteration order is random, so a |
| 92 | + // single call can pick the expected runner by chance |
| 93 | + for range 50 { |
| 94 | + assert.Equal(t, tc.want, DiscoverRunner("", zerolog.Nop()).ID()) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// verifyingRunner is a runner that returns a fixed commit verification result |
| 101 | +type verifyingRunner struct { |
| 102 | + *runners.Generic |
| 103 | + verification *commitverification.CommitVerification |
| 104 | +} |
| 105 | + |
| 106 | +func (r *verifyingRunner) VerifyCommitSignature(_ context.Context, _ string) *commitverification.CommitVerification { |
| 107 | + return r.verification |
| 108 | +} |
| 109 | + |
| 110 | +func TestVerifyCommitWithPlatformLogsUnavailableReason(t *testing.T) { |
| 111 | + testCases := []struct { |
| 112 | + name string |
| 113 | + verification *commitverification.CommitVerification |
| 114 | + wantStatus api.Commit_CommitVerification_VerificationStatus |
| 115 | + wantWarning bool |
| 116 | + }{ |
| 117 | + { |
| 118 | + name: "unavailable", |
| 119 | + verification: &commitverification.CommitVerification{ |
| 120 | + Attempted: true, |
| 121 | + Status: commitverification.VerificationStatusUnavailable, |
| 122 | + Reason: "GitLab API error: x509: certificate signed by unknown authority", |
| 123 | + Platform: "gitlab", |
| 124 | + }, |
| 125 | + wantStatus: api.Commit_CommitVerification_unavailable, |
| 126 | + wantWarning: true, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "verified", |
| 130 | + verification: &commitverification.CommitVerification{ |
| 131 | + Attempted: true, |
| 132 | + Status: commitverification.VerificationStatusVerified, |
| 133 | + Reason: "Commit signed and verified", |
| 134 | + Platform: "gitlab", |
| 135 | + }, |
| 136 | + wantStatus: api.Commit_CommitVerification_verified, |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + for _, tc := range testCases { |
| 141 | + t.Run(tc.name, func(t *testing.T) { |
| 142 | + var logs bytes.Buffer |
| 143 | + logger := zerolog.New(&logs) |
| 144 | + runner := &verifyingRunner{Generic: runners.NewGeneric(), verification: tc.verification} |
| 145 | + |
| 146 | + got := verifyCommitWithPlatform(&HeadCommit{Hash: "abc"}, runner, &logger) |
| 147 | + |
| 148 | + require.NotNil(t, got) |
| 149 | + assert.Equal(t, tc.wantStatus, got.GetStatus()) |
| 150 | + if tc.wantWarning { |
| 151 | + assert.Contains(t, logs.String(), `"level":"warn"`) |
| 152 | + assert.Contains(t, logs.String(), tc.verification.Reason) |
| 153 | + } else { |
| 154 | + assert.Empty(t, logs.String()) |
| 155 | + } |
| 156 | + }) |
| 157 | + } |
| 158 | +} |
0 commit comments