File: ifplugo_test.go

package info (click to toggle)
golang-github-satta-ifplugo 0.0~git20200508.ca679be-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 100 kB
  • sloc: makefile: 5
file content (104 lines) | stat: -rw-r--r-- 1,761 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package ifplugo

import (
	"log"
	"net"
	"sync"
	"testing"
	"time"
)

func TestIface(t *testing.T) {
	intfs, err := net.Interfaces()
	if err != nil {
		t.Fatal(err)
	}
	if len(intfs) == 0 {
		log.Println("No interfaces present, skipping...")
		t.SkipNow()
	}

	empty := true
	for _, intf := range intfs {
		stats, err := GetLinkStatus(intf.Name)
		if err != nil {
			continue
		}
		log.Println("Got status for ", intf.Name)

		if stats != InterfaceErr {
			empty = false
		}
	}

	if empty {
		t.Fatal("Unable to retrieve status from any interface of this system.")
	}
}

func TestMonitor(t *testing.T) {
	intfs, err := net.Interfaces()
	if err != nil {
		t.Fatal(err)
	}
	if len(intfs) == 0 {
		log.Println("No interfaces present, skipping...")
		t.SkipNow()
	}

	ifaces := make([]string, 0)
	for _, intf := range intfs {
		ifaces = append(ifaces, intf.Name)
	}

	waitChan := make(chan bool)
	outChan := make(chan LinkStatusSample)
	mon := MakeLinkStatusMonitor(2*time.Second, ifaces, outChan)

	var resMutex sync.Mutex
	cnt := 0
	results := make(map[string]int)
	go func(c *int) {
		for o := range outChan {
			resMutex.Lock()
			for k, v := range o.Ifaces {
				results[k]++
				log.Printf("got status for %s: %s", k, v)
			}
			(*c)++
			resMutex.Unlock()
		}
		close(waitChan)
	}(&cnt)

	mon.Run()
	time.Sleep(5 * time.Second)
	mon.Stop()

	resMutex.Lock()
	if cnt != 1 {
		t.Fatalf("expected 1 output, got %d", cnt)
	}
	for _, v := range ifaces {
		if results[v] == 0 {
			t.Fatalf("unseen interface %s", v)
		}
	}

	for k := range results {
		found := false
		for _, i := range ifaces {
			if i == k {
				found = true
				break
			}
		}
		if !found {
			t.Fatalf("unknown result interface %s", k)
		}
	}
	resMutex.Unlock()
	close(outChan)
	<-waitChan

}