master
go 160 lines 4.38 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package dockerhub
4
5 import (
6 "context"
7 "net/http"
8 "net/http/httptest"
9 "os"
10 "strings"
11 "testing"
12
13 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest"
14
15 "github.com/stretchr/testify/assert"
16 "github.com/stretchr/testify/require"
17 )
18
19 var (
20 dataConfigJSON, _ = os.ReadFile("testdata/config.json")
21 dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
22
23 dataRepo1, _ = os.ReadFile("testdata/repo1.txt")
24 dataRepo2, _ = os.ReadFile("testdata/repo2.txt")
25 dataRepo3, _ = os.ReadFile("testdata/repo3.txt")
26 )
27
28 func Test_testDataIsValid(t *testing.T) {
29 for name, data := range map[string][]byte{
30 "dataConfigJSON": dataConfigJSON,
31 "dataConfigYAML": dataConfigYAML,
32 "dataRepo1": dataRepo1,
33 "dataRepo2": dataRepo2,
34 "dataRepo3": dataRepo3,
35 } {
36 require.NotNil(t, data, name)
37 }
38 }
39
40 func TestCollector_ConfigurationSerialize(t *testing.T) {
41 collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML)
42 }
43
44 func TestCollector_Charts(t *testing.T) { assert.NotNil(t, New().Charts()) }
45
46 func TestCollector_Cleanup(t *testing.T) { New().Cleanup(context.Background()) }
47
48 func TestCollector_Init(t *testing.T) {
49 collr := New()
50 collr.Repositories = []string{"name/repo"}
51 assert.NoError(t, collr.Init(context.Background()))
52 assert.NotNil(t, collr.client)
53 }
54
55 func TestCollector_InitNG(t *testing.T) {
56 assert.Error(t, New().Init(context.Background()))
57 }
58
59 func TestCollector_Check(t *testing.T) {
60 ts := httptest.NewServer(
61 http.HandlerFunc(
62 func(w http.ResponseWriter, r *http.Request) {
63 switch {
64 case strings.HasSuffix(r.URL.Path, "name1/repo1"):
65 _, _ = w.Write(dataRepo1)
66 case strings.HasSuffix(r.URL.Path, "name2/repo2"):
67 _, _ = w.Write(dataRepo2)
68 case strings.HasSuffix(r.URL.Path, "name3/repo3"):
69 _, _ = w.Write(dataRepo3)
70 }
71 }))
72 defer ts.Close()
73
74 collr := New()
75 collr.URL = ts.URL
76 collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"}
77 require.NoError(t, collr.Init(context.Background()))
78 assert.NoError(t, collr.Check(context.Background()))
79 }
80
81 func TestCollector_CheckNG(t *testing.T) {
82 collr := New()
83 collr.URL = "http://127.0.0.1:38001/metrics"
84 collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"}
85 require.NoError(t, collr.Init(context.Background()))
86 assert.Error(t, collr.Check(context.Background()))
87 }
88
89 func TestCollector_Collect(t *testing.T) {
90 ts := httptest.NewServer(
91 http.HandlerFunc(
92 func(w http.ResponseWriter, r *http.Request) {
93 switch {
94 case strings.HasSuffix(r.URL.Path, "name1/repo1"):
95 _, _ = w.Write(dataRepo1)
96 case strings.HasSuffix(r.URL.Path, "name2/repo2"):
97 _, _ = w.Write(dataRepo2)
98 case strings.HasSuffix(r.URL.Path, "name3/repo3"):
99 _, _ = w.Write(dataRepo3)
100 }
101 }))
102 defer ts.Close()
103
104 collr := New()
105 collr.URL = ts.URL
106 collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"}
107 require.NoError(t, collr.Init(context.Background()))
108 require.NoError(t, collr.Check(context.Background()))
109
110 expected := map[string]int64{
111 "star_count_user1/name1": 45,
112 "pull_count_user1/name1": 18540191,
113 "status_user1/name1": 1,
114 "star_count_user2/name2": 45,
115 "pull_count_user2/name2": 18540192,
116 "status_user2/name2": 1,
117 "star_count_user3/name3": 45,
118 "pull_count_user3/name3": 18540193,
119 "status_user3/name3": 1,
120 "pull_sum": 55620576,
121 }
122
123 collected := collr.Collect(context.Background())
124
125 for k := range collected {
126 if strings.HasPrefix(k, "last") {
127 delete(collected, k)
128 }
129 }
130 assert.Equal(t, expected, collected)
131 }
132
133 func TestCollector_InvalidData(t *testing.T) {
134 ts := httptest.NewServer(
135 http.HandlerFunc(
136 func(w http.ResponseWriter, r *http.Request) {
137 _, _ = w.Write([]byte("hello and goodbye"))
138 }))
139 defer ts.Close()
140
141 collr := New()
142 collr.URL = ts.URL
143 collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"}
144 require.NoError(t, collr.Init(context.Background()))
145 assert.Error(t, collr.Check(context.Background()))
146 }
147
148 func TestCollector_404(t *testing.T) {
149 ts := httptest.NewServer(
150 http.HandlerFunc(
151 func(w http.ResponseWriter, r *http.Request) {
152 w.WriteHeader(http.StatusNotFound)
153 }))
154 defer ts.Close()
155
156 collr := New()
157 collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"}
158 require.NoError(t, collr.Init(context.Background()))
159 assert.Error(t, collr.Check(context.Background()))
160 }