proc_test.go 5.0 KB
Newer Older
O
ob-robot 已提交
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
package process

import (
	"fmt"
	"math/rand"
	"os"
	"os/user"
	"reflect"
	"strconv"
	"strings"
	"testing"
	"time"
)

func TestProc(t *testing.T) {
	proc := NewProc(ProcessConfig{
		Program: "echo",
		Args:    []string{"hello"},
	})

	err := proc.Start(nil)
	if err != nil {
		t.Error("start process failed", err)
		return
	}

	state := proc.State()
	if state.Pid <= 0 {
		t.Error("bad state pid")
	}
	if state.Exited == true {
		t.Error("bad state exited")
	}
	state = proc.Wait()
	state = proc.State()
	if state.Pid <= 0 {
		t.Error("bad state pid")
	}
	if state.Exited == false {
		t.Error("bad state exited")
	}
	if state.Success == false {
		t.Error("bad state success")
	}
	if string(state.Stdout) != "hello\n" {
		t.Errorf("stdout wrong: '%s'", string(state.Stdout))
	}
	if string(state.Stderr) != "" {
		t.Errorf("stderr wrong, '%s'", string(state.Stderr))
	}
	select {
	case <-proc.Done():
	default:
		t.Error("process not done")
	}
}

func uniqName() string {
	return fmt.Sprintf("%d_%d", time.Now().UnixNano(), rand.Int())
}

func TestProc_Stop(t *testing.T) {
	proc := NewProc(ProcessConfig{
		Program:   "sleep",
		Args:      []string{"5"},
		KillWait:  1 * time.Second,
		FinalWait: 1 * time.Second,
	})
	err := proc.Stop()
	if err == nil {
		t.Error("should failed")
	}

	_ = proc.Start(nil)
	if !proc.State().Running {
		t.Error("process should running")
	}

	proc.Stop()
	time.Sleep(1 * time.Second)
	if proc.State().Running {
		t.Error("process should be stopped")
	}
}

func TestCmd(t *testing.T) {
	cmd, err := createCmd(ProcessConfig{
		Program: "sleep",
		Args:    []string{"1"},
	})
	if err != nil {
		t.Error(err)
	}
	if !strings.HasSuffix(cmd.Path, "/sleep") || !reflect.DeepEqual(cmd.Args, []string{"sleep", "1"}) {
		t.Error("bad command")
	}

	if cmd.Stdin != nil || cmd.Stdout != nil || cmd.Stderr != nil {
		t.Error("stdio should be nil")
	}
	if cmd.SysProcAttr.Credential != nil {
		t.Error("Credential should be nil")
	}
}

func TestCmdIO(t *testing.T) {
	outFile := os.TempDir() + "/out"
	errFile := os.TempDir() + "/errors"
	defer os.Remove(outFile)
	defer os.Remove(errFile)

	cmd, err := createCmd(ProcessConfig{
		Program: "sleep",
		Args:    []string{"1"},
		Stdout:  outFile,
		Stderr:  errFile,
	})
	if err != nil {
		t.Error("create cmd failed", err)
		return
	}
	if cmd.Stdin != nil || cmd.Stdout == nil || cmd.Stderr == nil {
		t.Error("stdout, stderr should be nil")
	}
}

func TestCmdUser(t *testing.T) {
	uid := os.Getuid()
	gid := os.Getgid()
	u, err := user.LookupId(strconv.Itoa(uid))
	if err != nil {
		t.Errorf("can not find user of %d", uid)
		return
	}
	g, err := user.LookupGroupId(strconv.Itoa(gid))
	if err != nil {
		t.Errorf("can not find group of %d", gid)
		return
	}

	cmd, err := createCmd(ProcessConfig{
		Program: "sleep",
		Args:    []string{"1"},
		User:    u.Username,
		Group:   g.Name,
	})
	if err != nil {
		t.Error("create cmd failed", err)
		return
	}
	if cmd.SysProcAttr.Credential == nil {
		t.Error("Credential should not be nil")
	}
	if cmd.SysProcAttr.Credential.Uid != uint32(uid) || cmd.SysProcAttr.Credential.Gid != uint32(gid) {
		t.Errorf("bad uid or gid. expected: uid=%d,uname=%s gid=%d,gname=%s, got: uid:%d, gid:%d", uid, u.Name, gid, g.Name,
			cmd.SysProcAttr.Credential.Uid, cmd.SysProcAttr.Credential.Gid)
	}
	_, err = createCmd(ProcessConfig{
		Program: "sleep",
		Args:    []string{"1"},
		User:    "no_such_user",
	})
	if err == nil {
		t.Error("should be error")
	}
	_, err = createCmd(ProcessConfig{
		Program: "sleep",
		Args:    []string{"1"},
		Group:   "no_such_group",
	})
	if err == nil {
		t.Error("should be error")
	}
}

func TestCmdEnv(t *testing.T) {
	cmd, err := createCmd(ProcessConfig{
		Program: "sleep",
		Args:    []string{"1"},
		Envs: map[string]string{
			"NEW_ENV": "NEW_ENV_VALUE",
		},
	})
	if err != nil {
		t.Error("create process failed")
		return
	}
	for _, e := range cmd.Env {
		if e == "NEW_ENV=NEW_ENV_VALUE" {
			return
		}
	}
	t.Error("env not added")
}

func TestCmdRlimit(t *testing.T) {
	cmd, err := createCmd(ProcessConfig{
		Program: "sleep",
		Args:    []string{"1"},
		Rlimit: map[string]int64{
			RLimitProcess: 1000,
		},
	})
	if err != nil {
		t.Error("create process failed")
		return
	}
	if !strings.HasSuffix(cmd.Path, "/sh") {
		t.Error("not using sh")
	}
	if !strings.Contains(cmd.Args[2], " -u 1000") {
		t.Error("bad ulimit")
	}

	_, err = createCmd(ProcessConfig{
		Program: "sleep",
		Args:    []string{"1"},
		Rlimit: map[string]int64{
			"xxx": 1000,
		},
	})
	if err == nil {
		t.Error("bad rlimit should trigger error")
	}
}

func Test_replaceArgs(t *testing.T) {
	args := []string{"hello ${name}"}
	ret := replaceArgs(args, nil)
	if !reflect.DeepEqual(args, ret) {
		t.Error("should not replace")
	}
	ret = replaceArgs(args, replacer(map[string]string{"name": "world"}))
	if !reflect.DeepEqual(ret, []string{"hello world"}) {
		t.Error("should replaced")
	}
}

func TestRun(t *testing.T) {
	state, err := Run("echo", "hello")
	if err != nil {
		t.Error(err.Error())
	}
	if string(state.Stdout) != "hello\n" {
		t.Error("run result wrong")
	}
}