aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/yandex/tvm/tvmauth/tooltest/client_test.go
blob: a8d68e55ee3db67c278393c1fa17d44df8521727 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package tooltest

import (
	"context"
	"io/ioutil"
	"strconv"
	"testing"

	"github.com/stretchr/testify/require"

	"a.yandex-team.ru/library/go/core/log/nop"
	"a.yandex-team.ru/library/go/yandex/tvm"
	"a.yandex-team.ru/library/go/yandex/tvm/tvmauth"
)

func recipeToolOptions(t *testing.T) tvmauth.TvmToolSettings {
	var portStr, token []byte
	portStr, err := ioutil.ReadFile("tvmtool.port")
	require.NoError(t, err)

	var port int
	port, err = strconv.Atoi(string(portStr))
	require.NoError(t, err)

	token, err = ioutil.ReadFile("tvmtool.authtoken")
	require.NoError(t, err)

	return tvmauth.TvmToolSettings{Alias: "me", Port: port, AuthToken: string(token)}
}

func TestToolClient(t *testing.T) {
	c, err := tvmauth.NewToolClient(recipeToolOptions(t), &nop.Logger{})
	require.NoError(t, err)
	defer c.Destroy()

	t.Run("GetServiceTicketForID", func(t *testing.T) {
		_, err := c.GetServiceTicketForID(context.Background(), 100500)
		require.NoError(t, err)
	})

	t.Run("GetInvalidTicket", func(t *testing.T) {
		_, err := c.GetServiceTicketForID(context.Background(), 100999)
		require.Error(t, err)
		require.IsType(t, &tvm.Error{}, err)
		require.Equal(t, tvm.ErrorBrokenTvmClientSettings, err.(*tvm.Error).Code)
	})

	t.Run("ClientStatus", func(t *testing.T) {
		status, err := c.GetStatus(context.Background())
		require.NoError(t, err)

		t.Logf("Got client status: %v", status)

		require.Equal(t, tvm.ClientStatus(0), status.Status)
		require.Equal(t, "OK", status.LastError)
	})
}