1 package version
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/http/httptest"
9 "testing"
10 "time"
11 )
12
13 func TestGetLatestVersions(t *testing.T) {
14 four := int64(4)
15 testCases := []struct {
16 name string
17 resp interface{}
18 err error
19 latest Channels
20 }{
21 {
22 "valid response",
23 map[string]string{
24 "foo": "foo-1.2.3",
25 "fooHotpatch": "foo-1.2.3-4",
26 "stable": "stable-2.1.0",
27 "edge": "edge-2.1.0",
28 },
29 nil,
30 Channels{
31 []channelVersion{
32 {"foo", "1.2.3", nil, "foo-1.2.3"},
33 {"foo", "1.2.3", &four, "foo-1.2.3-4"},
34 {"stable", "2.1.0", nil, "stable-2.1.0"},
35 {"edge", "2.1.0", nil, "edge-2.1.0"},
36 },
37 },
38 },
39 {
40 "channel version mismatch",
41 map[string]string{
42 "foo": "foo-1.2.3",
43 "stable": "stable-2.1.0",
44 "badchannel": "edge-2.1.0",
45 },
46 fmt.Errorf("unexpected versioncheck response: channel in edge-2.1.0 does not match badchannel"),
47 Channels{},
48 },
49 {
50 "invalid version",
51 map[string]string{
52 "foo": "foo-1.2.3",
53 "stable": "badchannelversion",
54 },
55 fmt.Errorf("unexpected versioncheck response: unsupported version format: badchannelversion"),
56 Channels{},
57 },
58 {
59 "invalid JSON",
60 "bad response",
61 fmt.Errorf("json: cannot unmarshal string into Go value of type map[string]string"),
62 Channels{},
63 },
64 }
65
66 for _, tc := range testCases {
67 tc := tc
68 t.Run(tc.name, func(t *testing.T) {
69 j, err := json.Marshal(tc.resp)
70 if err != nil {
71 t.Fatalf("JSON marshal failed with: %s", err)
72 }
73
74 ts := httptest.NewServer(http.HandlerFunc(
75 func(w http.ResponseWriter, r *http.Request) {
76 w.Write(j)
77 }),
78 )
79 defer ts.Close()
80
81 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
82 defer cancel()
83 latest, err := getLatestVersions(ctx, ts.Client(), ts.URL)
84 if (err == nil && tc.err != nil) ||
85 (err != nil && tc.err == nil) ||
86 ((err != nil && tc.err != nil) && (err.Error() != tc.err.Error())) {
87 t.Fatalf("Expected \"%s\", got \"%s\"", tc.err, err)
88 }
89
90 if !channelsEqual(latest, tc.latest) {
91 t.Fatalf("Expected latest versions \"%s\", got \"%s\"", tc.latest, latest)
92 }
93 })
94 }
95 }
96
97 func channelsEqual(c1, c2 Channels) bool {
98 if len(c1.array) != len(c2.array) {
99 return false
100 }
101
102 for _, cv1 := range c1.array {
103 found := false
104 for _, cv2 := range c2.array {
105 if cv1.channel == cv2.channel && cv1.version == cv2.version && cv1.hotpatchEqual(cv2) {
106 found = true
107 break
108 }
109 }
110 if !found {
111 return false
112 }
113 }
114
115 return true
116 }
117
118 func TestChannelsMatch(t *testing.T) {
119 four := int64(4)
120 channels := Channels{
121 []channelVersion{
122 {"stable", "2.1.0", nil, "stable-2.1.0"},
123 {"foo", "1.2.3", nil, "foo-1.2.3"},
124 {"foo", "1.2.3", &four, "foo-1.2.3-4"},
125 {"version", "3.2.1", nil, "version-3.2.1"},
126 },
127 }
128
129 testCases := []struct {
130 actualVersion string
131 err error
132 }{
133 {"stable-2.1.0", nil},
134 {"stable-2.1.0-buildinfo", nil},
135 {"foo-1.2.3", nil},
136 {"foo-1.2.3-4", nil},
137 {"foo-1.2.3-4-buildinfo", nil},
138 {"version-3.2.1", nil},
139 {
140 "foo-1.2.2",
141 fmt.Errorf("is running version 1.2.2 but the latest foo version is 1.2.3"),
142 },
143 {
144 "foo-1.2.3-3",
145 fmt.Errorf("is running version 1.2.3-3 but the latest foo version is 1.2.3-4"),
146 },
147 {
148 "unsupportedChannel-1.2.3",
149 fmt.Errorf("unsupported version channel: unsupportedChannel-1.2.3"),
150 },
151 }
152
153 for i, tc := range testCases {
154 tc := tc
155 t.Run(fmt.Sprintf("test %d ChannelsMatch(%s, %s)", i, tc.actualVersion, tc.err), func(t *testing.T) {
156 err := channels.Match(tc.actualVersion)
157 if (err == nil && tc.err != nil) ||
158 (err != nil && tc.err == nil) ||
159 ((err != nil && tc.err != nil) && (err.Error() != tc.err.Error())) {
160 t.Fatalf("Expected \"%s\", got \"%s\"", tc.err, err)
161 }
162 })
163 }
164 }
165
View as plain text