File: action_test.go

package info (click to toggle)
golang-github-juju-names 4.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 340 kB
  • sloc: makefile: 14
file content (88 lines) | stat: -rw-r--r-- 2,432 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package names_test

import (
	jc "github.com/juju/testing/checkers"
	gc "gopkg.in/check.v1"

	"github.com/juju/names/v4"
)

type actionSuite struct{}

var _ = gc.Suite(&actionSuite{})

var parseActionTagTests = []struct {
	tag      string
	expected names.Tag
	err      error
}{
	{tag: "", err: names.InvalidTagError("", "")},
	{tag: "action-f47ac10b-58cc-4372-a567-0e02b2c3d479", expected: names.NewActionTag("f47ac10b-58cc-4372-a567-0e02b2c3d479")},
	{tag: "action-1", expected: names.NewActionTag("1")},
	{tag: "action-foo", err: names.InvalidTagError("action-foo", "action")},
	{tag: "bob", err: names.InvalidTagError("bob", "")},
	{tag: "application-ned", err: names.InvalidTagError("application-ned", names.ActionTagKind)}}

func (s *actionSuite) TestParseActionTag(c *gc.C) {
	for i, t := range parseActionTagTests {
		c.Logf("test %d: %s", i, t.tag)
		got, err := names.ParseActionTag(t.tag)
		if t.err != nil {
			c.Check(err, gc.DeepEquals, t.err)
			continue
		}
		c.Check(err, jc.ErrorIsNil)
		c.Check(got, gc.FitsTypeOf, t.expected)
		c.Check(got, gc.Equals, t.expected)
	}
}

func (s *actionSuite) TestActionReceiverTag(c *gc.C) {
	testCases := []struct {
		name     string
		expected names.Tag
		err      string
	}{
		{name: "mysql", err: `invalid actionreceiver name "mysql"`},
		{name: "mysql/3", expected: names.NewUnitTag("mysql/3")},
		{name: "3", expected: names.NewMachineTag("3")},
	}

	for _, tcase := range testCases {
		tag, err := names.ActionReceiverTag(tcase.name)
		if tcase.err != "" {
			c.Check(err, gc.ErrorMatches, tcase.err)
			c.Check(tag, gc.IsNil)
			continue
		}
		c.Check(err, jc.ErrorIsNil)
		c.Check(tag, gc.FitsTypeOf, tcase.expected)
		c.Check(tag, gc.Equals, tcase.expected)
	}

}

func (s *actionSuite) TestActionReceiverFromTag(c *gc.C) {
	for i, test := range []struct {
		name     string
		expected names.Tag
		err      string
	}{
		{name: "rambleon", err: `invalid actionreceiver tag "rambleon"`},
		{name: "unit-mysql-2", expected: names.NewUnitTag("mysql/2")},
		{name: "machine-13", expected: names.NewMachineTag("13")},
	} {
		c.Logf("test %d", i)
		tag, err := names.ActionReceiverFromTag(test.name)
		if test.err != "" {
			c.Check(err, gc.ErrorMatches, test.err)
			c.Check(tag, gc.IsNil)
			continue
		}
		c.Check(tag, gc.Equals, test.expected)
		c.Check(err, jc.ErrorIsNil)
	}
}