...

Source file src/github.com/datawire/ambassador/v2/cmd/entrypoint/testutil_fake_collision_test.go

Documentation: github.com/datawire/ambassador/v2/cmd/entrypoint

     1  package entrypoint_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/datawire/ambassador/v2/cmd/entrypoint"
    10  	v3bootstrap "github.com/datawire/ambassador/v2/pkg/api/envoy/config/bootstrap/v3"
    11  )
    12  
    13  func TestFakeCollision(t *testing.T) {
    14  	f := entrypoint.RunFake(t, entrypoint.FakeConfig{EnvoyConfig: true, DiagdDebug: true}, nil)
    15  
    16  	assert.NoError(t, f.UpsertFile("testdata/Collision1.yaml"))
    17  	f.Flush()
    18  
    19  	snap, err := f.GetSnapshot(HasMapping("staging", "subway-staging-socket-stable-mapping"))
    20  	require.NoError(t, err)
    21  
    22  	// assert.Equal(t, "hello", snap.Kubernetes.Mappings[0].Name)
    23  	assert.NotNil(t, snap)
    24  
    25  	// Grab the next envoy config that satisfies our predicate.
    26  	envoyConfig, err := f.GetEnvoyConfig(func(config *v3bootstrap.Bootstrap) bool {
    27  		// The first time we look at the Envoy config, we should find only two clusters.
    28  		//
    29  		// First up, a cluster named cluster_subway_staging_stable_staging_30-0,
    30  		// which should get its load assignments from EDS with a key of
    31  		// k8s/staging/subway-staging-stable/3000.
    32  		c0 := FindCluster(config, ClusterNameContains("cluster_subway_staging_stable_staging_30-0"))
    33  
    34  		if c0 == nil {
    35  			return false
    36  		}
    37  
    38  		if c0.EdsClusterConfig.ServiceName != "k8s/staging/subway-staging-stable/3000" {
    39  			return false
    40  		}
    41  
    42  		// We also need a cluster named cluster_subway_staging_stable_staging_30-1,
    43  		// which should get its load assignments from EDS with a key of
    44  		// k8s/staging/subway-staging-stable/3001.
    45  		c1 := FindCluster(config, ClusterNameContains("cluster_subway_staging_stable_staging_30-1"))
    46  
    47  		if c1 == nil {
    48  			return false
    49  		}
    50  
    51  		if c1.EdsClusterConfig.ServiceName != "k8s/staging/subway-staging-stable/3001" {
    52  			return false
    53  		}
    54  
    55  		// We need to _not_ have a cluster named cluster_subway_staging_stable_staging_30-2.
    56  
    57  		c2 := FindCluster(config, ClusterNameContains("cluster_subway_staging_stable_staging_30-2"))
    58  
    59  		if c2 != nil {
    60  			return false
    61  		}
    62  
    63  		return true
    64  	})
    65  	require.NoError(t, err)
    66  	assert.NotNil(t, envoyConfig)
    67  
    68  	LogJSON(t, envoyConfig)
    69  
    70  	assert.NoError(t, f.UpsertFile("testdata/Collision2.yaml"))
    71  	f.Flush()
    72  
    73  	snap, err = f.GetSnapshot(HasMapping("staging", "subway-staging-socket-stable-mapping"))
    74  
    75  	// assert.Equal(t, "hello", snap.Kubernetes.Mappings[0].Name)
    76  	assert.NotNil(t, snap)
    77  
    78  	// Grab the next envoy config that satisfies our predicate.
    79  	envoyConfig, err = f.GetEnvoyConfig(func(config *v3bootstrap.Bootstrap) bool {
    80  		// The second time we look at the Envoy config, we need to see three
    81  		// clusters, but note that some of the contents of the clusters will have
    82  		// changed.
    83  		//
    84  		// We still need a cluster named cluster_subway_staging_stable_staging_30-0,
    85  		// and it should still get its load assignments from EDS with a key of
    86  		// k8s/staging/subway-staging-stable/3000.
    87  		c0 := FindCluster(config, ClusterNameContains("cluster_subway_staging_stable_staging_30-0"))
    88  
    89  		if c0 == nil {
    90  			return false
    91  		}
    92  
    93  		if c0.EdsClusterConfig.ServiceName != "k8s/staging/subway-staging-stable/3000" {
    94  			return false
    95  		}
    96  
    97  		// We still need a cluster named cluster_subway_staging_stable_staging_30-1,
    98  		// but its load assignments should now also come from EDS with a key of
    99  		// k8s/staging/subway-staging-stable/3000.
   100  		c1 := FindCluster(config, ClusterNameContains("cluster_subway_staging_stable_staging_30-1"))
   101  
   102  		if c1 == nil {
   103  			return false
   104  		}
   105  
   106  		if c1.EdsClusterConfig.ServiceName != "k8s/staging/subway-staging-stable/3000" {
   107  			return false
   108  		}
   109  
   110  		// Finally, we need a cluster named cluster_subway_staging_stable_staging_30-2,
   111  		// with load assignments coming from EDS with a key of
   112  		// k8s/staging/subway-staging-stable/3001.
   113  		c2 := FindCluster(config, ClusterNameContains("cluster_subway_staging_stable_staging_30-2"))
   114  
   115  		if c2 == nil {
   116  			return false
   117  		}
   118  
   119  		if c2.EdsClusterConfig.ServiceName != "k8s/staging/subway-staging-stable/3001" {
   120  			return false
   121  		}
   122  
   123  		return true
   124  	})
   125  	require.NoError(t, err)
   126  	assert.NotNil(t, envoyConfig)
   127  
   128  	LogJSON(t, envoyConfig)
   129  }
   130  

View as plain text