github.com/letsencrypt/boulder@v0.20251208.0/observer/probers/crl/crl_conf_test.go (about) 1 package probers 2 3 import ( 4 "testing" 5 6 "github.com/prometheus/client_golang/prometheus" 7 "gopkg.in/yaml.v3" 8 9 "github.com/letsencrypt/boulder/observer/probers" 10 "github.com/letsencrypt/boulder/test" 11 ) 12 13 func TestCRLConf_MakeProber(t *testing.T) { 14 conf := CRLConf{} 15 colls := conf.Instrument() 16 badColl := prometheus.Collector(prometheus.NewGaugeVec( 17 prometheus.GaugeOpts{ 18 Name: "obs_crl_foo", 19 Help: "Hmmm, this shouldn't be here...", 20 }, 21 []string{}, 22 )) 23 type fields struct { 24 URL string 25 } 26 tests := []struct { 27 name string 28 fields fields 29 colls map[string]prometheus.Collector 30 wantErr bool 31 }{ 32 // valid 33 {"valid fqdn", fields{"http://example.com"}, colls, false}, 34 {"valid fqdn with path", fields{"http://example.com/foo/bar"}, colls, false}, 35 {"valid hostname", fields{"http://example"}, colls, false}, 36 // invalid 37 {"bad fqdn", fields{":::::"}, colls, true}, 38 {"missing scheme", fields{"example.com"}, colls, true}, 39 { 40 "unexpected collector", 41 fields{"http://example.com"}, 42 map[string]prometheus.Collector{"obs_crl_foo": badColl}, 43 true, 44 }, 45 { 46 "missing collectors", 47 fields{"http://example.com"}, 48 map[string]prometheus.Collector{}, 49 true, 50 }, 51 } 52 for _, tt := range tests { 53 t.Run(tt.name, func(t *testing.T) { 54 c := CRLConf{ 55 URL: tt.fields.URL, 56 } 57 p, err := c.MakeProber(tt.colls) 58 if tt.wantErr { 59 test.AssertError(t, err, "CRLConf.MakeProber()") 60 } else { 61 test.AssertNotError(t, err, "CRLConf.MakeProber()") 62 63 test.AssertNotNil(t, p, "CRLConf.MakeProber(): nil prober") 64 prober := p.(CRLProbe) 65 test.AssertNotNil(t, prober.cThisUpdate, "CRLConf.MakeProber(): nil cThisUpdate") 66 test.AssertNotNil(t, prober.cNextUpdate, "CRLConf.MakeProber(): nil cNextUpdate") 67 test.AssertNotNil(t, prober.cCertCount, "CRLConf.MakeProber(): nil cCertCount") 68 } 69 }) 70 } 71 } 72 73 func TestCRLConf_UnmarshalSettings(t *testing.T) { 74 tests := []struct { 75 name string 76 fields probers.Settings 77 want probers.Configurer 78 wantErr bool 79 }{ 80 {"valid", probers.Settings{"url": "google.com"}, CRLConf{"google.com", false}, false}, 81 {"valid with partitioned", probers.Settings{"url": "google.com", "partitioned": true}, CRLConf{"google.com", true}, false}, 82 {"invalid (map)", probers.Settings{"url": make(map[string]any)}, nil, true}, 83 {"invalid (list)", probers.Settings{"url": make([]string, 0)}, nil, true}, 84 } 85 for _, tt := range tests { 86 t.Run(tt.name, func(t *testing.T) { 87 settingsBytes, _ := yaml.Marshal(tt.fields) 88 t.Log(string(settingsBytes)) 89 c := CRLConf{} 90 got, err := c.UnmarshalSettings(settingsBytes) 91 if tt.wantErr { 92 test.AssertError(t, err, "CRLConf.UnmarshalSettings()") 93 } else { 94 test.AssertNotError(t, err, "CRLConf.UnmarshalSettings()") 95 } 96 test.AssertDeepEquals(t, got, tt.want) 97 }) 98 } 99 }