@@ -3,6 +3,7 @@ package proposal
33import (
44 "context"
55 "fmt"
6+ "strings"
67 "testing"
78
89 "github.com/google/go-cmp/cmp"
@@ -73,3 +74,121 @@ func TestController_Sync(t *testing.T) {
7374 })
7475 }
7576}
77+
78+ func TestClassifyUpdate (t * testing.T ) {
79+ tests := []struct {
80+ name string
81+ current string
82+ target string
83+ expected string
84+ }{
85+ {name : "z-stream" , current : "4.15.1" , target : "4.15.3" , expected : "z-stream" },
86+ {name : "minor" , current : "4.15.1" , target : "4.16.0" , expected : "minor" },
87+ {name : "major" , current : "4.15.1" , target : "5.0.0" , expected : "minor" },
88+ {name : "invalid current" , current : "bad" , target : "4.15.0" , expected : "unknown" },
89+ {name : "invalid target" , current : "4.15.0" , target : "bad" , expected : "unknown" },
90+ }
91+
92+ for _ , tt := range tests {
93+ t .Run (tt .name , func (t * testing.T ) {
94+ got := classifyUpdate (tt .current , tt .target )
95+ if got != tt .expected {
96+ t .Errorf ("classifyUpdate(%q, %q) = %q, want %q" , tt .current , tt .target , got , tt .expected )
97+ }
98+ })
99+ }
100+ }
101+
102+ func TestProposalName (t * testing.T ) {
103+ tests := []struct {
104+ current string
105+ target string
106+ expected string
107+ }{
108+ {"4.15.1" , "4.15.3" , "ota-4-15-1-to-4-15-3" },
109+ {"4.15.1" , "4.16.0" , "ota-4-15-1-to-4-16-0" },
110+ }
111+
112+ for _ , tt := range tests {
113+ t .Run (tt .current + "->" + tt .target , func (t * testing.T ) {
114+ got := proposalName (tt .current , tt .target )
115+ if got != tt .expected {
116+ t .Errorf ("proposalName(%q, %q) = %q, want %q" , tt .current , tt .target , got , tt .expected )
117+ }
118+ })
119+ }
120+ }
121+
122+ func TestSanitize (t * testing.T ) {
123+ tests := []struct {
124+ input string
125+ expected string
126+ }{
127+ {"4.15.1" , "4-15-1" },
128+ {"Hello World" , "hello-world" },
129+ {"a-very-long-version-string-that-is-too-long" , "a-very-long-version" },
130+ {"trailing-dot." , "trailing-dot" },
131+ {"trailing-dash-" , "trailing-dash" },
132+ }
133+
134+ for _ , tt := range tests {
135+ t .Run (tt .input , func (t * testing.T ) {
136+ got := sanitize (tt .input )
137+ if got != tt .expected {
138+ t .Errorf ("sanitize(%q) = %q, want %q" , tt .input , got , tt .expected )
139+ }
140+ })
141+ }
142+ }
143+
144+ func TestBuildRequest (t * testing.T ) {
145+ updates := []configv1.Release {
146+ {Version : "4.16.0" , URL : "https://example.com/errata/1" },
147+ {Version : "4.16.1" , URL : "https://example.com/errata/2" },
148+ }
149+
150+ t .Run ("recommended target" , func (t * testing.T ) {
151+ request := buildRequest ("" , "4.15.3" , "4.16.0" , "stable-4.16" , "minor" , "recommended" , updates , "" )
152+ if ! strings .Contains (request , "Current version: OCP 4.15.3" ) {
153+ t .Error ("request should contain current version" )
154+ }
155+ if ! strings .Contains (request , "Target version: OCP 4.16.0" ) {
156+ t .Error ("request should contain target version" )
157+ }
158+ if ! strings .Contains (request , "Update type: minor" ) {
159+ t .Error ("request should contain update type" )
160+ }
161+ if ! strings .Contains (request , "Update path: recommended" ) {
162+ t .Error ("request should contain update path" )
163+ }
164+ if strings .Contains (request , "WARNING" ) {
165+ t .Error ("recommended target should not have warning" )
166+ }
167+ if ! strings .Contains (request , "Other recommended versions available:" ) {
168+ t .Error ("should list other versions when more than one update" )
169+ }
170+ if ! strings .Contains (request , "4.16.1" ) {
171+ t .Error ("should list alternative version" )
172+ }
173+ })
174+
175+ t .Run ("conditional target" , func (t * testing.T ) {
176+ request := buildRequest ("" , "4.15.3" , "4.16.0" , "stable-4.16" , "minor" , "conditional" , updates , "" )
177+ if ! strings .Contains (request , "WARNING" ) {
178+ t .Error ("conditional target should have warning" )
179+ }
180+ if ! strings .Contains (request , "CONDITIONAL update" ) {
181+ t .Error ("conditional target should mention CONDITIONAL" )
182+ }
183+ })
184+
185+ t .Run ("readiness JSON embedded" , func (t * testing.T ) {
186+ request := buildRequest ("" , "4.15.3" , "4.16.0" , "stable-4.16" , "minor" , "recommended" , updates , `{"checks":{},"meta":{}}` )
187+ if ! strings .Contains (request , "## Cluster Readiness Data" ) {
188+ t .Error ("request should contain readiness data header" )
189+ }
190+ if ! strings .Contains (request , `{"checks":{},"meta":{}}` ) {
191+ t .Error ("request should contain readiness JSON" )
192+ }
193+ })
194+ }
0 commit comments