github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/runner/jujuc/k8s-spec-set_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc_test 5 6 import ( 7 "bytes" 8 "os" 9 "path/filepath" 10 11 "github.com/juju/cmd/v3" 12 "github.com/juju/cmd/v3/cmdtesting" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/worker/uniter/runner/jujuc" 17 ) 18 19 type K8sSpecSetSuite struct { 20 ContextSuite 21 } 22 23 var _ = gc.Suite(&K8sSpecSetSuite{}) 24 25 var ( 26 podSpecYaml = ` 27 podSpec: 28 foo: bar 29 `[1:] 30 31 k8sResourcesYaml = ` 32 kubernetesResources: 33 pod: 34 restartPolicy: OnFailure 35 activeDeadlineSeconds: 10 36 terminationGracePeriodSeconds: 20 37 securityContext: 38 runAsNonRoot: true 39 supplementalGroups: [1,2] 40 priority: 30 41 readinessGates: 42 - conditionType: PodScheduled 43 dnsPolicy: ClusterFirstWithHostNet 44 secrets: 45 - name: build-robot-secret 46 annotations: 47 kubernetes.io/service-account.name: build-robot 48 type: kubernetes.io/service-account-token 49 stringData: 50 config.yaml: |- 51 apiUrl: "https://my.api.com/api/v1" 52 username: fred 53 password: shhhh 54 `[1:] 55 ) 56 57 var k8sSpecSetInitTests = []struct { 58 args []string 59 err string 60 }{ 61 {[]string{"--file", "file", "extra"}, `unrecognized args: \["extra"\]`}, 62 } 63 64 func (s *K8sSpecSetSuite) TestK8sSpecSetInit(c *gc.C) { 65 for i, t := range k8sSpecSetInitTests { 66 c.Logf("test %d: %#v", i, t.args) 67 hctx := s.GetHookContext(c, -1, "") 68 com, err := jujuc.NewCommand(hctx, "k8s-spec-set") 69 c.Assert(err, jc.ErrorIsNil) 70 cmdtesting.TestInit(c, jujuc.NewJujucCommandWrappedForTest(com), t.args, t.err) 71 } 72 } 73 74 func (s *K8sSpecSetSuite) TestHelp(c *gc.C) { 75 hctx := s.GetHookContext(c, -1, "") 76 com, err := jujuc.NewCommand(hctx, "k8s-spec-set") 77 c.Assert(err, jc.ErrorIsNil) 78 ctx := cmdtesting.Context(c) 79 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--help"}) 80 c.Assert(code, gc.Equals, 0) 81 expectedHelp := "" + 82 "Usage: k8s-spec-set [options] --file <core spec file> [--k8s-resources <k8s spec file>]\n" + 83 "\n" + 84 "Summary:\n" + 85 "set k8s spec information\n" + 86 "\n" + 87 "Options:\n" + 88 "--file (= -)\n" + 89 " file containing pod spec\n" + 90 "--k8s-resources (= )\n" + 91 " file containing k8s specific resources not yet modelled by Juju\n" + 92 "\n" + 93 "Details:\n" + 94 "Sets configuration data to use for k8s resources.\n" + 95 "The spec applies to all units for the application.\n" 96 97 c.Assert(bufferString(ctx.Stdout), gc.Equals, expectedHelp) 98 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 99 } 100 101 func (s *K8sSpecSetSuite) TestK8sSpecSetNoData(c *gc.C) { 102 hctx := s.GetHookContext(c, -1, "") 103 com, err := jujuc.NewCommand(hctx, "k8s-spec-set") 104 c.Assert(err, jc.ErrorIsNil) 105 ctx := cmdtesting.Context(c) 106 107 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, nil) 108 c.Check(code, gc.Equals, 1) 109 c.Assert(bufferString( 110 ctx.Stderr), gc.Matches, 111 ".*no k8s spec specified: pipe k8s spec to command, or specify a file with --file\n") 112 c.Assert(bufferString(ctx.Stdout), gc.Equals, "") 113 } 114 115 func (s *K8sSpecSetSuite) TestK8sSpecSet(c *gc.C) { 116 s.assertK8sSpecSet(c, "specfile.yaml", false) 117 } 118 119 func (s *K8sSpecSetSuite) TestK8sSpecSetStdIn(c *gc.C) { 120 s.assertK8sSpecSet(c, "-", false) 121 } 122 123 func (s *K8sSpecSetSuite) TestK8sSpecSetWithK8sResource(c *gc.C) { 124 s.assertK8sSpecSet(c, "specfile.yaml", true) 125 } 126 127 func (s *K8sSpecSetSuite) TestK8sSpecSetStdInWithK8sResource(c *gc.C) { 128 s.assertK8sSpecSet(c, "-", true) 129 } 130 131 func (s *K8sSpecSetSuite) assertK8sSpecSet(c *gc.C, filename string, withK8sResource bool) { 132 hctx := s.GetHookContext(c, -1, "") 133 com, args, ctx := s.initCommand(c, hctx, podSpecYaml, filename, withK8sResource) 134 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, args) 135 c.Check(code, gc.Equals, 0) 136 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 137 c.Assert(bufferString(ctx.Stdout), gc.Equals, "") 138 expectedSpecYaml := podSpecYaml 139 if withK8sResource { 140 expectedSpecYaml += k8sResourcesYaml 141 } 142 c.Assert(hctx.info.K8sSpec, gc.Equals, expectedSpecYaml) 143 } 144 145 func (s *K8sSpecSetSuite) initCommand( 146 c *gc.C, hctx jujuc.Context, yaml string, filename string, withK8sResource bool, 147 ) (cmd.Command, []string, *cmd.Context) { 148 com, err := jujuc.NewCommand(hctx, "k8s-spec-set") 149 c.Assert(err, jc.ErrorIsNil) 150 ctx := cmdtesting.Context(c) 151 152 var args []string 153 if filename == "-" { 154 ctx.Stdin = bytes.NewBufferString(yaml) 155 } else if filename != "" { 156 filename = filepath.Join(c.MkDir(), filename) 157 err := os.WriteFile(filename, []byte(yaml), 0644) 158 c.Assert(err, jc.ErrorIsNil) 159 args = append(args, "--file", filename) 160 } 161 if withK8sResource { 162 k8sResourceFileName := "k8sresources.yaml" 163 k8sResourceFileName = filepath.Join(c.MkDir(), k8sResourceFileName) 164 err := os.WriteFile(k8sResourceFileName, []byte(k8sResourcesYaml), 0644) 165 c.Assert(err, jc.ErrorIsNil) 166 args = append(args, "--k8s-resources", k8sResourceFileName) 167 } 168 return jujuc.NewJujucCommandWrappedForTest(com), args, ctx 169 }