...

Source file src/github.com/letsencrypt/boulder/observer/probers/tls/tls_conf_test.go

Documentation: github.com/letsencrypt/boulder/observer/probers/tls

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

View as plain text