aboutsummaryrefslogtreecommitdiff
path: root/state/job_test.go
blob: 410401b6fafb88f91b58f19fb9098eccc592ca02 (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
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
package state

import (
	"bareos-zabbix-check/job"
	"bytes"
	"io"
	"reflect"
	"testing"
)

func Test_jobEntry_String(t *testing.T) {
	var badlyNamedJob [128]byte
	copy(badlyNamedJob[:], []byte("job_name"))
	var normalJob [128]byte
	copy(normalJob[:], []byte("normal_name.2012-06-01"))
	type fields struct {
		Errors         int32
		JobType        int32
		JobStatus      int32
		JobLevel       int32
		JobID          uint32
		VolSessionID   uint32
		VolSessionTime uint32
		JobFiles       uint32
		JobBytes       uint64
		StartTime      uint64
		EndTime        uint64
		Job            [maxNameLength]byte
	}
	tests := []struct {
		name   string
		fields fields
		want   string
	}{
		{
			"normal job",
			fields{Errors: 1, JobType: 'B', JobStatus: 'T', JobLevel: 'F', JobID: 2, VolSessionID: 3, VolSessionTime: 4, JobFiles: 5, JobBytes: 6, Job: badlyNamedJob},
			"Errors: 1, JobType: B, JobStatus: T, JobLevel: F, JobID: 2, VolSessionID: 3, VolSessionTime: 4, JobFiles: 5, JobBytes: 6, StartTime: 1970-01-01 01:00:00 +0100 CET, EndTime: 1970-01-01 01:00:00 +0100 CET, Job: job_name",
		},
		{
			"badly named job",
			fields{Errors: 1, JobType: 'B', JobStatus: 'T', JobLevel: 'F', JobID: 2, VolSessionID: 3, VolSessionTime: 4, JobFiles: 5, JobBytes: 6, Job: normalJob},
			"Errors: 1, JobType: B, JobStatus: T, JobLevel: F, JobID: 2, VolSessionID: 3, VolSessionTime: 4, JobFiles: 5, JobBytes: 6, StartTime: 1970-01-01 01:00:00 +0100 CET, EndTime: 1970-01-01 01:00:00 +0100 CET, Job: normal_name",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			je := jobEntry{
				Errors:         tt.fields.Errors,
				JobType:        tt.fields.JobType,
				JobStatus:      tt.fields.JobStatus,
				JobLevel:       tt.fields.JobLevel,
				JobID:          tt.fields.JobID,
				VolSessionID:   tt.fields.VolSessionID,
				VolSessionTime: tt.fields.VolSessionTime,
				JobFiles:       tt.fields.JobFiles,
				JobBytes:       tt.fields.JobBytes,
				StartTime:      tt.fields.StartTime,
				EndTime:        tt.fields.EndTime,
				Job:            tt.fields.Job,
			}
			if got := je.String(); got != tt.want {
				t.Errorf("jobEntry.String() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestParseJobs(t *testing.T) {
	readerEmpty := bytes.NewReader([]byte{})
	readerTooSmall := bytes.NewReader([]byte{
		1, // number of jobs
	})
	readerJobError := bytes.NewReader([]byte{
		1, 0, 0, 0, // number of jobs
	})
	readerJobTooSmall := bytes.NewReader([]byte{
		1, 0, 0, 0, // number of jobs
		0,
	})
	readerInvalidJobName := bytes.NewReader([]byte{
		1, 0, 0, 0, // number of jobs
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // pad
		0, 0, 0, 0, // Errors
		'B', 0, 0, 0, // JobType
		'T', 0, 0, 0, // JobStatus
		0, 0, 0, 0, // JobLevel
		0, 0, 0, 0, // JobID
		0, 0, 0, 0, // VolSessionID
		0, 0, 0, 0, // VolSessionTime
		0, 0, 0, 0, // JobFiles
		0, 0, 0, 0, 0, 0, 0, 0, // JobBytes
		1, 0, 0, 0, 0, 0, 0, 0, // StartTime
		0, 0, 0, 0, 0, 0, 0, 0, // EndTime
		't', 'e', 's', 't', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Job
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	})
	readerZeroJobs := bytes.NewReader([]byte{
		0, 0, 0, 0, // number of jobs
	})
	readerOneNonBackupJob := bytes.NewReader([]byte{
		1, 0, 0, 0, // number of jobs
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // pad
		0, 0, 0, 0, // Errors
		'R', 0, 0, 0, // JobType
		'T', 0, 0, 0, // JobStatus
		0, 0, 0, 0, // JobLevel
		0, 0, 0, 0, // JobID
		0, 0, 0, 0, // VolSessionID
		0, 0, 0, 0, // VolSessionTime
		0, 0, 0, 0, // JobFiles
		0, 0, 0, 0, 0, 0, 0, 0, // JobBytes
		1, 0, 0, 0, 0, 0, 0, 0, // StartTime
		0, 0, 0, 0, 0, 0, 0, 0, // EndTime
		't', 'e', 's', 't', '.', '2', '0', '1', '2', '-', '0', '2', '-', '0', '1', 0, // Job
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	})
	readerOneSuccessfulBackupJob := bytes.NewReader([]byte{
		1, 0, 0, 0, // number of jobs
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // pad
		0, 0, 0, 0, // Errors
		'B', 0, 0, 0, // JobType
		'T', 0, 0, 0, // JobStatus
		0, 0, 0, 0, // JobLevel
		0, 0, 0, 0, // JobID
		0, 0, 0, 0, // VolSessionID
		0, 0, 0, 0, // VolSessionTime
		0, 0, 0, 0, // JobFiles
		0, 0, 0, 0, 0, 0, 0, 0, // JobBytes
		1, 0, 0, 0, 0, 0, 0, 0, // StartTime
		0, 0, 0, 0, 0, 0, 0, 0, // EndTime
		't', 'e', 's', 't', '.', '2', '0', '1', '2', '-', '0', '2', '-', '0', '1', 0, // Job
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	})
	type args struct {
		handle io.Reader
	}
	tests := []struct {
		name     string
		args     args
		wantJobs []job.Job
		wantErr  bool
	}{
		{"read empty", args{readerEmpty}, nil, true},
		{"read too small", args{readerTooSmall}, nil, true},
		{"read job error", args{readerJobError}, nil, true},
		{"read job too small", args{readerJobTooSmall}, nil, true},
		{"read invalid job name", args{readerInvalidJobName}, nil, true},
		{"read zero jobs", args{readerZeroJobs}, nil, false},
		{"read one non backup job", args{readerOneNonBackupJob}, nil, false},
		{"read one successful backup job", args{readerOneSuccessfulBackupJob}, []job.Job{{Name: "test", Timestamp: 1, Success: true}}, false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gotJobs, err := ParseJobs(tt.args.handle)
			if (err != nil) != tt.wantErr {
				t.Errorf("ParseJobs() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(gotJobs, tt.wantJobs) {
				t.Errorf("ParseJobs() = %v, want %v", gotJobs, tt.wantJobs)
			}
		})
	}
}