...

Source file src/github.com/emissary-ingress/emissary/v3/cmd/entrypoint/testutil_fake_collision_test.go

Documentation: github.com/emissary-ingress/emissary/v3/cmd/entrypoint

     1  package entrypoint_test
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/emissary-ingress/emissary/v3/cmd/entrypoint"
    11  	v3bootstrap "github.com/emissary-ingress/emissary/v3/pkg/api/envoy/config/bootstrap/v3"
    12  	v3cluster "github.com/emissary-ingress/emissary/v3/pkg/api/envoy/config/cluster/v3"
    13  )
    14  
    15  func ClusterHasAltName(altName string) func(*v3cluster.Cluster) bool {
    16  	return func(c *v3cluster.Cluster) bool {
    17  		return c.AltStatName == altName
    18  	}
    19  }
    20  
    21  func TestFakeCollision(t *testing.T) {
    22  	f := entrypoint.RunFake(t, entrypoint.FakeConfig{EnvoyConfig: true, DiagdDebug: true}, nil)
    23  
    24  	assert.NoError(t, f.UpsertFile("testdata/Collision1.yaml"))
    25  	f.Flush()
    26  
    27  	snap, err := f.GetSnapshot(HasMapping("staging", "subway-staging-socket-stable-mapping"))
    28  	require.NoError(t, err)
    29  
    30  	// assert.Equal(t, "hello", snap.Kubernetes.Mappings[0].Name)
    31  	assert.NotNil(t, snap)
    32  
    33  	// The first time we look at the Envoy config, we should find only three clusters:
    34  	// one with an AltStatName of "127_0_0_1_8877", one with an AltStatName of
    35  	// "subway_staging_stable_staging_3000", and one with an AltStatName of
    36  	// "subway_staging_stable_staging_3001".
    37  
    38  	envoyConfig, err := f.GetEnvoyConfig(func(config *v3bootstrap.Bootstrap) bool {
    39  		// Three clusters...
    40  		if len(config.StaticResources.Clusters) != 3 {
    41  			return false
    42  		}
    43  
    44  		// ...each of which has an AltStatName that we like.
    45  		foundClusters := map[string]bool{}
    46  
    47  		for _, cluster := range config.StaticResources.Clusters {
    48  			foundClusters[cluster.AltStatName] = true
    49  		}
    50  
    51  		// Make sure we found exactly the clusters we need.
    52  		if !foundClusters["127_0_0_1_8877"] ||
    53  			!foundClusters["subway_staging_stable_staging_3000"] ||
    54  			!foundClusters["subway_staging_stable_staging_3001"] {
    55  			return false
    56  		}
    57  
    58  		return true
    59  	})
    60  
    61  	require.NoError(t, err)
    62  	assert.NotNil(t, envoyConfig)
    63  
    64  	LogJSON(t, envoyConfig)
    65  
    66  	// Once here, we _must_ have found the two clusters we wanted. Grab them and take
    67  	// a look inside to make sure all is well.
    68  
    69  	for _, cluster := range envoyConfig.StaticResources.Clusters {
    70  		LogJSON(t, cluster)
    71  	}
    72  
    73  	c0 := FindCluster(envoyConfig, ClusterHasAltName("subway_staging_stable_staging_3000"))
    74  	assert.NotNil(t, c0)
    75  
    76  	c1 := FindCluster(envoyConfig, ClusterHasAltName("subway_staging_stable_staging_3001"))
    77  	assert.NotNil(t, c1)
    78  
    79  	// OK. c0 should get its load assignments from EDS with a key of
    80  	// k8s/staging/subway-staging-stable/3000, and it should have a name that starts with
    81  	// cluster_subway_staging_stable_staging_30- and ends with -0.
    82  
    83  	assert.Equal(t, "k8s/staging/subway-staging-stable/3000", c0.EdsClusterConfig.ServiceName)
    84  
    85  	assert.True(t, strings.HasPrefix(c0.Name, "cluster_subway_staging_stable_staging_30-"))
    86  	assert.True(t, strings.HasSuffix(c0.Name, "-0"))
    87  
    88  	// For c1, we need load assignments from EDS with a key of
    89  	// k8s/staging/subway-staging-stable/3001, and its name should have... the same
    90  	// prefix and suffix as the first cluster did, actually!
    91  
    92  	assert.Equal(t, "k8s/staging/subway-staging-stable/3001", c1.EdsClusterConfig.ServiceName)
    93  
    94  	assert.True(t, strings.HasPrefix(c1.Name, "cluster_subway_staging_stable_staging_30-"))
    95  	assert.True(t, strings.HasSuffix(c1.Name, "-0"))
    96  
    97  	// Finally, no two clusters should have the same name -- even though c0 and c1 have names
    98  	// with the same prefix and suffix, they must be different.
    99  
   100  	nameMap := map[string]int{}
   101  
   102  	for _, cluster := range envoyConfig.StaticResources.Clusters {
   103  		nameMap[cluster.Name]++
   104  		assert.Equal(t, 1, nameMap[cluster.Name])
   105  	}
   106  
   107  	// Next up: add another Mapping to subway-staging-stable.staging:3000.
   108  	assert.NoError(t, f.UpsertFile("testdata/Collision2.yaml"))
   109  	f.Flush()
   110  
   111  	snap, err = f.GetSnapshot(HasMapping("staging", "subway-staging-socket-stable-mapping"))
   112  
   113  	// assert.Equal(t, "hello", snap.Kubernetes.Mappings[0].Name)
   114  	assert.NotNil(t, snap)
   115  
   116  	// Here, we need _four_ clusters: yes, we already have a Mapping that uses
   117  	// subway-staging-stable.staging:3000, but it uses it differently so we should have
   118  	// created a new cluster.
   119  	//
   120  	// We will still only three AltStatNames, though! The new cluster should have an
   121  	// AltStatName of "subway_staging_stable_staging_3000", like one of our original
   122  	// clusters.
   123  
   124  	envoyConfig, err = f.GetEnvoyConfig(func(config *v3bootstrap.Bootstrap) bool {
   125  		// Four clusters...
   126  		if len(config.StaticResources.Clusters) != 4 {
   127  			return false
   128  		}
   129  
   130  		// ...each of which has an AltStatName that we like.
   131  		foundClusters := map[string]bool{}
   132  
   133  		for _, cluster := range config.StaticResources.Clusters {
   134  			foundClusters[cluster.AltStatName] = true
   135  		}
   136  
   137  		// Make sure we found exactly the clusters we need.
   138  		if !foundClusters["127_0_0_1_8877"] ||
   139  			!foundClusters["subway_staging_stable_staging_3000"] ||
   140  			!foundClusters["subway_staging_stable_staging_3001"] {
   141  			return false
   142  		}
   143  
   144  		return true
   145  	})
   146  
   147  	require.NoError(t, err)
   148  	assert.NotNil(t, envoyConfig)
   149  
   150  	LogJSON(t, envoyConfig)
   151  
   152  	// Once here, we _must_ have found the three clusters we're really interested in. Grab them
   153  	// and take a look inside to make sure all is well.
   154  	//
   155  	// However, since we have two clusters with AltStatName "subway_staging_stable_staging_3000",
   156  	// we can't just grab clusters by AltStatName like we did the first time. Instead, we'll
   157  	// iterate over clusters and figure out what we have.
   158  
   159  	nameMap = map[string]int{}
   160  	altNameMap := map[string]int{}
   161  
   162  	for _, cluster := range envoyConfig.StaticResources.Clusters {
   163  		// No two clusters should have the same name.
   164  		nameMap[cluster.Name]++
   165  		assert.Equal(t, 1, nameMap[cluster.Name])
   166  
   167  		// But! two clusters should have the same AltStatName. So. First, bump the altNameMap
   168  		// for this cluster's AltStatName.
   169  		altNameMap[cluster.AltStatName]++
   170  
   171  		// Next, if this isn't the 127_0_0_1_8877 cluster, make sure its AltStatName has the
   172  		// correct prefix and suffix (and yes, the suffixes should all be -0 now -- we should
   173  		// honestly just get rid of those).
   174  
   175  		if cluster.AltStatName != "127_0_0_1_8877" {
   176  			assert.True(t, strings.HasPrefix(cluster.Name, "cluster_subway_staging_stable_staging_30-"))
   177  			assert.True(t, strings.HasSuffix(cluster.Name, "-0"))
   178  		}
   179  	}
   180  
   181  	// Once here, we know that we have four clusters, we know they have unique names, we've
   182  	// counted the AltStatNames, and we know the AltStatNames have the correct prefix and suffix.
   183  	// All that's left is to verify the counts of the AltStatNames.
   184  
   185  	assert.Equal(t, 1, altNameMap["127_0_0_1_8877"])
   186  	assert.Equal(t, 2, altNameMap["subway_staging_stable_staging_3000"])
   187  	assert.Equal(t, 1, altNameMap["subway_staging_stable_staging_3001"])
   188  }
   189  

View as plain text