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

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"github.com/juju/collections/set"
     8  	"github.com/juju/names/v5"
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	"github.com/juju/utils/v3"
    12  	"github.com/juju/utils/v3/ssh"
    13  	"github.com/juju/version/v2"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/charmhub"
    17  	"github.com/juju/juju/controller"
    18  	corebase "github.com/juju/juju/core/base"
    19  	"github.com/juju/juju/environs/config"
    20  	jujuversion "github.com/juju/juju/version"
    21  )
    22  
    23  // FakeAuthKeys holds the authorized key used for testing
    24  // purposes in FakeConfig. It is valid for parsing with the utils/ssh
    25  // authorized-key utilities.
    26  const FakeAuthKeys = `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAYQDP8fPSAMFm2PQGoVUks/FENVUMww1QTK6m++Y2qX9NGHm43kwEzxfoWR77wo6fhBhgFHsQ6ogE/cYLx77hOvjTchMEP74EVxSce0qtDjI7SwYbOpAButRId3g/Ef4STz8= joe@0.1.2.4`
    27  
    28  func init() {
    29  	_, err := ssh.ParseAuthorisedKey(FakeAuthKeys)
    30  	if err != nil {
    31  		panic("FakeAuthKeys does not hold a valid authorized key: " + err.Error())
    32  	}
    33  }
    34  
    35  var (
    36  	// FakeSupportedJujuSeries is used to provide a series of canned results
    37  	// of series to test bootstrap code against.
    38  	FakeSupportedJujuSeries = set.NewStrings("focal", "jammy", jujuversion.DefaultSupportedLTS())
    39  
    40  	// FakeSupportedJujuBases is used to provide a list of canned results
    41  	// of a base to test bootstrap code against.
    42  	FakeSupportedJujuBases = []corebase.Base{
    43  		corebase.MustParseBaseFromString("ubuntu@20.04"),
    44  		corebase.MustParseBaseFromString("ubuntu@22.04"),
    45  		jujuversion.DefaultSupportedLTSBase(),
    46  	}
    47  )
    48  
    49  // FakeVersionNumber is a valid version number that can be used in testing.
    50  var FakeVersionNumber = version.MustParse("2.99.0")
    51  
    52  // ModelTag is a defined known valid UUID that can be used in testing.
    53  var ModelTag = names.NewModelTag("deadbeef-0bad-400d-8000-4b1d0d06f00d")
    54  
    55  // ControllerTag is a defined known valid UUID that can be used in testing.
    56  var ControllerTag = names.NewControllerTag("deadbeef-1bad-500d-9000-4b1d0d06f00d")
    57  
    58  // FakeControllerConfig returns an environment configuration
    59  // that is expected to be found in state for a fake controller.
    60  func FakeControllerConfig() controller.Config {
    61  	return controller.Config{
    62  		"controller-uuid":           ControllerTag.Id(),
    63  		"ca-cert":                   CACert,
    64  		"state-port":                1234,
    65  		"api-port":                  17777,
    66  		"set-numa-control-policy":   false,
    67  		"model-logfile-max-backups": 1,
    68  		"model-logfile-max-size":    "1M",
    69  		"model-logs-size":           "1M",
    70  		"max-txn-log-size":          "10M",
    71  		"auditing-enabled":          false,
    72  		"audit-log-capture-args":    true,
    73  		"audit-log-max-size":        "200M",
    74  		"audit-log-max-backups":     5,
    75  		"query-tracing-threshold":   "1s",
    76  	}
    77  }
    78  
    79  // FakeConfig returns an environment configuration for a
    80  // fake provider with all required attributes set.
    81  func FakeConfig() Attrs {
    82  	return Attrs{
    83  		"type":                      "someprovider",
    84  		"name":                      "testmodel",
    85  		"uuid":                      ModelTag.Id(),
    86  		"authorized-keys":           FakeAuthKeys,
    87  		"firewall-mode":             config.FwInstance,
    88  		"ssl-hostname-verification": true,
    89  		"secret-backend":            "auto",
    90  		"development":               false,
    91  	}
    92  }
    93  
    94  // ModelConfig returns a default environment configuration suitable for
    95  // setting in the state.
    96  func ModelConfig(c *gc.C) *config.Config {
    97  	uuid := mustUUID()
    98  	return CustomModelConfig(c, Attrs{"uuid": uuid})
    99  }
   100  
   101  // mustUUID returns a stringified uuid or panics
   102  func mustUUID() string {
   103  	uuid, err := utils.NewUUID()
   104  	if err != nil {
   105  		panic(err)
   106  	}
   107  	return uuid.String()
   108  }
   109  
   110  // CustomModelConfig returns an environment configuration with
   111  // additional specified keys added.
   112  func CustomModelConfig(c *gc.C, extra Attrs) *config.Config {
   113  	attrs := FakeConfig().Merge(Attrs{
   114  		"agent-version": "2.0.0",
   115  		"charmhub-url":  charmhub.DefaultServerURL,
   116  	}).Merge(extra).Delete("admin-secret")
   117  	cfg, err := config.New(config.NoDefaults, attrs)
   118  	c.Assert(err, jc.ErrorIsNil)
   119  	return cfg
   120  }
   121  
   122  const DefaultMongoPassword = "conn-from-name-secret"
   123  
   124  // FakeJujuXDGDataHomeSuite isolates the user's home directory and
   125  // sets up a Juju home with a sample environment and certificate.
   126  type FakeJujuXDGDataHomeSuite struct {
   127  	JujuOSEnvSuite
   128  	testing.FakeHomeSuite
   129  }
   130  
   131  func (s *FakeJujuXDGDataHomeSuite) SetUpTest(c *gc.C) {
   132  	s.JujuOSEnvSuite.SetUpTest(c)
   133  	s.FakeHomeSuite.SetUpTest(c)
   134  }
   135  
   136  func (s *FakeJujuXDGDataHomeSuite) TearDownTest(c *gc.C) {
   137  	s.FakeHomeSuite.TearDownTest(c)
   138  	s.JujuOSEnvSuite.TearDownTest(c)
   139  }
   140  
   141  // AssertConfigParameterUpdated updates environment parameter and
   142  // asserts that no errors were encountered.
   143  func (s *FakeJujuXDGDataHomeSuite) AssertConfigParameterUpdated(c *gc.C, key, value string) {
   144  	s.PatchEnvironment(key, value)
   145  }