github.com/mboersma/deis@v1.13.4/tests/mock/mock.go (about)

     1  // Package mock provides mock objects and setup for Deis tests.
     2  package mock
     3  
     4  import (
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/deis/deis/tests/dockercli"
    10  	"github.com/deis/deis/tests/etcdutils"
    11  	"github.com/deis/deis/tests/utils"
    12  
    13  	"github.com/docker/docker/api/client"
    14  )
    15  
    16  // RunMockDatabase starts a mock postgresql database for testing.
    17  func RunMockDatabase(t *testing.T, tag string, etcdPort string, dbPort string) {
    18  	var err error
    19  	cli, stdout, stdoutPipe := dockercli.NewClient()
    20  	done := make(chan bool, 1)
    21  	dbImage := utils.ImagePrefix() + "test-postgresql:" + utils.BuildTag()
    22  	ipaddr := utils.HostAddress()
    23  	done <- true
    24  	go func() {
    25  		<-done
    26  		err = dockercli.RunContainer(cli,
    27  			"--name", "deis-test-database-"+tag,
    28  			"--rm",
    29  			"-p", dbPort+":5432",
    30  			"-e", "POSTGRES_USER=deis",
    31  			"-e", "POSTGRES_DB=deis",
    32  			"-e", "POSTGRES_PASSWORD=deis",
    33  			dbImage)
    34  	}()
    35  	time.Sleep(1000 * time.Millisecond)
    36  	dockercli.PrintToStdout(t, stdout, stdoutPipe, "Initialization complete.")
    37  	setkeys := []string{
    38  		"/deis/database/user",
    39  		"/deis/database/password",
    40  		"/deis/database/name",
    41  	}
    42  	setdir := []string{}
    43  	dbhandler := etcdutils.InitEtcd(setdir, setkeys, etcdPort)
    44  	etcdutils.PublishEtcd(t, dbhandler)
    45  	etcdutils.SetEtcd(t,
    46  		[]string{"/deis/database/host", "/deis/database/port", "/deis/database/engine"},
    47  		[]string{ipaddr, dbPort, "postgresql_psycopg2"}, dbhandler.C)
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  }
    52  
    53  // RunMockCeph runs a container used to mock a Ceph storage cluster
    54  func RunMockCeph(t *testing.T, name string, cli *client.DockerCli, etcdPort string) {
    55  
    56  	storeImage := utils.ImagePrefix() + "mock-store:" + utils.BuildTag()
    57  	etcdutils.SetSingle(t, "/deis/store/hosts/"+utils.HostAddress(), utils.HostAddress(), etcdPort)
    58  	var err error
    59  	cli, stdout, stdoutPipe := dockercli.NewClient()
    60  	ipaddr := utils.HostAddress()
    61  	fmt.Printf("--- Running %s at %s\n", storeImage, ipaddr)
    62  	done := make(chan bool, 1)
    63  	go func() {
    64  		done <- true
    65  		err = dockercli.RunContainer(cli,
    66  			"--name", name,
    67  			"--rm",
    68  			"-e", "HOST="+ipaddr,
    69  			"-e", "ETCD_PORT="+etcdPort,
    70  			"--net=host",
    71  			storeImage)
    72  	}()
    73  	dockercli.PrintToStdout(t, stdout, stdoutPipe, "deis-store-gateway running...")
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  }