diff options
author | Julien Dessaux | 2020-02-22 11:57:50 +0100 |
---|---|---|
committer | Julien Dessaux | 2020-02-22 11:57:50 +0100 |
commit | bcfaffac240d74cd79bec3c2a9d3c144d215b495 (patch) | |
tree | edd2c5f1e011afee759970323042fcf35bf68962 /state/header_test.go | |
parent | Improved tests for job package (diff) | |
download | bareos-zabbix-check-bcfaffac240d74cd79bec3c2a9d3c144d215b495.tar.gz bareos-zabbix-check-bcfaffac240d74cd79bec3c2a9d3c144d215b495.tar.bz2 bareos-zabbix-check-bcfaffac240d74cd79bec3c2a9d3c144d215b495.zip |
Added tests to the state package, and reworked the code around that
Diffstat (limited to '')
-rw-r--r-- | state/header_test.go | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/state/header_test.go b/state/header_test.go new file mode 100644 index 0000000..1ed71eb --- /dev/null +++ b/state/header_test.go @@ -0,0 +1,109 @@ +package state + +import ( + "bytes" + "io" + "reflect" + "testing" +) + +func Test_header_String(t *testing.T) { + var id [14]byte + copy(id[:], []byte("test")) + type fields struct { + ID [14]byte + Version int32 + LastJobsAddr uint64 + EndOfRecentJobResultsList uint64 + } + tests := []struct { + name string + fields fields + want string + }{ + {"default header", fields{ID: id, Version: 1, LastJobsAddr: 2, EndOfRecentJobResultsList: 3}, "ID: \"test\", Version: 1, LastJobsAddr: 2, EndOfRecentJobResultsList: 3"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + sfh := Header{ + ID: tt.fields.ID, + Version: tt.fields.Version, + LastJobsAddr: tt.fields.LastJobsAddr, + EndOfRecentJobResultsList: tt.fields.EndOfRecentJobResultsList, + } + if got := sfh.String(); got != tt.want { + t.Errorf("header.String() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_parseHeader(t *testing.T) { + readerEmpty := bytes.NewReader([]byte("")) + readerTooSmall := bytes.NewReader([]byte("abcd")) + readerNotBareosNorBacula := bytes.NewReader([]byte{ + 't', 'e', 's', 't', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ID + 0, 0, // padding + 4, 0, 0, 0, // version + 0, 0, 0, 0, //padding + 0, 0, 0, 0, 0, 0, 0, 0, // last job address + 0, 0, 0, 0, 0, 0, 0, 0, // EndOfRecentJobResultsLis + }) + readerBadVersion := bytes.NewReader([]byte{ + 'B', 'a', 'r', 'e', 'o', 's', ' ', 'S', 't', 'a', 't', 'e', '\n', 0, // ID + 0, 0, // padding + 3, 0, 0, 0, // version + 0, 0, 0, 0, //padding + 0, 0, 0, 0, 0, 0, 0, 0, // last job address + 0, 0, 0, 0, 0, 0, 0, 0, // EndOfRecentJobResultsLis + }) + readerNoJobs := bytes.NewReader([]byte{ + 'B', 'a', 'r', 'e', 'o', 's', ' ', 'S', 't', 'a', 't', 'e', '\n', 0, // ID + 0, 0, // padding + 4, 0, 0, 0, // version + 0, 0, 0, 0, //padding + 0, 0, 0, 0, 0, 0, 0, 0, // last job address + 0, 0, 0, 0, 0, 0, 0, 0, // EndOfRecentJobResultsList + }) + readerValid := bytes.NewReader([]byte{ + 'B', 'a', 'r', 'e', 'o', 's', ' ', 'S', 't', 'a', 't', 'e', '\n', 0, // ID + 0, 0, // padding + 4, 0, 0, 0, // version + 0, 0, 0, 0, //padding + 192, 0, 0, 0, 0, 0, 0, 0, // last job address + 254, 0, 0, 0, 0, 0, 0, 0, // EndOfRecentJobResultsList + }) + type args struct { + handle io.Reader + } + tests := []struct { + name string + args args + wantH *Header + wantErr bool + }{ + {"read error", args{readerEmpty}, nil, true}, + {"invalid header length", args{readerTooSmall}, nil, true}, + {"reader not bareos nor bacula", args{readerNotBareosNorBacula}, nil, true}, + {"reader bad version", args{readerBadVersion}, nil, true}, + {"reader no jobs", args{readerNoJobs}, nil, true}, + {"reader valid", args{readerValid}, &Header{ + ID: [14]byte{'B', 'a', 'r', 'e', 'o', 's', ' ', 'S', 't', 'a', 't', 'e', '\n', 0}, + Version: 4, + LastJobsAddr: 192, + EndOfRecentJobResultsList: 254, + }, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotH, err := ParseHeader(tt.args.handle) + if (err != nil) != tt.wantErr { + t.Errorf("parseHeader() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotH, tt.wantH) { + t.Errorf("parseHeader() = %v, want %v", gotH, tt.wantH) + } + }) + } +} |