Exemplo n.º 1
0
func TestSSH_Name(t *testing.T) {
	didFetchDroplet := false

	client := &godo.Client{
		Droplets: &doit.DropletsServiceMock{
			ListFn: func(*godo.ListOptions) ([]godo.Droplet, *godo.Response, error) {
				didFetchDroplet = true
				return testDropletList, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ms := &sshMock{}
		c.SSHFn = ms.cmd()

		ns := "test"

		err := RunSSH(ns, c, ioutil.Discard, []string{testDroplet.Name})
		assert.NoError(t, err)

		assert.Equal(t, "root", ms.user)
		assert.Equal(t, testDroplet.Networks.V4[0].IPAddress, ms.host)
	})
}
Exemplo n.º 2
0
func TestRecordsUpdate(t *testing.T) {
	client := &godo.Client{
		Domains: &doit.DomainsServiceMock{
			EditRecordFn: func(name string, id int, req *godo.DomainRecordEditRequest) (*godo.DomainRecord, *godo.Response, error) {
				expected := &godo.DomainRecordEditRequest{
					Type: "A",
					Name: "foo.example.com.",
					Data: "192.168.1.1",
				}

				assert.Equal(t, "example.com", name)
				assert.Equal(t, 1, id)
				assert.Equal(t, expected, req)

				return &testRecord, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"
		c.Set(ns, doit.ArgRecordID, 1)
		c.Set(ns, doit.ArgRecordType, "A")
		c.Set(ns, doit.ArgRecordName, "foo.example.com.")
		c.Set(ns, doit.ArgRecordData, "192.168.1.1")

		err := RunRecordUpdate(ns, c, ioutil.Discard, []string{"example.com"})
		assert.NoError(t, err)
	})
}
Exemplo n.º 3
0
func TestKeysUpdateByFingerprint(t *testing.T) {
	client := &godo.Client{
		Keys: &doit.KeysServiceMock{
			UpdateByFingerprintFn: func(fingerprint string, req *godo.KeyUpdateRequest) (*godo.Key, *godo.Response, error) {
				expected := &godo.KeyUpdateRequest{
					Name: "the key",
				}
				assert.Equal(t, req, expected)
				assert.Equal(t, fingerprint, "fingerprint")
				return &testKey, nil, nil
			},
			UpdateByIDFn: func(_ int, _ *godo.KeyUpdateRequest) (*godo.Key, *godo.Response, error) {
				t.Error("should update by fingerprint")
				return nil, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"
		c.Set(ns, doit.ArgKeyName, "the key")

		RunKeyUpdate(ns, c, ioutil.Discard, []string{"fingerprint"})
	})

}
Exemplo n.º 4
0
func TestSSH_ID(t *testing.T) {
	didFetchDroplet := false

	client := &godo.Client{
		Droplets: &doit.DropletsServiceMock{
			GetFn: func(id int) (*godo.Droplet, *godo.Response, error) {
				assert.Equal(t, id, testDroplet.ID, "droplet ids did not match")
				didFetchDroplet = true
				return &testDroplet, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ms := &sshMock{}
		c.SSHFn = ms.cmd()

		ns := "test"
		err := RunSSH(ns, c, ioutil.Discard, []string{strconv.Itoa(testDroplet.ID)})
		assert.NoError(t, err)
		assert.True(t, didFetchDroplet)
		assert.True(t, ms.didRun)
		assert.Equal(t, "root", ms.user)
		assert.Equal(t, testDroplet.Networks.V4[0].IPAddress, ms.host)
	})
}
Exemplo n.º 5
0
func TestDropletCreate(t *testing.T) {
	client := &godo.Client{
		Droplets: &doit.DropletsServiceMock{
			CreateFn: func(cr *godo.DropletCreateRequest) (*godo.Droplet, *godo.Response, error) {
				expected := &godo.DropletCreateRequest{
					Name:     "droplet",
					Image:    godo.DropletCreateImage{Slug: "image"},
					Region:   "dev0",
					Size:     "1gb",
					UserData: "#cloud-config",
					SSHKeys:  []godo.DropletCreateSSHKey{},
				}

				assert.Equal(t, cr, expected, "create requests did not match")

				return &testDroplet, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"
		c.Set(ns, doit.ArgRegionSlug, "dev0")
		c.Set(ns, doit.ArgSizeSlug, "1gb")
		c.Set(ns, doit.ArgImage, "image")
		c.Set(ns, doit.ArgUserData, "#cloud-config")

		err := RunDropletCreate(ns, c, ioutil.Discard, []string{"droplet"})
		assert.NoError(t, err)
	})
}
Exemplo n.º 6
0
func TestSSHPublicKeyImportWithName(t *testing.T) {
	pubkey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCn6eZ8ve0ha04rPRZuoPXK1AQ/h21qslWCzoDcOciXn5OcyafkZw+31k/afaBTeW62D8fXd8e/1xWbFfp/2GqmslYpNCTPrtpNhsE8I0yKjJ8FxX9FfsCOu/Sv83dWgSpiT7pNWVKarZjW9KdKKRQljq1i+H5pX3r5Q9I1v+66mYTe7qsKGas9KWy0vkGoNSqmTCl+d+Y0286chtqBqBjSCUCI8oLKPnJB86Lj344tFGmzDIsJKXMVHTL0dF8n3u6iWN4qiRU+JvkoIkI3v0JvyZXxhR2uPIS1yUAY2GC+2O5mfxydJQzBdtag5Uw8Y7H5yYR1gar/h16bAy5XzRvp testkey"
	path := filepath.Join(os.TempDir(), "key.pub")
	err := ioutil.WriteFile(path, []byte(pubkey), 0600)
	assert.NoError(t, err)
	defer os.Remove(path)

	client := &godo.Client{
		Keys: &doit.KeysServiceMock{
			CreateFn: func(req *godo.KeyCreateRequest) (*godo.Key, *godo.Response, error) {
				expected := &godo.KeyCreateRequest{
					Name:      "custom",
					PublicKey: pubkey,
				}
				assert.Equal(t, req, expected)
				return &testKey, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"
		c.Set(ns, doit.ArgKeyPublicKeyFile, path)

		RunKeyImport(ns, c, ioutil.Discard, []string{"custom"})
	})
}
Exemplo n.º 7
0
func TestDropletNeighbors(t *testing.T) {
	didRun := false
	client := &godo.Client{
		Droplets: &doit.DropletsServiceMock{
			NeighborsFn: func(id int) ([]godo.Droplet, *godo.Response, error) {
				didRun = true
				assert.Equal(t, id, 1)

				resp := &godo.Response{
					Links: &godo.Links{
						Pages: &godo.Pages{},
					},
				}
				return testDropletList, resp, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"
		c.Set(ns, doit.ArgDropletID, testDroplet.ID)

		err := RunDropletNeighbors(ns, c, ioutil.Discard, []string{})
		assert.NoError(t, err)
		assert.True(t, didRun)
	})
}
Exemplo n.º 8
0
func TestLiveCommand_Start(t *testing.T) {
	lc := NewLiveCommand("/bin/ls")
	err := lc.Start("/tmp")
	assert.NoError(t, err)

	assert.Equal(t, []string{"/bin/ls", "/tmp"}, lc.cmd.Args)

	err = lc.Stop()
	assert.NoError(t, err)
}
Exemplo n.º 9
0
func TestFloatingIPActionsGet(t *testing.T) {
	client := &godo.Client{
		FloatingIPActions: &doit.FloatingIPActionsServiceMock{
			GetFn: func(ip string, actionID int) (*godo.Action, *godo.Response, error) {
				assert.Equal(t, "127.0.0.1", ip)
				assert.Equal(t, 2, actionID)
				return &testAction, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"
		c.Set(ns, doit.ArgActionID, 2)

		RunFloatingIPActionsGet(ns, c, ioutil.Discard, []string{"127.0.0.1"})
	})

}
Exemplo n.º 10
0
func TestImageActionsGet(t *testing.T) {
	client := &godo.Client{
		ImageActions: &doit.ImageActionsServiceMock{
			GetFn: func(imageID, actionID int) (*godo.Action, *godo.Response, error) {
				assert.Equal(t, imageID, 1)
				assert.Equal(t, actionID, 2)
				return &testAction, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"
		c.Set(ns, doit.ArgActionID, 2)

		RunImageActionsGet(ns, c, ioutil.Discard, []string{"1"})
	})

}
Exemplo n.º 11
0
func TestFloatingIPsCreate(t *testing.T) {
	client := &godo.Client{
		FloatingIPs: &doit.FloatingIPsServiceMock{
			CreateFn: func(req *godo.FloatingIPCreateRequest) (*godo.FloatingIP, *godo.Response, error) {
				assert.Equal(t, "dev0", req.Region)
				assert.Equal(t, 1, req.DropletID)
				return &testFloatingIP, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"
		c.Set(ns, doit.ArgRegionSlug, "dev0")
		c.Set(ns, doit.ArgDropletID, 1)

		RunFloatingIPCreate(ns, c, ioutil.Discard, []string{})
	})
}
Exemplo n.º 12
0
func TestImagesUpdate(t *testing.T) {
	client := &godo.Client{
		Images: &doit.ImagesServiceMock{
			UpdateFn: func(id int, req *godo.ImageUpdateRequest) (*godo.Image, *godo.Response, error) {
				expected := &godo.ImageUpdateRequest{Name: "new-name"}
				assert.Equal(t, req, expected)
				assert.Equal(t, id, testImage.ID)

				return &testImage, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"
		c.Set(ns, doit.ArgImageName, "new-name")

		RunImagesUpdate(ns, c, ioutil.Discard, []string{strconv.Itoa(testImage.ID)})
	})
}
Exemplo n.º 13
0
func TestMockCommmand_Run(t *testing.T) {
	mc := NewMockCommand("/bin/ls")
	assert.Equal(t, "/bin/ls", mc.path)

	runErr := errors.New("an error")
	mc.runFn = func() error {
		return runErr
	}

	_, err := mc.Run()
	assert.Error(t, err)
}
Exemplo n.º 14
0
func TestImageActionsTransfer(t *testing.T) {
	client := &godo.Client{
		ImageActions: &doit.ImageActionsServiceMock{
			TransferFn: func(imageID int, req *godo.ActionRequest) (*godo.Action, *godo.Response, error) {
				assert.Equal(t, imageID, 1)

				region := (*req)["region"]
				assert.Equal(t, region, "dev0")

				return &testAction, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"
		c.Set(ns, doit.ArgRegionSlug, "dev0")

		RunImageActionsTransfer(ns, c, ioutil.Discard, []string{"1"})
	})
}
Exemplo n.º 15
0
func TestFloatingIPsDelete(t *testing.T) {
	client := &godo.Client{
		FloatingIPs: &doit.FloatingIPsServiceMock{
			DeleteFn: func(ip string) (*godo.Response, error) {
				assert.Equal(t, "127.0.0.1", ip)
				return nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"

		RunFloatingIPDelete(ns, c, ioutil.Discard, []string{"127.0.0.1"})
	})
}
Exemplo n.º 16
0
func TestImagesDelete(t *testing.T) {
	client := &godo.Client{
		Images: &doit.ImagesServiceMock{
			DeleteFn: func(id int) (*godo.Response, error) {
				assert.Equal(t, id, testImage.ID)
				return nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"

		RunImagesDelete(ns, c, ioutil.Discard, []string{strconv.Itoa(testImage.ID)})
	})

}
Exemplo n.º 17
0
func TestDropletGet(t *testing.T) {
	client := &godo.Client{
		Droplets: &doit.DropletsServiceMock{
			GetFn: func(id int) (*godo.Droplet, *godo.Response, error) {
				assert.Equal(t, id, testDroplet.ID, "droplet ids did not match")
				return &testDroplet, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"

		err := RunDropletGet(ns, c, ioutil.Discard, []string{strconv.Itoa(testDroplet.ID)})
		assert.NoError(t, err)
	})
}
Exemplo n.º 18
0
func Test_extractHostInfo(t *testing.T) {
	cases := []struct {
		s string
		e sshHostInfo
	}{
		{s: "host", e: sshHostInfo{host: "host"}},
		{s: "root@host", e: sshHostInfo{user: "******", host: "host"}},
		{s: "root@host:22", e: sshHostInfo{user: "******", host: "host", port: "22"}},
		{s: "host:22", e: sshHostInfo{host: "host", port: "22"}},
		{s: "dokku@simple-task-02efb9c4", e: sshHostInfo{host: "simple-task-02efb9c4", user: "******"}},
	}

	for _, c := range cases {
		i := extractHostInfo(c.s)
		assert.Equal(t, c.e, i)
	}
}
Exemplo n.º 19
0
func TestFloatingIPActionsUnassign(t *testing.T) {
	client := &godo.Client{
		FloatingIPActions: &doit.FloatingIPActionsServiceMock{
			UnassignFn: func(ip string) (*godo.Action, *godo.Response, error) {

				assert.Equal(t, ip, "127.0.0.1")

				return &testAction, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"

		RunFloatingIPActionsUnassign(ns, c, ioutil.Discard, []string{"127.0.0.1"})
	})
}
Exemplo n.º 20
0
func TestKeysGetByFingerprint(t *testing.T) {
	client := &godo.Client{
		Keys: &doit.KeysServiceMock{
			GetByFingerprintFn: func(fingerprint string) (*godo.Key, *godo.Response, error) {
				assert.Equal(t, fingerprint, testKey.Fingerprint)
				return &testKey, nil, nil
			},
			GetByIDFn: func(_ int) (*godo.Key, *godo.Response, error) {
				t.Error("should not request by id")
				return nil, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"

		RunKeyGet(ns, c, ioutil.Discard, []string{testKey.Fingerprint})
	})
}
Exemplo n.º 21
0
func TestImagesGetByID(t *testing.T) {
	client := &godo.Client{
		Images: &doit.ImagesServiceMock{
			GetByIDFn: func(id int) (*godo.Image, *godo.Response, error) {
				assert.Equal(t, id, testImage.ID, "image id not equal")
				return &testImage, nil, nil
			},
			GetBySlugFn: func(slug string) (*godo.Image, *godo.Response, error) {
				t.Error("should not try to load slug")
				return nil, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"

		RunImagesGet(ns, c, ioutil.Discard, []string{strconv.Itoa(testImage.ID)})
	})
}
Exemplo n.º 22
0
func TestKeysDeleteByID(t *testing.T) {
	client := &godo.Client{
		Keys: &doit.KeysServiceMock{
			DeleteByIDFn: func(id int) (*godo.Response, error) {
				assert.Equal(t, id, 1)
				return nil, nil
			},
			DeleteByFingerprintFn: func(fingerprint string) (*godo.Response, error) {
				t.Errorf("should not call fingerprint")
				return nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"

		RunKeyDelete(ns, c, ioutil.Discard, []string{"1"})
	})
}
Exemplo n.º 23
0
func TestKeysCreate(t *testing.T) {
	client := &godo.Client{
		Keys: &doit.KeysServiceMock{
			CreateFn: func(req *godo.KeyCreateRequest) (*godo.Key, *godo.Response, error) {
				expected := &godo.KeyCreateRequest{
					Name:      "the key",
					PublicKey: "fingerprint",
				}
				assert.Equal(t, req, expected)
				return &testKey, nil, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"
		c.Set(ns, doit.ArgKeyPublicKey, "fingerprint")

		RunKeyCreate(ns, c, ioutil.Discard, []string{"the key"})
	})
}
Exemplo n.º 24
0
func TestDropletBackupList(t *testing.T) {
	client := &godo.Client{
		Droplets: &doit.DropletsServiceMock{
			BackupsFn: func(id int, opts *godo.ListOptions) ([]godo.Image, *godo.Response, error) {
				assert.Equal(t, 1, id)

				resp := &godo.Response{
					Links: &godo.Links{
						Pages: &godo.Pages{},
					},
				}
				return testImageList, resp, nil
			},
		},
	}

	withTestClient(client, func(c *TestConfig) {
		ns := "test"
		c.Set(ns, doit.ArgDropletID, 1)
		err := RunDropletBackups(ns, c, ioutil.Discard, []string{})
		assert.NoError(t, err)
	})
}
Exemplo n.º 25
0
func TestMissingArgsErr(t *testing.T) {
	err := NewMissingArgsErr("test-cmd")
	assert.Equal(t, "(test-cmd) command is missing required arguments", err.Error())
}
Exemplo n.º 26
0
func TestMockRunner(t *testing.T) {
	e := fmt.Errorf("an error")
	mr := MockRunner{e}

	assert.Equal(t, e, mr.Run())
}