github.com/dahs81/otto@v0.2.1-0.20160126165905-6400716cf085/directory/testing.go (about) 1 package directory 2 3 import ( 4 "bytes" 5 "io" 6 "reflect" 7 "strings" 8 "testing" 9 ) 10 11 // TestBackend is a public test helper that verifies a backend 12 // functions properly. 13 func TestBackend(t *testing.T, b Backend) { 14 var buf bytes.Buffer 15 var err error 16 17 // Through this method we use "Errorf" instead of "Fatalf". This is 18 // important and deliberate: for our RPC tests, Fatalf causes the 19 // test to hang in a failure due to our RPC model. Errorf causes it 20 // to end properly. 21 22 //--------------------------------------------------------------- 23 // Blob 24 //--------------------------------------------------------------- 25 26 // GetBlob (doesn't exist) 27 data, err := b.GetBlob("foo") 28 if err != nil { 29 t.Errorf("GetBlob error: %s", err) 30 return 31 } 32 if data != nil { 33 data.Close() 34 t.Errorf("GetBlob should be nil data") 35 return 36 } 37 38 // PutBlob 39 err = b.PutBlob("foo", &BlobData{Data: strings.NewReader("bar")}) 40 if err != nil { 41 t.Errorf("PutBlob error: %s", err) 42 return 43 } 44 45 // GetBlob (exists) 46 data, err = b.GetBlob("foo") 47 if err != nil { 48 t.Errorf("GetBlob error: %s", err) 49 return 50 } 51 _, err = io.Copy(&buf, data.Data) 52 data.Close() 53 if err != nil { 54 t.Errorf("GetBlob error: %s", err) 55 return 56 } 57 if buf.String() != "bar" { 58 t.Errorf("GetBlob bad data: %s", buf.String()) 59 return 60 } 61 62 //--------------------------------------------------------------- 63 // Infra 64 //--------------------------------------------------------------- 65 66 // GetInfra (doesn't exist) 67 infra := &Infra{Lookup: Lookup{Infra: "foo"}} 68 actualInfra, err := b.GetInfra(infra) 69 if err != nil { 70 t.Errorf("GetInfra (non-exist) error: %s", err) 71 return 72 } 73 if actualInfra != nil { 74 t.Error("GetInfra (non-exist): infra should be nil") 75 return 76 } 77 78 // PutInfra (doesn't exist) 79 infra.Outputs = map[string]string{"foo": "bar"} 80 if infra.ID != "" { 81 t.Errorf("PutInfra: ID should be empty before set") 82 return 83 } 84 if err := b.PutInfra(infra); err != nil { 85 t.Errorf("PutInfra err: %s", err) 86 return 87 } 88 if infra.ID == "" { 89 t.Errorf("PutInfra: infra ID not set") 90 return 91 } 92 93 // GetInfra (exists) 94 actualInfra, err = b.GetInfra(infra) 95 if err != nil { 96 t.Errorf("GetInfra (exist) error: %s", err) 97 return 98 } 99 if !reflect.DeepEqual(actualInfra, infra) { 100 t.Errorf("GetInfra (exist) bad: %#v", actualInfra) 101 return 102 } 103 104 // GetInfra with foundation (doesn't exist) 105 infra = &Infra{Lookup: Lookup{Infra: "foo", Foundation: "bar"}} 106 actualInfra, err = b.GetInfra(infra) 107 if err != nil { 108 t.Errorf("GetInfra (non-exist) error: %s", err) 109 return 110 } 111 if actualInfra != nil { 112 t.Error("GetInfra (non-exist): infra should be nil") 113 return 114 } 115 116 // PutInfra with foundation (doesn't exist) 117 infra.Outputs = map[string]string{"foo": "bar"} 118 if infra.ID != "" { 119 t.Errorf("PutInfra: ID should be empty before set") 120 return 121 } 122 if err := b.PutInfra(infra); err != nil { 123 t.Errorf("PutInfra err: %s", err) 124 return 125 } 126 if infra.ID == "" { 127 t.Errorf("PutInfra: infra ID not set") 128 return 129 } 130 131 // GetInfra with foundation (exists) 132 actualInfra, err = b.GetInfra(infra) 133 if err != nil { 134 t.Errorf("GetInfra (exist) error: %s", err) 135 return 136 } 137 if !reflect.DeepEqual(actualInfra, infra) { 138 t.Errorf("GetInfra (exist) bad: %#v", actualInfra) 139 return 140 } 141 142 //--------------------------------------------------------------- 143 // Deploy 144 //--------------------------------------------------------------- 145 146 // GetDeploy (doesn't exist) 147 deploy := &Deploy{Lookup: Lookup{ 148 AppID: "foo", Infra: "bar", InfraFlavor: "baz"}} 149 deployResult, err := b.GetDeploy(deploy) 150 if err != nil { 151 t.Errorf("GetDeploy (non-exist) error: %s", err) 152 return 153 } 154 if deployResult != nil { 155 t.Error("GetDeploy (non-exist): result should be nil") 156 return 157 } 158 159 // PutDeploy (doesn't exist) 160 if deploy.ID != "" { 161 t.Errorf("PutDeploy: ID should be empty before set") 162 return 163 } 164 if err := b.PutDeploy(deploy); err != nil { 165 t.Errorf("PutDeploy err: %s", err) 166 return 167 } 168 if deploy.ID == "" { 169 t.Errorf("PutDeploy: deploy ID not set") 170 return 171 } 172 173 // GetDeploy (exists) 174 deployResult, err = b.GetDeploy(deploy) 175 if err != nil { 176 t.Errorf("GetDeploy (exist) error: %s", err) 177 return 178 } 179 if !reflect.DeepEqual(deployResult, deploy) { 180 t.Errorf("GetDeploy (exist) bad: %#v", deployResult) 181 return 182 } 183 184 //--------------------------------------------------------------- 185 // Dev 186 //--------------------------------------------------------------- 187 188 // GetDev (doesn't exist) 189 dev := &Dev{Lookup: Lookup{AppID: "foo"}} 190 devResult, err := b.GetDev(dev) 191 if err != nil { 192 t.Errorf("GetDev (non-exist) error: %s", err) 193 return 194 } 195 if devResult != nil { 196 t.Error("GetDev (non-exist): result should be nil") 197 return 198 } 199 200 // PutDev (doesn't exist) 201 if dev.ID != "" { 202 t.Errorf("PutDev: ID should be empty before set") 203 return 204 } 205 if err := b.PutDev(dev); err != nil { 206 t.Errorf("PutDev err: %s", err) 207 return 208 } 209 if dev.ID == "" { 210 t.Errorf("PutDev: dev ID not set") 211 return 212 } 213 214 // GetDev (exists) 215 devResult, err = b.GetDev(dev) 216 if err != nil { 217 t.Errorf("GetDev (exist) error: %s", err) 218 return 219 } 220 if !reflect.DeepEqual(devResult, dev) { 221 t.Errorf("GetDev (exist) bad: %#v", devResult) 222 return 223 } 224 225 // DeleteDev (exists) 226 err = b.DeleteDev(dev) 227 if err != nil { 228 t.Errorf("DeleteDev error: %s", err) 229 return 230 } 231 devResult, err = b.GetDev(dev) 232 if err != nil { 233 t.Errorf("GetDev (non-exist) error: %s", err) 234 return 235 } 236 if devResult != nil { 237 t.Error("GetDev (non-exist): result should be nil") 238 return 239 } 240 241 // DeleteDev (doesn't exist) 242 err = b.DeleteDev(dev) 243 if err != nil { 244 t.Errorf("DeleteDev error: %s", err) 245 return 246 } 247 }