github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/agent/identity_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Copyright 2014 Cloudbase Solutions SRL
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  package agent
     6  
     7  import (
     8  	"os"
     9  
    10  	"github.com/juju/names/v5"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/controller"
    15  	"github.com/juju/juju/testing"
    16  	jujuversion "github.com/juju/juju/version"
    17  )
    18  
    19  type identitySuite struct {
    20  	testing.BaseSuite
    21  }
    22  
    23  var _ = gc.Suite(&identitySuite{})
    24  
    25  var attributeParams = AgentConfigParams{
    26  	Tag:               names.NewMachineTag("1"),
    27  	UpgradedToVersion: jujuversion.Current,
    28  	Password:          "sekrit",
    29  	CACert:            "ca cert",
    30  	APIAddresses:      []string{"localhost:1235"},
    31  	Nonce:             "a nonce",
    32  	Controller:        testing.ControllerTag,
    33  	Model:             testing.ModelTag,
    34  }
    35  
    36  var servingInfo = controller.StateServingInfo{
    37  	Cert:           "old cert",
    38  	PrivateKey:     "old key",
    39  	CAPrivateKey:   "old ca key",
    40  	StatePort:      69,
    41  	APIPort:        47,
    42  	SharedSecret:   "shared",
    43  	SystemIdentity: "identity",
    44  }
    45  
    46  func (s *identitySuite) TestWriteSystemIdentityFile(c *gc.C) {
    47  	params := attributeParams
    48  	params.Paths.DataDir = c.MkDir()
    49  	conf, err := NewStateMachineConfig(params, servingInfo)
    50  	c.Assert(err, jc.ErrorIsNil)
    51  	err = WriteSystemIdentityFile(conf)
    52  	c.Assert(err, jc.ErrorIsNil)
    53  
    54  	contents, err := os.ReadFile(conf.SystemIdentityPath())
    55  	c.Assert(err, jc.ErrorIsNil)
    56  	c.Check(string(contents), gc.Equals, servingInfo.SystemIdentity)
    57  
    58  	fi, err := os.Stat(conf.SystemIdentityPath())
    59  	c.Assert(err, jc.ErrorIsNil)
    60  	c.Check(fi.Mode().Perm(), gc.Equals, os.FileMode(0600))
    61  	// ensure that file is deleted when SystemIdentity is empty
    62  	info := servingInfo
    63  	info.SystemIdentity = ""
    64  	conf, err = NewStateMachineConfig(params, info)
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	err = WriteSystemIdentityFile(conf)
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	_, err = os.Stat(conf.SystemIdentityPath())
    69  	c.Assert(err, jc.Satisfies, os.IsNotExist)
    70  }