github.com/tetratelabs/proxy-wasm-go-sdk@v0.23.1-0.20240517021853-021aa9cf78e8/properties/wasm_test.go (about)

     1  package properties
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/proxytest"
     9  )
    10  
    11  func TestGetPluginName(t *testing.T) {
    12  	opt := proxytest.NewEmulatorOption().WithProperty(pluginName, []byte("istio-ingress.print-properties"))
    13  	_, reset := proxytest.NewHostEmulator(opt)
    14  	defer reset()
    15  
    16  	result, err := GetPluginName()
    17  	require.NoError(t, err)
    18  	require.Equal(t, "istio-ingress.print-properties", result)
    19  }
    20  
    21  func TestGetPluginRootId(t *testing.T) {
    22  	opt := proxytest.NewEmulatorOption().WithProperty(pluginRootId, []byte("print-properties"))
    23  	_, reset := proxytest.NewHostEmulator(opt)
    24  	defer reset()
    25  
    26  	result, err := GetPluginRootId()
    27  	require.NoError(t, err)
    28  	require.Equal(t, "print-properties", result)
    29  }
    30  
    31  func TestGetPluginVmId(t *testing.T) {
    32  	opt := proxytest.NewEmulatorOption().WithProperty(pluginVmId, []byte("plugin-vm-id-value"))
    33  	_, reset := proxytest.NewHostEmulator(opt)
    34  	defer reset()
    35  
    36  	result, err := GetPluginVmId()
    37  	require.NoError(t, err)
    38  	require.Equal(t, "plugin-vm-id-value", result)
    39  }
    40  
    41  func TestGetClusterName(t *testing.T) {
    42  	opt := proxytest.NewEmulatorOption().WithProperty(clusterName, []byte("outbound|80||httpbin.org"))
    43  	_, reset := proxytest.NewHostEmulator(opt)
    44  	defer reset()
    45  
    46  	result, err := GetClusterName()
    47  	require.NoError(t, err)
    48  	require.Equal(t, "outbound|80||httpbin.org", result)
    49  }
    50  
    51  func TestGetRouteName(t *testing.T) {
    52  	opt := proxytest.NewEmulatorOption().WithProperty(routeName, []byte("route-name-value"))
    53  	_, reset := proxytest.NewHostEmulator(opt)
    54  	defer reset()
    55  
    56  	result, err := GetRouteName()
    57  	require.NoError(t, err)
    58  	require.Equal(t, "route-name-value", result)
    59  }
    60  
    61  func TestGetListenerDirection(t *testing.T) {
    62  	tests := []struct {
    63  		name           string
    64  		input          uint64
    65  		expectedResult EnvoyTrafficDirection
    66  	}{
    67  		{
    68  			name:           "Unspecified",
    69  			input:          0,
    70  			expectedResult: EnvoyTrafficDirectionUnspecified,
    71  		},
    72  		{
    73  			name:           "Inbound",
    74  			input:          1,
    75  			expectedResult: EnvoyTrafficDirectionInbound,
    76  		},
    77  		{
    78  			name:           "Outound",
    79  			input:          2,
    80  			expectedResult: EnvoyTrafficDirectionOutbound,
    81  		},
    82  	}
    83  
    84  	for _, tt := range tests {
    85  		t.Run(tt.name, func(t *testing.T) {
    86  			opt := proxytest.NewEmulatorOption().WithProperty(listenerDirection, serializeUint64(tt.input))
    87  			_, reset := proxytest.NewHostEmulator(opt)
    88  			defer reset()
    89  
    90  			result, err := GetListenerDirection()
    91  			require.NoError(t, err)
    92  			require.Equal(t, tt.expectedResult, result)
    93  		})
    94  	}
    95  }
    96  
    97  func TestGetNodeId(t *testing.T) {
    98  	opt := proxytest.NewEmulatorOption().WithProperty(nodeId, []byte("router~10.244.0.22~istio-ingress-6d78c67d85-qsbtz.istio-ingress~istio-ingress.svc.cluster.local"))
    99  	_, reset := proxytest.NewHostEmulator(opt)
   100  	defer reset()
   101  
   102  	result, err := GetNodeId()
   103  	require.NoError(t, err)
   104  	require.Equal(t, "router~10.244.0.22~istio-ingress-6d78c67d85-qsbtz.istio-ingress~istio-ingress.svc.cluster.local", result)
   105  }
   106  
   107  func TestGetNodeCluster(t *testing.T) {
   108  	opt := proxytest.NewEmulatorOption().WithProperty(nodeCluster, []byte("istio-ingress.istio-ingress"))
   109  	_, reset := proxytest.NewHostEmulator(opt)
   110  	defer reset()
   111  
   112  	result, err := GetNodeCluster()
   113  	require.NoError(t, err)
   114  	require.Equal(t, "istio-ingress.istio-ingress", result)
   115  }
   116  
   117  func TestGetNodeDynamicParams(t *testing.T) {
   118  	opt := proxytest.NewEmulatorOption().WithProperty(nodeDynamicParams, []byte("dynamic-params-value"))
   119  	_, reset := proxytest.NewHostEmulator(opt)
   120  	defer reset()
   121  
   122  	result, err := GetNodeDynamicParams()
   123  	require.NoError(t, err)
   124  	require.Equal(t, "dynamic-params-value", result)
   125  }
   126  
   127  func TestGetNodeLocality(t *testing.T) {
   128  	opt := proxytest.NewEmulatorOption().
   129  		WithProperty(nodeLocalityRegion, []byte("region-value")).
   130  		WithProperty(nodeLocalityZone, []byte("zone-value")).
   131  		WithProperty(nodeLocalitySubzone, []byte("subzone-value"))
   132  	_, reset := proxytest.NewHostEmulator(opt)
   133  	defer reset()
   134  
   135  	result, err := GetNodeLocality()
   136  	require.NoError(t, err)
   137  	require.Equal(t, "region-value", result.Region)
   138  	require.Equal(t, "zone-value", result.Zone)
   139  	require.Equal(t, "subzone-value", result.Subzone)
   140  }
   141  
   142  func TestGetNodeUserAgentName(t *testing.T) {
   143  	opt := proxytest.NewEmulatorOption().WithProperty(nodeUserAgentName, []byte("envoy"))
   144  	_, reset := proxytest.NewHostEmulator(opt)
   145  	defer reset()
   146  
   147  	result, err := GetNodeUserAgentName()
   148  	require.NoError(t, err)
   149  	require.Equal(t, "envoy", result)
   150  }
   151  
   152  func TestGetNodeUserAgentVersion(t *testing.T) {
   153  	opt := proxytest.NewEmulatorOption().WithProperty(nodeUserAgentVersion, []byte("1.12.2"))
   154  	_, reset := proxytest.NewHostEmulator(opt)
   155  	defer reset()
   156  
   157  	result, err := GetNodeUserAgentVersion()
   158  	require.NoError(t, err)
   159  	require.Equal(t, "1.12.2", result)
   160  }
   161  
   162  func TestGetNodeUserAgentBuildVersion(t *testing.T) {
   163  	opt := proxytest.NewEmulatorOption().WithProperty(nodeUserAgentBuildVersion, []byte("build-version-value"))
   164  	_, reset := proxytest.NewHostEmulator(opt)
   165  	defer reset()
   166  
   167  	result, err := GetNodeUserAgentBuildVersion()
   168  	require.NoError(t, err)
   169  	require.Equal(t, "build-version-value", result)
   170  }
   171  
   172  func TestGetNodeClientFeatures(t *testing.T) {
   173  	opt := proxytest.NewEmulatorOption().WithProperty(nodeClientFeatures, serializeProtoStringSlice([]string{"feature1-data", "feature2-data"}))
   174  	_, reset := proxytest.NewHostEmulator(opt)
   175  	defer reset()
   176  
   177  	result, err := GetNodeClientFeatures()
   178  	require.NoError(t, err)
   179  	require.Equal(t, []string{"feature1-data", "feature2-data"}, result)
   180  }
   181  
   182  func TestGetNodeListeningAddresses(t *testing.T) {
   183  	opt := proxytest.NewEmulatorOption().WithProperty(nodeListeningAddresses, serializeStringSlice([]string{"192.168.0.10", "10.0.0.20"}))
   184  	_, reset := proxytest.NewHostEmulator(opt)
   185  	defer reset()
   186  
   187  	result, err := GetNodeListeningAddresses()
   188  	require.NoError(t, err)
   189  	require.Equal(t, []string{"192.168.0.10", "10.0.0.20"}, result)
   190  }