-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
107 lines (92 loc) · 1.96 KB
/
main_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
package main
import (
"encoding/json"
"github.com/Masterminds/semver/v3"
"github.com/pmoscode/helm-chart-update-check/pkg/cli"
"github.com/pmoscode/helm-chart-update-check/pkg/dockerhub"
"net/http"
"net/http/httptest"
"testing"
)
type Tests struct {
name string
server *httptest.Server
expectedResult []string
}
func TestCheckVersionNormal(t *testing.T) {
innerTest(t, "")
}
func TestCheckVersionWithExcludesSimple(t *testing.T) {
innerTest(t, "1.2.3")
}
func TestCheckVersionWithExcludesRangeMajor(t *testing.T) {
innerTest(t, "^1.0.0-0")
}
func TestCheckVersionWithExcludesRangeMinor(t *testing.T) {
innerTest(t, "~3.3.0-0")
}
func innerTest(t *testing.T, excludeVersions string) {
debug := true
fail := false
cliOptions := &cli.Options{
Debug: &debug,
FailOnExistingUpdate: &fail,
ExcludeVersions: &excludeVersions,
}
test := Tests{
name: "complete",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(getTestData())
})),
}
defer test.server.Close()
hub := dockerhub.CreateDockerHubWithUri(test.server.URL, *cliOptions.Debug)
dockerVersions := hub.GetVersions()
chartVersion, _ := semver.NewVersion("v1.5.0")
_, err := checkVersion(chartVersion, dockerVersions, cliOptions)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
}
func getTestData() []byte {
var serverResponseBody = &dockerhub.ResponseBody{
Results: []dockerhub.Results{
{
Name: "1.2.3",
},
{
Name: "v1.2.3",
},
{
Name: "1.2.3-pre.1",
},
{
Name: "1.2.3-dev",
},
{
Name: "v1.5.0-rc",
},
{
Name: "v1.5.0-rc1",
},
{
Name: "v1.5.0",
},
{
Name: "v1.5.0-nightly.1",
},
{
Name: "v2.2.0",
},
{
Name: "v3.2.1",
},
{
Name: "v3.3.4",
},
},
}
var serverResponseBodyString, _ = json.Marshal(serverResponseBody)
return serverResponseBodyString
}