github.com/google/osv-scalibr@v0.4.1/extractor/standalone/containers/docker/fakeclient/fakeclient.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 fakeclient contains a fake implementation of the docker client for testing purposes.
    16  package fakeclient
    17  
    18  import (
    19  	"context"
    20  	"slices"
    21  
    22  	"github.com/docker/docker/api/types/container"
    23  	"github.com/google/osv-scalibr/extractor/standalone/containers/docker"
    24  )
    25  
    26  type fakeClient struct {
    27  	ctrs []container.Summary
    28  }
    29  
    30  // New creates a new fake docker client.
    31  func New(ctrs []container.Summary) docker.Client {
    32  	return &fakeClient{
    33  		ctrs: ctrs,
    34  	}
    35  }
    36  
    37  func (f *fakeClient) ContainerList(ctx context.Context, options container.ListOptions) ([]container.Summary, error) {
    38  	ctrs := slices.Clone(f.ctrs)
    39  	if !options.All {
    40  		ctrs = slices.DeleteFunc(ctrs, func(ctr container.Summary) bool { return ctr.State != "running" })
    41  	}
    42  	return ctrs, nil
    43  }