-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorigin-type_test.go
151 lines (119 loc) · 3.31 KB
/
origin-type_test.go
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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package logfwd_test
import (
"github.com/juju/errors"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/logfwd"
)
type OriginTypeSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&OriginTypeSuite{})
func (s *OriginTypeSuite) TestZeroValue(c *gc.C) {
var ot logfwd.OriginType
c.Check(ot, gc.Equals, logfwd.OriginTypeUnknown)
}
func (s *OriginTypeSuite) TestParseOriginTypeValid(c *gc.C) {
tests := map[string]logfwd.OriginType{
"unknown": logfwd.OriginTypeUnknown,
"user": logfwd.OriginTypeUser,
"machine": logfwd.OriginTypeMachine,
"unit": logfwd.OriginTypeUnit,
}
for str, expected := range tests {
c.Logf("trying %q", str)
ot, err := logfwd.ParseOriginType(str)
c.Assert(err, jc.ErrorIsNil)
c.Check(ot, gc.Equals, expected)
}
}
func (s *OriginTypeSuite) TestParseOriginTypeEmpty(c *gc.C) {
_, err := logfwd.ParseOriginType("")
c.Check(err, gc.ErrorMatches, `unrecognized origin type ""`)
}
func (s *OriginTypeSuite) TestParseOriginTypeInvalid(c *gc.C) {
_, err := logfwd.ParseOriginType("spam")
c.Check(err, gc.ErrorMatches, `unrecognized origin type "spam"`)
}
func (s *OriginTypeSuite) TestString(c *gc.C) {
tests := map[logfwd.OriginType]string{
logfwd.OriginTypeUnknown: "unknown",
logfwd.OriginTypeUser: "user",
logfwd.OriginTypeMachine: "machine",
logfwd.OriginTypeUnit: "unit",
}
for ot, expected := range tests {
c.Logf("trying %q", ot)
str := ot.String()
c.Check(str, gc.Equals, expected)
}
}
func (s *OriginTypeSuite) TestValidateValid(c *gc.C) {
tests := []logfwd.OriginType{
logfwd.OriginTypeUnknown,
logfwd.OriginTypeUser,
logfwd.OriginTypeMachine,
logfwd.OriginTypeUnit,
}
for _, ot := range tests {
c.Logf("trying %q", ot)
err := ot.Validate()
c.Check(err, jc.ErrorIsNil)
}
}
func (s *OriginTypeSuite) TestValidateZero(c *gc.C) {
var ot logfwd.OriginType
err := ot.Validate()
c.Check(err, jc.ErrorIsNil)
}
func (s *OriginTypeSuite) TestValidateInvalid(c *gc.C) {
ot := logfwd.OriginType(999)
err := ot.Validate()
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `unsupported origin type`)
}
func (s *OriginTypeSuite) TestValidateNameValid(c *gc.C) {
tests := map[logfwd.OriginType]string{
logfwd.OriginTypeUnknown: "",
logfwd.OriginTypeUser: "a-user",
logfwd.OriginTypeMachine: "99",
logfwd.OriginTypeUnit: "svc-a/0",
}
for ot, name := range tests {
c.Logf("trying %q + %q", ot, name)
err := ot.ValidateName(name)
c.Check(err, jc.ErrorIsNil)
}
}
func (s *OriginTypeSuite) TestValidateNameInvalid(c *gc.C) {
tests := []struct {
ot logfwd.OriginType
name string
err string
}{{
ot: logfwd.OriginTypeUnknown,
name: "...",
err: `origin name must not be set if type is unknown`,
}, {
ot: logfwd.OriginTypeUser,
name: "...",
err: `bad user name`,
}, {
ot: logfwd.OriginTypeMachine,
name: "...",
err: `bad machine name`,
}, {
ot: logfwd.OriginTypeUnit,
name: "...",
err: `bad unit name`,
}}
for _, test := range tests {
c.Logf("trying %q + %q", test.ot, test.name)
err := test.ot.ValidateName(test.name)
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, test.err)
}
}