1// Copyright (C) 2019 Storj Labs, Inc.
2// See LICENSE for copying information
3
4package main
5
6import (
7	"os"
8	"testing"
9	"text/tabwriter"
10
11	"github.com/stretchr/testify/require"
12	"go.uber.org/zap"
13
14	"storj.io/common/storj"
15	"storj.io/common/testcontext"
16	"storj.io/storj/private/testplanet"
17	"storj.io/storj/satellite"
18)
19
20func TestGracefulExitTooEarly(t *testing.T) {
21	testplanet.Run(t, testplanet.Config{
22		SatelliteCount: 2, StorageNodeCount: 1, UplinkCount: 1,
23		Reconfigure: testplanet.Reconfigure{
24			Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
25				if index == 1 {
26					config.GracefulExit.NodeMinAgeInMonths = 0
27				} else {
28					config.GracefulExit.NodeMinAgeInMonths = 6
29				}
30			},
31		},
32	}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
33		address := planet.StorageNodes[0].Server.PrivateAddr().String()
34
35		client, err := dialGracefulExitClient(ctx, address)
36		require.NoError(t, err)
37
38		response, err := client.gracefulExitFeasibility(ctx, planet.Satellites[0].ID())
39		require.NoError(t, err)
40		require.Equal(t, response.IsAllowed, false)
41
42		response2, err := client.gracefulExitFeasibility(ctx, planet.Satellites[1].ID())
43		require.NoError(t, err)
44		require.Equal(t, response2.IsAllowed, true)
45	})
46}
47
48func TestGracefulExitInit(t *testing.T) {
49	testplanet.Run(t, testplanet.Config{
50		SatelliteCount: 2, StorageNodeCount: 1, UplinkCount: 1,
51		Reconfigure: testplanet.Reconfigure{
52			Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
53				if index == 1 {
54					config.GracefulExit.NodeMinAgeInMonths = 0
55				} else {
56					config.GracefulExit.NodeMinAgeInMonths = 6
57				}
58			},
59		},
60	}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
61		var satelliteIDs []storj.NodeID
62		w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
63
64		address := planet.StorageNodes[0].Server.PrivateAddr().String()
65		satelliteIDs = append(satelliteIDs, planet.Satellites[0].ID(), planet.Satellites[1].ID())
66
67		client, err := dialGracefulExitClient(ctx, address)
68		require.NoError(t, err)
69
70		err = gracefulExitInit(ctx, satelliteIDs, w, client)
71		require.Error(t, err)
72	})
73}
74