github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/utils/nfqparser/nfqparser_test.go (about) 1 // +build !windows 2 3 package nfqparser 4 5 import ( 6 "fmt" 7 "io/ioutil" 8 "testing" 9 10 . "github.com/smartystreets/goconvey/convey" 11 ) 12 13 const ( 14 testFilePath = "/tmp/nfqdrops" 15 testNFQData = ` 0 13206 0 2 65531 0 0 0 1 16 1 3333107750 0 2 65531 0 0 0 1 17 2 3881398569 0 2 65531 0 0 1 1 18 3 2633750685 0 2 65531 0 0 0 1 19 4 3605545056 0 2 65531 0 0 0 1 20 5 3473230188 0 2 65531 0 0 2 1 21 6 4025478776 0 2 65531 0 0 3 1 22 7 2806986372 0 2 65531 0 0 1 1` 23 ) 24 25 func init() { 26 testCreateFile() 27 } 28 29 func testProperLayout() *NFQLayout { 30 31 return &NFQLayout{ 32 QueueNum: "6", 33 PeerPortID: "4025478776", 34 QueueTotal: "0", 35 CopyMode: "2", 36 CopyRange: "65531", 37 QueueDropped: "0", 38 UserDropped: "0", 39 IDSequence: "3", 40 } 41 } 42 43 func testCreateFile() { 44 45 if err := ioutil.WriteFile(testFilePath, []byte(testNFQData), 0777); err != nil { 46 panic(err) 47 } 48 } 49 50 func TestNFQParserRetrieveByQueue(t *testing.T) { 51 52 Convey("Given I create a new nfqparser instance", t, func() { 53 nfqParser := NewNFQParser() 54 nfqParser.filePath = testFilePath 55 56 Convey("Given I try to synchronize data", func() { 57 err := nfqParser.Synchronize() 58 59 Convey("Then I should not get any errors", func() { 60 So(err, ShouldBeNil) 61 }) 62 63 Convey("Given I try to retrieve data for a queue", func() { 64 queueData := nfqParser.RetrieveByQueue("6") 65 66 Convey("Then queue data should match", func() { 67 So(queueData, ShouldResemble, testProperLayout()) 68 }) 69 }) 70 71 Convey("Given I try to retrieve data for a queue and compare a specific field", func() { 72 queueData := nfqParser.RetrieveByQueue("6") 73 74 Convey("Then queue data should match", func() { 75 So(queueData.CopyRange, ShouldEqual, testProperLayout().CopyRange) 76 }) 77 }) 78 79 Convey("Given I try to retrieve data for a queue with different expected data", func() { 80 queueData := nfqParser.RetrieveByQueue("1") 81 82 Convey("Then queue data should match", func() { 83 So(queueData, ShouldNotResemble, testProperLayout()) 84 }) 85 }) 86 87 Convey("Given I try to retrieve data for a invalid queue num", func() { 88 queueData := nfqParser.RetrieveByQueue("9") 89 90 Convey("Then queue data should match", func() { 91 So(queueData, ShouldBeNil) 92 }) 93 }) 94 }) 95 }) 96 } 97 98 func TestNFQParserRetrieveAll(t *testing.T) { 99 100 Convey("Given I create a new nfqparser instance", t, func() { 101 nfqParser := NewNFQParser() 102 nfqParser.filePath = testFilePath 103 104 Convey("Given I try to synchronize data", func() { 105 err := nfqParser.Synchronize() 106 107 Convey("Then I should not get any errors", func() { 108 So(err, ShouldBeNil) 109 }) 110 111 Convey("Given I try to retrieve all", func() { 112 queueData := nfqParser.RetrieveAll() 113 114 Convey("Then length of queue data should be equal", func() { 115 So(len(queueData), ShouldEqual, 8) 116 }) 117 }) 118 }) 119 }) 120 } 121 122 func TestNFQParserString(t *testing.T) { 123 124 Convey("Given I create a new nfqparser instance", t, func() { 125 nfqParser := NewNFQParser() 126 nfqParser.filePath = testFilePath 127 128 Convey("Given I try to synchronize data", func() { 129 err := nfqParser.Synchronize() 130 131 Convey("Then I should not get any errors", func() { 132 So(err, ShouldBeNil) 133 }) 134 135 Convey("Given I try to get string representation", func() { 136 strData := nfqParser.String() 137 138 Convey("Then queue data should match", func() { 139 So(strData, ShouldEqual, testNFQData) 140 }) 141 }) 142 143 Convey("Given I try to retrieve data for a queue with different expected data", func() { 144 queueData := nfqParser.RetrieveByQueue("6") 145 146 Convey("Then queue data should match", func() { 147 So(queueData, ShouldResemble, testProperLayout()) 148 }) 149 150 Convey("Given I try to get string representation of particular queue number", func() { 151 queueData := nfqParser.RetrieveByQueue("6") 152 153 Convey("Then queue data should match", func() { 154 So(queueData.String(), ShouldEqual, fmt.Sprintf("%v", testProperLayout())) 155 }) 156 }) 157 158 Convey("Given I try to get string representation of empty layout, it should not panic", func() { 159 nfqParser := NewNFQParser() 160 str := nfqParser.RetrieveByQueue("6").String() 161 162 Convey("Then queue data should match", func() { 163 So(str, ShouldEqual, "") 164 }) 165 }) 166 }) 167 }) 168 }) 169 }