github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/runner/jujuc/secret-info-get_test.go (about)

     1  // Copyright 2022 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc_test
     5  
     6  import (
     7  	"github.com/juju/cmd/v3"
     8  	"github.com/juju/cmd/v3/cmdtesting"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/core/secrets"
    13  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    14  )
    15  
    16  type SecretInfoGetSuite struct {
    17  	ContextSuite
    18  }
    19  
    20  var _ = gc.Suite(&SecretInfoGetSuite{})
    21  
    22  func (s *SecretInfoGetSuite) TestSecretGetInit(c *gc.C) {
    23  
    24  	for _, t := range []struct {
    25  		args []string
    26  		err  string
    27  	}{{
    28  		args: []string{},
    29  		err:  "ERROR require either a secret URI or label",
    30  	}, {
    31  		args: []string{"secret:9m4e2mr0ui3e8a215n4g", "--label", "foo"},
    32  		err:  "ERROR specify either a secret URI or label but not both",
    33  	}} {
    34  		hctx, _ := s.ContextSuite.NewHookContext()
    35  		com, err := jujuc.NewCommand(hctx, "secret-info-get")
    36  		c.Assert(err, jc.ErrorIsNil)
    37  		ctx := cmdtesting.Context(c)
    38  		code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, t.args)
    39  		c.Check(code, gc.Equals, 2)
    40  		c.Check(bufferString(ctx.Stderr), gc.Equals, t.err+"\n")
    41  	}
    42  }
    43  
    44  func (s *SecretInfoGetSuite) TestSecretInfoGetURI(c *gc.C) {
    45  	hctx, _ := s.ContextSuite.NewHookContext()
    46  
    47  	com, err := jujuc.NewCommand(hctx, "secret-info-get")
    48  	c.Assert(err, jc.ErrorIsNil)
    49  	ctx := cmdtesting.Context(c)
    50  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"secret:9m4e2mr0ui3e8a215n4g"})
    51  	c.Assert(code, gc.Equals, 0)
    52  
    53  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    54  	c.Assert(bufferString(ctx.Stdout), gc.Equals, `
    55  9m4e2mr0ui3e8a215n4g:
    56    revision: 666
    57    label: label
    58    owner: application
    59    description: description
    60    rotation: hourly
    61  `[1:])
    62  }
    63  
    64  func (s *SecretInfoGetSuite) TestSecretInfoGetWithGrants(c *gc.C) {
    65  	hctx, _ := s.ContextSuite.NewHookContext()
    66  	hctx.ContextSecrets.Access = []secrets.AccessInfo{
    67  		{
    68  			Target: "application-gitlab",
    69  			Scope:  "relation-key",
    70  			Role:   secrets.RoleView,
    71  		},
    72  	}
    73  
    74  	com, err := jujuc.NewCommand(hctx, "secret-info-get")
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	ctx := cmdtesting.Context(c)
    77  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"secret:9m4e2mr0ui3e8a215n4g"})
    78  	c.Assert(code, gc.Equals, 0)
    79  
    80  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    81  	c.Assert(bufferString(ctx.Stdout), gc.Equals, `
    82  9m4e2mr0ui3e8a215n4g:
    83    revision: 666
    84    label: label
    85    owner: application
    86    description: description
    87    rotation: hourly
    88    access:
    89    - target: application-gitlab
    90      scope: relation-key
    91      role: view
    92  `[1:])
    93  }
    94  
    95  func (s *SecretInfoGetSuite) TestSecretInfoGetFailedNotFound(c *gc.C) {
    96  	hctx, _ := s.ContextSuite.NewHookContext()
    97  
    98  	com, err := jujuc.NewCommand(hctx, "secret-info-get")
    99  	c.Assert(err, jc.ErrorIsNil)
   100  	ctx := cmdtesting.Context(c)
   101  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"secret:cd88u16ffbaql5kgmlh0"})
   102  	c.Assert(code, gc.Equals, 1)
   103  
   104  	c.Assert(bufferString(ctx.Stderr), gc.Matches, `ERROR secret "cd88u16ffbaql5kgmlh0" not found\n`)
   105  	c.Assert(bufferString(ctx.Stdout), gc.Equals, ``)
   106  }
   107  
   108  func (s *SecretInfoGetSuite) TestSecretInfoGetByLabelFailedNotFound(c *gc.C) {
   109  	hctx, _ := s.ContextSuite.NewHookContext()
   110  
   111  	com, err := jujuc.NewCommand(hctx, "secret-info-get")
   112  	c.Assert(err, jc.ErrorIsNil)
   113  	ctx := cmdtesting.Context(c)
   114  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--label", "not-found-label"})
   115  	c.Assert(code, gc.Equals, 1)
   116  
   117  	c.Assert(bufferString(ctx.Stderr), gc.Matches, `ERROR secret "not-found-label" not found\n`)
   118  	c.Assert(bufferString(ctx.Stdout), gc.Equals, ``)
   119  }
   120  
   121  func (s *SecretInfoGetSuite) TestSecretInfoGetByLabel(c *gc.C) {
   122  	hctx, _ := s.ContextSuite.NewHookContext()
   123  
   124  	com, err := jujuc.NewCommand(hctx, "secret-info-get")
   125  	c.Assert(err, jc.ErrorIsNil)
   126  	ctx := cmdtesting.Context(c)
   127  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--label", "label"})
   128  	c.Assert(code, gc.Equals, 0)
   129  
   130  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   131  	c.Assert(bufferString(ctx.Stdout), gc.Equals, `
   132  9m4e2mr0ui3e8a215n4g:
   133    revision: 666
   134    label: label
   135    owner: application
   136    description: description
   137    rotation: hourly
   138  `[1:])
   139  }