github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/hook/hook_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package hook_test
     5  
     6  import (
     7  	"github.com/juju/charm/v12/hooks"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/testing"
    12  	"github.com/juju/juju/worker/uniter/hook"
    13  )
    14  
    15  type InfoSuite struct {
    16  	testing.BaseSuite
    17  }
    18  
    19  var _ = gc.Suite(&InfoSuite{})
    20  
    21  var validateTests = []struct {
    22  	info hook.Info
    23  	err  string
    24  }{
    25  	{
    26  		hook.Info{Kind: hooks.RelationJoined},
    27  		`"relation-joined" hook requires a remote unit`,
    28  	}, {
    29  		hook.Info{Kind: hooks.RelationJoined, RemoteUnit: "foo/0"},
    30  		`"relation-joined" hook has a remote unit but no application`,
    31  	}, {
    32  		hook.Info{Kind: hooks.RelationChanged},
    33  		`"relation-changed" hook requires a remote unit or application`,
    34  	}, {
    35  		hook.Info{Kind: hooks.RelationChanged, RemoteUnit: "foo/0"},
    36  		`"relation-changed" hook has a remote unit but no application`,
    37  	}, {
    38  		hook.Info{Kind: hooks.RelationDeparted},
    39  		`"relation-departed" hook requires a remote unit`,
    40  	}, {
    41  		hook.Info{Kind: hooks.RelationDeparted, RemoteUnit: "foo/0"},
    42  		`"relation-departed" hook has a remote unit but no application`,
    43  	}, {
    44  		hook.Info{Kind: hooks.Kind("grok")},
    45  		`unknown hook kind "grok"`,
    46  	}, {
    47  		hook.Info{Kind: hooks.PebbleReady},
    48  		`"pebble-ready" hook requires a workload name`,
    49  	}, {
    50  		hook.Info{Kind: hooks.PebbleCustomNotice},
    51  		`"pebble-custom-notice" hook requires a workload name`,
    52  	}, {
    53  		hook.Info{Kind: hooks.PebbleCustomNotice, WorkloadName: "test"},
    54  		`"pebble-custom-notice" hook requires a notice ID, type, and key`,
    55  	}, {
    56  		hook.Info{Kind: hooks.PreSeriesUpgrade},
    57  		`"pre-series-upgrade" hook requires a target base`,
    58  	}, {
    59  		hook.Info{Kind: hooks.SecretRotate},
    60  		`"secret-rotate" hook requires a secret URI`,
    61  	}, {
    62  		hook.Info{Kind: hooks.SecretExpired, SecretURI: "secret:9m4e2mr0ui3e8a215n4g"},
    63  		`"secret-expired" hook requires a secret revision`,
    64  	}, {
    65  		hook.Info{Kind: hooks.SecretRotate, SecretURI: "foo"},
    66  		`invalid secret URI "foo"`,
    67  	},
    68  	{hook.Info{Kind: hooks.Install}, ""},
    69  	{hook.Info{Kind: hooks.Start}, ""},
    70  	{hook.Info{Kind: hooks.ConfigChanged}, ""},
    71  	{hook.Info{Kind: hooks.CollectMetrics}, ""},
    72  	{hook.Info{Kind: hooks.MeterStatusChanged}, ""},
    73  	{hook.Info{Kind: hooks.Action}, "hooks.Kind Action is deprecated"},
    74  	{hook.Info{Kind: hooks.UpgradeCharm}, ""},
    75  	{hook.Info{Kind: hooks.Stop}, ""},
    76  	{hook.Info{Kind: hooks.Remove}, ""},
    77  	{hook.Info{Kind: hooks.RelationJoined, RemoteUnit: "x/0", RemoteApplication: "x"}, ""},
    78  	{hook.Info{Kind: hooks.RelationChanged, RemoteUnit: "x/0", RemoteApplication: "x"}, ""},
    79  	{hook.Info{Kind: hooks.RelationChanged, RemoteApplication: "x"}, ""},
    80  	{hook.Info{Kind: hooks.RelationDeparted, RemoteUnit: "x/0", RemoteApplication: "x"}, ""},
    81  	{hook.Info{Kind: hooks.RelationBroken}, ""},
    82  	{hook.Info{Kind: hooks.StorageAttached}, `invalid storage ID ""`},
    83  	{hook.Info{Kind: hooks.StorageAttached, StorageId: "data/0"}, ""},
    84  	{hook.Info{Kind: hooks.StorageDetaching, StorageId: "data/0"}, ""},
    85  	{hook.Info{Kind: hooks.PebbleReady, WorkloadName: "gitlab"}, ""},
    86  	{hook.Info{Kind: hooks.PreSeriesUpgrade, MachineUpgradeTarget: "ubuntu@20.04"}, ""},
    87  }
    88  
    89  func (s *InfoSuite) TestValidate(c *gc.C) {
    90  	for i, t := range validateTests {
    91  		c.Logf("test %d", i)
    92  		err := t.info.Validate()
    93  		if t.err == "" {
    94  			c.Assert(err, jc.ErrorIsNil)
    95  		} else {
    96  			c.Assert(err, gc.ErrorMatches, t.err)
    97  		}
    98  	}
    99  }