github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/api4/job_test.go (about)

     1  // Copyright (c) 2017-present Xenia, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/xzl8028/xenia-server/model"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestCreateJob(t *testing.T) {
    15  	th := Setup().InitBasic()
    16  	defer th.TearDown()
    17  
    18  	job := &model.Job{
    19  		Type: model.JOB_TYPE_DATA_RETENTION,
    20  		Data: map[string]string{
    21  			"thing": "stuff",
    22  		},
    23  	}
    24  
    25  	received, resp := th.SystemAdminClient.CreateJob(job)
    26  	CheckNoError(t, resp)
    27  
    28  	defer th.App.Srv.Store.Job().Delete(received.Id)
    29  
    30  	job = &model.Job{
    31  		Type: model.NewId(),
    32  	}
    33  
    34  	_, resp = th.SystemAdminClient.CreateJob(job)
    35  	CheckBadRequestStatus(t, resp)
    36  
    37  	_, resp = th.Client.CreateJob(job)
    38  	CheckForbiddenStatus(t, resp)
    39  }
    40  
    41  func TestGetJob(t *testing.T) {
    42  	th := Setup().InitBasic()
    43  	defer th.TearDown()
    44  
    45  	job := &model.Job{
    46  		Id:     model.NewId(),
    47  		Status: model.JOB_STATUS_PENDING,
    48  	}
    49  	_, err := th.App.Srv.Store.Job().Save(job)
    50  	require.Nil(t, err)
    51  
    52  	defer th.App.Srv.Store.Job().Delete(job.Id)
    53  
    54  	received, resp := th.SystemAdminClient.GetJob(job.Id)
    55  	CheckNoError(t, resp)
    56  
    57  	if received.Id != job.Id || received.Status != job.Status {
    58  		t.Fatal("incorrect job received")
    59  	}
    60  
    61  	_, resp = th.SystemAdminClient.GetJob("1234")
    62  	CheckBadRequestStatus(t, resp)
    63  
    64  	_, resp = th.Client.GetJob(job.Id)
    65  	CheckForbiddenStatus(t, resp)
    66  
    67  	_, resp = th.SystemAdminClient.GetJob(model.NewId())
    68  	CheckNotFoundStatus(t, resp)
    69  }
    70  
    71  func TestGetJobs(t *testing.T) {
    72  	th := Setup().InitBasic()
    73  	defer th.TearDown()
    74  
    75  	jobType := model.NewId()
    76  
    77  	t0 := model.GetMillis()
    78  	jobs := []*model.Job{
    79  		{
    80  			Id:       model.NewId(),
    81  			Type:     jobType,
    82  			CreateAt: t0 + 1,
    83  		},
    84  		{
    85  			Id:       model.NewId(),
    86  			Type:     jobType,
    87  			CreateAt: t0,
    88  		},
    89  		{
    90  			Id:       model.NewId(),
    91  			Type:     jobType,
    92  			CreateAt: t0 + 2,
    93  		},
    94  	}
    95  
    96  	for _, job := range jobs {
    97  		_, err := th.App.Srv.Store.Job().Save(job)
    98  		require.Nil(t, err)
    99  		defer th.App.Srv.Store.Job().Delete(job.Id)
   100  	}
   101  
   102  	received, resp := th.SystemAdminClient.GetJobs(0, 2)
   103  	CheckNoError(t, resp)
   104  
   105  	if len(received) != 2 {
   106  		t.Fatal("received wrong number of jobs")
   107  	} else if received[0].Id != jobs[2].Id {
   108  		t.Fatal("should've received newest job first")
   109  	} else if received[1].Id != jobs[0].Id {
   110  		t.Fatal("should've received second newest job second")
   111  	}
   112  
   113  	received, resp = th.SystemAdminClient.GetJobs(1, 2)
   114  	CheckNoError(t, resp)
   115  
   116  	if received[0].Id != jobs[1].Id {
   117  		t.Fatal("should've received oldest job last")
   118  	}
   119  
   120  	_, resp = th.Client.GetJobs(0, 60)
   121  	CheckForbiddenStatus(t, resp)
   122  }
   123  
   124  func TestGetJobsByType(t *testing.T) {
   125  	th := Setup().InitBasic()
   126  	defer th.TearDown()
   127  
   128  	jobType := model.NewId()
   129  
   130  	jobs := []*model.Job{
   131  		{
   132  			Id:       model.NewId(),
   133  			Type:     jobType,
   134  			CreateAt: 1000,
   135  		},
   136  		{
   137  			Id:       model.NewId(),
   138  			Type:     jobType,
   139  			CreateAt: 999,
   140  		},
   141  		{
   142  			Id:       model.NewId(),
   143  			Type:     jobType,
   144  			CreateAt: 1001,
   145  		},
   146  		{
   147  			Id:       model.NewId(),
   148  			Type:     model.NewId(),
   149  			CreateAt: 1002,
   150  		},
   151  	}
   152  
   153  	for _, job := range jobs {
   154  		_, err := th.App.Srv.Store.Job().Save(job)
   155  		require.Nil(t, err)
   156  		defer th.App.Srv.Store.Job().Delete(job.Id)
   157  	}
   158  
   159  	received, resp := th.SystemAdminClient.GetJobsByType(jobType, 0, 2)
   160  	CheckNoError(t, resp)
   161  
   162  	if len(received) != 2 {
   163  		t.Fatal("received wrong number of jobs")
   164  	} else if received[0].Id != jobs[2].Id {
   165  		t.Fatal("should've received newest job first")
   166  	} else if received[1].Id != jobs[0].Id {
   167  		t.Fatal("should've received second newest job second")
   168  	}
   169  
   170  	received, resp = th.SystemAdminClient.GetJobsByType(jobType, 1, 2)
   171  	CheckNoError(t, resp)
   172  
   173  	if len(received) != 1 {
   174  		t.Fatal("received wrong number of jobs")
   175  	} else if received[0].Id != jobs[1].Id {
   176  		t.Fatal("should've received oldest job last")
   177  	}
   178  
   179  	_, resp = th.SystemAdminClient.GetJobsByType("", 0, 60)
   180  	CheckNotFoundStatus(t, resp)
   181  
   182  	_, resp = th.SystemAdminClient.GetJobsByType(strings.Repeat("a", 33), 0, 60)
   183  	CheckBadRequestStatus(t, resp)
   184  
   185  	_, resp = th.Client.GetJobsByType(jobType, 0, 60)
   186  	CheckForbiddenStatus(t, resp)
   187  }
   188  
   189  func TestCancelJob(t *testing.T) {
   190  	th := Setup().InitBasic()
   191  	defer th.TearDown()
   192  
   193  	jobs := []*model.Job{
   194  		{
   195  			Id:     model.NewId(),
   196  			Type:   model.NewId(),
   197  			Status: model.JOB_STATUS_PENDING,
   198  		},
   199  		{
   200  			Id:     model.NewId(),
   201  			Type:   model.NewId(),
   202  			Status: model.JOB_STATUS_IN_PROGRESS,
   203  		},
   204  		{
   205  			Id:     model.NewId(),
   206  			Type:   model.NewId(),
   207  			Status: model.JOB_STATUS_SUCCESS,
   208  		},
   209  	}
   210  
   211  	for _, job := range jobs {
   212  		_, err := th.App.Srv.Store.Job().Save(job)
   213  		require.Nil(t, err)
   214  		defer th.App.Srv.Store.Job().Delete(job.Id)
   215  	}
   216  
   217  	_, resp := th.Client.CancelJob(jobs[0].Id)
   218  	CheckForbiddenStatus(t, resp)
   219  
   220  	_, resp = th.SystemAdminClient.CancelJob(jobs[0].Id)
   221  	CheckNoError(t, resp)
   222  
   223  	_, resp = th.SystemAdminClient.CancelJob(jobs[1].Id)
   224  	CheckNoError(t, resp)
   225  
   226  	_, resp = th.SystemAdminClient.CancelJob(jobs[2].Id)
   227  	CheckInternalErrorStatus(t, resp)
   228  
   229  	_, resp = th.SystemAdminClient.CancelJob(model.NewId())
   230  	CheckInternalErrorStatus(t, resp)
   231  }