github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/cli/internal/test/store.go (about)

     1  package test
     2  
     3  import (
     4  	"github.com/docker/cli/cli/config/credentials"
     5  	"github.com/docker/cli/cli/config/types"
     6  )
     7  
     8  // FakeStore implements a credentials.Store that only acts as an in memory map
     9  type FakeStore struct {
    10  	store      map[string]types.AuthConfig
    11  	eraseFunc  func(serverAddress string) error
    12  	getFunc    func(serverAddress string) (types.AuthConfig, error)
    13  	getAllFunc func() (map[string]types.AuthConfig, error)
    14  	storeFunc  func(authConfig types.AuthConfig) error
    15  }
    16  
    17  // NewFakeStore creates a new file credentials store.
    18  func NewFakeStore() credentials.Store {
    19  	return &FakeStore{store: map[string]types.AuthConfig{}}
    20  }
    21  
    22  // SetStore is used to overrides Set function
    23  func (c *FakeStore) SetStore(store map[string]types.AuthConfig) {
    24  	c.store = store
    25  }
    26  
    27  // SetEraseFunc is used to overrides Erase function
    28  func (c *FakeStore) SetEraseFunc(eraseFunc func(string) error) {
    29  	c.eraseFunc = eraseFunc
    30  }
    31  
    32  // SetGetFunc is used to overrides Get function
    33  func (c *FakeStore) SetGetFunc(getFunc func(string) (types.AuthConfig, error)) {
    34  	c.getFunc = getFunc
    35  }
    36  
    37  // SetGetAllFunc is used to  overrides GetAll function
    38  func (c *FakeStore) SetGetAllFunc(getAllFunc func() (map[string]types.AuthConfig, error)) {
    39  	c.getAllFunc = getAllFunc
    40  }
    41  
    42  // SetStoreFunc is used to override Store function
    43  func (c *FakeStore) SetStoreFunc(storeFunc func(types.AuthConfig) error) {
    44  	c.storeFunc = storeFunc
    45  }
    46  
    47  // Erase removes the given credentials from the map store
    48  func (c *FakeStore) Erase(serverAddress string) error {
    49  	if c.eraseFunc != nil {
    50  		return c.eraseFunc(serverAddress)
    51  	}
    52  	delete(c.store, serverAddress)
    53  	return nil
    54  }
    55  
    56  // Get retrieves credentials for a specific server from the map store.
    57  func (c *FakeStore) Get(serverAddress string) (types.AuthConfig, error) {
    58  	if c.getFunc != nil {
    59  		return c.getFunc(serverAddress)
    60  	}
    61  	return c.store[serverAddress], nil
    62  }
    63  
    64  // GetAll returns the key value pairs of ServerAddress => Username
    65  func (c *FakeStore) GetAll() (map[string]types.AuthConfig, error) {
    66  	if c.getAllFunc != nil {
    67  		return c.getAllFunc()
    68  	}
    69  	return c.store, nil
    70  }
    71  
    72  // Store saves the given credentials in the map store.
    73  func (c *FakeStore) Store(authConfig types.AuthConfig) error {
    74  	if c.storeFunc != nil {
    75  		return c.storeFunc(authConfig)
    76  	}
    77  	c.store[authConfig.ServerAddress] = authConfig
    78  	return nil
    79  }