istio.io/istio@v0.0.0-20240520182934-d79c90f27776/cni/pkg/install/binaries_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 install
    16  
    17  import (
    18  	"path/filepath"
    19  	"testing"
    20  
    21  	"istio.io/istio/pkg/test/util/assert"
    22  	"istio.io/istio/pkg/test/util/file"
    23  )
    24  
    25  func TestCopyBinaries(t *testing.T) {
    26  	cases := []struct {
    27  		name          string
    28  		srcFiles      map[string]string
    29  		existingFiles map[string]string
    30  		expectedFiles map[string]string
    31  	}{
    32  		{
    33  			name:          "basic",
    34  			srcFiles:      map[string]string{"istio-cni": "cni111", "istio-iptables": "iptables111"},
    35  			expectedFiles: map[string]string{"istio-cni": "cni111", "istio-iptables": "iptables111"},
    36  		},
    37  		{
    38  			name:          "update binaries",
    39  			srcFiles:      map[string]string{"istio-cni": "cni111", "istio-iptables": "iptables111"},
    40  			existingFiles: map[string]string{"istio-cni": "cni000", "istio-iptables": "iptables111"},
    41  			expectedFiles: map[string]string{"istio-cni": "cni111", "istio-iptables": "iptables111"},
    42  		},
    43  	}
    44  
    45  	for _, c := range cases {
    46  		t.Run(c.name, func(t *testing.T) {
    47  			srcDir := t.TempDir()
    48  			for filename, contents := range c.srcFiles {
    49  				file.WriteOrFail(t, filepath.Join(srcDir, filename), []byte(contents))
    50  			}
    51  
    52  			targetDir := t.TempDir()
    53  			for filename, contents := range c.existingFiles {
    54  				file.WriteOrFail(t, filepath.Join(targetDir, filename), []byte(contents))
    55  			}
    56  
    57  			binariesCopied, err := copyBinaries(srcDir, []string{targetDir})
    58  			if err != nil {
    59  				t.Fatal(err)
    60  			}
    61  
    62  			for filename, expectedContents := range c.expectedFiles {
    63  				contents := file.AsStringOrFail(t, filepath.Join(targetDir, filename))
    64  				assert.Equal(t, contents, expectedContents)
    65  
    66  				wasCopied := false
    67  				for _, bin := range binariesCopied.UnsortedList() {
    68  					if bin == filename {
    69  						wasCopied = true
    70  					}
    71  				}
    72  				assert.Equal(t, wasCopied, true)
    73  			}
    74  		})
    75  	}
    76  }