github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/runner/testing/utils.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"path/filepath"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/names/v5"
    11  	"github.com/juju/worker/v3"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/core/leadership"
    15  	"github.com/juju/juju/core/secrets"
    16  	"github.com/juju/juju/juju/sockets"
    17  	jujusecrets "github.com/juju/juju/secrets"
    18  	"github.com/juju/juju/storage"
    19  	"github.com/juju/juju/worker/uniter/runner/context"
    20  )
    21  
    22  // RealPaths implements Paths for tests that do touch the filesystem.
    23  type RealPaths struct {
    24  	tools        string
    25  	charm        string
    26  	base         string
    27  	socket       sockets.Socket
    28  	metricsspool string
    29  }
    30  
    31  func osDependentSockPath(c *gc.C) sockets.Socket {
    32  	sockPath := filepath.Join(c.MkDir(), "test.sock")
    33  	return sockets.Socket{Network: "unix", Address: sockPath}
    34  }
    35  
    36  func NewRealPaths(c *gc.C) RealPaths {
    37  	return RealPaths{
    38  		tools:        c.MkDir(),
    39  		charm:        c.MkDir(),
    40  		base:         c.MkDir(),
    41  		socket:       osDependentSockPath(c),
    42  		metricsspool: c.MkDir(),
    43  	}
    44  }
    45  
    46  func (p RealPaths) GetMetricsSpoolDir() string {
    47  	return p.metricsspool
    48  }
    49  
    50  func (p RealPaths) GetToolsDir() string {
    51  	return p.tools
    52  }
    53  
    54  func (p RealPaths) GetCharmDir() string {
    55  	return p.charm
    56  }
    57  
    58  func (p RealPaths) GetBaseDir() string {
    59  	return p.base
    60  }
    61  
    62  func (p RealPaths) GetJujucClientSocket(remote bool) sockets.Socket {
    63  	return p.socket
    64  }
    65  
    66  func (p RealPaths) GetJujucServerSocket(remote bool) sockets.Socket {
    67  	return p.socket
    68  }
    69  
    70  func (p RealPaths) GetResourcesDir() string {
    71  	return filepath.Join(p.base, "resources")
    72  }
    73  
    74  type ContextStorage struct {
    75  	CTag      names.StorageTag
    76  	CKind     storage.StorageKind
    77  	CLocation string
    78  }
    79  
    80  func (c *ContextStorage) Tag() names.StorageTag {
    81  	return c.CTag
    82  }
    83  
    84  func (c *ContextStorage) Kind() storage.StorageKind {
    85  	return c.CKind
    86  }
    87  
    88  func (c *ContextStorage) Location() string {
    89  	return c.CLocation
    90  }
    91  
    92  type FakeTracker struct {
    93  	leadership.Tracker
    94  	worker.Worker
    95  
    96  	AllowClaimLeader bool
    97  }
    98  
    99  func (t *FakeTracker) ApplicationName() string {
   100  	return "application-name"
   101  }
   102  
   103  func (t *FakeTracker) ClaimLeader() leadership.Ticket {
   104  	return &FakeTicket{t.AllowClaimLeader}
   105  }
   106  
   107  type FakeTicket struct {
   108  	WaitResult bool
   109  }
   110  
   111  var _ leadership.Ticket = &FakeTicket{}
   112  
   113  func (ft *FakeTicket) Wait() bool {
   114  	return ft.WaitResult
   115  }
   116  
   117  func (ft *FakeTicket) Ready() <-chan struct{} {
   118  	c := make(chan struct{})
   119  	close(c)
   120  	return c
   121  }
   122  
   123  type SecretsContextAccessor struct {
   124  	context.SecretsAccessor
   125  	jujusecrets.BackendsClient
   126  }
   127  
   128  func (s SecretsContextAccessor) CreateSecretURIs(int) ([]*secrets.URI, error) {
   129  	return []*secrets.URI{{
   130  		ID: "8m4e2mr0ui3e8a215n4g",
   131  	}}, nil
   132  }
   133  
   134  func (s SecretsContextAccessor) SecretMetadata() ([]secrets.SecretOwnerMetadata, error) {
   135  	uri, _ := secrets.ParseURI("secret:9m4e2mr0ui3e8a215n4g")
   136  	return []secrets.SecretOwnerMetadata{{
   137  		Metadata: secrets.SecretMetadata{
   138  			URI:            uri,
   139  			LatestRevision: 666,
   140  			OwnerTag:       "application-mariadb",
   141  			Description:    "description",
   142  			RotatePolicy:   secrets.RotateHourly,
   143  			Label:          "label",
   144  		},
   145  		Revisions: []int{666},
   146  	}}, nil
   147  }
   148  
   149  func (s SecretsContextAccessor) SaveContent(uri *secrets.URI, revision int, value secrets.SecretValue) (secrets.ValueRef, error) {
   150  	return secrets.ValueRef{}, errors.NotSupportedf("")
   151  }
   152  
   153  func (s SecretsContextAccessor) DeleteContent(uri *secrets.URI, revision int) error {
   154  	return errors.NotSupportedf("")
   155  }