summaryrefslogtreecommitdiff
path: root/internal/control/upgrade_test.go
blob: c3e5404356341c716ba9c69657bcd9d4d1f5aeef (plain)
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
package control

import (
	"context"
	"strings"
	"testing"
)

type fakeRunner struct{}

func (fakeRunner) Run(ctx context.Context, node Node, script string) (*CommandResult, error) {
	if strings.Contains(script, "HEALTHZ_HTTP_CODE=") {
		return &CommandResult{
			Stdout: "{\"Service\":\"sing-box\",\"Status\":\"running\"}\nHEALTHZ_HTTP_CODE=200\n",
		}, nil
	}
	return &CommandResult{Stdout: "ok\n"}, nil
}

func (fakeRunner) Check(ctx context.Context, node Node) (*CommandResult, error) {
	return &CommandResult{Stdout: "ok"}, nil
}

func (fakeRunner) CopyFile(ctx context.Context, node Node, localPath, remotePath string) error {
	return nil
}

func TestUpgradeNode(t *testing.T) {
	t.Parallel()

	state, err := UpgradeNode(context.Background(), fakeRunner{}, Node{
		ID:      "nl-01",
		Name:    "NL 01",
		Region:  "nl",
		Host:    "203.0.113.10",
		Domain:  "nl-01.example.com",
		Enabled: true,
		SSH:     SSHConfig{User: "root", Port: 22, Auth: "key", IdentityFile: "~/.ssh/id_ed25519"},
		Protocols: []ProtocolProfile{
			{Type: "vless", Enabled: true, Port: 443, TLS: &TLSProfile{Enabled: true, ServerName: "nl-01.example.com"}, Auth: &AuthProfile{UUID: "11111111-1111-1111-1111-111111111111"}, Extra: map[string]any{"path": "/ws"}},
		},
	}, t.TempDir())
	if err != nil {
		t.Fatalf("UpgradeNode() error = %v", err)
	}
	if state == nil {
		t.Fatal("expected state")
	}
	if state.BootstrapStatus != "healthy" {
		t.Fatalf("BootstrapStatus = %q, want healthy", state.BootstrapStatus)
	}
	if got := state.Metadata["lifecycle_action"]; got != "upgrade" {
		t.Fatalf("lifecycle_action = %v, want upgrade", got)
	}
}