forked from altessa-s/go-atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion_test.go
More file actions
157 lines (142 loc) · 4.06 KB
/
Copy pathversion_test.go
File metadata and controls
157 lines (142 loc) · 4.06 KB
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
// Copyright 2021-2026 ALTESSA SOLUTIONS INC. All rights reserved.
// Use of this source code is governed by license that can be found in
// the LICENSE file.
package plugins
import (
"bytes"
"errors"
"log/slog"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMajorOf(t *testing.T) {
t.Parallel()
tests := []struct {
name string
version string
want string
}{
{name: "empty", version: "", want: ""},
{name: "bare major", version: "1", want: "1"},
{name: "v-prefixed bare", version: "v2", want: "2"},
{name: "full semver", version: "1.2.3", want: "1"},
{name: "v-prefixed semver", version: "v3.4.5", want: "3"},
{name: "prerelease", version: "v2.0.0-rc.1", want: "2"},
{name: "build metadata", version: "3.0.0+build.5", want: "3"},
{name: "pseudo-version", version: "v0.0.0-20240101000000-abcdef", want: "0"},
{name: "non-numeric major preserved", version: "abc", want: "abc"},
{name: "leading dot", version: ".1.2", want: ""},
{name: "double v", version: "vv1", want: "v1"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.want, majorOf(tc.version))
})
}
}
func TestManager_checkHostMajor(t *testing.T) {
t.Parallel()
tests := []struct {
name string
mode HostVersionMode
hostVersion string
descVersion string
wantErr error
wantWarnLog bool
}{
{
name: "enforce match",
mode: HostVersionEnforce,
hostVersion: "2.5.3",
descVersion: "2.1.0",
},
{
name: "enforce mismatch",
mode: HostVersionEnforce,
hostVersion: "3.0.0",
descVersion: "2.0.0",
wantErr: ErrHostVersionMismatch,
},
{
name: "enforce v-prefix mismatch",
mode: HostVersionEnforce,
hostVersion: "v3.0.0",
descVersion: "v2.0.0-rc.1",
wantErr: ErrHostVersionMismatch,
},
{
name: "enforce host empty skips",
mode: HostVersionEnforce,
hostVersion: "",
descVersion: "2.0.0",
},
{
name: "enforce desc empty skips (backward compat)",
mode: HostVersionEnforce,
hostVersion: "2.0.0",
descVersion: "",
},
{
name: "warn match no log",
mode: HostVersionWarn,
hostVersion: "2.0.0",
descVersion: "2.5.0",
},
{
name: "warn mismatch logs and loads",
mode: HostVersionWarn,
hostVersion: "3.0.0",
descVersion: "2.0.0",
wantWarnLog: true,
},
{
name: "disabled mismatch ignored",
mode: HostVersionDisabled,
hostVersion: "3.0.0",
descVersion: "2.0.0",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
mgr := NewManager(
WithLogger(slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelWarn}))),
WithHostVersion(tc.hostVersion),
WithHostVersionMode(tc.mode),
)
t.Cleanup(func() { _ = mgr.Close() })
err := mgr.checkHostMajor(&Descriptor{Name: "p", HostVersion: tc.descVersion})
if tc.wantErr != nil {
require.Error(t, err)
assert.True(t, errors.Is(err, tc.wantErr), "want %v, got %v", tc.wantErr, err)
} else {
require.NoError(t, err)
}
if tc.wantWarnLog {
assert.Contains(t, buf.String(), "plugin host major version mismatch")
} else {
assert.NotContains(t, buf.String(), "plugin host major version mismatch")
}
})
}
}
func TestManager_checkHostMajor_ErrorMessageIncludesVersions(t *testing.T) {
t.Parallel()
mgr := NewManager(
WithHostVersion("3.1.0"),
WithHostVersionMode(HostVersionEnforce),
)
t.Cleanup(func() { _ = mgr.Close() })
err := mgr.checkHostMajor(&Descriptor{Name: "p", HostVersion: "2.0.0-rc.1"})
require.Error(t, err)
require.True(t, errors.Is(err, ErrHostVersionMismatch))
msg := err.Error()
assert.True(t, strings.Contains(msg, `"2"`) && strings.Contains(msg, `"3"`),
"error should report both majors, got: %s", msg)
assert.Contains(t, msg, "2.0.0-rc.1", "error should include full plugin version")
assert.Contains(t, msg, "3.1.0", "error should include full host version")
}