@@ -3,6 +3,7 @@ package docker
33import (
44 "context"
55 "crypto/tls"
6+ "errors"
67 "io"
78 "reflect"
89 "sort"
@@ -1774,3 +1775,84 @@ func TestPodmanStatsCache(t *testing.T) {
17741775 require .NotContains (t , d .statsCache , "old-container" )
17751776 require .Contains (t , d .statsCache , testID )
17761777}
1778+
1779+ func TestStartWithUnavailableDocker (t * testing.T ) {
1780+ // Test that Telegraf starts successfully even when Docker is unavailable
1781+ // This is a regression test for https://github.com/influxdata/telegraf/issues/18089
1782+ var acc testutil.Accumulator
1783+ d := Docker {
1784+ Log : testutil.Logger {},
1785+ newClient : func (string , * tls.Config ) (dockerClient , error ) {
1786+ return nil , errors .New ("cannot connect to the Docker daemon" )
1787+ },
1788+ newEnvClient : func () (dockerClient , error ) {
1789+ return nil , errors .New ("cannot connect to the Docker daemon" )
1790+ },
1791+ }
1792+
1793+ require .NoError (t , d .Init ())
1794+ // Start should NOT return an error even when Docker is unavailable
1795+ require .NoError (t , d .Start (& acc ))
1796+ // Client should be nil since connection failed
1797+ require .Nil (t , d .client )
1798+
1799+ // Gather should return an error since Docker is still unavailable
1800+ err := d .Gather (& acc )
1801+ require .Error (t , err )
1802+ require .Contains (t , err .Error (), "failed to connect to Docker daemon" )
1803+ }
1804+
1805+ func TestLazyClientInitialization (t * testing.T ) {
1806+ // Test that client is initialized lazily on first Gather if Start failed to connect
1807+ var acc testutil.Accumulator
1808+
1809+ // Track connection attempts
1810+ connectionAttempts := 0
1811+
1812+ d := Docker {
1813+ Log : testutil.Logger {},
1814+ newClient : func (string , * tls.Config ) (dockerClient , error ) {
1815+ connectionAttempts ++
1816+ // First attempt fails, subsequent attempts succeed
1817+ if connectionAttempts == 1 {
1818+ return nil , errors .New ("docker daemon not ready" )
1819+ }
1820+ return & mockClient {
1821+ InfoF : func () (system.Info , error ) {
1822+ return system.Info {
1823+ Name : "docker-desktop" ,
1824+ ServerVersion : "20.10.0" ,
1825+ }, nil
1826+ },
1827+ ContainerListF : func (container.ListOptions ) ([]container.Summary , error ) {
1828+ return nil , nil
1829+ },
1830+ ClientVersionF : func () string {
1831+ return "1.24.0"
1832+ },
1833+ CloseF : func () error {
1834+ return nil
1835+ },
1836+ }, nil
1837+ },
1838+ newEnvClient : func () (dockerClient , error ) {
1839+ return nil , errors .New ("not using env client" )
1840+ },
1841+ }
1842+
1843+ require .NoError (t , d .Init ())
1844+ // Start should succeed even though connection fails
1845+ require .NoError (t , d .Start (& acc ))
1846+ require .Equal (t , 1 , connectionAttempts )
1847+ require .Nil (t , d .client )
1848+
1849+ // First Gather fails because Docker is still unavailable (same mock returns error on attempt 1)
1850+ // Reset connection attempts to simulate Docker becoming available
1851+ connectionAttempts = 1 // Set to 1 so next attempt (2) will succeed
1852+
1853+ // Second Gather should succeed after lazy initialization
1854+ err := d .Gather (& acc )
1855+ require .NoError (t , err )
1856+ require .Equal (t , 2 , connectionAttempts )
1857+ require .NotNil (t , d .client )
1858+ }
0 commit comments