<commit_before>package iptables

// import . "gopkg.in/check.v1"

// func Test(t *testing.T) { TestingT(t) }
//
// type IptablesSuite struct {
// 	iptablesMngr *IptablesMngr
// }
//
// var _ = Suite(&IptablesSuite{})
//
// func (s *IptablesSuite) SetUpSuite(c *C) {
// 	var err error
// 	s.iptablesMngr, err = New(defaultConfig())
// 	c.Assert(err, IsNil)
// }
//
// func (s *IptablesSuite) SetUpTest(c *C) {
// }
//
// func (s *IptablesSuite) TearDownSuite(c *C) {
// 	rules, err := s.iptablesMngr.getSnatRules()
// 	c.Assert(err, IsNil)
//
// 	for _, r := range rules {
// 		err := s.iptablesMngr.removeRule(r)
// 		c.Assert(err, IsNil)
// 	}
// }
//
// func (s *IptablesSuite) TestSync(c *C) {
// 	if os.Getenv("TRAVIS") == "true" {
// 		c.Skip("Skipping test because travis-ci do not allow iptables")
// 	}
//
// 	state, err := state.New(defaultConfig())
// 	c.Assert(err, IsNil)
//
// 	s1 := &types.Service{
// 		Name:         "test",
// 		Host:         "172.16.31.10",
// 		Port:         80,
// 		Mode:         "nat",
// 		Protocol:     "tcp",
// 		Destinations: []types.Destination{},
// 	}
// 	state.AddService(s1)
//
// 	state.AddService(&types.Service{
// 		Name:         "test2",
// 		Host:         "172.16.58.3",
// 		Port:         80,
// 		Protocol:     "tcp",
// 		Mode:         "nat",
// 		Destinations: []types.Destination{},
// 	})
//
// 	toSource, err := net.GetIpByInterface("eth0")
// 	c.Assert(err, IsNil)
// 	rule2 := SnatRule{
// 		vaddr:    "172.16.58.3",
// 		vport:    "80",
// 		toSource: toSource,
// 	}
// 	rule3 := SnatRule{
// 		vaddr:    "192.168.3.11",
// 		vport:    "80",
// 		toSource: toSource,
// 	}
//
// 	err = s.iptablesMngr.addRule(rule2)
// 	c.Assert(err, IsNil)
// 	err = s.iptablesMngr.addRule(rule3)
// 	c.Assert(err, IsNil)
//
// 	err = s.iptablesMngr.Sync(*state)
// 	c.Assert(err, IsNil)
//
// 	rules, err := s.iptablesMngr.getKernelRulesSet()
// 	c.Assert(err, IsNil)
//
// 	rule1, err := s.iptablesMngr.serviceToSnatRule(*s1)
// 	c.Assert(err, IsNil)
//
// 	c.Assert(rules.Contains(rule2, *rule1), Equals, true)
// }
//
// func defaultConfig() *config.BalancerConfig {
// 	return &config.BalancerConfig{
// 		PublicInterface:  "eth0",
// 		PrivateInterface: "eth0",
// 		Name:             "Test",
// 		ConfigPath:       "/tmp/test",
// 		Bootstrap:        true,
// 		Provider: config.Provider{
// 			Type: "none",
// 			Params: map[string]string{
// 				"interface": "eth0",
// 				"vip-range": "192.168.0.0/28",
// 			},
// 		},
// 	}
// }
<commit_msg>Fixing iptables tests<commit_after>package iptables

import (
	"os"
	"testing"

	"github.com/luizbafilho/fusis/api/types"
	"github.com/luizbafilho/fusis/config"
	"github.com/luizbafilho/fusis/net"
	"github.com/luizbafilho/fusis/state"
	. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }

type IptablesSuite struct {
	iptablesMngr *IptablesMngr
}

var _ = Suite(&IptablesSuite{})

func (s *IptablesSuite) SetUpSuite(c *C) {
	var err error
	s.iptablesMngr, err = New(defaultConfig())
	c.Assert(err, IsNil)
}

func (s *IptablesSuite) SetUpTest(c *C) {
}

func (s *IptablesSuite) TearDownSuite(c *C) {
}

func (s *IptablesSuite) cleanupRules(c *C) {
	rules, err := s.iptablesMngr.getSnatRules()
	c.Assert(err, IsNil)

	for _, r := range rules {
		err := s.iptablesMngr.removeRule(r)
		c.Assert(err, IsNil)
	}
}

func (s *IptablesSuite) TestSync(c *C) {
	if os.Getenv("TRAVIS") == "true" {
		c.Skip("Skipping test because travis-ci do not allow iptables")
	}

	state, err := state.New(defaultConfig())
	c.Assert(err, IsNil)

	s1 := &types.Service{
		Name:         "test",
		Host:         "10.0.1.1",
		Port:         80,
		Mode:         "nat",
		Protocol:     "tcp",
		Destinations: []types.Destination{},
	}
	state.AddService(s1)

	state.AddService(&types.Service{
		Name:         "test2",
		Host:         "10.0.1.2",
		Port:         80,
		Protocol:     "tcp",
		Mode:         "nat",
		Destinations: []types.Destination{},
	})

	toSource, err := net.GetIpByInterface("eth0")
	c.Assert(err, IsNil)
	rule2 := SnatRule{
		vaddr:    "10.0.1.2",
		vport:    "80",
		toSource: toSource,
	}
	rule3 := SnatRule{
		vaddr:    "10.0.1.3",
		vport:    "80",
		toSource: toSource,
	}

	err = s.iptablesMngr.addRule(rule2)
	c.Assert(err, IsNil)
	err = s.iptablesMngr.addRule(rule3)
	c.Assert(err, IsNil)

	err = s.iptablesMngr.Sync(*state)
	c.Assert(err, IsNil)

	rules, err := s.iptablesMngr.getKernelRulesSet()
	c.Assert(err, IsNil)

	rule1, err := s.iptablesMngr.serviceToSnatRule(*s1)
	c.Assert(err, IsNil)

	c.Assert(rules.Contains(rule2, *rule1), Equals, true)

	s.cleanupRules(c)
}

func defaultConfig() *config.BalancerConfig {
	return &config.BalancerConfig{
		PublicInterface:  "eth0",
		PrivateInterface: "eth0",
		Name:             "Test",
		ConfigPath:       "/tmp/test",
		Bootstrap:        true,
		Provider: config.Provider{
			Type: "none",
			Params: map[string]string{
				"interface": "eth0",
				"vip-range": "192.168.0.0/28",
			},
		},
	}
}

{'fraction_non_alphanumeric': 0.196291817815397, 'fraction_numerical': 0.024586860137041516, 'mean_word_length': 2.61734693877551, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 10, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2818372', 'n_tokens_mistral': 2265, 'n_tokens_neox': 2121, 'n_words': 358}