|
| 1 | +// Copyright 2024-2026 The Chainloop Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "path/filepath" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + conf "github.com/chainloop-dev/chainloop/app/controlplane/internal/conf/controlplane/config/v1" |
| 24 | + "github.com/go-kratos/kratos/v2/config" |
| 25 | + "github.com/go-kratos/kratos/v2/config/file" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + "google.golang.org/protobuf/types/known/durationpb" |
| 29 | +) |
| 30 | + |
| 31 | +func TestWorkflowRunExpirerOptsMapping(t *testing.T) { |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + config *conf.Bootstrap |
| 35 | + want time.Duration |
| 36 | + wantCheckInterval time.Duration |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "missing attestations configuration", |
| 40 | + config: &conf.Bootstrap{}, |
| 41 | + want: 0, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "unset expiration window", |
| 45 | + config: &conf.Bootstrap{ |
| 46 | + Attestations: &conf.Attestations{}, |
| 47 | + }, |
| 48 | + want: 0, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "configured expiration window", |
| 52 | + config: &conf.Bootstrap{ |
| 53 | + Attestations: &conf.Attestations{ |
| 54 | + WorkflowRunExpirationWindow: durationpb.New(2 * time.Hour), |
| 55 | + WorkflowRunExpirationCheckInterval: durationpb.New(30 * time.Second), |
| 56 | + }, |
| 57 | + }, |
| 58 | + wantCheckInterval: 30 * time.Second, |
| 59 | + want: 2 * time.Hour, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + for _, tt := range tests { |
| 64 | + t.Run(tt.name, func(t *testing.T) { |
| 65 | + opts := workflowRunExpirerOpts(tt.config) |
| 66 | + assert.Equal(t, tt.want, opts.ExpirationWindow) |
| 67 | + assert.Equal(t, tt.wantCheckInterval, opts.CheckInterval) |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestWorkflowRunExpirationDurationValidation(t *testing.T) { |
| 73 | + validator, err := newProtoValidator() |
| 74 | + require.NoError(t, err) |
| 75 | + |
| 76 | + for _, duration := range []time.Duration{0, -time.Second} { |
| 77 | + t.Run(duration.String(), func(t *testing.T) { |
| 78 | + bootstrap := &conf.Bootstrap{ |
| 79 | + Attestations: &conf.Attestations{ |
| 80 | + WorkflowRunExpirationWindow: durationpb.New(duration), |
| 81 | + }, |
| 82 | + } |
| 83 | + require.Error(t, validator.Validate(bootstrap)) |
| 84 | + |
| 85 | + bootstrap = &conf.Bootstrap{ |
| 86 | + Attestations: &conf.Attestations{ |
| 87 | + WorkflowRunExpirationCheckInterval: durationpb.New(duration), |
| 88 | + }, |
| 89 | + } |
| 90 | + require.Error(t, validator.Validate(bootstrap)) |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestMalformedWorkflowRunExpirationWindow(t *testing.T) { |
| 96 | + configPath := filepath.Join(t.TempDir(), "config.yaml") |
| 97 | + require.NoError(t, os.WriteFile(configPath, []byte( |
| 98 | + "attestations:\n workflow_run_expiration_window: definitely-not-a-duration\n", |
| 99 | + ), 0o600)) |
| 100 | + |
| 101 | + c := config.New(config.WithSource(file.NewSource(configPath))) |
| 102 | + t.Cleanup(func() { |
| 103 | + require.NoError(t, c.Close()) |
| 104 | + }) |
| 105 | + require.NoError(t, c.Load()) |
| 106 | + |
| 107 | + var bootstrap conf.Bootstrap |
| 108 | + require.Error(t, c.Scan(&bootstrap)) |
| 109 | +} |
| 110 | + |
| 111 | +func TestMalformedWorkflowRunExpirationCheckInterval(t *testing.T) { |
| 112 | + configPath := filepath.Join(t.TempDir(), "config.yaml") |
| 113 | + require.NoError(t, os.WriteFile(configPath, []byte( |
| 114 | + "attestations:\n workflow_run_expiration_check_interval: definitely-not-a-duration\n", |
| 115 | + ), 0o600)) |
| 116 | + |
| 117 | + c := config.New(config.WithSource(file.NewSource(configPath))) |
| 118 | + t.Cleanup(func() { |
| 119 | + require.NoError(t, c.Close()) |
| 120 | + }) |
| 121 | + require.NoError(t, c.Load()) |
| 122 | + |
| 123 | + var bootstrap conf.Bootstrap |
| 124 | + require.Error(t, c.Scan(&bootstrap)) |
| 125 | +} |
0 commit comments