github.com/hy3/cuto@v0.9.8-0.20160830082821-aa6652f877b7/servant/job/jobcheck_test.go (about)

     1  package job
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/unirita/cuto/message"
    10  	"github.com/unirita/cuto/servant/config"
    11  	"github.com/unirita/cuto/testutil"
    12  	"github.com/unirita/cuto/utctime"
    13  )
    14  
    15  func init() {
    16  	time.Local = time.FixedZone("JST", 9*60*60)
    17  }
    18  
    19  func getJobCheckTestConfig() *config.ServantConfig {
    20  	testDir := filepath.Join(testutil.GetBaseDir(), "servant", "job", "_testdata", "jobcheck")
    21  
    22  	conf := new(config.ServantConfig)
    23  	conf.Dir.JoblogDir = filepath.Join(testDir, "joblog")
    24  	conf.Dir.LogDir = filepath.Join(testDir, "log")
    25  	return conf
    26  }
    27  
    28  func TestDoJobResultCheck_Base(t *testing.T) {
    29  	chk := &message.JobCheck{
    30  		Type:    "jobcheck",
    31  		Version: "1.2.3",
    32  		NID:     1,
    33  		JID:     "job1",
    34  	}
    35  
    36  	result := DoJobResultCheck(chk, getJobCheckTestConfig())
    37  	if result == nil {
    38  		t.Fatalf("DoJobResultCheck() returned nil.")
    39  	}
    40  	if result.NID != 1 {
    41  		t.Errorf("result.NID => %d, wants %d", result.NID, 1)
    42  	}
    43  	if result.JID != "job1" {
    44  		t.Errorf("result.JID => %s, wants %s", result.JID, "job1")
    45  	}
    46  	if result.RC != 5 {
    47  		t.Errorf("result.RC => %d, wants %d", result.RC, 5)
    48  	}
    49  	if result.Stat != 2 {
    50  		t.Errorf("result.Stat => %d, wants %d", result.Stat, 2)
    51  	}
    52  	if result.Var != "testvar" {
    53  		t.Errorf("result.Var => %s, wants %s", result.Var, "testvar")
    54  	}
    55  	if result.St != "2015-08-01 03:05:25.123" {
    56  		t.Errorf("result.St => %s, wants %s", result.St, "2015-08-01 03:05:25.123")
    57  	}
    58  	if result.Et != "2015-08-01 03:34:56.789" {
    59  		t.Errorf("result.Et => %s, wants %s", result.Et, "2015-08-01 03:34:56.789")
    60  	}
    61  }
    62  
    63  func TestDoJobResultCheck_RegardNoRecordJobAsUnexecuted(t *testing.T) {
    64  	chk := &message.JobCheck{
    65  		Type:    "jobcheck",
    66  		Version: "1.2.3",
    67  		NID:     1,
    68  		JID:     "noexists",
    69  	}
    70  
    71  	result := DoJobResultCheck(chk, getJobCheckTestConfig())
    72  	if result == nil {
    73  		t.Fatalf("DoJobResultCheck() returned nil.")
    74  	}
    75  	if result.NID != 1 {
    76  		t.Errorf("result.NID => %d, wants %d", result.NID, 1)
    77  	}
    78  	if result.JID != "noexists" {
    79  		t.Errorf("result.JID => %s, wants %s", result.JID, "noexists")
    80  	}
    81  	if result.Stat != -1 {
    82  		t.Errorf("result.Stat => %d, wants %d", result.Stat, -1)
    83  	}
    84  }
    85  
    86  func TestDoJobResultCheck_DifferentNID(t *testing.T) {
    87  	chk := &message.JobCheck{
    88  		Type:    "jobcheck",
    89  		Version: "1.2.3",
    90  		NID:     2,
    91  		JID:     "job1",
    92  	}
    93  
    94  	result := DoJobResultCheck(chk, getJobCheckTestConfig())
    95  	if result == nil {
    96  		t.Fatalf("DoJobResultCheck() returned nil.")
    97  	}
    98  	if result.NID != 2 {
    99  		t.Errorf("result.NID => %d, wants %d", result.NID, 1)
   100  	}
   101  	if result.JID != "job1" {
   102  		t.Errorf("result.JID => %s, wants %s", result.JID, "job1")
   103  	}
   104  	if result.RC != 15 {
   105  		t.Errorf("result.RC => %d, wants %d", result.RC, 15)
   106  	}
   107  	if result.Stat != 9 {
   108  		t.Errorf("result.Stat => %d, wants %d", result.Stat, 9)
   109  	}
   110  	if result.Var != "error" {
   111  		t.Errorf("result.Var => %s, wants %s", result.Var, "error")
   112  	}
   113  	if result.St != "2015-07-31 14:45:56.321" {
   114  		t.Errorf("result.St => %s, wants %s", result.St, "2015-07-31 14:45:56.321")
   115  	}
   116  	if result.Et != "2015-07-31 15:12:34.567" {
   117  		t.Errorf("result.Et => %s, wants %s", result.Et, "2015-07-31 15:12:34.567")
   118  	}
   119  }
   120  
   121  func TestDoJobResultCheck_RegardOnlyStartJobAsExecuting(t *testing.T) {
   122  	chk := &message.JobCheck{
   123  		Type:    "jobcheck",
   124  		Version: "1.2.3",
   125  		NID:     3,
   126  		JID:     "job1",
   127  	}
   128  
   129  	result := DoJobResultCheck(chk, getJobCheckTestConfig())
   130  	if result == nil {
   131  		t.Fatalf("DoJobResultCheck() returned nil.")
   132  	}
   133  	if result.NID != 3 {
   134  		t.Errorf("result.NID => %d, wants %d", result.NID, 3)
   135  	}
   136  	if result.JID != "job1" {
   137  		t.Errorf("result.JID => %s, wants %s", result.JID, "job1")
   138  	}
   139  	if result.Stat != 0 {
   140  		t.Errorf("result.Stat => %d, wants %d", result.Stat, 0)
   141  	}
   142  }
   143  
   144  func TestSearchEndRecordFromLogEndRecord(t *testing.T) {
   145  	conf := getJobCheckTestConfig()
   146  	logPath := filepath.Join(conf.Dir.LogDir, "servant.log")
   147  	record, err := searchJobEndRecordFromLog(logPath, 1, "job1")
   148  	if err != nil {
   149  		t.Fatalf("Unexpected error occured: %s", err)
   150  	}
   151  
   152  	expected := `2015-08-01 12:34:56.789 [16594] [INF] CTS011I JOB [/home/cuto/testjob] ENDED. INSTANCE [1] ID [job1] STATUS [2] RC [5].`
   153  	if record != expected {
   154  		t.Errorf("Record is not expected value.")
   155  		t.Log("Actual:")
   156  		t.Log(record)
   157  		t.Log("Expected: ")
   158  		t.Log(expected)
   159  	}
   160  }
   161  
   162  func TestSearchEndRecordFromLog_StartRecord(t *testing.T) {
   163  	conf := getJobCheckTestConfig()
   164  	logPath := filepath.Join(conf.Dir.LogDir, "servant.log")
   165  	record, err := searchJobEndRecordFromLog(logPath, 3, "job1")
   166  	if err != nil {
   167  		t.Fatalf("Unexpected error occured: %s", err)
   168  	}
   169  
   170  	expected := `2015-08-01 09:20:25.012 [3412] [INF] CTS010I JOB [/home/cuto/testjob] STARTED. INSTANCE [3] ID [job1] PID [2341].`
   171  	if record != expected {
   172  		t.Errorf("Record is not expected value.")
   173  		t.Log("Actual:")
   174  		t.Log(record)
   175  		t.Log("Expected: ")
   176  		t.Log(expected)
   177  	}
   178  }
   179  
   180  func TestSearchLatestJoblog(t *testing.T) {
   181  	conf := getJobCheckTestConfig()
   182  	et, _ := utctime.Parse(utctime.Default, "2015-08-01 03:34:56.789")
   183  	path, err := searchLatestJoblog(conf.Dir.JoblogDir, 1, "job1", et)
   184  	if err != nil {
   185  		t.Fatalf("Unexpected error occured: %s", err)
   186  	}
   187  
   188  	if !strings.HasSuffix(path, "1.testjob.job1.20150801120525.123.log") {
   189  		t.Error("Unexpected path was got:")
   190  		t.Log(path)
   191  	}
   192  }