...

Source file src/edge-infra.dev/pkg/edge/datasync/controllers/couchctl/mock_secretmanager.go

Documentation: edge-infra.dev/pkg/edge/datasync/controllers/couchctl

     1  package couchctl
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	secretmanagerpb "cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
     9  	"google.golang.org/api/option"
    10  )
    11  
    12  // const DefaultSecretValue = "mock secret value"
    13  
    14  // Common test utils
    15  type mockSecretManager struct {
    16  	clients map[string]*mockSecretManagerClient
    17  }
    18  
    19  func (sm *mockSecretManager) NewWithOptions(_ context.Context, projectID string, _ ...option.ClientOption) (secretManagerClient, error) {
    20  	if sm.clients[projectID] == nil {
    21  		sm.clients[projectID] = &mockSecretManagerClient{
    22  			secrets: make(map[string]*mockSecret),
    23  		}
    24  	}
    25  	return sm.clients[projectID], nil
    26  }
    27  
    28  type mockSecretManagerClient struct {
    29  	secrets map[string]*mockSecret
    30  }
    31  
    32  type mockSecret struct {
    33  	value []byte
    34  }
    35  
    36  // GetLatestSecretValue returns a secret value from the mock storage, or JIT creates it if missing. The controller
    37  // under test reads from one Secret Manager and writes to another, but this mock only has one storage location. So
    38  // the first read is expected to miss
    39  func (smc *mockSecretManagerClient) GetLatestSecretValue(_ context.Context, secretID string) ([]byte, error) {
    40  	s := smc.secrets[secretID]
    41  	if s == nil {
    42  		return nil, fmt.Errorf("secret %s not found", secretID)
    43  	}
    44  	return smc.secrets[secretID].value, nil
    45  }
    46  
    47  func (smc *mockSecretManagerClient) GetSecret(_ context.Context, secretID string) (*secretmanagerpb.Secret, error) {
    48  	s := smc.secrets[secretID]
    49  	if s == nil {
    50  		return nil, fmt.Errorf("secret %s not found", secretID)
    51  	}
    52  	spb := &secretmanagerpb.Secret{
    53  		Name: secretID,
    54  	}
    55  	return spb, nil
    56  }
    57  
    58  func (smc *mockSecretManagerClient) AddSecret(_ context.Context, secretID string, secretValue []byte, _ map[string]string, _ bool, _ *time.Time, _ string) error {
    59  	smc.secrets[secretID] = &mockSecret{value: secretValue}
    60  	return nil
    61  }
    62  

View as plain text