func TestHandler(t *testing.T) { var buf bytes.Buffer handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(200) fmt.Fprint(w, "Hello World\n") }) logger := logrus.New() logger.Level = logrus.InfoLevel logger.Formatter = &logrus.JSONFormatter{} logger.Out = &buf l := Middleware{ Name: "example", Logger: logger, } lh := l.Handler(http.HandlerFunc(handler), "homepage") http.Handle("/", lh) lh.ServeHTTP(httptest.NewRecorder(), newRequest("GET", "/foo")) h.Assert(t, buf.Len() > 0, "buffer should not be empty") h.Assert(t, strings.Contains(buf.String(), `"component":"homepage"`), "buffer did not match expected result") }
func TestSubnetSaveFail(t *testing.T) { s := newSubnet(t) defer removeSubnet(t, s) s.CIDR = nil err := s.Save() h.Assert(t, err != nil, "should have got an error") h.Assert(t, strings.Contains(err.Error(), "CIDR cannot be nil"), "unexpected error message") }
func TestHypervisorWithBadID(t *testing.T) { c := newContext(t) _, err := c.Hypervisor("") h.Assert(t, err != nil, "should have got an error") h.Assert(t, strings.Contains(err.Error(), "invalid UUID"), "unexpected error") _, err = c.Hypervisor("foo") h.Assert(t, err != nil, "should have got an error") h.Assert(t, strings.Contains(err.Error(), "invalid UUID"), "unexpected error") }
func TestSubnetSaveInvalidRange(t *testing.T) { s := newSubnet(t) defer removeSubnet(t, s) s.StartRange = net.IPv4(10, 10, 11, 10) err := s.Save() h.Assert(t, err != nil, "should have got an error") h.Assert(t, strings.Contains(err.Error(), "does not contain"), "unexpected error message") }
func TestFirstGuest(t *testing.T) { c := newContext(t) defer contextCleanup(t) f := c.NewFlavor() f.Resources.Memory = 1024 f.Resources.CPU = 2 f.Resources.Disk = 8192 h.Ok(t, f.Save()) g := newGuest(t) g.MAC, _ = net.ParseMAC("72:00:04:30:c9:e0") g.FlavorID = f.ID h.Ok(t, g.Save()) g = newGuest(t) g.MAC, _ = net.ParseMAC("72:00:04:30:c9:e1") g.FlavorID = f.ID h.Ok(t, g.Save()) g = newGuest(t) g.MAC, _ = net.ParseMAC("72:00:04:30:c9:e2") g.FlavorID = f.ID h.Ok(t, g.Save()) found, err := c.FirstGuest(func(g *lochness.Guest) bool { return g.ID == "foo" }) h.Ok(t, err) h.Assert(t, found == nil, "unexpected value") found, err = c.FirstGuest(func(g2 *lochness.Guest) bool { return g.ID == g2.ID }) h.Ok(t, err) h.Assert(t, found != nil, "unexpected nil") }
func TestFirstHypervisor(t *testing.T) { c := newContext(t) newHypervisor(t) newHypervisor(t) hv := newHypervisor(t) defer contextCleanup(t) found, err := c.FirstHypervisor(func(h *lochness.Hypervisor) bool { return h.ID == "foo" }) h.Ok(t, err) h.Assert(t, found == nil, "unexpected value") found, err = c.FirstHypervisor(func(h *lochness.Hypervisor) bool { return h.ID == hv.ID }) h.Ok(t, err) h.Assert(t, found != nil, "unexpected nil") }
func TestFWGroupJson(t *testing.T) { data := `{"id": "EF8D7367-F14F-49C9-B960-2625947CA929", "rules": [ {"source": "192.168.1.0/24", "portStart": 80, "portEnd": 80, "protocol": "tcp", "action": "allow"} ] }` f := lochness.FWGroup{} err := json.Unmarshal([]byte(data), &f) h.Ok(t, err) h.Equals(t, "EF8D7367-F14F-49C9-B960-2625947CA929", f.ID) h.Equals(t, 1, len(f.Rules)) h.Equals(t, "192.168.1.0", f.Rules[0].Source.IP.String()) b, err := json.Marshal(&f) h.Ok(t, err) h.Assert(t, strings.Contains(string(b), "192.168.1.0/24"), "incorrect source information") }
func reserveAddress(t *testing.T, s *lochness.Subnet) net.IP { ip, err := s.ReserveAddress("fake") h.Ok(t, err) h.Assert(t, strings.Contains(ip.String(), "10.10.10."), "unexpected ip address") h.Equals(t, 90, len(s.AvailibleAddresses())) h.Equals(t, 1, len(s.Addresses())) // make sure change persists err = s.Refresh() h.Ok(t, err) h.Equals(t, 90, len(s.AvailibleAddresses())) return ip }
func TestVerifyOnHV(t *testing.T) { defer contextCleanup(t) hv := newHypervisor(t) // failing test uuid := "d3cac004-4d89-4f26-9776-97df74a41417" id, err := lochness.SetHypervisorID(uuid) h.Ok(t, err) h.Equals(t, uuid, id) err = hv.VerifyOnHV() h.Assert(t, err != nil, "should have got an error") // passing id, err = lochness.SetHypervisorID(hv.ID) h.Ok(t, err) h.Equals(t, hv.ID, id) err = hv.VerifyOnHV() h.Ok(t, err) }
func TestSetHypervisorID(t *testing.T) { // passing test with uuid uuid := "d3cac004-4d89-4f26-9776-97df74a41417" id, err := lochness.SetHypervisorID(uuid) h.Ok(t, err) h.Equals(t, uuid, id) id, err = lochness.SetHypervisorID("foo") h.Assert(t, err != nil, "should have got an error") h.Equals(t, "", id) // set with ENV uuid = "3e0f2128-0342-49f6-8e5f-ecd401bae99e" if err := os.Setenv("HYPERVISOR_ID", uuid); err != nil { log.WithFields(log.Fields{ "error": err, "uuid": uuid, }).Error("failed to set HYPERVISOR_ID env variable") } id, err = lochness.SetHypervisorID("") h.Ok(t, err) h.Equals(t, uuid, id) }
func TestNewFail(t *testing.T) { _, err := whitelist.New([]string{"0.0/8"}) h.Assert(t, err != nil, "new should fail") }
func TestNewFail(t *testing.T) { _, err := realip.New([]string{"X-Forwarded-For"}, []string{"0.0/8"}) h.Assert(t, err != nil, "new should fail") }