istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/xds/vm_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package xds_test
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  	"time"
    21  
    22  	"istio.io/api/networking/v1alpha3"
    23  	"istio.io/istio/pilot/pkg/features"
    24  	"istio.io/istio/pilot/pkg/model"
    25  	"istio.io/istio/pilot/test/xds"
    26  	"istio.io/istio/pkg/config"
    27  	"istio.io/istio/pkg/config/schema/gvk"
    28  	"istio.io/istio/pkg/test"
    29  	"istio.io/istio/pkg/test/util/assert"
    30  	"istio.io/istio/pkg/test/util/retry"
    31  )
    32  
    33  // TestRegistration is an e2e test for registration. Most tests are in autoregister package, but this
    34  // exercises the full XDS flow.
    35  func TestRegistration(t *testing.T) {
    36  	// TODO: allow fake XDS to be "authenticated"
    37  	test.SetForTest(t, &features.ValidateWorkloadEntryIdentity, false)
    38  	ds := xds.NewFakeDiscoveryServer(t, xds.FakeOptions{})
    39  	ds.Store().Create(config.Config{
    40  		Meta: config.Meta{
    41  			GroupVersionKind: gvk.WorkloadGroup,
    42  			Name:             "wg",
    43  			Namespace:        "namespace",
    44  		},
    45  		Spec: &v1alpha3.WorkloadGroup{
    46  			Template: &v1alpha3.WorkloadEntry{
    47  				Labels: map[string]string{
    48  					"merge": "wg",
    49  					"wg":    "1",
    50  				},
    51  			},
    52  		},
    53  		Status: nil,
    54  	})
    55  	proxy := &model.Proxy{
    56  		Labels:      map[string]string{"merge": "me"},
    57  		IPAddresses: []string{"1.1.1.1"},
    58  		Metadata: &model.NodeMetadata{
    59  			AutoRegisterGroup: "wg",
    60  			Namespace:         "namespace",
    61  			Network:           "network1",
    62  			Labels:            map[string]string{"merge": "meta", "meta": "2"},
    63  		},
    64  	}
    65  	ds.Connect(ds.SetupProxy(proxy), nil, nil)
    66  	var we *config.Config
    67  	retry.UntilSuccessOrFail(t, func() error {
    68  		we = ds.Store().Get(gvk.WorkloadEntry, "wg-1.1.1.1-network1", "namespace")
    69  		if we == nil {
    70  			return fmt.Errorf("no WE found")
    71  		}
    72  		return nil
    73  	}, retry.Timeout(time.Second*10))
    74  	assert.Equal(t, we.Spec.(*v1alpha3.WorkloadEntry).Labels, map[string]string{
    75  		"merge": "meta",
    76  		"meta":  "2",
    77  		"wg":    "1",
    78  	})
    79  }