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