Exemple #1
0
func TestGetScpCmd(t *testing.T) {
	mcn, _ := libmachine.New(ScpFakeStore{})

	// TODO: This is a little "integration-ey".  Perhaps
	// make an ScpDispatcher (name?) interface so that the reliant
	// methods can be mocked.
	expectedArgs := append(
		baseSSHArgs,
		"-3",
		"-i",
		"/fake/keypath/id_rsa",
		"/tmp/foo",
		"[email protected]:/home/docker/foo",
	)
	expectedCmd := exec.Command("/usr/bin/scp", expectedArgs...)

	cmd, err := getScpCmd("/tmp/foo", "myfunhost:/home/docker/foo", append(baseSSHArgs, "-3"), *mcn)
	if err != nil {
		t.Fatalf("Unexpected err getting scp command: %s", err)
	}

	correct := reflect.DeepEqual(expectedCmd, cmd)
	if !correct {
		fmt.Println(expectedCmd)
		fmt.Println(cmd)
		t.Fatal("Expected scp cmd structs to be equal but there was mismatch")
	}
}
Exemple #2
0
func main() {
	flag.Parse()

	log.Printf("using store: %s\n", storePath)

	store := libmachine.NewFilestore(storePath, "", "")

	m, err := libmachine.New(store)
	if err != nil {
		log.Fatal(err)
	}

	mcn = m

	wsContainer := restful.NewContainer()
	h := HostResource{}
	h.Register(wsContainer)

	config := swagger.Config{
		WebServices:     wsContainer.RegisteredWebServices(),
		WebServicesUrl:  "http://localhost:8080",
		ApiPath:         "/apidocs.json",
		SwaggerPath:     "/apidocs/",
		SwaggerFilePath: "swagger"}

	swagger.RegisterSwaggerService(config, wsContainer)

	log.Printf("start listening on localhost:8080")
	server := &http.Server{Addr: ":8080", Handler: wsContainer}
	log.Fatal(server.ListenAndServe())
}
Exemple #3
0
func NewCluster(gopt *options.Options) (*Cluster, error) {
	rootPath := gopt.String("storage-path")
	os.Setenv("MACHINE_STORAGE_PATH", rootPath)
	if gopt.Bool("native-ssh") {
		ssh.SetDefaultClient(ssh.Native)
	}

	auth := getTLSAuthOptions(gopt)
	store := libmachine.NewFilestore(rootPath, auth.CaCertPath, auth.CaKeyPath)
	provider, _ := libmachine.New(store)
	hosts, err := provider.List()
	if err != nil {
		return nil, err
	}
	machines := make(map[string]*Machine, len(hosts))
	for _, h := range hosts {
		machines[h.Name] = &Machine{
			Host: h,
		}
	}

	c := &Cluster{
		provider:    provider,
		machines:    machines,
		authOptions: auth,
	}
	c.LoadStates()
	return c, nil
}
Exemple #4
0
func TestGetInfoForScpArg(t *testing.T) {
	mcn, _ := libmachine.New(ScpFakeStore{})

	expectedPath := "/tmp/foo"
	host, path, opts, err := getInfoForScpArg("/tmp/foo", *mcn)
	if err != nil {
		t.Fatalf("Unexpected error in local getInfoForScpArg call: %s", err)
	}
	if path != expectedPath {
		t.Fatalf("Path %s not equal to expected path %s", path, expectedPath)
	}
	if host != nil {
		t.Fatal("host should be nil")
	}
	if opts != nil {
		t.Fatal("opts should be nil")
	}

	host, path, opts, err = getInfoForScpArg("myfunhost:/home/docker/foo", *mcn)
	if err != nil {
		t.Fatal("Unexpected error in machine-based getInfoForScpArg call: %s", err)
	}
	expectedOpts := []string{
		"-i",
		"/fake/keypath/id_rsa",
	}
	for i := range opts {
		if expectedOpts[i] != opts[i] {
			t.Fatalf("Mismatch in returned opts: %s != %s", expectedOpts[i], opts[i])
		}
	}
	if host.Name != "myfunhost" {
		t.Fatal("Expected host.Name to be myfunhost, got %s", host.Name)
	}
	if path != "/home/docker/foo" {
		t.Fatalf("Expected path to be /home/docker/foo, got %s", path)
	}

	host, path, opts, err = getInfoForScpArg("foo:bar:widget", *mcn)
	if err != ErrMalformedInput {
		t.Fatalf("Didn't get back an error when we were expecting it for malformed args")
	}
}
Exemple #5
0
func newProvider(store libmachine.Store) (*libmachine.Provider, error) {
	return libmachine.New(store)
}
Exemple #6
0
func TestCmdEnvBash(t *testing.T) {
	stdout := os.Stdout
	shell := os.Getenv("SHELL")
	r, w, _ := os.Pipe()

	os.Stdout = w
	os.Setenv("MACHINE_STORAGE_PATH", TestStoreDir)
	os.Setenv("SHELL", "/bin/bash")

	defer func() {
		os.Setenv("MACHINE_STORAGE_PATH", "")
		os.Setenv("SHELL", shell)
		os.Stdout = stdout
	}()

	if err := clearHosts(); err != nil {
		t.Fatal(err)
	}

	flags := getTestDriverFlags()

	store, sErr := getTestStore()
	if sErr != nil {
		t.Fatal(sErr)
	}

	mcn, err := libmachine.New(store)
	if err != nil {
		t.Fatal(err)
	}

	hostOptions := &libmachine.HostOptions{
		EngineOptions: &engine.EngineOptions{},
		SwarmOptions: &swarm.SwarmOptions{
			Master:    false,
			Discovery: "",
			Address:   "",
			Host:      "",
		},
		AuthOptions: &auth.AuthOptions{},
	}

	host, err := mcn.Create("test-a", "none", hostOptions, flags)
	if err != nil {
		t.Fatal(err)
	}

	host, err = mcn.Get("test-a")
	if err != nil {
		t.Fatalf("error loading host: %v", err)
	}

	if err := mcn.SetActive(host); err != nil {
		t.Fatalf("error setting active host: %v", err)
	}

	outStr := make(chan string)

	go func() {
		var testOutput bytes.Buffer
		io.Copy(&testOutput, r)
		outStr <- testOutput.String()
	}()

	set := flag.NewFlagSet("config", 0)
	c := cli.NewContext(nil, set, set)
	cmdEnv(c)

	w.Close()

	out := <-outStr

	// parse the output into a map of envvar:value for easier testing below
	envvars := make(map[string]string)
	for _, e := range strings.Split(strings.TrimSpace(out), "\n") {
		if !strings.HasPrefix(e, "export ") {
			continue
		}
		kv := strings.SplitN(e, "=", 2)
		key, value := kv[0], kv[1]
		envvars[strings.Replace(key, "export ", "", 1)] = value
	}

	testMachineDir := filepath.Join(store.GetPath(), "machines", host.Name)

	expected := map[string]string{
		"DOCKER_TLS_VERIFY": "1",
		"DOCKER_CERT_PATH":  fmt.Sprintf("\"%s\"", testMachineDir),
		"DOCKER_HOST":       "unix:///var/run/docker.sock",
	}

	for k, v := range envvars {
		if v != expected[k] {
			t.Fatalf("Expected %s == <%s>, but was <%s>", k, expected[k], v)
		}
	}
}
Exemple #7
0
func TestCmdConfig(t *testing.T) {
	defer cleanup()

	stdout := os.Stdout
	r, w, _ := os.Pipe()

	os.Stdout = w

	defer func() {
		os.Stdout = stdout
	}()

	store, err := getTestStore()
	if err != nil {
		t.Fatal(err)
	}

	mcn, err := libmachine.New(store)
	if err != nil {
		t.Fatal(err)
	}

	flags := getTestDriverFlags()
	hostOptions := &libmachine.HostOptions{
		EngineOptions: &engine.EngineOptions{},
		SwarmOptions: &swarm.SwarmOptions{
			Master:    false,
			Discovery: "",
			Address:   "",
			Host:      "",
		},
		AuthOptions: &auth.AuthOptions{},
	}

	host, err := mcn.Create("test-a", "none", hostOptions, flags)
	if err != nil {
		t.Fatal(err)
	}

	outStr := make(chan string)

	go func() {
		var testOutput bytes.Buffer
		io.Copy(&testOutput, r)
		outStr <- testOutput.String()
	}()

	set := flag.NewFlagSet("config", 0)
	set.Parse([]string{"test-a"})
	globalSet := flag.NewFlagSet("test", 0)
	globalSet.String("storage-path", store.GetPath(), "")

	c := cli.NewContext(nil, set, globalSet)

	cmdConfig(c)

	w.Close()

	out := <-outStr

	if !strings.Contains(out, "--tlsverify") {
		t.Fatalf("Expect --tlsverify")
	}

	testMachineDir := filepath.Join(store.GetPath(), "machines", host.Name)

	tlscacert := fmt.Sprintf("--tlscacert=\"%s/ca.pem\"", testMachineDir)
	if !strings.Contains(out, tlscacert) {
		t.Fatalf("Expected to find %s in %s", tlscacert, out)
	}

	tlscert := fmt.Sprintf("--tlscert=\"%s/cert.pem\"", testMachineDir)
	if !strings.Contains(out, tlscert) {
		t.Fatalf("Expected to find %s in %s", tlscert, out)
	}

	tlskey := fmt.Sprintf("--tlskey=\"%s/key.pem\"", testMachineDir)
	if !strings.Contains(out, tlskey) {
		t.Fatalf("Expected to find %s in %s", tlskey, out)
	}

	if !strings.Contains(out, "-H=unix:///var/run/docker.sock") {
		t.Fatalf("Expect docker host URL")
	}
}
Exemple #8
0
func newMcn(store libmachine.Store) (*libmachine.Machine, error) {
	return libmachine.New(store)
}
Exemple #9
0
func runInspectCommand(t *testing.T, args []string) (string, *libmachine.Host) {
	stdout := os.Stdout
	stderr := os.Stderr
	shell := os.Getenv("SHELL")
	r, w, _ := os.Pipe()

	os.Stdout = w
	os.Stderr = w
	os.Setenv("MACHINE_STORAGE_PATH", TestStoreDir)
	os.Setenv("SHELL", "/bin/bash")

	defer func() {
		os.Setenv("MACHINE_STORAGE_PATH", "")
		os.Setenv("SHELL", shell)
		os.Stdout = stdout
		os.Stderr = stderr
	}()

	if err := clearHosts(); err != nil {
		t.Fatal(err)
	}

	store, sErr := getTestStore()
	if sErr != nil {
		t.Fatal(sErr)
	}

	provider, err := libmachine.New(store)
	if err != nil {
		t.Fatal(err)
	}

	hostOptions := &libmachine.HostOptions{
		EngineOptions: &engine.EngineOptions{},
		SwarmOptions: &swarm.SwarmOptions{
			Master:    false,
			Discovery: "",
			Address:   "",
			Host:      "",
		},
		AuthOptions: &auth.AuthOptions{},
	}

	flags := getTestDriverFlags()
	_, err = provider.Create("test-a", "none", hostOptions, flags)
	if err != nil {
		t.Fatal(err)
	}

	outStr := make(chan string)

	go func() {
		var testOutput bytes.Buffer
		io.Copy(&testOutput, r)
		outStr <- testOutput.String()
	}()

	set := flag.NewFlagSet("inspect", 0)
	set.String("format", "", "")
	set.Parse(args)
	c := cli.NewContext(nil, set, set)
	cmdInspect(c)

	w.Close()

	out := <-outStr

	return strings.TrimSpace(out), getHost(c)
}