istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/config/monitor/file_snapshot_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 monitor
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  
    24  	networking "istio.io/api/networking/v1alpha3"
    25  	"istio.io/istio/pkg/config/schema/collection"
    26  	"istio.io/istio/pkg/config/schema/collections"
    27  )
    28  
    29  var gatewayYAML = `
    30  apiVersion: networking.istio.io/v1alpha3
    31  kind: Gateway
    32  metadata:
    33    name: some-ingress
    34  spec:
    35    servers:
    36    - port:
    37        number: 80
    38        name: http
    39        protocol: http
    40      hosts:
    41      - "*.example.com"
    42  `
    43  
    44  var statusRegressionYAML = `
    45  apiVersion: networking.istio.io/v1alpha3
    46  kind: Gateway
    47  metadata:
    48    name: test
    49    namespace: test-1
    50  spec:
    51    selector:
    52      app: istio-ingressgateway
    53    servers:
    54    - hosts:
    55      - example.com
    56      port:
    57        name: http
    58        number: 80
    59        protocol: HTTP
    60  status: {}`
    61  
    62  var virtualServiceYAML = `
    63  apiVersion: networking.istio.io/v1alpha3
    64  kind: VirtualService
    65  metadata:
    66    name: route-for-myapp
    67  spec:
    68    hosts:
    69    - some.example.com
    70    gateways:
    71    - some-ingress
    72    http:
    73    - route:
    74      - destination:
    75          host: some.example.internal
    76  `
    77  
    78  func TestFileSnapshotNoFilter(t *testing.T) {
    79  	g := NewWithT(t)
    80  
    81  	ts := &testState{
    82  		ConfigFiles: map[string][]byte{"gateway.yml": []byte(gatewayYAML)},
    83  	}
    84  
    85  	ts.testSetup(t)
    86  
    87  	fileWatcher := NewFileSnapshot(ts.rootPath, collection.SchemasFor(), "foo")
    88  	configs, err := fileWatcher.ReadConfigFiles()
    89  	g.Expect(err).NotTo(HaveOccurred())
    90  	g.Expect(configs).To(HaveLen(1))
    91  	g.Expect(configs[0].Domain).To(Equal("foo"))
    92  
    93  	gateway := configs[0].Spec.(*networking.Gateway)
    94  	g.Expect(gateway.Servers[0].Port.Number).To(Equal(uint32(80)))
    95  	g.Expect(gateway.Servers[0].Port.Protocol).To(Equal("http"))
    96  	g.Expect(gateway.Servers[0].Hosts).To(Equal([]string{"*.example.com"}))
    97  }
    98  
    99  func TestFileSnapshotWithFilter(t *testing.T) {
   100  	g := NewWithT(t)
   101  
   102  	ts := &testState{
   103  		ConfigFiles: map[string][]byte{
   104  			"gateway.yml":         []byte(gatewayYAML),
   105  			"virtual_service.yml": []byte(virtualServiceYAML),
   106  		},
   107  	}
   108  
   109  	ts.testSetup(t)
   110  
   111  	fileWatcher := NewFileSnapshot(ts.rootPath, collection.SchemasFor(collections.VirtualService), "")
   112  	configs, err := fileWatcher.ReadConfigFiles()
   113  	g.Expect(err).NotTo(HaveOccurred())
   114  	g.Expect(configs).To(HaveLen(1))
   115  
   116  	virtualService := configs[0].Spec.(*networking.VirtualService)
   117  	g.Expect(virtualService.Hosts).To(Equal([]string{"some.example.com"}))
   118  }
   119  
   120  func TestFileSnapshotSorting(t *testing.T) {
   121  	g := NewWithT(t)
   122  
   123  	ts := &testState{
   124  		ConfigFiles: map[string][]byte{
   125  			"z.yml": []byte(gatewayYAML),
   126  			"a.yml": []byte(virtualServiceYAML),
   127  		},
   128  	}
   129  
   130  	ts.testSetup(t)
   131  
   132  	fileWatcher := NewFileSnapshot(ts.rootPath, collection.SchemasFor(), "")
   133  
   134  	configs, err := fileWatcher.ReadConfigFiles()
   135  	g.Expect(err).NotTo(HaveOccurred())
   136  	g.Expect(configs).To(HaveLen(2))
   137  
   138  	g.Expect(configs[0].Spec).To(BeAssignableToTypeOf(&networking.Gateway{}))
   139  	g.Expect(configs[1].Spec).To(BeAssignableToTypeOf(&networking.VirtualService{}))
   140  }
   141  
   142  type testState struct {
   143  	ConfigFiles map[string][]byte
   144  	rootPath    string
   145  }
   146  
   147  func (ts *testState) testSetup(t *testing.T) {
   148  	var err error
   149  
   150  	ts.rootPath = t.TempDir()
   151  
   152  	for name, content := range ts.ConfigFiles {
   153  		err = os.WriteFile(filepath.Join(ts.rootPath, name), content, 0o600)
   154  		if err != nil {
   155  			t.Fatal(err)
   156  		}
   157  	}
   158  }