Luigit
repositories / bugabinga.net

bugabinga.net

personal infrastructure for bugabinga!

owned by admin

services/luci/internal/sshcmd/sshcmd_test.go

Raw
package sshcmd

import (
	"strings"
	"testing"
)

func TestParseAllowsRun(t *testing.T) {
	args, err := Parse("luci run repo check --ref main")
	if err != nil {
		t.Fatalf("Parse() error = %v", err)
	}
	if len(args) != 5 || args[0] != "run" || args[4] != "main" {
		t.Fatalf("args = %#v", args)
	}
}

func TestParseAllowsCiPrefixEnvCheckAndRev(t *testing.T) {
	args, err := Parse("ci env-check")
	if err != nil {
		t.Fatalf("Parse env-check error = %v", err)
	}
	if len(args) != 1 || args[0] != "env-check" {
		t.Fatalf("env-check args = %#v", args)
	}
	args, err = Parse("run repo check --rev abc")
	if err != nil {
		t.Fatalf("Parse rev error = %v", err)
	}
	if args[3] != "--rev" || args[4] != "abc" {
		t.Fatalf("rev args = %#v", args)
	}
}

func TestParseAllowsReadOnlyWebsiteCapabilities(t *testing.T) {
	for _, command := range []string{"status", "status --all", "repo smith", "show 42", "watch 42 --json --logs --timeout 30m", "log 42 001", "docs"} {
		args, err := Parse("ci " + command)
		if err != nil || len(args) == 0 {
			t.Fatalf("%q args=%#v err=%v", command, args, err)
		}
	}
}

func TestParseMapsHelpToDocs(t *testing.T) {
	args, err := Parse("ci help")
	if err != nil {
		t.Fatalf("help error = %v", err)
	}
	if len(args) != 1 || args[0] != "docs" {
		t.Fatalf("help args = %#v", args)
	}
	if _, err := Parse("ci help now"); err == nil {
		t.Fatal("help with argument accepted")
	}
}

func TestParseRejectsShell(t *testing.T) {
	if _, err := Parse("run repo check --ref main; sh"); err == nil {
		t.Fatal("shell command accepted")
	}
}

func TestParseRejectsEmptyMissingAndBadUsage(t *testing.T) {
	cases := []string{
		"",
		"luci",
		"ci",
		"run repo check --branch main",
		"run repo check --ref",
		"env-check now",
		"status --bad",
		"repo",
		"show a b",
		"watch a b",
		"watch a --json b",
		"watch --timeout",
		"watch --bad",
		"log 42 001 extra",
		"docs now",
		"serve",
		"serve --once",
	}
	for _, input := range cases {
		if _, err := Parse(input); err == nil {
			t.Fatalf("accepted %q", input)
		}
	}
}

func TestParseMapsRunsToStatusAll(t *testing.T) {
	args, err := Parse("ci runs")
	if err != nil {
		t.Fatalf("runs error = %v", err)
	}
	if len(args) != 2 || args[0] != "status" || args[1] != "--all" {
		t.Fatalf("runs args = %#v", args)
	}
}

func TestLogAcceptsRunOnlyChildIdAndJob(t *testing.T) {
	for _, input := range []string{"log", "log 42", "log 42 001", "log 42 check", "log check", "show", "show smith"} {
		if _, err := Parse("ci " + input); err != nil {
			t.Fatalf("%q rejected: %v", input, err)
		}
	}
	if _, err := Parse("ci log a b c"); err == nil {
		t.Fatal("overspecified log accepted")
	}
}

func TestParseRejectsUnknown(t *testing.T) {
	_, err := Parse("clone repo")
	if err == nil {
		t.Fatal("unknown command accepted")
	}
	if !strings.Contains(err.Error(), "see: ci docs") {
		t.Fatalf("missing docs pointer: %v", err)
	}
}