Skip to content

Commit de2a482

Browse files
dudejasarampricea-hassanin
authored
check if err is nil on metrics server run (#119)
* check if err is nil on metrics server run * add documentation about running the tests * add tests for metrics server start failure * Specs: address linter issue * Add script/test-unit information --------- Co-authored-by: aram price <aramprice@users.noreply.github.com> Co-authored-by: Ahmed Hassanin <ahmed.hassanin@sap.com>
1 parent 73f15ed commit de2a482

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,29 @@ This repository is a `GOPATH`. The [`.envrc`](.envrc) file provides a setup that
1818
Be careful with `go get`. In this repository `go get` will end up putting artifacts in the `src` directory, which you probably don't want to commit. It's impractical to `.gitignore` every possible package root in there so we have to apply discipline.
1919

2020
To build a dev release, run a `bosh create-release` from this repo.
21+
22+
## Running the tests
23+
24+
Before the tests can be run, you need to execute the following commands:
25+
26+
### On macOS
27+
28+
```bash
29+
sudo ifconfig lo0 alias 127.0.0.2 up
30+
sudo ifconfig lo0 alias 127.0.0.3 up
31+
sudo ifconfig lo0 alias 127.0.0.253 up
32+
sudo ifconfig lo0 alias 127.0.0.254 up
33+
sudo sysctl -w kern.ipc.somaxconn=1024 # default is 128
34+
```
35+
36+
Then run the tests:
37+
38+
```bash
39+
./scripts/test-unit
40+
```
41+
42+
You could also use ginkgo to run a specific test suite
43+
44+
```bash
45+
ginkgo -v --procs=1 src/bosh-dns/dns/
46+
```

src/bosh-dns/dns/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ func mainExitCode() int {
251251
if metricsServerWrapper != nil {
252252
go func() {
253253
err := metricsServerWrapper.Run(shutdown)
254-
logger.Error(logTag, "could not start metric server: %s", err.Error())
254+
if err != nil {
255+
logger.Error(logTag, "could not start metrics server: %s", err.Error())
256+
}
255257
}()
256258
}
257259

src/bosh-dns/dns/main_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,6 +1875,94 @@ var _ = Describe("main", func() {
18751875
Eventually(session.Out).Should(gbytes.Say(`[main].*ERROR - loading addresses configuration:.*addresses config file malformed:`))
18761876
Expect(session.Out.Contents()).To(ContainSubstring(fmt.Sprintf(`addresses config file malformed: %s`, addressesFile2.Name())))
18771877
})
1878+
Context("metrics server error scenarios", func() {
1879+
var (
1880+
session *gexec.Session
1881+
)
1882+
1883+
AfterEach(func() {
1884+
if session != nil {
1885+
session.Kill()
1886+
session.Wait()
1887+
}
1888+
})
1889+
1890+
It("logs error when metrics server fails to start due to port conflict", func() {
1891+
dnsPort, err := testhelpers.GetFreePort()
1892+
Expect(err).NotTo(HaveOccurred())
1893+
1894+
apiPort, err := testhelpers.GetFreePort()
1895+
Expect(err).NotTo(HaveOccurred())
1896+
1897+
metricsPort, err := testhelpers.GetFreePort()
1898+
Expect(err).NotTo(HaveOccurred())
1899+
1900+
// Create a conflict by binding to the metrics port first
1901+
conflictListener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", metricsPort))
1902+
Expect(err).NotTo(HaveOccurred())
1903+
defer conflictListener.Close() //nolint:errcheck
1904+
1905+
cfg := config.NewDefaultConfig()
1906+
cfg.Address = listenAddress
1907+
cfg.Port = dnsPort
1908+
cfg.JobsDir = jobsDir
1909+
cfg.Metrics = config.MetricsConfig{
1910+
Enabled: true,
1911+
Address: "127.0.0.1",
1912+
Port: metricsPort, // This will conflict with our listener
1913+
}
1914+
cfg.API = config.APIConfig{
1915+
Port: apiPort,
1916+
CAFile: "api/assets/test_certs/test_ca.pem",
1917+
CertificateFile: "api/assets/test_certs/test_server.pem",
1918+
PrivateKeyFile: "api/assets/test_certs/test_server.key",
1919+
}
1920+
1921+
cmd = newCommandWithConfig(cfg)
1922+
session, err = gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
1923+
Expect(err).NotTo(HaveOccurred())
1924+
1925+
// Wait for DNS server to start (so we know the process is running)
1926+
Expect(testhelpers.WaitForListeningTCP(dnsPort)).To(Succeed())
1927+
1928+
// Verify the error is logged
1929+
Eventually(session.Out, 10*time.Second).Should(
1930+
gbytes.Say(`\[main\].*ERROR.*could not start metrics server:`),
1931+
)
1932+
})
1933+
1934+
It("logs error when metrics server fails due to invalid address", func() {
1935+
dnsPort, err := testhelpers.GetFreePort()
1936+
Expect(err).NotTo(HaveOccurred())
1937+
1938+
apiPort, err := testhelpers.GetFreePort()
1939+
Expect(err).NotTo(HaveOccurred())
1940+
1941+
cfg := config.NewDefaultConfig()
1942+
cfg.Address = listenAddress
1943+
cfg.Port = dnsPort
1944+
cfg.JobsDir = jobsDir
1945+
cfg.Metrics = config.MetricsConfig{
1946+
Enabled: true,
1947+
Address: "invalid.address.that.does.not.exist",
1948+
Port: 8080,
1949+
}
1950+
cfg.API = config.APIConfig{
1951+
Port: apiPort,
1952+
CAFile: "api/assets/test_certs/test_ca.pem",
1953+
CertificateFile: "api/assets/test_certs/test_server.pem",
1954+
PrivateKeyFile: "api/assets/test_certs/test_server.key",
1955+
}
1956+
1957+
cmd = newCommandWithConfig(cfg)
1958+
session, err = gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
1959+
Expect(err).NotTo(HaveOccurred())
1960+
1961+
Eventually(session.Out, 10*time.Second).Should(
1962+
gbytes.Say(`\[main\].*ERROR.*could not start metrics server:`),
1963+
)
1964+
})
1965+
})
18781966
})
18791967
})
18801968

0 commit comments

Comments
 (0)