File: monitor_test.go

package info (click to toggle)
golang-github-socketplane-libovsdb 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,468 kB
  • sloc: makefile: 59; sh: 27
file content (84 lines) | stat: -rw-r--r-- 2,046 bytes parent folder | download | duplicates (3)
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
package client

import (
	"encoding/json"
	"testing"

	"github.com/ovn-kubernetes/libovsdb/model"
	"github.com/ovn-kubernetes/libovsdb/ovsdb"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestWithTable(t *testing.T) {
	client, err := newOVSDBClient(defDB)
	require.NoError(t, err)
	m := newMonitor()
	opt := WithTable(&OpenvSwitch{})

	err = opt(client, m)
	require.NoError(t, err)

	assert.Len(t, m.Tables, 1)
}

func populateClientModel(t *testing.T, client *ovsdbClient) {
	var s ovsdb.DatabaseSchema
	err := json.Unmarshal([]byte(schema), &s)
	require.NoError(t, err)
	clientDBModel, err := model.NewClientDBModel("Open_vSwitch", map[string]model.Model{
		"Bridge":       &Bridge{},
		"Open_vSwitch": &OpenvSwitch{},
	})
	require.NoError(t, err)
	dbModel, errs := model.NewDatabaseModel(s, clientDBModel)
	assert.Empty(t, errs)
	client.primaryDB().model = dbModel
	require.NoError(t, err)
}

func TestWithTableAndFields(t *testing.T) {
	client, err := newOVSDBClient(defDB)
	require.NoError(t, err)
	populateClientModel(t, client)

	m := newMonitor()
	ovs := OpenvSwitch{}
	opt := WithTable(&ovs, &ovs.Bridges, &ovs.CurCfg)
	err = opt(client, m)
	require.NoError(t, err)

	assert.Len(t, m.Tables, 1)
	assert.ElementsMatch(t, []string{"bridges", "cur_cfg"}, m.Tables[0].Fields)
}

func TestWithTableAndFieldsAndConditions(t *testing.T) {
	client, err := newOVSDBClient(defDB)
	require.NoError(t, err)
	populateClientModel(t, client)

	m := newMonitor()
	bridge := Bridge{}

	conditions := []model.Condition{
		{
			Field:    &bridge.Name,
			Function: ovsdb.ConditionEqual,
			Value:    "foo",
		},
	}

	opt := WithConditionalTable(&bridge, conditions, &bridge.Name, &bridge.DatapathType)
	err = opt(client, m)
	require.NoError(t, err)

	assert.Len(t, m.Tables, 1)
	assert.ElementsMatch(t, []string{"name", "datapath_type"}, m.Tables[0].Fields)
	assert.ElementsMatch(t, []ovsdb.Condition{
		{
			Column:   "name",
			Function: ovsdb.ConditionEqual,
			Value:    "foo",
		},
	}, m.Tables[0].Conditions)
}