github.com/jiasir/deis@v1.12.2/builder/tests/builder_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/deis/deis/tests/dockercli"
    11  	"github.com/deis/deis/tests/etcdutils"
    12  	"github.com/deis/deis/tests/utils"
    13  
    14  	"io/ioutil"
    15  	"os"
    16  )
    17  
    18  func TestBuilder(t *testing.T) {
    19  	var err error
    20  	var errfile error
    21  	setkeys := []string{
    22  		"/deis/registry/protocol",
    23  		"/deis/registry/host",
    24  		"/deis/registry/port",
    25  		"/deis/cache/host",
    26  		"/deis/cache/port",
    27  		"/deis/controller/protocol",
    28  		"/deis/controller/host",
    29  		"/deis/controller/port",
    30  		"/deis/controller/builderKey",
    31  	}
    32  	setdir := []string{
    33  		"/deis/controller",
    34  		"/deis/cache",
    35  		"/deis/database",
    36  		"/deis/registry",
    37  		"/deis/domains",
    38  		"/deis/services",
    39  	}
    40  	setproxy := []byte("HTTP_PROXY=\nhttp_proxy=\n")
    41  
    42  	tmpfile, errfile := ioutil.TempFile("/tmp", "deis-test-")
    43  	if errfile != nil {
    44  		t.Fatal(errfile)
    45  	}
    46  	ioutil.WriteFile(tmpfile.Name(), setproxy, 0644)
    47  	defer os.Remove(tmpfile.Name())
    48  
    49  	tag, etcdPort := utils.BuildTag(), utils.RandomPort()
    50  	imageName := utils.ImagePrefix() + "builder:" + tag
    51  	etcdName := "deis-etcd-" + tag
    52  	cli, stdout, stdoutPipe := dockercli.NewClient()
    53  	dockercli.RunTestEtcd(t, etcdName, etcdPort)
    54  	defer cli.CmdRm("-f", etcdName)
    55  	handler := etcdutils.InitEtcd(setdir, setkeys, etcdPort)
    56  	etcdutils.PublishEtcd(t, handler)
    57  	host, port := utils.HostAddress(), utils.RandomPort()
    58  	fmt.Printf("--- Run %s at %s:%s\n", imageName, host, port)
    59  
    60  	// Run a mock registry to test whether the builder can push its initial
    61  	// images.
    62  	regport := utils.RandomPort()
    63  	mockRegistry(host, regport, t)
    64  	setupRegistry("http", host, regport, t, handler)
    65  	// When we switch to Registry v2, we probably want to uncomment this
    66  	// and then remove mockRegistry.
    67  	// dockercli.RunTestRegistry(t, "registry", host, regport)
    68  
    69  	name := "deis-builder-" + tag
    70  	defer cli.CmdRm("-f", "-v", name)
    71  	go func() {
    72  		_ = cli.CmdRm("-f", "-v", name)
    73  		err = dockercli.RunContainer(cli,
    74  			"--name", name,
    75  			"--rm",
    76  			"-p", port+":2223",
    77  			"-e", "PORT=2223",
    78  			"-e", "HOST="+host,
    79  			"-e", "ETCD_PORT="+etcdPort,
    80  			"-e", "EXTERNAL_PORT="+port,
    81  			"--privileged",
    82  			"-v", tmpfile.Name()+":/etc/environment_proxy",
    83  			imageName)
    84  	}()
    85  	dockercli.PrintToStdout(t, stdout, stdoutPipe, "Builder is running")
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  	time.Sleep(5000 * time.Millisecond)
    90  	dockercli.DeisServiceTest(t, name, port, "tcp")
    91  	etcdutils.VerifyEtcdValue(t, "/deis/builder/host", host, etcdPort)
    92  	etcdutils.VerifyEtcdValue(t, "/deis/builder/port", port, etcdPort)
    93  }
    94  
    95  // mockRegistry mocks a Docker v1 registry.
    96  //
    97  // This is largely derived from the Docker repo's mock:
    98  // https://github.com/docker/docker/blob/84e917b8767c749b9bd1400a5a2253d972635bcf/registry/registry_mock_test.go
    99  func mockRegistry(host, port string, t *testing.T) {
   100  	addr := host + ":" + port
   101  
   102  	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
   103  		fmt.Printf("Mock Registry request: %s %s\n", r.Method, r.RequestURI)
   104  
   105  		if strings.Contains(r.RequestURI, "v2") {
   106  			fmt.Println("**WARNING: mockRegistry does not support the v2 API**")
   107  		}
   108  
   109  		switch r.RequestURI {
   110  		case "/v2/", "/v1/_ping":
   111  			w.WriteHeader(200)
   112  		case "/v1/repositories/deis/slugrunner/", "/v1/repositories/deis/slugbuilder/":
   113  			w.Header().Add("X-Docker-Endpoints", addr)
   114  			w.Header().Add("X-Docker-Token", fmt.Sprintf("FAKE-SESSION-%d", time.Now().UnixNano()))
   115  			w.WriteHeader(200)
   116  		case "/v1/repositories/deis/slugrunner/images", "/v1/repositories/deis/slugbuilder/images":
   117  			w.WriteHeader(204)
   118  		default:
   119  			w.Header().Add("X-Docker-Size", "2000")
   120  			w.WriteHeader(200)
   121  		}
   122  
   123  	})
   124  
   125  	fmt.Printf("Starting mock registry on %s\n", addr)
   126  	go http.ListenAndServe(addr, nil)
   127  }
   128  
   129  func setupRegistry(proto, host, port string, t *testing.T, handler *etcdutils.EtcdHandle) {
   130  	vals := map[string]string{
   131  		"/deis/registry/protocol": proto,
   132  		"/deis/registry/port":     port,
   133  		"/deis/registry/host":     host,
   134  	}
   135  
   136  	for k, v := range vals {
   137  		fmt.Printf("Setting etcd key %s to %s\n", k, v)
   138  		if _, err := handler.C.Set(k, v, 0); err != nil {
   139  			t.Fatalf("Error setting %s to %s: %s\n", k, v, err)
   140  		}
   141  	}
   142  
   143  }