|
1 | 1 | package main |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "testing" |
| 7 | +) |
4 | 8 |
|
5 | 9 | func TestContains(t *testing.T) { |
6 | 10 | env := map[string]string{ |
@@ -99,3 +103,83 @@ func TestGroupByMulti(t *testing.T) { |
99 | 103 | t.Fatalf("expected 2 got %s", groups["demo3.localhost"][0].ID) |
100 | 104 | } |
101 | 105 | } |
| 106 | + |
| 107 | +func TestDict(t *testing.T) { |
| 108 | + containers := []*RuntimeContainer{ |
| 109 | + &RuntimeContainer{ |
| 110 | + Env: map[string]string{ |
| 111 | + "VIRTUAL_HOST": "demo1.localhost", |
| 112 | + }, |
| 113 | + ID: "1", |
| 114 | + }, |
| 115 | + &RuntimeContainer{ |
| 116 | + Env: map[string]string{ |
| 117 | + "VIRTUAL_HOST": "demo1.localhost,demo3.localhost", |
| 118 | + }, |
| 119 | + ID: "2", |
| 120 | + }, |
| 121 | + &RuntimeContainer{ |
| 122 | + Env: map[string]string{ |
| 123 | + "VIRTUAL_HOST": "demo2.localhost", |
| 124 | + }, |
| 125 | + ID: "3", |
| 126 | + }, |
| 127 | + } |
| 128 | + d, err := dict("/", containers) |
| 129 | + if err != nil { |
| 130 | + t.Fatal(err) |
| 131 | + } |
| 132 | + if d["/"] == nil { |
| 133 | + t.Fatalf("did not find containers in dict: %s", d) |
| 134 | + } |
| 135 | + if d["MISSING"] != nil { |
| 136 | + t.Fail() |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestSha1(t *testing.T) { |
| 141 | + sum := hashSha1("/path") |
| 142 | + if sum != "4f26609ad3f5185faaa9edf1e93aa131e2131352" { |
| 143 | + t.Fatal("Incorrect SHA1 sum") |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func TestJson(t *testing.T) { |
| 148 | + containers := []*RuntimeContainer{ |
| 149 | + &RuntimeContainer{ |
| 150 | + Env: map[string]string{ |
| 151 | + "VIRTUAL_HOST": "demo1.localhost", |
| 152 | + }, |
| 153 | + ID: "1", |
| 154 | + }, |
| 155 | + &RuntimeContainer{ |
| 156 | + Env: map[string]string{ |
| 157 | + "VIRTUAL_HOST": "demo1.localhost,demo3.localhost", |
| 158 | + }, |
| 159 | + ID: "2", |
| 160 | + }, |
| 161 | + &RuntimeContainer{ |
| 162 | + Env: map[string]string{ |
| 163 | + "VIRTUAL_HOST": "demo2.localhost", |
| 164 | + }, |
| 165 | + ID: "3", |
| 166 | + }, |
| 167 | + } |
| 168 | + output, err := marshalJson(containers) |
| 169 | + if err != nil { |
| 170 | + t.Fatal(err) |
| 171 | + } |
| 172 | + |
| 173 | + buf := bytes.NewBufferString(output) |
| 174 | + dec := json.NewDecoder(buf) |
| 175 | + if err != nil { |
| 176 | + t.Fatal(err) |
| 177 | + } |
| 178 | + var decoded []*RuntimeContainer |
| 179 | + if err := dec.Decode(&decoded); err != nil { |
| 180 | + t.Fatal(err) |
| 181 | + } |
| 182 | + if len(decoded) != len(containers) { |
| 183 | + t.Fatal("Incorrect unmarshaled container count. Expected %d, got %d.", len(containers), len(decoded)) |
| 184 | + } |
| 185 | +} |
0 commit comments