You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.1 KiB
36 lines
1.1 KiB
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestReadJournalOperation(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "status.json")
|
|
if err := os.WriteFile(path, []byte(`{"format_version":1,"operation_id":"fw-123","state":"validated","ignored":"allowed"}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
identity, err := readJournalOperation(path)
|
|
if err != nil || identity.OperationID != "fw-123" || identity.State != "validated" {
|
|
t.Fatalf("identity = %+v, error = %v", identity, err)
|
|
}
|
|
}
|
|
|
|
func TestReadJournalOperationRejectsMissingOrTrailingIdentity(t *testing.T) {
|
|
for name, content := range map[string]string{
|
|
"missing-operation": `{"state":"validated"}`,
|
|
"missing-state": `{"operation_id":"fw-123"}`,
|
|
"trailing-json": `{"operation_id":"fw-123","state":"validated"}{}`,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "status.json")
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := readJournalOperation(path); err == nil {
|
|
t.Fatal("invalid journal was accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|