From 44c2c7aeda3de09a405ed06aadacbc2c0c7f2a67 Mon Sep 17 00:00:00 2001 From: "Joe Richey joerichey@google.com" Date: Tue, 23 May 2017 18:38:38 -0700 Subject: metadata: reorganize and add consistency checks This commit adds in IsValid() checks for the metadata structures that let us enforce stronger invariants than those imposed by the protobuf package. The main uses of this will be to check that metadata is valid before writing it to the filesystem, and to check that the filesystem contains valid metadata before returning it to the user. These functions also will log the exact reason if the validity checks fail. To have these checks in the metadata package, all of the various constants have been moved to a single metadata/constants.go file. The uses of these constants were changed accordingly. Finally, this commit standardizes our use of errors so that they always begin with an appropriate prefix. Change-Id: I99008e2ee803ebe5f6236eb8d83fc83efcd22718 --- metadata/config_test.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'metadata/config_test.go') diff --git a/metadata/config_test.go b/metadata/config_test.go index 1903785..206098e 100644 --- a/metadata/config_test.go +++ b/metadata/config_test.go @@ -20,6 +20,7 @@ package metadata import ( + "bytes" "reflect" "testing" ) @@ -42,27 +43,31 @@ var testConfigString = `{ "memory": "4096", "parallelism": "8" }, + "compatibility": "", "options": { "padding": "32", - "contents_mode": "XTS", - "filenames_mode": "CTS" + "contents": "XTS", + "filenames": "CTS" } -}` +} +` // Makes sure that writing a config and reading it back gives the same thing. func TestWrite(t *testing.T) { - str, err := WriteConfig(testConfig) + var b bytes.Buffer + err := WriteConfig(testConfig, &b) if err != nil { t.Fatal(err) } - t.Logf("json encoded config:\n%s", str) - if str != testConfigString { + t.Logf("json encoded config:\n%s", b.String()) + if b.String() != testConfigString { t.Errorf("did not match: %s", testConfigString) } } func TestRead(t *testing.T) { - cfg, err := ReadConfig(testConfigString) + buf := bytes.NewBufferString(testConfigString) + cfg, err := ReadConfig(buf) if err != nil { t.Fatal(err) } -- cgit v1.2.3