github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/build/docker/client_test.go (about)

     1  package docker
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func TestHostFromEnv(t *testing.T) {
    10  	os.Setenv("DOCKER_HOST", "tcp://1.1.1.1:4243")
    11  	defer os.Setenv("DOCKER_HOST", "")
    12  
    13  	client := New()
    14  
    15  	if client.proto != "tcp" {
    16  		t.Fail()
    17  	}
    18  
    19  	if client.addr != "1.1.1.1:4243" {
    20  		t.Fail()
    21  	}
    22  }
    23  
    24  func TestInvalidHostFromEnv(t *testing.T) {
    25  	os.Setenv("DOCKER_HOST", "tcp:1.1.1.1:4243") // missing tcp:// prefix
    26  	defer os.Setenv("DOCKER_HOST", "")
    27  
    28  	client := New()
    29  
    30  	if client.addr == "1.1.1.1:4243" {
    31  		t.Fail()
    32  	}
    33  }
    34  
    35  func TestSocketHost(t *testing.T) {
    36  	// create temporary file to represent the docker socket
    37  	file, err := ioutil.TempFile("", "TestDefaultUnixHost")
    38  	if err != nil {
    39  		t.Fail()
    40  	}
    41  	file.Close()
    42  	defer os.Remove(file.Name())
    43  
    44  	client := &Client{}
    45  	client.setHost(file.Name())
    46  
    47  	if client.proto != "unix" {
    48  		t.Fail()
    49  	}
    50  
    51  	if client.addr != file.Name() {
    52  		t.Fail()
    53  	}
    54  }
    55  
    56  func TestDefaultTcpHost(t *testing.T) {
    57  	client := &Client{}
    58  	client.setHost("/tmp/missing_socket")
    59  
    60  	if client.proto != "tcp" {
    61  		t.Fail()
    62  	}
    63  
    64  	if client.addr != "0.0.0.0:4243" {
    65  		t.Fail()
    66  	}
    67  }