blob: 6bad1808ed33925addff198a5d4f53adb6429426 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Cosmos Nicolaou486d3492014-09-30 22:21:20 -07005package exec
Bogdan Capritaa4d9ee42014-06-20 16:42:53 -07006
7import (
Bogdan Capritabf356d72015-01-08 17:28:48 -08008 "reflect"
Bogdan Capritaa4d9ee42014-06-20 16:42:53 -07009 "testing"
Bogdan Capritabf356d72015-01-08 17:28:48 -080010
Jiri Simsa6ac95222015-02-23 16:11:49 -080011 "v.io/v23/verror"
Bogdan Capritaa4d9ee42014-06-20 16:42:53 -070012)
13
14func checkPresent(t *testing.T, c Config, k, wantV string) {
15 if v, err := c.Get(k); err != nil {
16 t.Errorf("Expected value %q for key %q, got error %v instead", wantV, k, err)
17 } else if v != wantV {
18 t.Errorf("Expected value %q for key %q, got %q instead", wantV, k, v)
19 }
20}
21
22func checkAbsent(t *testing.T, c Config, k string) {
Todd Wang8fa38762015-03-25 14:04:59 -070023 if v, err := c.Get(k); verror.ErrorID(err) != verror.ErrNoExist.ID {
Jiri Simsa074bf362015-02-17 09:29:45 -080024 t.Errorf("Expected (\"\", %v) for key %q, got (%q, %v) instead", verror.ErrNoExist, k, v, err)
Bogdan Capritaa4d9ee42014-06-20 16:42:53 -070025 }
26}
27
28// TestConfig checks that Set and Get work as expected.
29func TestConfig(t *testing.T) {
Cosmos Nicolaou486d3492014-09-30 22:21:20 -070030 c := NewConfig()
Bogdan Capritaa4d9ee42014-06-20 16:42:53 -070031 c.Set("foo", "bar")
32 checkPresent(t, c, "foo", "bar")
33 checkAbsent(t, c, "food")
34 c.Set("foo", "baz")
35 checkPresent(t, c, "foo", "baz")
Jiri Simsa37893392014-11-07 10:55:45 -080036 c.Clear("foo")
37 checkAbsent(t, c, "foo")
Bogdan Capritabf356d72015-01-08 17:28:48 -080038 if want, got := map[string]string{}, c.Dump(); !reflect.DeepEqual(want, got) {
39 t.Errorf("Expected %v for Dump, got %v instead", want, got)
40 }
Bogdan Capritaa4d9ee42014-06-20 16:42:53 -070041}
42
43// TestSerialize checks that serializing the config and merging from a
44// serialized config work as expected.
45func TestSerialize(t *testing.T) {
Cosmos Nicolaou486d3492014-09-30 22:21:20 -070046 c := NewConfig()
Bogdan Capritaa4d9ee42014-06-20 16:42:53 -070047 c.Set("k1", "v1")
48 c.Set("k2", "v2")
49 s, err := c.Serialize()
50 if err != nil {
51 t.Fatalf("Failed to serialize: %v", err)
52 }
Cosmos Nicolaou486d3492014-09-30 22:21:20 -070053 readC := NewConfig()
Bogdan Capritaa4d9ee42014-06-20 16:42:53 -070054 if err := readC.MergeFrom(s); err != nil {
55 t.Fatalf("Failed to deserialize: %v", err)
56 }
57 checkPresent(t, readC, "k1", "v1")
58 checkPresent(t, readC, "k2", "v2")
59
60 readC.Set("k2", "newv2") // This should be overwritten by the next merge.
61 checkPresent(t, readC, "k2", "newv2")
62 readC.Set("k3", "v3") // This should survive the next merge.
63
64 c.Set("k1", "newv1") // This should overwrite v1 in the next merge.
65 c.Set("k4", "v4") // This should be added following the next merge.
66 s, err = c.Serialize()
67 if err != nil {
68 t.Fatalf("Failed to serialize: %v", err)
69 }
70 if err := readC.MergeFrom(s); err != nil {
71 t.Fatalf("Failed to deserialize: %v", err)
72 }
73 checkPresent(t, readC, "k1", "newv1")
74 checkPresent(t, readC, "k2", "v2")
75 checkPresent(t, readC, "k3", "v3")
76 checkPresent(t, readC, "k4", "v4")
Bogdan Capritabf356d72015-01-08 17:28:48 -080077 if want, got := map[string]string{"k1": "newv1", "k2": "v2", "k3": "v3", "k4": "v4"}, readC.Dump(); !reflect.DeepEqual(want, got) {
78 t.Errorf("Expected %v for Dump, got %v instead", want, got)
79 }
Bogdan Capritaa4d9ee42014-06-20 16:42:53 -070080}