diff --git a/endpoint/endpoint_test.go b/endpoint/endpoint_test.go index f6c4b52..ddc69e2 100644 --- a/endpoint/endpoint_test.go +++ b/endpoint/endpoint_test.go @@ -4,10 +4,58 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" lua "github.com/yuin/gopher-lua" ) -func TestLuaTableConversions(t *testing.T) { +func TestToLuaTable(t *testing.T) { + state := lua.NewState() + defer state.Close() + err := state.DoFile("test_lua/test_to_lua_table.lua") + require.NoError(t, err) + testFunc := state.Get(-1).(*lua.LFunction) + + endpoint := &Endpoint{ + Hostname: "test-host", + IPv4s: []string{"192.0.2.1"}, + IPv6s: []string{"2001:db8::1"}, + RecordTTL: 60, + SourceProperties: map[string]any{ + "source_prop": "prop", + }, + ProviderProperties: map[string]any{ + "provider_prop": "prop", + }, + } + ltEndpoint := endpoint.ToLuaTable(state) + co, _ := state.NewThread() + st, err, _ := state.Resume(co, testFunc, ltEndpoint) + require.NoError(t, err) + require.Equal(t, st, lua.ResumeOK) +} + +func TestFromLuaTable(t *testing.T) { + state := lua.NewState() + defer state.Close() + err := state.DoFile("test_lua/test_from_lua_table.lua") + require.NoError(t, err) + testFunc := state.Get(-1).(*lua.LFunction) + co, _ := state.NewThread() + st, err, values := state.Resume(co, testFunc) + require.NoError(t, err) + require.Equal(t, st, lua.ResumeOK) + ltEndpoint := values[0].(*lua.LTable) + endpoint := FromLuaTable(state, ltEndpoint) + assert.Equal(t, "test-host", endpoint.Hostname) + assert.Equal(t, []string{"192.0.2.1"}, endpoint.IPv4s) + assert.Equal(t, []string{"2001:db8::1"}, endpoint.IPv6s) + assert.Equal(t, int64(60), endpoint.RecordTTL) + assert.Equal(t, map[string]any{"source_prop": "prop"}, endpoint.SourceProperties) + assert.Equal(t, map[string]any{"provider_prop": "prop"}, endpoint.ProviderProperties) +} + +func TestLuaTableDiff(t *testing.T) { tests := map[string]struct { endpoint *Endpoint }{ diff --git a/endpoint/test_lua/test_from_lua_table.lua b/endpoint/test_lua/test_from_lua_table.lua new file mode 100644 index 0000000..6a5c1e6 --- /dev/null +++ b/endpoint/test_lua/test_from_lua_table.lua @@ -0,0 +1,14 @@ +return function () + return { + hostname = "test-host", + ipv4s = { "192.0.2.1" }, + ipv6s = { "2001:db8::1" }, + record_ttl = 60, + source_properties = { + source_prop = "prop", + }, + provider_properties = { + provider_prop = "prop", + }, + } +end diff --git a/endpoint/test_lua/test_to_lua_table.lua b/endpoint/test_lua/test_to_lua_table.lua new file mode 100644 index 0000000..dbd289f --- /dev/null +++ b/endpoint/test_lua/test_to_lua_table.lua @@ -0,0 +1,14 @@ +local assert_equal = function(a, b) + if not a == b then + error(string.format("mismatch: '%s' != '%s'.", a, b)) + end +end + +return function (endpoint) + assert_equal(endpoint.hostname, "test-host") + assert_equal(endpoint.ipv4s[1], "192.0.2.1") + assert_equal(endpoint.ipv6s[1], "2001:db8::1") + assert_equal(endpoint.record_ttl, 60) + assert_equal(endpoint.source_properties.source_prop, "prop") + assert_equal(endpoint.provider_properties.provider_prop, "prop") +end