File: cloudcredential_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 (163 lines) | stat: -rw-r--r-- 4,606 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2016 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 cloudCredentialSuite struct{}

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

func (s *cloudCredentialSuite) TestCloudCredentialTag(c *gc.C) {
	for i, t := range []struct {
		input  string
		string string
		cloud  names.CloudTag
		owner  names.UserTag
		name   string
	}{
		{
			input:  "aws/bob/foo",
			string: "cloudcred-aws_bob_foo",
			cloud:  names.NewCloudTag("aws"),
			owner:  names.NewUserTag("bob"),
			name:   "foo",
		}, {
			input:  "manual_cloud/bob/foo",
			string: "cloudcred-manual%5fcloud_bob_foo",
			cloud:  names.NewCloudTag("manual_cloud"),
			owner:  names.NewUserTag("bob"),
			name:   "foo",
		}, {
			input:  "aws/bob@remote/foo",
			string: "cloudcred-aws_bob@remote_foo",
			cloud:  names.NewCloudTag("aws"),
			owner:  names.NewUserTag("bob@remote"),
			name:   "foo",
		}, {
			input:  "aws/bob@remote/foo@somewhere.com",
			string: "cloudcred-aws_bob@remote_foo@somewhere.com",
			cloud:  names.NewCloudTag("aws"),
			owner:  names.NewUserTag("bob@remote"),
			name:   "foo@somewhere.com",
		}, {
			input:  "aws/bob@remote/foo_bar",
			string: `cloudcred-aws_bob@remote_foo%5fbar`,
			cloud:  names.NewCloudTag("aws"),
			owner:  names.NewUserTag("bob@remote"),
			name:   "foo_bar",
		}, {
			input:  "google/bob+bob@remote/foo_bar",
			string: `cloudcred-google_bob+bob@remote_foo%5fbar`,
			cloud:  names.NewCloudTag("google"),
			owner:  names.NewUserTag("bob+bob@remote"),
			name:   "foo_bar",
		},
	} {
		c.Logf("test %d: %s", i, t.input)
		cloudTag := names.NewCloudCredentialTag(t.input)
		c.Check(cloudTag.String(), gc.Equals, t.string)
		c.Check(cloudTag.Id(), gc.Equals, t.input)
		c.Check(cloudTag.Cloud(), gc.Equals, t.cloud)
		c.Check(cloudTag.Owner(), gc.Equals, t.owner)
		c.Check(cloudTag.Name(), gc.Equals, t.name)
	}
}

func (s *cloudCredentialSuite) TestIsValidCloudCredential(c *gc.C) {
	for i, t := range []struct {
		string string
		expect bool
	}{
		{"", false},
		{"aws/bob/foo", true},
		{"manual_cloud/bob/foo", true},
		{"aws/bob@local/foo", true},
		{"google/bob+bob@local/foo", true},
		{"/bob/foo", false},
		{"aws//foo", false},
		{"aws/bob/", false},
	} {
		c.Logf("test %d: %s", i, t.string)
		c.Assert(names.IsValidCloudCredential(t.string), gc.Equals, t.expect, gc.Commentf("%s", t.string))
	}
}

func (s *cloudCredentialSuite) TestIsValidCloudCredentialName(c *gc.C) {
	for i, t := range []struct {
		string string
		expect bool
	}{
		{"", false},
		{"foo", true},
		{"f00b4r", true},
		{"foo-bar", true},
		{"foo@bar", true},
		{"foo+foo@bar", true},
		{"foo_bar", true},
		{"123", false},
		{"0foo", false},
	} {
		c.Logf("test %d: %s", i, t.string)
		c.Check(names.IsValidCloudCredentialName(t.string), gc.Equals, t.expect, gc.Commentf("%s", t.string))
	}
}

func (s *cloudCredentialSuite) TestParseCloudCredentialTag(c *gc.C) {
	for i, t := range []struct {
		tag      string
		expected names.Tag
		err      error
	}{{
		tag: "",
		err: names.InvalidTagError("", ""),
	}, {
		tag:      "cloudcred-aws_bob_foo",
		expected: names.NewCloudCredentialTag("aws/bob/foo"),
	}, {
		tag:      "cloudcred-manual%5fcloud_bob_foo",
		expected: names.NewCloudCredentialTag("manual_cloud/bob/foo"),
	}, {
		tag:      "cloudcred-aws-china_bob_foo-manchu",
		expected: names.NewCloudCredentialTag("aws-china/bob/foo-manchu"),
	}, {
		tag:      "cloudcred-aws-china_bob_foo@somewhere.com",
		expected: names.NewCloudCredentialTag("aws-china/bob/foo@somewhere.com"),
	}, {
		tag:      `cloudcred-aws-china_bob_foo%5fbar`,
		expected: names.NewCloudCredentialTag("aws-china/bob/foo_bar"),
	}, {
		tag: "foo",
		err: names.InvalidTagError("foo", ""),
	}, {
		tag: "unit-aws",
		err: names.InvalidTagError("unit-aws", names.UnitTagKind), // not a valid unit name either
	}} {
		c.Logf("test %d: %s", i, t.tag)
		got, err := names.ParseCloudCredentialTag(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)
	}
}

func (s *cloudCredentialSuite) TestIsZero(c *gc.C) {
	c.Assert(names.CloudCredentialTag{}.IsZero(), gc.Equals, true)
	c.Assert(names.NewCloudCredentialTag("aws/bob/foo").IsZero(), gc.Equals, false)
}

func (s *cloudCredentialSuite) TestZeroString(c *gc.C) {
	c.Assert(names.CloudCredentialTag{}.String(), gc.Equals, "")
}

func (s *cloudCredentialSuite) TestZeroId(c *gc.C) {
	c.Assert(names.CloudCredentialTag{}.Id(), gc.Equals, "")
}