Revision 3c81218ba373248450e46728f5667a899bb38109 authored by Grot (@grafanabot) on 14 May 2021, 06:52:03 UTC, committed by GitHub on 14 May 2021, 06:52:03 UTC
* Timeline: Text align option, but does not work

* working text alignment

* Refactoring and fixing rendering text values on the right edge that does not fit

Co-authored-by: Leon Sorokin <leeoniya@gmail.com>
(cherry picked from commit 96183d70a8bebdd6ee79a01fcc3611a8fb3b9da1)

Co-authored-by: Torkel Ödegaard <torkel@grafana.org>
1 parent 728a2bd
Raw File
address_test.go
package network

import (
	"net"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestGetIPFromAddress(t *testing.T) {
	testCases := []struct {
		desc   string
		input  string
		exp    string
		expErr string
	}{
		{
			desc:  "Valid IPv4",
			input: "192.168.2.1",
			exp:   "192.168.2.1",
		},
		{
			desc:  "Valid IPv6",
			input: "2001:0db8:0000:0000:0000:ff00:0042:8329",
			exp:   "2001:db8::ff00:42:8329",
		},
		{
			desc:  "Valid IPv6 enclosed in square brackets",
			input: "[2001:0db8:0000:0000:0000:ff00:0042:8329]",
			exp:   "2001:db8::ff00:42:8329",
		},
		{
			desc:  "Valid IPv4/port pair",
			input: "192.168.2.1:5000",
			exp:   "192.168.2.1",
		},
		{
			desc:  "Valid IPv6/port pair",
			input: "[2001:0db8:0000:0000:0000:ff00:0042:8329]:5000",
			exp:   "2001:db8::ff00:42:8329",
		},
		{
			desc:   "Invalid IPv6/port pair",
			input:  "[2001:0db8:0000:0000:0000:ff00:0042:8329]:5000:2000",
			expErr: `not a valid IP address or IP address/port pair: "[2001:0db8:0000:0000:0000:ff00:0042:8329]:5000:2000"`,
		},
		{
			desc:   "IPv6 with too many parts",
			input:  "2001:0db8:0000:0000:0000:ff00:0042:8329:1234",
			expErr: `not a valid IP address or IP address/port pair: "2001:0db8:0000:0000:0000:ff00:0042:8329:1234"`,
		},
		{
			desc:   "IPv6 with too few parts",
			input:  "2001:0db8:0000:0000:0000:ff00:0042",
			expErr: `not a valid IP address or IP address/port pair: "2001:0db8:0000:0000:0000:ff00:0042"`,
		},
		{
			desc:  "Valid shortened IPv6",
			input: "2001:db8::ff00:42:8329",
			exp:   "2001:db8::ff00:42:8329",
		},
		{
			desc:  "IPv6 loopback address",
			input: "::1",
			exp:   "::1",
		},
	}
	for _, tc := range testCases {
		t.Run(tc.desc, func(t *testing.T) {
			ip, err := GetIPFromAddress(tc.input)
			if tc.expErr == "" {
				exp := net.ParseIP(tc.exp)
				require.NotNil(t, exp)
				require.NoError(t, err)
				assert.Equal(t, exp, ip)
			} else {
				require.EqualError(t, err, tc.expErr)
			}
		})
	}
}
back to top