github.com/google/osv-scalibr@v0.4.1/veles/velestest/fakevalidator.go (about) 1 // Copyright 2025 Google LLC 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 velestest 16 17 import ( 18 "context" 19 20 "github.com/google/osv-scalibr/veles" 21 ) 22 23 var _ veles.Validator[FakeStringSecret] = &FakeValidator[FakeStringSecret]{} 24 25 // FakeValidator is a fake veles.Validator for a specific Secret type S. 26 // 27 // If Err is non-nil, Validate will always return ValidationStatusFailed 28 // regardless of the value of Status. 29 type FakeValidator[S veles.Secret] struct { 30 Status veles.ValidationStatus 31 Err error 32 } 33 34 // NewFakeValidator returns a new fake veles.Validator for a specific Secret S. 35 func NewFakeValidator[S veles.Secret](status veles.ValidationStatus, err error) *FakeValidator[S] { 36 return &FakeValidator[S]{ 37 Status: status, 38 Err: err, 39 } 40 } 41 42 // NewFakeStringSecretValidator creates a fake Validator for FakeStringSecrets. 43 func NewFakeStringSecretValidator(status veles.ValidationStatus, err error) *FakeValidator[FakeStringSecret] { 44 return NewFakeValidator[FakeStringSecret](status, err) 45 } 46 47 // NewFakeIntSecretValidator creates a fake Validator for FakeIntSecrets. 48 func NewFakeIntSecretValidator(status veles.ValidationStatus, err error) *FakeValidator[FakeIntSecret] { 49 return NewFakeValidator[FakeIntSecret](status, err) 50 } 51 52 // Validate returns the internal state of the fake while also respecting the 53 // context. 54 func (v *FakeValidator[S]) Validate(ctx context.Context, s S) (veles.ValidationStatus, error) { 55 if err := ctx.Err(); err != nil { 56 return veles.ValidationFailed, err 57 } 58 if err := v.Err; err != nil { 59 return veles.ValidationFailed, err 60 } 61 return v.Status, nil 62 }