github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/apiservercertwatcher/manifold_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package apiservercertwatcher_test
     5  
     6  import (
     7  	"sync"
     8  
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	"github.com/juju/worker/v3"
    12  	"github.com/juju/worker/v3/dependency"
    13  	dt "github.com/juju/worker/v3/dependency/testing"
    14  	"github.com/juju/worker/v3/workertest"
    15  	gc "gopkg.in/check.v1"
    16  
    17  	"github.com/juju/juju/agent"
    18  	"github.com/juju/juju/controller"
    19  	"github.com/juju/juju/pki"
    20  	coretesting "github.com/juju/juju/testing"
    21  	"github.com/juju/juju/worker/apiservercertwatcher"
    22  )
    23  
    24  type ManifoldSuite struct {
    25  	testing.IsolationSuite
    26  
    27  	manifold dependency.Manifold
    28  	context  dependency.Context
    29  	agent    *mockAgent
    30  }
    31  
    32  var _ = gc.Suite(&ManifoldSuite{})
    33  
    34  func (s *ManifoldSuite) SetUpTest(c *gc.C) {
    35  	s.IsolationSuite.SetUpTest(c)
    36  
    37  	s.agent = &mockAgent{
    38  		conf: mockConfig{
    39  			caCert: coretesting.OtherCACert,
    40  			info: &controller.StateServingInfo{
    41  				CAPrivateKey: coretesting.OtherCAKey,
    42  				Cert:         coretesting.ServerCert,
    43  				PrivateKey:   coretesting.ServerKey,
    44  			},
    45  		},
    46  	}
    47  	s.context = dt.StubContext(nil, map[string]interface{}{
    48  		"agent": s.agent,
    49  	})
    50  	s.manifold = apiservercertwatcher.Manifold(apiservercertwatcher.ManifoldConfig{
    51  		AgentName: "agent",
    52  	})
    53  }
    54  
    55  func (s *ManifoldSuite) TestInputs(c *gc.C) {
    56  	c.Assert(s.manifold.Inputs, jc.SameContents, []string{"agent"})
    57  }
    58  
    59  func (s *ManifoldSuite) TestNoAgent(c *gc.C) {
    60  	context := dt.StubContext(nil, map[string]interface{}{
    61  		"agent": dependency.ErrMissing,
    62  	})
    63  	_, err := s.manifold.Start(context)
    64  	c.Assert(err, gc.Equals, dependency.ErrMissing)
    65  }
    66  
    67  func (s *ManifoldSuite) TestNoStateServingInfo(c *gc.C) {
    68  	s.agent.conf.info = nil
    69  	_, err := s.manifold.Start(s.context)
    70  	c.Assert(err, gc.ErrorMatches, "setting up initial ca authority: no state serving info in agent config")
    71  }
    72  
    73  func (s *ManifoldSuite) TestStart(c *gc.C) {
    74  	w := s.startWorkerClean(c)
    75  	workertest.CleanKill(c, w)
    76  }
    77  
    78  func (s *ManifoldSuite) TestOutput(c *gc.C) {
    79  	w := s.startWorkerClean(c)
    80  	defer workertest.CleanKill(c, w)
    81  
    82  	var authority pki.Authority
    83  	err := s.manifold.Output(w, &authority)
    84  	c.Assert(err, jc.ErrorIsNil)
    85  }
    86  
    87  func (s *ManifoldSuite) startWorkerClean(c *gc.C) worker.Worker {
    88  	w, err := s.manifold.Start(s.context)
    89  	c.Assert(err, jc.ErrorIsNil)
    90  	workertest.CheckAlive(c, w)
    91  	return w
    92  }
    93  
    94  type mockAgent struct {
    95  	agent.Agent
    96  	conf mockConfig
    97  }
    98  
    99  func (ma *mockAgent) CurrentConfig() agent.Config {
   100  	return &ma.conf
   101  }
   102  
   103  type mockConfig struct {
   104  	agent.Config
   105  
   106  	mu     sync.Mutex
   107  	info   *controller.StateServingInfo
   108  	caCert string
   109  }
   110  
   111  func (mc *mockConfig) CACert() string {
   112  	mc.mu.Lock()
   113  	defer mc.mu.Unlock()
   114  	return mc.caCert
   115  }
   116  
   117  func (mc *mockConfig) StateServingInfo() (controller.StateServingInfo, bool) {
   118  	mc.mu.Lock()
   119  	defer mc.mu.Unlock()
   120  	if mc.info != nil {
   121  		return *mc.info, true
   122  	}
   123  	return controller.StateServingInfo{}, false
   124  }