File: subnet_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 (66 lines) | stat: -rw-r--r-- 1,566 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
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package names_test

import (
	gc "gopkg.in/check.v1"

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

type subnetSuite struct{}

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

func (s *subnetSuite) TestNewSubnetTag(c *gc.C) {
	id := "16"
	tag := names.NewSubnetTag(id)
	parsed, err := names.ParseSubnetTag(tag.String())
	c.Assert(err, gc.IsNil)
	c.Assert(parsed.Kind(), gc.Equals, names.SubnetTagKind)
	c.Assert(parsed.Id(), gc.Equals, id)
	c.Assert(parsed.String(), gc.Equals, names.SubnetTagKind+"-"+id)

	f := func() {
		tag = names.NewSubnetTag("foo")
	}
	c.Assert(f, gc.PanicMatches, "foo is not a valid subnet ID")
}

var parseSubnetTagTests = []struct {
	tag      string
	expected names.Tag
	err      error
}{{
	tag: "",
	err: names.InvalidTagError("", ""),
}, {
	tag:      "subnet-16",
	expected: names.NewSubnetTag("16"),
}, {
	tag: "subnet-foo",
	err: names.InvalidTagError("subnet-foo", names.SubnetTagKind),
}, {
	tag: "subnet-",
	err: names.InvalidTagError("subnet-", names.SubnetTagKind),
}, {
	tag: "foobar",
	err: names.InvalidTagError("foobar", ""),
}, {
	tag: "unit-foo-0",
	err: names.InvalidTagError("unit-foo-0", names.SubnetTagKind),
}}

func (s *subnetSuite) TestParseSubnetTag(c *gc.C) {
	for i, t := range parseSubnetTagTests {
		c.Logf("test %d: %s", i, t.tag)
		got, err := names.ParseSubnetTag(t.tag)
		if err != nil || t.err != nil {
			c.Check(err, gc.DeepEquals, t.err)
			continue
		}
		c.Check(got, gc.FitsTypeOf, t.expected)
		c.Check(got, gc.Equals, t.expected)
	}
}