Example #1
0
func TestFetchSubnets(t *testing.T) {

	// Setup
	f := NewFetcher("http://127.0.0.1:4001")
	defer testhelper.Cleanup(f.etcdClient)

	// Create supporting object
	n, err := testhelper.NewNetwork(f.context)
	h.Ok(t, err)

	// Create two subnets
	s1, err := testhelper.NewSubnet(f.context, "10.10.10.0/24", net.IPv4(10, 10, 10, 1), net.IPv4(10, 10, 10, 10), net.IPv4(10, 10, 10, 250), n)
	h.Ok(t, err)
	s2, err := testhelper.NewSubnet(f.context, "12.12.12.0/24", net.IPv4(12, 12, 12, 1), net.IPv4(12, 12, 12, 12), net.IPv4(12, 12, 12, 250), n)
	h.Ok(t, err)

	// Fetch and make sure they're present
	ss, err := f.Subnets()
	h.Ok(t, err)
	if _, ok := ss[s1.ID]; !ok {
		t.Error("Subnet #1 is missing from list")
	}
	h.Equals(t, ss[s1.ID].CIDR.String(), "10.10.10.0/24")
	if _, ok := ss[s2.ID]; !ok {
		t.Error("Subnet #2 is missing from list")
	}
	h.Equals(t, ss[s2.ID].CIDR.String(), "12.12.12.0/24")

}
Example #2
0
func TestFetchHypervisors(t *testing.T) {

	// Setup
	f := NewFetcher("http://127.0.0.1:4001")
	defer testhelper.Cleanup(f.etcdClient)

	// Create supporting objects
	n, err := testhelper.NewNetwork(f.context)
	h.Ok(t, err)
	s, err := testhelper.NewSubnet(f.context, "10.10.10.0/24", net.IPv4(10, 10, 10, 1), net.IPv4(10, 10, 10, 10), net.IPv4(10, 10, 10, 250), n)
	h.Ok(t, err)

	// Create two hypervisors
	h1, err := testhelper.NewHypervisor(f.context, "de:ad:be:ef:7f:21", net.IPv4(192, 168, 100, 200), net.IPv4(192, 168, 100, 1), net.IPv4(255, 255, 255, 0), "br0", s)
	h.Ok(t, err)
	h2, err := testhelper.NewHypervisor(f.context, "de:ad:be:ef:7f:23", net.IPv4(192, 168, 100, 203), net.IPv4(192, 168, 100, 1), net.IPv4(255, 255, 255, 0), "br0", s)
	h.Ok(t, err)

	// Fetch and make sure they're present
	hvs, err := f.Hypervisors()
	h.Ok(t, err)
	if _, ok := hvs[h1.ID]; !ok {
		t.Error("Hypervisor #1 is missing from list")
	}
	h.Equals(t, hvs[h1.ID].MAC.String(), "de:ad:be:ef:7f:21")
	if _, ok := hvs[h2.ID]; !ok {
		t.Error("Hypervisor #2 is missing from list")
	}
	h.Equals(t, hvs[h2.ID].MAC.String(), "de:ad:be:ef:7f:23")

}
Example #3
0
func TestFetchGuests(t *testing.T) {

	// Setup
	f := NewFetcher("http://127.0.0.1:4001")
	defer testhelper.Cleanup(f.etcdClient)

	// Create supporting objects
	f1, err := testhelper.NewFlavor(f.context, 4, 4096, 8192)
	h.Ok(t, err)
	n, err := testhelper.NewNetwork(f.context)
	h.Ok(t, err)
	fw, err := testhelper.NewFirewallGroup(f.context)
	h.Ok(t, err)
	s, err := testhelper.NewSubnet(f.context, "10.10.10.0/24", net.IPv4(10, 10, 10, 1), net.IPv4(10, 10, 10, 10), net.IPv4(10, 10, 10, 250), n)
	h.Ok(t, err)
	h1, err := testhelper.NewHypervisor(f.context, "de:ad:be:ef:7f:21", net.IPv4(192, 168, 100, 200), net.IPv4(192, 168, 100, 1), net.IPv4(255, 255, 255, 0), "br0", s)
	h.Ok(t, err)

	// Create two guests
	g1, err := testhelper.NewGuest(f.context, "01:23:45:67:89:ab", n, s, f1, fw, h1)
	h.Ok(t, err)
	g2, err := testhelper.NewGuest(f.context, "23:45:67:89:ab:cd", n, s, f1, fw, h1)
	h.Ok(t, err)

	// Fetch and make sure they're present
	gs, err := f.Guests()
	h.Ok(t, err)
	if _, ok := gs[g1.ID]; !ok {
		t.Error("Guest #1 is missing from list")
	}
	h.Equals(t, gs[g1.ID].MAC.String(), "01:23:45:67:89:ab")
	if _, ok := gs[g2.ID]; !ok {
		t.Error("Guest #2 is missing from list")
	}
	h.Equals(t, gs[g2.ID].MAC.String(), "23:45:67:89:ab:cd")

}
Example #4
0
func main() {

	// Command line options
	var logLevel string
	flag.StringVarP(&logLevel, "log-level", "l", "info", "log level: debug/info/warning/error/critical/fatal")
	flag.Parse()

	// Logging
	if err := logx.DefaultSetup(logLevel); err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "logx.DefaultSetup",
		}).Fatal("Could not set up logrus")
	}

	// Write logs to this directory
	testDir = "cdhcpd-integration-test-" + uuid.New()
	if err := os.Mkdir(testDir, 0755); err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "os.Mkdir",
			"path":  testDir,
		}).Fatal("Could not create directory for test logs")
	}
	hconfPath = testDir + "/hypervisors.conf"
	gconfPath = testDir + "/guests.conf"

	// From now on, write the logs from this script to a file in the test directory as well
	var err error
	selfLog, err = os.Create(testDir + "/integrationtest.log")
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "os.Open",
		}).Fatal("Could not open self-log file for writing")
	}
	defer func() {
		if err := selfLog.Sync(); err != nil {
			fmt.Println("Could not sync self-log file")
		}
		if err := selfLog.Close(); err != nil {
			fmt.Println("Could not close self-log file")
			os.Exit(1)
		}
	}()
	log.SetOutput(selfLog)

	// Set up report and global ok
	r := &bytes.Buffer{}
	_, _ = r.WriteString("cdhcpd Integration Test Results\n")
	_, _ = r.WriteString("==================================\n")
	testOk = true

	// Start up processes
	log.Info("Starting etcd")
	ep := newTestProcess("etcd", exec.Command("etcd", "--data-dir", testDir+"/data.etcd",
		"--listen-client-urls", etcdClientAddress,
		"--listen-peer-urls", etcdPeerAddress,
		"--initial-advertise-peer-urls", etcdPeerAddress,
		"--initial-cluster", "default="+etcdPeerAddress,
		"--advertise-client-urls", etcdClientAddress,
	))
	if err := ep.captureOutput(true); err != nil {
		cleanupAfterError(err, "testProcess.captureOutput", r, nil, ep, nil)
	}
	if err := ep.start(); err != nil {
		cleanupAfterError(err, "testProcess.start", r, nil, ep, nil)
	}
	log.Info("Starting cdhcpd")
	dp := newTestProcess("cdhcpd", exec.Command("cdhcpd", "-e", etcdClientAddress,
		"-d", "example.com",
		"-l", logLevel,
		"-c", testDir,
	))
	if err := dp.captureOutput(true); err != nil {
		if err := ep.finish(); err != nil {
			log.Error("Could not close out etcd")
		}
		cleanupAfterError(err, "testProcess.captureOutput", r, nil, ep, dp)
	}
	if err := dp.start(); err != nil {
		if err := ep.finish(); err != nil {
			log.Error("Could not close out etcd")
		}
		cleanupAfterError(err, "testProcess.start", r, nil, ep, dp)
	}

	// Begin test
	log.Info("Running test")
	time.Sleep(time.Second)
	if ok := reportConfStatus(r, "on start", "not present", "not present"); !ok {
		log.Warning("Failure testing conf status on start")
		testOk = false
	}

	// Set up context
	e := etcd.NewClient([]string{etcdClientAddress})
	c := lochness.NewContext(e)

	// Roughly follow the steps of the demo's guest-setup.sh
	hs := make(map[string]*lochness.Hypervisor)
	gs := make(map[string]*lochness.Guest)

	// Basic setup
	f, err := testhelper.NewFlavor(c, 1, 512, 1024)
	if err != nil {
		cleanupAfterError(err, "testhelper.NewFlavor", r, e, ep, dp)
	}
	n, err := testhelper.NewNetwork(c)
	if err != nil {
		cleanupAfterError(err, "testhelper.NewNetwork", r, e, ep, dp)
	}
	fw, err := testhelper.NewFirewallGroup(c)
	if err != nil {
		cleanupAfterError(err, "testhelper.NewFirewallGroup", r, e, ep, dp)
	}
	time.Sleep(time.Second)
	if ok := reportConfStatus(r, "after setup", "created", "created"); !ok {
		log.Warning("Failure testing conf status after setup")
		testOk = false
	}

	// Add a subnet
	s, err := testhelper.NewSubnet(c, "192.168.100.0/24", net.IPv4(192, 168, 100, 1), net.IPv4(192, 168, 100, 10), net.IPv4(192, 168, 100, 200), n)
	if err != nil {
		cleanupAfterError(err, "testhelper.NewSubnet", r, e, ep, dp)
	}
	time.Sleep(time.Second)
	if ok := reportConfStatus(r, "after subnet creation", "not touched", "not touched"); !ok {
		log.Warning("Failure testing conf status after subnet creation")
		testOk = false
	}

	// Add a hypervisor
	h, err := testhelper.NewHypervisor(c, "fe:dc:ba:98:76:54", net.IPv4(192, 168, 100, 200), net.IPv4(192, 168, 100, 1), net.IPv4(255, 255, 255, 0), "br0", s)
	if err != nil {
		cleanupAfterError(err, "testhelper.NewHypervisor", r, e, ep, dp)
	}
	hs[h.ID] = h
	time.Sleep(time.Second)
	if ok := reportConfStatus(r, "after hypervisor creation", "changed", "not touched"); !ok {
		log.Warning("Failure testing conf status after hypervisor creation")
		testOk = false
	}
	if ok := reportHasHosts(r, "after hypervisor creation", hs, gs); !ok {
		log.Warning("Failure testing for hosts in confs after hypervisor creation")
		testOk = false
	}

	// Create a guest, without setting the subnet or hypervisor (will NOT appear in the conf)
	g, err := testhelper.NewGuest(c, "A4:75:C1:6B:E3:49", n, nil, f, fw, nil)
	if err != nil {
		cleanupAfterError(err, "testhelper.NewGuest", r, e, ep, dp)
	}
	time.Sleep(time.Second)
	if ok := reportConfStatus(r, "after guest creation", "not touched", "not touched"); !ok {
		log.Warning("Failure testing conf status after guest creation")
		testOk = false
	}
	if ok := reportHasHosts(r, "after guest creation", hs, gs); !ok {
		log.Warning("Failure testing for hosts in confs after guest creation")
		testOk = false
	}

	// Add the guest to the hypervisor (would normally be performed by cplacerd, having pulled it from the queue)
	if err := h.AddGuest(g); err != nil {
		cleanupAfterError(err, "hypervisor.AddGuest", r, e, ep, dp)
	}
	gs[g.ID] = g
	time.Sleep(time.Second)
	if ok := reportConfStatus(r, "after adding guest to hypervisor", "not touched", "changed"); !ok {
		log.Warning("Failure testing conf status after adding guest to hypervisor")
		testOk = false
	}
	if ok := reportHasHosts(r, "after adding guest to hypervisor", hs, gs); !ok {
		log.Warning("Failure testing for hosts in confs after adding guest to hypervisor")
		testOk = false
	}

	// Sleep for a few seconds to make sure everything finished, then clean up
	time.Sleep(2 * time.Second)
	log.WithField("path", testDir).Info("Creating test output directory")
	showTestStatus(true)
	cleanup(r, e, ep, dp)
}
Example #5
0
func TestFetchAll(t *testing.T) {

	// Setup
	f := NewFetcher("http://127.0.0.1:4001")
	defer testhelper.Cleanup(f.etcdClient)

	// Create objects
	f1, err := testhelper.NewFlavor(f.context, 4, 4096, 8192)
	h.Ok(t, err)
	f2, err := testhelper.NewFlavor(f.context, 6, 8192, 1024)
	h.Ok(t, err)
	n, err := testhelper.NewNetwork(f.context)
	h.Ok(t, err)
	fw, err := testhelper.NewFirewallGroup(f.context)
	h.Ok(t, err)
	s, err := testhelper.NewSubnet(f.context, "10.10.10.0/24", net.IPv4(10, 10, 10, 1), net.IPv4(10, 10, 10, 10), net.IPv4(10, 10, 10, 250), n)
	h.Ok(t, err)
	h1, err := testhelper.NewHypervisor(f.context, "de:ad:be:ef:7f:21", net.IPv4(192, 168, 100, 200), net.IPv4(192, 168, 100, 1), net.IPv4(255, 255, 255, 0), "br0", s)
	h.Ok(t, err)
	h2, err := testhelper.NewHypervisor(f.context, "de:ad:be:ef:7f:23", net.IPv4(192, 168, 100, 203), net.IPv4(192, 168, 100, 1), net.IPv4(255, 255, 255, 0), "br0", s)
	h.Ok(t, err)
	g1, err := testhelper.NewGuest(f.context, "01:23:45:67:89:ab", n, s, f1, fw, h1)
	h.Ok(t, err)
	g2, err := testhelper.NewGuest(f.context, "23:45:67:89:ab:cd", n, s, f1, fw, h1)
	h.Ok(t, err)
	g3, err := testhelper.NewGuest(f.context, "45:67:89:ab:cd:ef", n, s, f1, fw, h2)
	h.Ok(t, err)
	g4, err := testhelper.NewGuest(f.context, "67:89:ab:cd:ef:01", n, s, f2, fw, h2)
	h.Ok(t, err)

	// Fetch and make sure everything expected is present
	err = f.FetchAll()
	h.Ok(t, err)

	// Check hypervisors
	hvs, err := f.Hypervisors()
	h.Ok(t, err)
	if _, ok := hvs[h1.ID]; !ok {
		t.Error("Hypervisor #1 is missing from list")
	}
	h.Equals(t, hvs[h1.ID].MAC.String(), "de:ad:be:ef:7f:21")
	if _, ok := hvs[h2.ID]; !ok {
		t.Error("Hypervisor #2 is missing from list")
	}
	h.Equals(t, hvs[h2.ID].MAC.String(), "de:ad:be:ef:7f:23")

	// Check guests
	gs, err := f.Guests()
	if _, ok := gs[g1.ID]; !ok {
		t.Error("Guest #1 is missing from list")
	}
	h.Equals(t, gs[g1.ID].MAC.String(), "01:23:45:67:89:ab")
	if _, ok := gs[g2.ID]; !ok {
		t.Error("Guest #2 is missing from list")
	}
	h.Equals(t, gs[g2.ID].MAC.String(), "23:45:67:89:ab:cd")
	if _, ok := gs[g3.ID]; !ok {
		t.Error("Guest #3 is missing from list")
	}
	h.Equals(t, gs[g3.ID].MAC.String(), "45:67:89:ab:cd:ef")
	if _, ok := gs[g4.ID]; !ok {
		t.Error("Guest #4 is missing from list")
	}
	h.Equals(t, gs[g4.ID].MAC.String(), "67:89:ab:cd:ef:01")

	// Check subnet
	ss, err := f.Subnets()
	h.Ok(t, err)
	if _, ok := ss[s.ID]; !ok {
		t.Error("Subnet is missing from list")
	}
	h.Equals(t, ss[s.ID].CIDR.String(), "10.10.10.0/24")

}