github.com/google/osv-scalibr@v0.4.1/artifact/image/layerscanning/testing/fakeimage/fake_image.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 fakeimage provides a fake implementation of the image.Image interface for testing 16 // purposes. 17 package fakeimage 18 19 import ( 20 "github.com/google/osv-scalibr/artifact/image" 21 scalibrfs "github.com/google/osv-scalibr/fs" 22 ) 23 24 // FakeImage is a fake implementation of the image.Image interface for testing purposes. 25 type FakeImage struct { 26 FakeChainLayers []image.ChainLayer 27 } 28 29 // New returns a new FakeImage. 30 func New(chainLayers []image.ChainLayer) *FakeImage { 31 return &FakeImage{ 32 FakeChainLayers: chainLayers, 33 } 34 } 35 36 // Layers returns the layers of the image. 37 func (i *FakeImage) Layers() ([]image.Layer, error) { 38 res := make([]image.Layer, len(i.FakeChainLayers)) 39 for i, layer := range i.FakeChainLayers { 40 res[i] = layer.Layer() 41 } 42 return res, nil 43 } 44 45 // ChainLayers returns the chain layers of the image. 46 func (i *FakeImage) ChainLayers() ([]image.ChainLayer, error) { 47 return i.FakeChainLayers, nil 48 } 49 50 // FS returns a SCALIBR compliant filesystem that represents the image. 51 func (i *FakeImage) FS() scalibrfs.FS { 52 return i.FakeChainLayers[len(i.FakeChainLayers)-1].FS() 53 }