github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/cloudconfig/podcfg/podcfg_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package podcfg_test 5 6 import ( 7 jc "github.com/juju/testing/checkers" 8 "github.com/juju/version/v2" 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/api" 12 "github.com/juju/juju/cloudconfig/podcfg" 13 "github.com/juju/juju/core/constraints" 14 "github.com/juju/juju/core/model" 15 "github.com/juju/juju/environs/config" 16 "github.com/juju/juju/testing" 17 ) 18 19 type podcfgSuite struct { 20 testing.BaseSuite 21 } 22 23 var _ = gc.Suite(&podcfgSuite{}) 24 25 func (*podcfgSuite) TestPodLabelsController(c *gc.C) { 26 cfg := testing.CustomModelConfig(c, testing.Attrs{}) 27 controllerJobs := []model.MachineJob{model.JobManageModel} 28 nonControllerJobs := []model.MachineJob{model.JobHostUnits} 29 testPodLabels(c, cfg, controllerJobs, map[string]string{ 30 "juju-model-uuid": testing.ModelTag.Id(), 31 "juju-controller-uuid": testing.ControllerTag.Id(), 32 "juju-is-controller": "true", 33 }) 34 testPodLabels(c, cfg, nonControllerJobs, map[string]string{ 35 "juju-model-uuid": testing.ModelTag.Id(), 36 "juju-controller-uuid": testing.ControllerTag.Id(), 37 "juju-is-controller": "true", 38 }) 39 } 40 41 func (*podcfgSuite) TestPodLabelsUserSpecified(c *gc.C) { 42 cfg := testing.CustomModelConfig(c, testing.Attrs{ 43 "resource-tags": "a=b c=", 44 }) 45 testPodLabels(c, cfg, nil, map[string]string{ 46 "juju-model-uuid": testing.ModelTag.Id(), 47 "juju-controller-uuid": testing.ControllerTag.Id(), 48 "a": "b", 49 "c": "", 50 "juju-is-controller": "true", 51 }) 52 } 53 54 func testPodLabels(c *gc.C, cfg *config.Config, jobs []model.MachineJob, expectTags map[string]string) { 55 tags := podcfg.PodLabels(testing.ModelTag.Id(), testing.ControllerTag.Id(), cfg, jobs) 56 c.Assert(tags, jc.DeepEquals, expectTags) 57 } 58 59 func (*podcfgSuite) TestOperatorImagesDefaultRepo(c *gc.C) { 60 cfg := testing.FakeControllerConfig() 61 cfg["juju-db-snap-channel"] = "9.9/stable" 62 podConfig, err := podcfg.NewBootstrapControllerPodConfig( 63 cfg, 64 "controller-1", 65 "ubuntu", 66 constraints.Value{}, 67 ) 68 c.Assert(err, jc.ErrorIsNil) 69 podConfig.JujuVersion = version.MustParse("6.6.6.666") 70 path, err := podConfig.GetControllerImagePath() 71 c.Assert(err, jc.ErrorIsNil) 72 c.Assert(path, gc.Equals, "docker.io/jujusolutions/jujud-operator:6.6.6.666") 73 path, err = podConfig.GetJujuDbOCIImagePath() 74 c.Assert(err, jc.ErrorIsNil) 75 c.Assert(path, gc.Equals, "docker.io/jujusolutions/juju-db:9.9") 76 } 77 78 func (*podcfgSuite) TestOperatorImagesCustomRepo(c *gc.C) { 79 cfg := testing.FakeControllerConfig() 80 cfg["caas-image-repo"] = "path/to/my/repo" 81 cfg["juju-db-snap-channel"] = "9.9" 82 podConfig, err := podcfg.NewBootstrapControllerPodConfig( 83 cfg, 84 "controller-1", 85 "ubuntu", 86 constraints.Value{}, 87 ) 88 c.Assert(err, jc.ErrorIsNil) 89 podConfig.JujuVersion = version.MustParse("6.6.6.666") 90 c.Assert(err, jc.ErrorIsNil) 91 path, err := podConfig.GetControllerImagePath() 92 c.Assert(err, jc.ErrorIsNil) 93 c.Assert(path, gc.Equals, "path/to/my/repo/jujud-operator:6.6.6.666") 94 path, err = podConfig.GetJujuDbOCIImagePath() 95 c.Assert(err, jc.ErrorIsNil) 96 c.Assert(path, gc.Equals, "path/to/my/repo/juju-db:9.9") 97 } 98 99 func (*podcfgSuite) TestBootstrapConstraints(c *gc.C) { 100 cfg := testing.FakeControllerConfig() 101 cons := constraints.MustParse("mem=4G") 102 podConfig, err := podcfg.NewBootstrapControllerPodConfig( 103 cfg, 104 "controller-1", 105 "ubuntu", 106 cons, 107 ) 108 c.Assert(err, jc.ErrorIsNil) 109 c.Assert(podConfig.Bootstrap.BootstrapMachineConstraints, gc.DeepEquals, cons) 110 } 111 112 func (*podcfgSuite) TestFinishControllerPodConfig(c *gc.C) { 113 cfg := testing.CustomModelConfig(c, testing.Attrs{ 114 "type": "kubernetes", 115 "ssl-hostname-verification": false, 116 "juju-https-proxy": "https-proxy", 117 }) 118 podConfig, err := podcfg.NewBootstrapControllerPodConfig( 119 testing.FakeControllerConfig(), 120 "controller-1", 121 "ubuntu", 122 constraints.Value{}, 123 ) 124 c.Assert(err, jc.ErrorIsNil) 125 podcfg.FinishControllerPodConfig( 126 podConfig, 127 cfg, 128 map[string]string{"foo": "bar"}, 129 ) 130 c.Assert(podConfig.DisableSSLHostnameVerification, jc.IsTrue) 131 c.Assert(podConfig.ProxySettings.Https, gc.Equals, "https-proxy") 132 c.Assert(podConfig.AgentEnvironment, jc.DeepEquals, map[string]string{ 133 "PROVIDER_TYPE": "kubernetes", 134 "foo": "bar", 135 }) 136 } 137 138 func (*podcfgSuite) TestUnitAgentConfig(c *gc.C) { 139 cfg := testing.FakeControllerConfig() 140 podConfig, err := podcfg.NewBootstrapControllerPodConfig( 141 cfg, 142 "controller-1", 143 "ubuntu", 144 constraints.Value{}, 145 ) 146 podConfig.APIInfo = &api.Info{ 147 ModelTag: testing.ModelTag, 148 CACert: testing.CACert, 149 } 150 podConfig.Bootstrap.StateServingInfo.APIPort = 1234 151 podConfig.JujuVersion = version.MustParse("6.6.6") 152 c.Assert(err, jc.ErrorIsNil) 153 agentCfg, err := podConfig.UnitAgentConfig() 154 c.Assert(err, jc.ErrorIsNil) 155 apiInfo, ok := agentCfg.APIInfo() 156 c.Assert(ok, jc.IsTrue) 157 c.Assert(agentCfg.OldPassword(), gc.Equals, apiInfo.Password) 158 c.Assert(apiInfo.Addrs, gc.DeepEquals, []string{"localhost:1234"}) 159 }