istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/cluster/staticvm/staticvm_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 staticvm
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	"gopkg.in/yaml.v3"
    22  
    23  	"istio.io/istio/pkg/test/framework/components/cluster"
    24  	"istio.io/istio/pkg/test/framework/components/echo"
    25  	"istio.io/istio/pkg/test/framework/components/namespace"
    26  )
    27  
    28  func TestBuild(t *testing.T) {
    29  	cfg := cluster.Config{}
    30  	if err := yaml.Unmarshal([]byte(`
    31  kind: StaticVM
    32  clusterName: static-vms
    33  primaryClusterName: istio-testing
    34  meta:
    35    deployments:
    36    - service: vm
    37      namespace: echo
    38      instances:
    39      - ip: 172.17.0.4
    40        instanceIP: 10.0.0.1
    41  `), &cfg); err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	got, err := build(cfg, cluster.Topology{})
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	if diff := cmp.Diff(got.(*vmcluster).vms, []echo.Config{{
    49  		Service:         "vm",
    50  		Namespace:       namespace.Static("echo"),
    51  		StaticAddresses: []string{"172.17.0.4:10.0.0.1"},
    52  	}}); diff != "" {
    53  		t.Fatal(diff)
    54  	}
    55  }
    56  
    57  func TestVmcluster_CanDeploy(t *testing.T) {
    58  	aSvc := "a"
    59  	echoNS := namespace.Static("echo")
    60  	echoGenNS := namespace.Static("echo-1234")
    61  	ips := []string{"1.2.3.4"}
    62  	vms := vmcluster{vms: []echo.Config{{
    63  		Service: aSvc, Namespace: echoNS,
    64  		StaticAddresses: ips,
    65  	}}}
    66  
    67  	for name, tc := range map[string]struct {
    68  		given  echo.Config
    69  		want   echo.Config
    70  		wantOk bool
    71  	}{
    72  		"match": {
    73  			given:  echo.Config{DeployAsVM: true, Service: aSvc, Namespace: echoGenNS, Ports: []echo.Port{{Name: "grpc"}}},
    74  			want:   echo.Config{DeployAsVM: true, Service: aSvc, Namespace: echoGenNS, Ports: []echo.Port{{Name: "grpc"}}, StaticAddresses: ips},
    75  			wantOk: true,
    76  		},
    77  		"non vm": {
    78  			given: echo.Config{Service: aSvc, Namespace: echoGenNS, Ports: []echo.Port{{Name: "grpc"}}},
    79  		},
    80  		"namespace mismatch": {
    81  			given: echo.Config{DeployAsVM: true, Service: aSvc, Namespace: namespace.Static("other"), Ports: []echo.Port{{Name: "grpc"}}},
    82  		},
    83  		"service mismatch": {
    84  			given: echo.Config{DeployAsVM: true, Service: "b", Namespace: echoNS, Ports: []echo.Port{{Name: "grpc"}}},
    85  		},
    86  	} {
    87  		t.Run(name, func(t *testing.T) {
    88  			got, ok := vms.CanDeploy(tc.given)
    89  			if ok != tc.wantOk {
    90  				t.Errorf("got %v but wanted %v", ok, tc.wantOk)
    91  			}
    92  			if diff := cmp.Diff(tc.want, got); diff != "" {
    93  				t.Error(diff)
    94  			}
    95  		})
    96  	}
    97  }