github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/apiserver/facades/client/backups/backups_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package backups_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names/v5"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/apiserver/common"
    13  	apiservererrors "github.com/juju/juju/apiserver/errors"
    14  	backupsAPI "github.com/juju/juju/apiserver/facades/client/backups"
    15  	apiservertesting "github.com/juju/juju/apiserver/testing"
    16  	"github.com/juju/juju/controller"
    17  	"github.com/juju/juju/juju/testing"
    18  	"github.com/juju/juju/state"
    19  	"github.com/juju/juju/state/backups"
    20  	backupstesting "github.com/juju/juju/state/backups/testing"
    21  )
    22  
    23  type backupsSuite struct {
    24  	testing.JujuConnSuite
    25  	resources  *common.Resources
    26  	authorizer *apiservertesting.FakeAuthorizer
    27  	api        *backupsAPI.API
    28  	meta       *backups.Metadata
    29  	machineTag names.MachineTag
    30  }
    31  
    32  var _ = gc.Suite(&backupsSuite{})
    33  
    34  func (s *backupsSuite) SetUpTest(c *gc.C) {
    35  	s.JujuConnSuite.SetUpTest(c)
    36  
    37  	s.machineTag = names.NewMachineTag("0")
    38  	s.resources = common.NewResources()
    39  	s.resources.RegisterNamed("dataDir", common.StringResource(s.DataDir()))
    40  	s.resources.RegisterNamed("machineID", common.StringResource(s.machineTag.Id()))
    41  
    42  	ssInfo, err := s.State.StateServingInfo()
    43  	c.Assert(err, jc.ErrorIsNil)
    44  	agentConfig := s.AgentConfigForTag(c, s.machineTag)
    45  	agentConfig.SetStateServingInfo(controller.StateServingInfo{
    46  		PrivateKey:   ssInfo.PrivateKey,
    47  		Cert:         ssInfo.Cert,
    48  		CAPrivateKey: ssInfo.CAPrivateKey,
    49  		SharedSecret: ssInfo.SharedSecret,
    50  		APIPort:      ssInfo.APIPort,
    51  		StatePort:    ssInfo.StatePort,
    52  	})
    53  	err = agentConfig.Write()
    54  	c.Assert(err, jc.ErrorIsNil)
    55  
    56  	tag := names.NewLocalUserTag("admin")
    57  	s.authorizer = &apiservertesting.FakeAuthorizer{Tag: tag}
    58  	shim := &stateShim{
    59  		State:            s.State,
    60  		Model:            s.Model,
    61  		controllerNodesF: func() ([]state.ControllerNode, error) { return nil, nil },
    62  		machineF:         func(id string) (backupsAPI.Machine, error) { return &testMachine{}, nil },
    63  	}
    64  	s.api, err = backupsAPI.NewAPI(shim, s.resources, s.authorizer)
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	s.meta = backupstesting.NewMetadataStarted()
    67  }
    68  
    69  func (s *backupsSuite) setBackups(c *gc.C, meta *backups.Metadata, err string) *backupstesting.FakeBackups {
    70  	fake := backupstesting.FakeBackups{
    71  		Meta:     meta,
    72  		Filename: "test-filename",
    73  	}
    74  	if meta != nil {
    75  		fake.MetaList = append(fake.MetaList, meta)
    76  	}
    77  	if err != "" {
    78  		fake.Error = errors.Errorf(err)
    79  	}
    80  	s.PatchValue(backupsAPI.NewBackups,
    81  		func(paths *backups.Paths) backups.Backups {
    82  			return &fake
    83  		},
    84  	)
    85  	return &fake
    86  }
    87  
    88  func (s *backupsSuite) TestNewAPIOkay(c *gc.C) {
    89  	_, err := backupsAPI.NewAPI(&stateShim{State: s.State, Model: s.Model}, s.resources, s.authorizer)
    90  	c.Check(err, jc.ErrorIsNil)
    91  }
    92  
    93  func (s *backupsSuite) TestNewAPINotAuthorized(c *gc.C) {
    94  	s.authorizer.Tag = names.NewApplicationTag("eggs")
    95  	_, err := backupsAPI.NewAPI(&stateShim{State: s.State, Model: s.Model}, s.resources, s.authorizer)
    96  	c.Check(errors.Cause(err), gc.Equals, apiservererrors.ErrPerm)
    97  }
    98  
    99  func (s *backupsSuite) TestNewAPIHostedEnvironmentFails(c *gc.C) {
   100  	otherState := s.Factory.MakeModel(c, nil)
   101  	defer otherState.Close()
   102  	otherModel, err := otherState.Model()
   103  	c.Assert(err, jc.ErrorIsNil)
   104  	_, err = backupsAPI.NewAPI(&stateShim{State: otherState, Model: otherModel}, s.resources, s.authorizer)
   105  	c.Check(err, gc.ErrorMatches, "backups are only supported from the controller model\nUse juju switch to select the controller model")
   106  }
   107  
   108  func (s *backupsSuite) TestBackupsCAASFails(c *gc.C) {
   109  	otherState := s.Factory.MakeCAASModel(c, nil)
   110  	defer otherState.Close()
   111  	otherModel, err := otherState.Model()
   112  	c.Assert(err, jc.ErrorIsNil)
   113  
   114  	isController := true
   115  	_, err = backupsAPI.NewAPI(&stateShim{State: otherState, Model: otherModel, isController: &isController}, s.resources, s.authorizer)
   116  	c.Assert(err, gc.ErrorMatches, "backups on kubernetes controllers not supported")
   117  }