File: ifplugo_test.go

package info (click to toggle)
golang-github-satta-ifplugo 0.0~git20180801.8b80699-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 204 kB
  • sloc: ansic: 986; makefile: 5
file content (120 lines) | stat: -rw-r--r-- 2,494 bytes parent folder | download
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package ifplugo

// This file is part of ifplugo.
//
// ifplugo is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// ifplugo is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ifplugo; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

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

}