Move prober and config to own package (#214)
authorJohannes 'fish' Ziemke <github@freigeist.org>
Fri, 1 Sep 2017 15:01:22 +0000 (17:01 +0200)
committerBrian Brazil <brian.brazil@robustperception.io>
Fri, 1 Sep 2017 15:01:22 +0000 (16:01 +0100)
18 files changed:
config/config.go [moved from config.go with 93% similarity]
config/config_test.go [moved from config_test.go with 76% similarity]
config/testdata/blackbox-bad.yml [moved from testdata/blackbox-bad.yml with 100% similarity]
config/testdata/blackbox-good.yml [moved from testdata/blackbox-good.yml with 100% similarity]
config/testdata/invalid-dns-module.yml [moved from testdata/invalid-dns-module.yml with 100% similarity]
main.go
main_test.go [new file with mode: 0644]
prober/dns.go [moved from dns.go with 95% similarity]
prober/dns_test.go [moved from dns_test.go with 86% similarity]
prober/http.go [moved from http.go with 94% similarity]
prober/http_test.go [moved from http_test.go with 75% similarity]
prober/icmp.go [moved from icmp.go with 94% similarity]
prober/prober.go [new file with mode: 0644]
prober/tcp.go [moved from tcp.go with 90% similarity]
prober/tcp_test.go [moved from tcp_test.go with 85% similarity]
prober/tls.go [moved from tls.go with 98% similarity]
prober/utils.go [moved from utils.go with 98% similarity]
prober/utils_test.go [moved from utils_test.go with 97% similarity]

similarity index 93%
rename from config.go
rename to config/config.go
index 3d1d324f9baab1850d5b32a72eff61c2c63c2a67..5d4baf440653f791c9eac505cccf406142b376dc 100644 (file)
--- a/config.go
@@ -1,12 +1,15 @@
-package main
+package config
 
 import (
        "errors"
        "fmt"
+       "io/ioutil"
        "strings"
        "sync"
        "time"
 
+       yaml "gopkg.in/yaml.v2"
+
        "github.com/prometheus/common/config"
 )
 
@@ -22,6 +25,25 @@ type SafeConfig struct {
        C *Config
 }
 
+func (sc *SafeConfig) ReloadConfig(confFile string) (err error) {
+       var c = &Config{}
+
+       yamlFile, err := ioutil.ReadFile(confFile)
+       if err != nil {
+               return fmt.Errorf("Error reading config file: %s", err)
+       }
+
+       if err := yaml.Unmarshal(yamlFile, c); err != nil {
+               return fmt.Errorf("Error parsing config file: %s", err)
+       }
+
+       sc.Lock()
+       sc.C = c
+       sc.Unlock()
+
+       return nil
+}
+
 type Module struct {
        Prober  string        `yaml:"prober,omitempty"`
        Timeout time.Duration `yaml:"timeout,omitempty"`
similarity index 76%
rename from config_test.go
rename to config/config_test.go
index 1379098507eacc020be67bb2abc28f30ea827c06..4042994f26082c466ffa3d57c97cfc2f667f92d1 100644 (file)
@@ -1,4 +1,4 @@
-package main
+package config
 
 import (
        "strings"
@@ -12,7 +12,7 @@ func TestLoadConfig(t *testing.T) {
                C: &Config{},
        }
 
-       err := sc.reloadConfig("testdata/blackbox-good.yml")
+       err := sc.ReloadConfig("testdata/blackbox-good.yml")
        if err != nil {
                t.Errorf("Error loading config %v: %v", "blackbox.yml", err)
        }
@@ -28,15 +28,15 @@ func TestLoadBadConfigs(t *testing.T) {
        }{
                {
                        ConfigFile:    "testdata/blackbox-bad.yml",
-                       ExpectedError: "unknown fields in dns probe: invalid_extra_field",
+                       ExpectedError: "Error parsing config file: unknown fields in dns probe: invalid_extra_field",
                },
                {
                        ConfigFile:    "testdata/invalid-dns-module.yml",
-                       ExpectedError: "Query name must be set for DNS module",
+                       ExpectedError: "Error parsing config file: Query name must be set for DNS module",
                },
        }
        for i, test := range tests {
-               err := sc.reloadConfig(test.ConfigFile)
+               err := sc.ReloadConfig(test.ConfigFile)
                if err.Error() != test.ExpectedError {
                        t.Errorf("In case %v:\nExpected:\n%v\nGot:\n%v", i, test.ExpectedError, err.Error())
                }
@@ -48,7 +48,7 @@ func TestHideConfigSecrets(t *testing.T) {
                C: &Config{},
        }
 
-       err := sc.reloadConfig("testdata/blackbox-good.yml")
+       err := sc.ReloadConfig("testdata/blackbox-good.yml")
        if err != nil {
                t.Errorf("Error loading config %v: %v", "testdata/blackbox-good.yml", err)
        }
diff --git a/main.go b/main.go
index 164597640f262d8d1de88dce42d2e618b8696d6b..bb79c03a5e571adfb18d88bee6c8f9b7f24e97c2 100644 (file)
--- a/main.go
+++ b/main.go
@@ -16,7 +16,6 @@ package main
 import (
        "context"
        "fmt"
-       "io/ioutil"
        "net/http"
        "os"
        "os/signal"
@@ -31,48 +30,29 @@ import (
        "github.com/prometheus/client_golang/prometheus/promhttp"
        "github.com/prometheus/common/log"
        "github.com/prometheus/common/version"
+
+       "github.com/prometheus/blackbox_exporter/config"
+       "github.com/prometheus/blackbox_exporter/prober"
 )
 
 var (
-       sc = &SafeConfig{
-               C: &Config{},
+       sc = &config.SafeConfig{
+               C: &config.Config{},
        }
 
        configFile    = kingpin.Flag("config.file", "Blackbox exporter configuration file.").Default("blackbox.yml").String()
        listenAddress = kingpin.Flag("web.listen-address", "The address to listen on for HTTP requests.").Default(":9115").String()
        timeoutOffset = kingpin.Flag("timeout-offset", "Offset to subtract from timeout in seconds.").Default("0.5").Float64()
-)
-
-var Probers = map[string]func(context.Context, string, Module, *prometheus.Registry) bool{
-       "http": probeHTTP,
-       "tcp":  probeTCP,
-       "icmp": probeICMP,
-       "dns":  probeDNS,
-}
-
-func (sc *SafeConfig) reloadConfig(confFile string) (err error) {
-       var c = &Config{}
-
-       yamlFile, err := ioutil.ReadFile(confFile)
-       if err != nil {
-               log.Errorf("Error reading config file: %s", err)
-               return err
-       }
 
-       if err := yaml.Unmarshal(yamlFile, c); err != nil {
-               log.Errorf("Error parsing config file: %s", err)
-               return err
+       Probers = map[string]prober.ProbeFn{
+               "http": prober.ProbeHTTP,
+               "tcp":  prober.ProbeTCP,
+               "icmp": prober.ProbeICMP,
+               "dns":  prober.ProbeDNS,
        }
+)
 
-       sc.Lock()
-       sc.C = c
-       sc.Unlock()
-
-       log.Infoln("Loaded config file")
-       return nil
-}
-
-func probeHandler(w http.ResponseWriter, r *http.Request, c *Config) {
+func probeHandler(w http.ResponseWriter, r *http.Request, c *config.Config) {
        moduleName := r.URL.Query().Get("module")
        if moduleName == "" {
                moduleName = "http_2xx"
@@ -151,9 +131,10 @@ func main() {
        log.Infoln("Starting blackbox_exporter", version.Info())
        log.Infoln("Build context", version.BuildContext())
 
-       if err := sc.reloadConfig(*configFile); err != nil {
+       if err := sc.ReloadConfig(*configFile); err != nil {
                log.Fatalf("Error loading config: %s", err)
        }
+       log.Infoln("Loaded config file")
 
        hup := make(chan os.Signal)
        reloadCh := make(chan chan error)
@@ -162,14 +143,17 @@ func main() {
                for {
                        select {
                        case <-hup:
-                               if err := sc.reloadConfig(*configFile); err != nil {
+                               if err := sc.ReloadConfig(*configFile); err != nil {
                                        log.Errorf("Error reloading config: %s", err)
+                                       continue
                                }
+                               log.Infoln("Loaded config file")
                        case rc := <-reloadCh:
-                               if err := sc.reloadConfig(*configFile); err != nil {
+                               if err := sc.ReloadConfig(*configFile); err != nil {
                                        log.Errorf("Error reloading config: %s", err)
                                        rc <- err
                                } else {
+                                       log.Infoln("Loaded config file")
                                        rc <- nil
                                }
                        }
diff --git a/main_test.go b/main_test.go
new file mode 100644 (file)
index 0000000..4665ef5
--- /dev/null
@@ -0,0 +1,44 @@
+package main
+
+import (
+       "net/http"
+       "net/http/httptest"
+       "testing"
+       "time"
+
+       "github.com/prometheus/blackbox_exporter/config"
+)
+
+var c = &config.Config{
+       Modules: map[string]config.Module{
+               "http_2xx": config.Module{
+                       Prober:  "http",
+                       Timeout: 10 * time.Second,
+               },
+       },
+}
+
+func TestPrometheusTimeoutHTTP(t *testing.T) {
+
+       ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               time.Sleep(2 * time.Second)
+       }))
+       defer ts.Close()
+
+       req, err := http.NewRequest("GET", "?target="+ts.URL, nil)
+       if err != nil {
+               t.Fatal(err)
+       }
+       req.Header.Set("X-Prometheus-Scrape-Timeout-Seconds", "1")
+
+       rr := httptest.NewRecorder()
+       handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               probeHandler(w, r, c)
+       })
+
+       handler.ServeHTTP(rr, req)
+
+       if status := rr.Code; status != http.StatusOK {
+               t.Errorf("probe request handler returned wrong status code: %v, want %v", status, http.StatusOK)
+       }
+}
similarity index 95%
rename from dns.go
rename to prober/dns.go
index 97c5ff22177290fa06b472b004192d7a6061fa01..2a7f0d9913d65289d7fa136b15aa5cf78d731c1a 100644 (file)
--- a/dns.go
@@ -11,7 +11,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package prober
 
 import (
        "context"
@@ -22,10 +22,12 @@ import (
        "github.com/miekg/dns"
        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/common/log"
+
+       "github.com/prometheus/blackbox_exporter/config"
 )
 
 // validRRs checks a slice of RRs received from the server against a DNSRRValidator.
-func validRRs(rrs *[]dns.RR, v *DNSRRValidator) bool {
+func validRRs(rrs *[]dns.RR, v *config.DNSRRValidator) bool {
        // Fail the probe if there are no RRs of a given type, but a regexp match is required
        // (i.e. FailIfNotMatchesRegexp is set).
        if len(*rrs) == 0 && len(v.FailIfNotMatchesRegexp) > 0 {
@@ -82,7 +84,7 @@ func validRcode(rcode int, valid []string) bool {
        return false
 }
 
-func probeDNS(ctx context.Context, target string, module Module, registry *prometheus.Registry) bool {
+func ProbeDNS(ctx context.Context, target string, module config.Module, registry *prometheus.Registry) bool {
        var numAnswer, numAuthority, numAdditional int
        var dialProtocol string
        probeDNSAnswerRRSGauge := prometheus.NewGauge(prometheus.GaugeOpts{
similarity index 86%
rename from dns_test.go
rename to prober/dns_test.go
index 93113b2294889db5902d37619ac259d85d9b772b..642518599fd32336042def552d1d6d3325bc64c0 100644 (file)
@@ -11,7 +11,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package prober
 
 import (
        "context"
@@ -22,6 +22,8 @@ import (
 
        "github.com/miekg/dns"
        "github.com/prometheus/client_golang/prometheus"
+
+       "github.com/prometheus/blackbox_exporter/config"
 )
 
 var PROTOCOLS = [...]string{"udp", "tcp"}
@@ -70,41 +72,41 @@ func recursiveDNSHandler(w dns.ResponseWriter, r *dns.Msg) {
 
 func TestRecursiveDNSResponse(t *testing.T) {
        tests := []struct {
-               Probe         DNSProbe
+               Probe         config.DNSProbe
                ShouldSucceed bool
        }{
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName: "example.com",
                        }, true,
                },
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName:   "example.com",
                                ValidRcodes: []string{"SERVFAIL", "NXDOMAIN"},
                        }, false,
                },
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName: "example.com",
-                               ValidateAnswer: DNSRRValidator{
+                               ValidateAnswer: config.DNSRRValidator{
                                        FailIfMatchesRegexp:    []string{".*7200.*"},
                                        FailIfNotMatchesRegexp: []string{".*3600.*"},
                                },
                        }, true,
                },
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName: "example.com",
-                               ValidateAuthority: DNSRRValidator{
+                               ValidateAuthority: config.DNSRRValidator{
                                        FailIfMatchesRegexp: []string{".*7200.*"},
                                },
                        }, true,
                },
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName: "example.com",
-                               ValidateAdditional: DNSRRValidator{
+                               ValidateAdditional: config.DNSRRValidator{
                                        FailIfNotMatchesRegexp: []string{".*3600.*"},
                                },
                        }, false,
@@ -122,7 +124,7 @@ func TestRecursiveDNSResponse(t *testing.T) {
 
                        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
                        defer cancel()
-                       result := probeDNS(testCTX, addr.String(), Module{Timeout: time.Second, DNS: test.Probe}, registry)
+                       result := ProbeDNS(testCTX, addr.String(), config.Module{Timeout: time.Second, DNS: test.Probe}, registry)
                        if result != test.ShouldSucceed {
                                t.Fatalf("Test %d had unexpected result: %v", i, result)
                        }
@@ -182,58 +184,58 @@ func authoritativeDNSHandler(w dns.ResponseWriter, r *dns.Msg) {
 
 func TestAuthoritativeDNSResponse(t *testing.T) {
        tests := []struct {
-               Probe         DNSProbe
+               Probe         config.DNSProbe
                ShouldSucceed bool
        }{
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName: "example.com",
                        }, true,
                },
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName:   "example.com",
                                ValidRcodes: []string{"SERVFAIL", "NXDOMAIN"},
                        }, false,
                },
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName: "example.com",
-                               ValidateAnswer: DNSRRValidator{
+                               ValidateAnswer: config.DNSRRValidator{
                                        FailIfMatchesRegexp:    []string{".*3600.*"},
                                        FailIfNotMatchesRegexp: []string{".*3600.*"},
                                },
                        }, false,
                },
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName: "example.com",
-                               ValidateAnswer: DNSRRValidator{
+                               ValidateAnswer: config.DNSRRValidator{
                                        FailIfMatchesRegexp:    []string{".*7200.*"},
                                        FailIfNotMatchesRegexp: []string{".*7200.*"},
                                },
                        }, false,
                },
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName: "example.com",
-                               ValidateAuthority: DNSRRValidator{
+                               ValidateAuthority: config.DNSRRValidator{
                                        FailIfNotMatchesRegexp: []string{"ns.*.isp.net"},
                                },
                        }, true,
                },
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName: "example.com",
-                               ValidateAdditional: DNSRRValidator{
+                               ValidateAdditional: config.DNSRRValidator{
                                        FailIfNotMatchesRegexp: []string{"^ns.*.isp"},
                                },
                        }, true,
                },
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName: "example.com",
-                               ValidateAdditional: DNSRRValidator{
+                               ValidateAdditional: config.DNSRRValidator{
                                        FailIfMatchesRegexp: []string{"^ns.*.isp"},
                                },
                        }, false,
@@ -249,7 +251,7 @@ func TestAuthoritativeDNSResponse(t *testing.T) {
                        registry := prometheus.NewRegistry()
                        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
                        defer cancel()
-                       result := probeDNS(testCTX, addr.String(), Module{Timeout: time.Second, DNS: test.Probe}, registry)
+                       result := ProbeDNS(testCTX, addr.String(), config.Module{Timeout: time.Second, DNS: test.Probe}, registry)
                        if result != test.ShouldSucceed {
                                t.Fatalf("Test %d had unexpected result: %v", i, result)
                        }
@@ -269,28 +271,28 @@ func TestAuthoritativeDNSResponse(t *testing.T) {
 
 func TestServfailDNSResponse(t *testing.T) {
        tests := []struct {
-               Probe         DNSProbe
+               Probe         config.DNSProbe
                ShouldSucceed bool
        }{
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName: "example.com",
                        }, false,
                },
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName:   "example.com",
                                ValidRcodes: []string{"SERVFAIL", "NXDOMAIN"},
                        }, true,
                },
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName: "example.com",
                                QueryType: "NOT_A_VALID_QUERY_TYPE",
                        }, false,
                },
                {
-                       DNSProbe{
+                       config.DNSProbe{
                                QueryName:   "example.com",
                                ValidRcodes: []string{"NOT_A_VALID_RCODE"},
                        }, false,
@@ -307,7 +309,7 @@ func TestServfailDNSResponse(t *testing.T) {
                        registry := prometheus.NewRegistry()
                        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
                        defer cancel()
-                       result := probeDNS(testCTX, addr.String(), Module{Timeout: time.Second, DNS: test.Probe}, registry)
+                       result := ProbeDNS(testCTX, addr.String(), config.Module{Timeout: time.Second, DNS: test.Probe}, registry)
                        if result != test.ShouldSucceed {
                                t.Fatalf("Test %d had unexpected result: %v", i, result)
                        }
@@ -344,9 +346,9 @@ func TestDNSProtocol(t *testing.T) {
                _, port, _ := net.SplitHostPort(addr.String())
 
                // Force IPv4
-               module := Module{
+               module := config.Module{
                        Timeout: time.Second,
-                       DNS: DNSProbe{
+                       DNS: config.DNSProbe{
                                QueryName:           "example.com",
                                TransportProtocol:   protocol,
                                PreferredIPProtocol: "ip4",
@@ -355,7 +357,7 @@ func TestDNSProtocol(t *testing.T) {
                registry := prometheus.NewRegistry()
                testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
                defer cancel()
-               result := probeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry)
+               result := ProbeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry)
                if !result {
                        t.Fatalf("DNS protocol: \"%v4\" connection test failed, expected success.", protocol)
                }
@@ -370,9 +372,9 @@ func TestDNSProtocol(t *testing.T) {
                checkRegistryResults(expectedResults, mfs, t)
 
                // Force IPv6
-               module = Module{
+               module = config.Module{
                        Timeout: time.Second,
-                       DNS: DNSProbe{
+                       DNS: config.DNSProbe{
                                QueryName:           "example.com",
                                TransportProtocol:   protocol,
                                PreferredIPProtocol: "ip6",
@@ -381,7 +383,7 @@ func TestDNSProtocol(t *testing.T) {
                registry = prometheus.NewRegistry()
                testCTX, cancel = context.WithTimeout(context.Background(), 10*time.Second)
                defer cancel()
-               result = probeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry)
+               result = ProbeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry)
                if !result {
                        t.Fatalf("DNS protocol: \"%v6\" connection test failed, expected success.", protocol)
                }
@@ -395,9 +397,9 @@ func TestDNSProtocol(t *testing.T) {
                checkRegistryResults(expectedResults, mfs, t)
 
                // Prefer IPv6
-               module = Module{
+               module = config.Module{
                        Timeout: time.Second,
-                       DNS: DNSProbe{
+                       DNS: config.DNSProbe{
                                QueryName:           "example.com",
                                TransportProtocol:   protocol,
                                PreferredIPProtocol: "ip6",
@@ -406,7 +408,7 @@ func TestDNSProtocol(t *testing.T) {
                registry = prometheus.NewRegistry()
                testCTX, cancel = context.WithTimeout(context.Background(), 10*time.Second)
                defer cancel()
-               result = probeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry)
+               result = ProbeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry)
                if !result {
                        t.Fatalf("DNS protocol: \"%v\", preferred \"ip6\" connection test failed, expected success.", protocol)
                }
@@ -420,9 +422,9 @@ func TestDNSProtocol(t *testing.T) {
                checkRegistryResults(expectedResults, mfs, t)
 
                // Prefer IPv4
-               module = Module{
+               module = config.Module{
                        Timeout: time.Second,
-                       DNS: DNSProbe{
+                       DNS: config.DNSProbe{
                                QueryName:           "example.com",
                                TransportProtocol:   protocol,
                                PreferredIPProtocol: "ip4",
@@ -431,7 +433,7 @@ func TestDNSProtocol(t *testing.T) {
                registry = prometheus.NewRegistry()
                testCTX, cancel = context.WithTimeout(context.Background(), 10*time.Second)
                defer cancel()
-               result = probeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry)
+               result = ProbeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry)
                if !result {
                        t.Fatalf("DNS protocol: \"%v\", preferred \"ip4\" connection test failed, expected success.", protocol)
                }
@@ -446,9 +448,9 @@ func TestDNSProtocol(t *testing.T) {
                checkRegistryResults(expectedResults, mfs, t)
 
                // Prefer none
-               module = Module{
+               module = config.Module{
                        Timeout: time.Second,
-                       DNS: DNSProbe{
+                       DNS: config.DNSProbe{
                                QueryName:         "example.com",
                                TransportProtocol: protocol,
                        },
@@ -456,7 +458,7 @@ func TestDNSProtocol(t *testing.T) {
                registry = prometheus.NewRegistry()
                testCTX, cancel = context.WithTimeout(context.Background(), 10*time.Second)
                defer cancel()
-               result = probeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry)
+               result = ProbeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry)
                if !result {
                        t.Fatalf("DNS protocol: \"%v\" connection test failed, expected success.", protocol)
                }
@@ -471,16 +473,16 @@ func TestDNSProtocol(t *testing.T) {
                checkRegistryResults(expectedResults, mfs, t)
 
                // No protocol
-               module = Module{
+               module = config.Module{
                        Timeout: time.Second,
-                       DNS: DNSProbe{
+                       DNS: config.DNSProbe{
                                QueryName: "example.com",
                        },
                }
                registry = prometheus.NewRegistry()
                testCTX, cancel = context.WithTimeout(context.Background(), 10*time.Second)
                defer cancel()
-               result = probeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry)
+               result = ProbeDNS(testCTX, net.JoinHostPort("localhost", port), module, registry)
                if protocol == "udp" {
                        if !result {
                                t.Fatalf("DNS test connection with protocol %s failed, expected success.", protocol)
similarity index 94%
rename from http.go
rename to prober/http.go
index bd1f37a3d981e1bef2189a86e954e470129d28c9..39dbfa0f9dcf8347b40fb746dc2a4cc6d4e0605c 100644 (file)
--- a/http.go
@@ -11,7 +11,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package prober
 
 import (
        "context"
@@ -26,11 +26,13 @@ import (
        "strings"
 
        "github.com/prometheus/client_golang/prometheus"
-       "github.com/prometheus/common/config"
+       pconfig "github.com/prometheus/common/config"
        "github.com/prometheus/common/log"
+
+       "github.com/prometheus/blackbox_exporter/config"
 )
 
-func matchRegularExpressions(reader io.Reader, httpConfig HTTPProbe) bool {
+func matchRegularExpressions(reader io.Reader, httpConfig config.HTTPProbe) bool {
        body, err := ioutil.ReadAll(reader)
        if err != nil {
                log.Errorf("Error reading HTTP body: %s", err)
@@ -59,7 +61,7 @@ func matchRegularExpressions(reader io.Reader, httpConfig HTTPProbe) bool {
        return true
 }
 
-func probeHTTP(ctx context.Context, target string, module Module, registry *prometheus.Registry) (success bool) {
+func ProbeHTTP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry) (success bool) {
        var redirects int
        var (
                contentLengthGauge = prometheus.NewGauge(prometheus.GaugeOpts{
@@ -128,7 +130,7 @@ func probeHTTP(ctx context.Context, target string, module Module, registry *prom
 
        httpClientConfig := &module.HTTP.HTTPClientConfig
 
-       client, err := config.NewHTTPClientFromConfig(httpClientConfig)
+       client, err := pconfig.NewHTTPClientFromConfig(httpClientConfig)
        if err != nil {
                log.Errorf("Error generating HTTP client: %v", err)
                return false
similarity index 75%
rename from http_test.go
rename to prober/http_test.go
index 5e0a6325f1c9bc84602f7db7bac6aaaa158b280c..43b9e3186b1a406416ef1ced303074ec3f7b93f6 100644 (file)
@@ -11,7 +11,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package prober
 
 import (
        "context"
@@ -23,7 +23,9 @@ import (
        "time"
 
        "github.com/prometheus/client_golang/prometheus"
-       "github.com/prometheus/common/config"
+       pconfig "github.com/prometheus/common/config"
+
+       "github.com/prometheus/blackbox_exporter/config"
 )
 
 func TestHTTPStatusCodes(t *testing.T) {
@@ -52,8 +54,8 @@ func TestHTTPStatusCodes(t *testing.T) {
                recorder := httptest.NewRecorder()
                testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
                defer cancel()
-               result := probeHTTP(testCTX, ts.URL,
-                       Module{Timeout: time.Second, HTTP: HTTPProbe{ValidStatusCodes: test.ValidStatusCodes}}, registry)
+               result := ProbeHTTP(testCTX, ts.URL,
+                       config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{ValidStatusCodes: test.ValidStatusCodes}}, registry)
                body := recorder.Body.String()
                if result != test.ShouldSucceed {
                        t.Fatalf("Test %d had unexpected result: %s", i, body)
@@ -77,8 +79,8 @@ func TestValidHTTPVersion(t *testing.T) {
                defer ts.Close()
                recorder := httptest.NewRecorder()
                registry := prometheus.NewRegistry()
-               result := probeHTTP(context.Background(), ts.URL,
-                       Module{Timeout: time.Second, HTTP: HTTPProbe{
+               result := ProbeHTTP(context.Background(), ts.URL,
+                       config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{
                                ValidHTTPVersions: test.ValidHTTPVersions,
                        }}, registry)
                body := recorder.Body.String()
@@ -101,7 +103,7 @@ func TestRedirectFollowed(t *testing.T) {
        registry := prometheus.NewRegistry()
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
-       result := probeHTTP(testCTX, ts.URL, Module{Timeout: time.Second, HTTP: HTTPProbe{}}, registry)
+       result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{}}, registry)
        body := recorder.Body.String()
        if !result {
                t.Fatalf("Redirect test failed unexpectedly, got %s", body)
@@ -128,8 +130,8 @@ func TestRedirectNotFollowed(t *testing.T) {
        registry := prometheus.NewRegistry()
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
-       result := probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{NoFollowRedirects: true, ValidStatusCodes: []int{302}}}, registry)
+       result := ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{NoFollowRedirects: true, ValidStatusCodes: []int{302}}}, registry)
        body := recorder.Body.String()
        if !result {
                t.Fatalf("Redirect test failed unexpectedly, got %s", body)
@@ -149,8 +151,8 @@ func TestPost(t *testing.T) {
        registry := prometheus.NewRegistry()
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
-       result := probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{Method: "POST"}}, registry)
+       result := ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{Method: "POST"}}, registry)
        body := recorder.Body.String()
        if !result {
                t.Fatalf("Post test failed unexpectedly, got %s", body)
@@ -166,11 +168,11 @@ func TestBasicAuth(t *testing.T) {
        registry := prometheus.NewRegistry()
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
-       result := probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{
-                       HTTPClientConfig: config.HTTPClientConfig{
-                               TLSConfig: config.TLSConfig{InsecureSkipVerify: false},
-                               BasicAuth: &config.BasicAuth{Username: "username", Password: "password"},
+       result := ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{
+                       HTTPClientConfig: pconfig.HTTPClientConfig{
+                               TLSConfig: pconfig.TLSConfig{InsecureSkipVerify: false},
+                               BasicAuth: &pconfig.BasicAuth{Username: "username", Password: "password"},
                        },
                }}, registry)
        body := recorder.Body.String()
@@ -188,10 +190,10 @@ func TestBearerToken(t *testing.T) {
        registry := prometheus.NewRegistry()
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
-       result := probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{
-                       HTTPClientConfig: config.HTTPClientConfig{
-                               BearerToken: config.Secret("mysecret"),
+       result := ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{
+                       HTTPClientConfig: pconfig.HTTPClientConfig{
+                               BearerToken: pconfig.Secret("mysecret"),
                        },
                }}, registry)
        body := recorder.Body.String()
@@ -209,8 +211,8 @@ func TestFailIfNotSSL(t *testing.T) {
        registry := prometheus.NewRegistry()
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
-       result := probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfNotSSL: true}}, registry)
+       result := ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfNotSSL: true}}, registry)
        body := recorder.Body.String()
        if result {
                t.Fatalf("Fail if not SSL test suceeded unexpectedly, got %s", body)
@@ -235,8 +237,8 @@ func TestFailIfMatchesRegexp(t *testing.T) {
        registry := prometheus.NewRegistry()
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
-       result := probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database"}}}, registry)
+       result := ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database"}}}, registry)
        body := recorder.Body.String()
        if result {
                t.Fatalf("Regexp test succeeded unexpectedly, got %s", body)
@@ -257,8 +259,8 @@ func TestFailIfMatchesRegexp(t *testing.T) {
 
        recorder = httptest.NewRecorder()
        registry = prometheus.NewRegistry()
-       result = probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database"}}}, registry)
+       result = ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database"}}}, registry)
        body = recorder.Body.String()
        if !result {
                t.Fatalf("Regexp test failed unexpectedly, got %s", body)
@@ -281,8 +283,8 @@ func TestFailIfMatchesRegexp(t *testing.T) {
 
        recorder = httptest.NewRecorder()
        registry = prometheus.NewRegistry()
-       result = probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database", "internal error"}}}, registry)
+       result = ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database", "internal error"}}}, registry)
        body = recorder.Body.String()
        if result {
                t.Fatalf("Regexp test succeeded unexpectedly, got %s", body)
@@ -295,8 +297,8 @@ func TestFailIfMatchesRegexp(t *testing.T) {
 
        recorder = httptest.NewRecorder()
        registry = prometheus.NewRegistry()
-       result = probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database", "internal error"}}}, registry)
+       result = ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfMatchesRegexp: []string{"could not connect to database", "internal error"}}}, registry)
        body = recorder.Body.String()
        if !result {
                t.Fatalf("Regexp test failed unexpectedly, got %s", body)
@@ -313,8 +315,8 @@ func TestFailIfNotMatchesRegexp(t *testing.T) {
        registry := prometheus.NewRegistry()
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
-       result := probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here"}}}, registry)
+       result := ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here"}}}, registry)
        body := recorder.Body.String()
        if result {
                t.Fatalf("Regexp test succeeded unexpectedly, got %s", body)
@@ -327,8 +329,8 @@ func TestFailIfNotMatchesRegexp(t *testing.T) {
 
        recorder = httptest.NewRecorder()
        registry = prometheus.NewRegistry()
-       result = probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here"}}}, registry)
+       result = ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here"}}}, registry)
        body = recorder.Body.String()
        if !result {
                t.Fatalf("Regexp test failed unexpectedly, got %s", body)
@@ -343,8 +345,8 @@ func TestFailIfNotMatchesRegexp(t *testing.T) {
 
        recorder = httptest.NewRecorder()
        registry = prometheus.NewRegistry()
-       result = probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here", "Copyright 2015"}}}, registry)
+       result = ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here", "Copyright 2015"}}}, registry)
        body = recorder.Body.String()
        if result {
                t.Fatalf("Regexp test succeeded unexpectedly, got %s", body)
@@ -357,8 +359,8 @@ func TestFailIfNotMatchesRegexp(t *testing.T) {
 
        recorder = httptest.NewRecorder()
        registry = prometheus.NewRegistry()
-       result = probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here", "Copyright 2015"}}}, registry)
+       result = ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{FailIfNotMatchesRegexp: []string{"Download the latest version here", "Copyright 2015"}}}, registry)
        body = recorder.Body.String()
        if !result {
                t.Fatalf("Regexp test failed unexpectedly, got %s", body)
@@ -389,7 +391,7 @@ func TestHTTPHeaders(t *testing.T) {
        registry := prometheus.NewRegistry()
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
-       result := probeHTTP(testCTX, ts.URL, Module{Timeout: time.Second, HTTP: HTTPProbe{
+       result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{
                Headers: headers,
        }}, registry)
        if !result {
@@ -406,10 +408,10 @@ func TestFailIfSelfSignedCA(t *testing.T) {
        registry := prometheus.NewRegistry()
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
-       result := probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{
-                       HTTPClientConfig: config.HTTPClientConfig{
-                               TLSConfig: config.TLSConfig{InsecureSkipVerify: false},
+       result := ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{
+                       HTTPClientConfig: pconfig.HTTPClientConfig{
+                               TLSConfig: pconfig.TLSConfig{InsecureSkipVerify: false},
                        },
                }}, registry)
        body := recorder.Body.String()
@@ -435,10 +437,10 @@ func TestSucceedIfSelfSignedCA(t *testing.T) {
        registry := prometheus.NewRegistry()
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
-       result := probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{
-                       HTTPClientConfig: config.HTTPClientConfig{
-                               TLSConfig: config.TLSConfig{InsecureSkipVerify: true},
+       result := ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{
+                       HTTPClientConfig: pconfig.HTTPClientConfig{
+                               TLSConfig: pconfig.TLSConfig{InsecureSkipVerify: true},
                        },
                }}, registry)
        body := recorder.Body.String()
@@ -464,10 +466,10 @@ func TestTLSConfigIsIgnoredForPlainHTTP(t *testing.T) {
        registry := prometheus.NewRegistry()
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
-       result := probeHTTP(testCTX, ts.URL,
-               Module{Timeout: time.Second, HTTP: HTTPProbe{
-                       HTTPClientConfig: config.HTTPClientConfig{
-                               TLSConfig: config.TLSConfig{InsecureSkipVerify: false},
+       result := ProbeHTTP(testCTX, ts.URL,
+               config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{
+                       HTTPClientConfig: pconfig.HTTPClientConfig{
+                               TLSConfig: pconfig.TLSConfig{InsecureSkipVerify: false},
                        },
                }}, registry)
        body := recorder.Body.String()
@@ -483,37 +485,3 @@ func TestTLSConfigIsIgnoredForPlainHTTP(t *testing.T) {
        }
        checkRegistryResults(expectedResults, mfs, t)
 }
-
-var c = &Config{
-       Modules: map[string]Module{
-               "http_2xx": Module{
-                       Prober:  "http",
-                       Timeout: 10 * time.Second,
-               },
-       },
-}
-
-func TestPrometheusTimeoutHTTP(t *testing.T) {
-
-       ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-               time.Sleep(2 * time.Second)
-       }))
-       defer ts.Close()
-
-       req, err := http.NewRequest("GET", "?target="+ts.URL, nil)
-       if err != nil {
-               t.Fatal(err)
-       }
-       req.Header.Set("X-Prometheus-Scrape-Timeout-Seconds", "1")
-
-       rr := httptest.NewRecorder()
-       handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-               probeHandler(w, r, c)
-       })
-
-       handler.ServeHTTP(rr, req)
-
-       if status := rr.Code; status != http.StatusOK {
-               t.Errorf("probe request handler returned wrong status code: %v, want %v", status, http.StatusOK)
-       }
-}
similarity index 94%
rename from icmp.go
rename to prober/icmp.go
index 56754e7ee7256f71d8ebb75147aaf52afdd5148d..9c1a5d65bda88f39378b6ec050048ccf96eb2f39 100644 (file)
--- a/icmp.go
@@ -11,7 +11,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package prober
 
 import (
        "bytes"
@@ -27,6 +27,8 @@ import (
 
        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/common/log"
+
+       "github.com/prometheus/blackbox_exporter/config"
 )
 
 var (
@@ -41,7 +43,7 @@ func getICMPSequence() uint16 {
        return icmpSequence
 }
 
-func probeICMP(ctx context.Context, target string, module Module, registry *prometheus.Registry) (success bool) {
+func ProbeICMP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry) (success bool) {
        var (
                socket      *icmp.PacketConn
                requestType icmp.Type
diff --git a/prober/prober.go b/prober/prober.go
new file mode 100644 (file)
index 0000000..c3a5fe8
--- /dev/null
@@ -0,0 +1,11 @@
+package prober
+
+import (
+       "context"
+
+       "github.com/prometheus/client_golang/prometheus"
+
+       "github.com/prometheus/blackbox_exporter/config"
+)
+
+type ProbeFn func(ctx context.Context, target string, config config.Module, registry *prometheus.Registry) bool
similarity index 90%
rename from tcp.go
rename to prober/tcp.go
index 5abbb51028f40b01a93dc4f8defa81e182a881d4..74c935654c4a62c4d87451c8815c3e90ed485f07 100644 (file)
--- a/tcp.go
@@ -11,7 +11,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package prober
 
 import (
        "bufio"
@@ -22,11 +22,13 @@ import (
        "regexp"
 
        "github.com/prometheus/client_golang/prometheus"
-       "github.com/prometheus/common/config"
+       pconfig "github.com/prometheus/common/config"
        "github.com/prometheus/common/log"
+
+       "github.com/prometheus/blackbox_exporter/config"
 )
 
-func dialTCP(ctx context.Context, target string, module Module, registry *prometheus.Registry) (net.Conn, error) {
+func dialTCP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry) (net.Conn, error) {
        var dialProtocol, dialTarget string
        dialer := &net.Dialer{}
        targetAddress, port, err := net.SplitHostPort(target)
@@ -51,7 +53,7 @@ func dialTCP(ctx context.Context, target string, module Module, registry *promet
        if !module.TCP.TLS {
                return dialer.DialContext(ctx, dialProtocol, dialTarget)
        }
-       tlsConfig, err := config.NewTLSConfig(&module.TCP.TLSConfig)
+       tlsConfig, err := pconfig.NewTLSConfig(&module.TCP.TLSConfig)
        if err != nil {
                log.Errorf("Error creating TLS configuration: %v", err)
                return nil, err
@@ -62,7 +64,7 @@ func dialTCP(ctx context.Context, target string, module Module, registry *promet
        return tls.DialWithDialer(dialer, dialProtocol, dialTarget, tlsConfig)
 }
 
-func probeTCP(ctx context.Context, target string, module Module, registry *prometheus.Registry) bool {
+func ProbeTCP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry) bool {
        probeSSLEarliestCertExpiry := prometheus.NewGauge(prometheus.GaugeOpts{
                Name: "probe_ssl_earliest_cert_expiry",
                Help: "Returns earliest SSL cert expiry date",
similarity index 85%
rename from tcp_test.go
rename to prober/tcp_test.go
index a612317f0005ff56e71b8e8af5a3ee8b301bc535..4e720998a111d1dbeb81148baeeb7a8b03df1884 100644 (file)
@@ -11,7 +11,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package prober
 
 import (
        "context"
@@ -22,6 +22,8 @@ import (
        "time"
 
        "github.com/prometheus/client_golang/prometheus"
+
+       "github.com/prometheus/blackbox_exporter/config"
 )
 
 func TestTCPConnection(t *testing.T) {
@@ -43,7 +45,7 @@ func TestTCPConnection(t *testing.T) {
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
        registry := prometheus.NewRegistry()
-       if !probeTCP(testCTX, ln.Addr().String(), Module{}, registry) {
+       if !ProbeTCP(testCTX, ln.Addr().String(), config.Module{}, registry) {
                t.Fatalf("TCP module failed, expected success.")
        }
        <-ch
@@ -54,7 +56,7 @@ func TestTCPConnectionFails(t *testing.T) {
        registry := prometheus.NewRegistry()
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
-       if probeTCP(testCTX, ":0", Module{}, registry) {
+       if ProbeTCP(testCTX, ":0", config.Module{}, registry) {
                t.Fatalf("TCP module suceeded, expected failure.")
        }
 }
@@ -69,9 +71,9 @@ func TestTCPConnectionQueryResponseIRC(t *testing.T) {
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
 
-       module := Module{
-               TCP: TCPProbe{
-                       QueryResponse: []QueryResponse{
+       module := config.Module{
+               TCP: config.TCPProbe{
+                       QueryResponse: []config.QueryResponse{
                                {Send: "NICK prober"},
                                {Send: "USER prober prober prober :prober"},
                                {Expect: "^:[^ ]+ 001"},
@@ -94,7 +96,7 @@ func TestTCPConnectionQueryResponseIRC(t *testing.T) {
                ch <- struct{}{}
        }()
        registry := prometheus.NewRegistry()
-       if !probeTCP(testCTX, ln.Addr().String(), module, registry) {
+       if !ProbeTCP(testCTX, ln.Addr().String(), module, registry) {
                t.Fatalf("TCP module failed, expected success.")
        }
        <-ch
@@ -113,7 +115,7 @@ func TestTCPConnectionQueryResponseIRC(t *testing.T) {
                ch <- struct{}{}
        }()
        registry = prometheus.NewRegistry()
-       if probeTCP(testCTX, ln.Addr().String(), module, registry) {
+       if ProbeTCP(testCTX, ln.Addr().String(), module, registry) {
                t.Fatalf("TCP module succeeded, expected failure.")
        }
        mfs, err := registry.Gather()
@@ -137,9 +139,9 @@ func TestTCPConnectionQueryResponseMatching(t *testing.T) {
        testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
        time.Sleep(time.Millisecond * 100)
-       module := Module{
-               TCP: TCPProbe{
-                       QueryResponse: []QueryResponse{
+       module := config.Module{
+               TCP: config.TCPProbe{
+                       QueryResponse: []config.QueryResponse{
                                {
                                        Expect: "SSH-2.0-(OpenSSH_6.9p1) Debian-2",
                                        Send:   "CONFIRM ${1}",
@@ -162,7 +164,7 @@ func TestTCPConnectionQueryResponseMatching(t *testing.T) {
                ch <- version
        }()
        registry := prometheus.NewRegistry()
-       if !probeTCP(testCTX, ln.Addr().String(), module, registry) {
+       if !ProbeTCP(testCTX, ln.Addr().String(), module, registry) {
                t.Fatalf("TCP module failed, expected success.")
        }
        if got, want := <-ch, "OpenSSH_6.9p1"; got != want {
@@ -203,14 +205,14 @@ func TestTCPConnectionProtocol(t *testing.T) {
        _, port, _ := net.SplitHostPort(ln.Addr().String())
 
        // Force IPv4
-       module := Module{
-               TCP: TCPProbe{
+       module := config.Module{
+               TCP: config.TCPProbe{
                        PreferredIPProtocol: "ip4",
                },
        }
 
        registry := prometheus.NewRegistry()
-       result := probeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry)
+       result := ProbeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry)
        if !result {
                t.Fatalf("TCP protocol: \"tcp4\" connection test failed, expected success.")
        }
@@ -224,12 +226,12 @@ func TestTCPConnectionProtocol(t *testing.T) {
        checkRegistryResults(expectedResults, mfs, t)
 
        // Force IPv6
-       module = Module{
-               TCP: TCPProbe{},
+       module = config.Module{
+               TCP: config.TCPProbe{},
        }
 
        registry = prometheus.NewRegistry()
-       result = probeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry)
+       result = ProbeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry)
        if !result {
                t.Fatalf("TCP protocol: \"tcp6\" connection test failed, expected success.")
        }
@@ -243,14 +245,14 @@ func TestTCPConnectionProtocol(t *testing.T) {
        checkRegistryResults(expectedResults, mfs, t)
 
        // Prefer IPv4
-       module = Module{
-               TCP: TCPProbe{
+       module = config.Module{
+               TCP: config.TCPProbe{
                        PreferredIPProtocol: "ip4",
                },
        }
 
        registry = prometheus.NewRegistry()
-       result = probeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry)
+       result = ProbeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry)
        if !result {
                t.Fatalf("TCP protocol: \"tcp\", prefer: \"ip4\" connection test failed, expected success.")
        }
@@ -264,14 +266,14 @@ func TestTCPConnectionProtocol(t *testing.T) {
        checkRegistryResults(expectedResults, mfs, t)
 
        // Prefer IPv6
-       module = Module{
-               TCP: TCPProbe{
+       module = config.Module{
+               TCP: config.TCPProbe{
                        PreferredIPProtocol: "ip6",
                },
        }
 
        registry = prometheus.NewRegistry()
-       result = probeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry)
+       result = ProbeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry)
        if !result {
                t.Fatalf("TCP protocol: \"tcp\", prefer: \"ip6\" connection test failed, expected success.")
        }
@@ -285,12 +287,12 @@ func TestTCPConnectionProtocol(t *testing.T) {
        checkRegistryResults(expectedResults, mfs, t)
 
        // Prefer nothing
-       module = Module{
-               TCP: TCPProbe{},
+       module = config.Module{
+               TCP: config.TCPProbe{},
        }
 
        registry = prometheus.NewRegistry()
-       result = probeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry)
+       result = ProbeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry)
        if !result {
                t.Fatalf("TCP protocol: \"tcp\" connection test failed, expected success.")
        }
@@ -304,12 +306,12 @@ func TestTCPConnectionProtocol(t *testing.T) {
        checkRegistryResults(expectedResults, mfs, t)
 
        // No protocol
-       module = Module{
-               TCP: TCPProbe{},
+       module = config.Module{
+               TCP: config.TCPProbe{},
        }
 
        registry = prometheus.NewRegistry()
-       result = probeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry)
+       result = ProbeTCP(testCTX, net.JoinHostPort("localhost", port), module, registry)
        if !result {
                t.Fatalf("TCP connection test with protocol unspecified failed, expected success.")
        }
@@ -342,8 +344,8 @@ func TestPrometheusTimeoutTCP(t *testing.T) {
        testCTX, cancel := context.WithTimeout(context.Background(), 2*time.Second)
        defer cancel()
        registry := prometheus.NewRegistry()
-       if probeTCP(testCTX, ln.Addr().String(), Module{TCP: TCPProbe{
-               QueryResponse: []QueryResponse{
+       if ProbeTCP(testCTX, ln.Addr().String(), config.Module{TCP: config.TCPProbe{
+               QueryResponse: []config.QueryResponse{
                        {
                                Expect: "SSH-2.0-(OpenSSH_6.9p1) Debian-2",
                        },
similarity index 98%
rename from tls.go
rename to prober/tls.go
index fffd8b9102e50fb3d338ea8805d5779405051684..ea21dd15bf825b8cf7d12541aeda0b4b93306cf3 100644 (file)
--- a/tls.go
@@ -11,7 +11,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package prober
 
 import (
        "crypto/tls"
similarity index 98%
rename from utils.go
rename to prober/utils.go
index 1828a4fb4eb3e249c6b4a113fc5686f75a40d1db..fb3890c2917677a846b0c9b462cc8d068770ac04 100644 (file)
--- a/utils.go
@@ -1,4 +1,4 @@
-package main
+package prober
 
 import (
        "net"
similarity index 97%
rename from utils_test.go
rename to prober/utils_test.go
index c121c583ee39424202bd3d9156449a76b5186afb..a81608a662a4b0441a70a970d7f1214aa3438acb 100644 (file)
@@ -1,4 +1,4 @@
-package main
+package prober
 
 import (
        "testing"