github.com/webmeshproj/webmesh-cni@v0.0.27/internal/types/install_test.go (about)

     1  /*
     2  Copyright 2023 Avi Zimmerman <avi.zimmerman@gmail.com>.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package types
    18  
    19  import (
    20  	"bytes"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"k8s.io/client-go/rest"
    26  )
    27  
    28  func TestInstallCNI(t *testing.T) {
    29  	// Setup temp directories
    30  	setSuidBit = func(string) error {
    31  		return nil
    32  	}
    33  	getInstallRestConfig = func() (*rest.Config, error) {
    34  		return &rest.Config{}, nil
    35  	}
    36  	i := NewTestInstallation(t, "TODO")
    37  	opts := i.Options()
    38  	err := opts.RunInstall()
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	i.ValidateInstallation(t)
    43  }
    44  
    45  type TestInstallation struct {
    46  	SourceDir        string
    47  	SourceBinaryName string
    48  	BinaryDestDir    string
    49  	ConfDestDir      string
    50  	ConfDestName     string
    51  	ConfTemplate     string
    52  	HostLocalNetDir  string
    53  }
    54  
    55  func NewTestInstallation(t *testing.T, confTemplate string) *TestInstallation {
    56  	t.Helper()
    57  	var i TestInstallation
    58  	var err error
    59  	i.ConfTemplate = confTemplate
    60  	i.SourceDir, err = os.MkdirTemp("", "")
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	// Write a fake source binary
    65  	err = os.WriteFile(i.SourceDir+"/source-bin", []byte("test"), 0644)
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	i.SourceBinaryName = "source-bin"
    70  	t.Cleanup(func() { os.RemoveAll(i.SourceDir) })
    71  	i.BinaryDestDir, err = os.MkdirTemp("", "")
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	t.Cleanup(func() { os.RemoveAll(i.BinaryDestDir) })
    76  	i.ConfDestDir, err = os.MkdirTemp("", "")
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	i.ConfDestName = "test-conf"
    81  	t.Cleanup(func() { os.RemoveAll(i.ConfDestDir) })
    82  	i.HostLocalNetDir, err = os.MkdirTemp("", "")
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	t.Cleanup(func() { os.RemoveAll(i.HostLocalNetDir) })
    87  	return &i
    88  }
    89  
    90  func (i *TestInstallation) Options() *InstallOptions {
    91  	return &InstallOptions{
    92  		SourceBinary:    filepath.Join(i.SourceDir, i.SourceBinaryName),
    93  		BinaryDestBin:   i.BinaryDestDir,
    94  		ConfDestDir:     i.ConfDestDir,
    95  		ConfDestName:    i.ConfDestName,
    96  		HostLocalNetDir: i.HostLocalNetDir,
    97  		NetConfTemplate: i.ConfTemplate,
    98  		NodeName:        "test-node",
    99  		Namespace:       "test-namespace",
   100  	}
   101  }
   102  
   103  func (i *TestInstallation) ValidateInstallation(t *testing.T) {
   104  	t.Helper()
   105  	// Check that the binary was copied
   106  	installedBin := filepath.Join(i.BinaryDestDir, PluginBinaryName)
   107  	data, err := os.ReadFile(installedBin)
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  	if !bytes.Equal(data, []byte("test")) {
   112  		t.Fatal("Expected binary to be copied, got", string(data))
   113  	}
   114  	// Check that a kubeconfig exists.
   115  	kubeconfigPath := filepath.Join(i.ConfDestDir, PluginKubeconfigName)
   116  	_, err = os.Stat(kubeconfigPath)
   117  	if err != nil {
   118  		t.Fatal(err)
   119  	}
   120  	// Check that the config was written
   121  	confPath := filepath.Join(i.ConfDestDir, i.ConfDestName)
   122  	data, err = os.ReadFile(confPath)
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	if !bytes.Equal(data, []byte(i.ConfTemplate)) {
   127  		t.Fatal("Expected config to be written, got", string(data))
   128  	}
   129  }