github.com/etecs-ru/gnomock@v0.13.2/gnomock_internal_test.go (about) 1 package gnomock 2 3 import ( 4 "context" 5 "errors" 6 "os" 7 "runtime" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 const testImage = "docker.io/orlangure/gnomock-test-image" 16 17 func TestWaitForContainerNetwork(t *testing.T) { 18 t.Parallel() 19 20 // this test starts a regular container using existing API, and then uses 21 // it to test specific error flows 22 namedPorts := NamedPorts{ 23 "web80": TCP(80), 24 "web8080": TCP(8080), 25 } 26 container, err := StartCustom( 27 testImage, namedPorts, 28 WithTimeout(time.Second*30), 29 ) 30 id, _ := parseID(container.ID) 31 32 require.NoError(t, err) 33 require.NotNil(t, container) 34 35 gg, err := newG(false) 36 require.NoError(t, err) 37 38 d, err := gg.dockerConnect() 39 require.NoError(t, err) 40 41 ctx := context.Background() 42 43 t.Run("fails after context cancellation", func(t *testing.T) { 44 ctx, cancel := context.WithCancel(ctx) 45 cancel() 46 47 _, err = d.waitForContainerNetwork(ctx, id, namedPorts) 48 require.EqualError(t, err, "container network is unavailable after timeout") 49 }) 50 51 t.Run("fails with wrong container id", func(t *testing.T) { 52 _, err = d.waitForContainerNetwork(ctx, "wrong-id", namedPorts) 53 require.Error(t, err) 54 require.Contains(t, err.Error(), "No such container") 55 }) 56 57 t.Run("returns ErrPortNotFound for wrong port number", func(t *testing.T) { 58 _, err := d.waitForContainerNetwork(ctx, id, DefaultTCP(42)) 59 require.Error(t, err) 60 require.True(t, errors.Is(err, ErrPortNotFound), err.Error()) 61 }) 62 } 63 64 func TestEnvAwareClone(t *testing.T) { 65 // this test cannot run in parallel with other tests since it modifies the 66 // environment, which affects other tests 67 original := &Container{ 68 ID: "foo", 69 Host: "bar", 70 gateway: "gateway", 71 } 72 73 t.Run("same host returned in regular flow", func(t *testing.T) { 74 cloned := envAwareClone(original) 75 require.Equal(t, original.ID, cloned.ID) 76 require.Equal(t, original.Host, cloned.Host) 77 }) 78 79 t.Run("container gateway returned when no internal host available", func(t *testing.T) { 80 currentEnv := os.Getenv("GNOMOCK_ENV") 81 _ = os.Setenv("GNOMOCK_ENV", "gnomockd") 82 83 defer func() { 84 _ = os.Setenv("GNOMOCK_ENV", currentEnv) 85 }() 86 87 cloned := envAwareClone(original) 88 require.Equal(t, original.ID, cloned.ID) 89 if runtime.GOOS == "windows" { 90 // possible issue in https://stackoverflow.com/questions/40746453/ 91 // /how-to-connect-to-docker-host-from-container-on-windows-10-docker-for-windows 92 // or in https://docs.docker.com/docker-for-windows/networking/ 93 require.Equal(t, "host.docker.internal", cloned.Host) 94 } else { 95 require.Equal(t, original.gateway, cloned.Host) 96 } 97 }) 98 } 99 100 func TestStartCustom(t *testing.T) { 101 // this test cannot run in parallel with other tests since it modifies the 102 // environment, which affects other tests 103 t.Run("fails with misconfigured docker host", func(t *testing.T) { 104 currentHost := os.Getenv("DOCKER_HOST") 105 106 defer func() { 107 _ = os.Setenv("DOCKER_HOST", currentHost) 108 }() 109 110 _ = os.Setenv("DOCKER_HOST", "example.com") 111 112 c, err := StartCustom(testImage, DefaultTCP(80)) 113 assert.NoError(t, err) 114 require.NotNil(t, c) 115 }) 116 }