github.com/letsencrypt/boulder@v0.20251208.0/observer/probers/tls/tls_conf_test.go (about)

     1  package probers
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/prometheus/client_golang/prometheus"
     8  	"gopkg.in/yaml.v3"
     9  
    10  	"github.com/letsencrypt/boulder/observer/probers"
    11  )
    12  
    13  func TestTLSConf_MakeProber(t *testing.T) {
    14  	goodHostname, goodRootCN, goodResponse := "example.com", "ISRG Root X1", "valid"
    15  	colls := TLSConf{}.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  		Hostname string
    25  		RootCN   string
    26  		Response string
    27  	}
    28  	tests := []struct {
    29  		name    string
    30  		fields  fields
    31  		colls   map[string]prometheus.Collector
    32  		wantErr bool
    33  	}{
    34  		// valid
    35  		{"valid hostname", fields{"example.com", goodRootCN, "valid"}, colls, false},
    36  		{"valid hostname with path", fields{"example.com/foo/bar", "ISRG Root X2", "Revoked"}, colls, false},
    37  		{"valid hostname with port", fields{"example.com:8080", goodRootCN, "expired"}, colls, false},
    38  
    39  		// invalid hostname
    40  		{"bad hostname", fields{":::::", goodRootCN, goodResponse}, colls, true},
    41  		{"included scheme", fields{"https://example.com", goodRootCN, goodResponse}, colls, true},
    42  		{"included scheme and port", fields{"https://example.com:443", goodRootCN, goodResponse}, colls, true},
    43  
    44  		// invalid response
    45  		{"empty response", fields{goodHostname, goodRootCN, ""}, colls, true},
    46  		{"unaccepted response", fields{goodHostname, goodRootCN, "invalid"}, colls, true},
    47  
    48  		// invalid collector
    49  		{
    50  			"unexpected collector",
    51  			fields{"http://example.com", goodRootCN, goodResponse},
    52  			map[string]prometheus.Collector{"obs_crl_foo": badColl},
    53  			true,
    54  		},
    55  		{
    56  			"missing collectors",
    57  			fields{"http://example.com", goodRootCN, goodResponse},
    58  			map[string]prometheus.Collector{},
    59  			true,
    60  		},
    61  	}
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			c := TLSConf{
    65  				Hostname: tt.fields.Hostname,
    66  				RootCN:   tt.fields.RootCN,
    67  				Response: tt.fields.Response,
    68  			}
    69  			if _, err := c.MakeProber(tt.colls); (err != nil) != tt.wantErr {
    70  				t.Errorf("TLSConf.Validate() error = %v, wantErr %v", err, tt.wantErr)
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func TestTLSConf_UnmarshalSettings(t *testing.T) {
    77  	type fields struct {
    78  		hostname any
    79  		rootOrg  any
    80  		rootCN   any
    81  		response any
    82  	}
    83  	tests := []struct {
    84  		name    string
    85  		fields  fields
    86  		want    probers.Configurer
    87  		wantErr bool
    88  	}{
    89  		{"valid", fields{"google.com", "", "ISRG Root X1", "valid"}, TLSConf{"google.com", "", "ISRG Root X1", "valid"}, false},
    90  		{"invalid hostname (map)", fields{make(map[string]any), 42, 42, 42}, nil, true},
    91  		{"invalid rootOrg (list)", fields{42, make([]string, 0), 42, 42}, nil, true},
    92  		{"invalid response (list)", fields{42, 42, 42, make([]string, 0)}, nil, true},
    93  	}
    94  	for _, tt := range tests {
    95  		t.Run(tt.name, func(t *testing.T) {
    96  			settings := probers.Settings{
    97  				"hostname": tt.fields.hostname,
    98  				"rootOrg":  tt.fields.rootOrg,
    99  				"rootCN":   tt.fields.rootCN,
   100  				"response": tt.fields.response,
   101  			}
   102  			settingsBytes, _ := yaml.Marshal(settings)
   103  			c := TLSConf{}
   104  			got, err := c.UnmarshalSettings(settingsBytes)
   105  			if (err != nil) != tt.wantErr {
   106  				t.Errorf("DNSConf.UnmarshalSettings() error = %v, wantErr %v", err, tt.wantErr)
   107  				return
   108  			}
   109  			if !reflect.DeepEqual(got, tt.want) {
   110  				t.Errorf("DNSConf.UnmarshalSettings() = %v, want %v", got, tt.want)
   111  			}
   112  		})
   113  	}
   114  }