...

Source file src/k8s.io/kubernetes/pkg/kubelet/pluginmanager/cache/actual_state_of_world_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet/pluginmanager/cache

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     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 cache
    18  
    19  import (
    20  	"runtime"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  // Calls AddPlugin() to add a plugin
    28  // Verifies newly added plugin exists in GetRegisteredPlugins()
    29  // Verifies PluginExistsWithCorrectTimestamp returns true for the plugin
    30  func Test_ASW_AddPlugin_Positive_NewPlugin(t *testing.T) {
    31  	pluginInfo := PluginInfo{
    32  		SocketPath: "/var/lib/kubelet/device-plugins/test-plugin.sock",
    33  		Timestamp:  time.Now(),
    34  		Handler:    nil,
    35  		Name:       "test",
    36  	}
    37  	asw := NewActualStateOfWorld()
    38  	err := asw.AddPlugin(pluginInfo)
    39  	// Assert
    40  	if err != nil {
    41  		t.Fatalf("AddPlugin failed. Expected: <no error> Actual: <%v>", err)
    42  	}
    43  
    44  	// Get registered plugins and check the newly added plugin is there
    45  	aswPlugins := asw.GetRegisteredPlugins()
    46  	if len(aswPlugins) != 1 {
    47  		t.Fatalf("Actual state of world length should be one but it's %d", len(aswPlugins))
    48  	}
    49  	if aswPlugins[0] != pluginInfo {
    50  		t.Fatalf("Expected\n%v\nin actual state of world, but got\n%v\n", pluginInfo, aswPlugins[0])
    51  	}
    52  
    53  	// Check PluginExistsWithCorrectTimestamp returns true
    54  	if !asw.PluginExistsWithCorrectTimestamp(pluginInfo) {
    55  		t.Fatalf("PluginExistsWithCorrectTimestamp returns false for plugin that should be registered")
    56  	}
    57  }
    58  
    59  // Calls AddPlugin() to add an empty string for socket path
    60  // Verifies the plugin does not exist in GetRegisteredPlugins()
    61  // Verifies PluginExistsWithCorrectTimestamp returns false
    62  func Test_ASW_AddPlugin_Negative_EmptySocketPath(t *testing.T) {
    63  	asw := NewActualStateOfWorld()
    64  	pluginInfo := PluginInfo{
    65  		SocketPath: "",
    66  		Timestamp:  time.Now(),
    67  		Handler:    nil,
    68  		Name:       "test",
    69  	}
    70  	err := asw.AddPlugin(pluginInfo)
    71  	require.EqualError(t, err, "socket path is empty")
    72  
    73  	// Get registered plugins and check the newly added plugin is there
    74  	aswPlugins := asw.GetRegisteredPlugins()
    75  	if len(aswPlugins) != 0 {
    76  		t.Fatalf("Actual state of world length should be zero but it's %d", len(aswPlugins))
    77  	}
    78  
    79  	// Check PluginExistsWithCorrectTimestamp returns false
    80  	if asw.PluginExistsWithCorrectTimestamp(pluginInfo) {
    81  		t.Fatalf("PluginExistsWithCorrectTimestamp returns true for plugin that's not registered")
    82  	}
    83  }
    84  
    85  // Calls RemovePlugin() to remove a plugin
    86  // Verifies newly removed plugin no longer exists in GetRegisteredPlugins()
    87  // Verifies PluginExistsWithCorrectTimestamp returns false
    88  func Test_ASW_RemovePlugin_Positive(t *testing.T) {
    89  	// First, add a plugin
    90  	asw := NewActualStateOfWorld()
    91  	pluginInfo := PluginInfo{
    92  		SocketPath: "/var/lib/kubelet/device-plugins/test-plugin.sock",
    93  		Timestamp:  time.Now(),
    94  		Handler:    nil,
    95  		Name:       "test",
    96  	}
    97  	err := asw.AddPlugin(pluginInfo)
    98  	// Assert
    99  	if err != nil {
   100  		t.Fatalf("AddPlugin failed. Expected: <no error> Actual: <%v>", err)
   101  	}
   102  
   103  	// Try removing this plugin
   104  	asw.RemovePlugin(pluginInfo.SocketPath)
   105  
   106  	// Get registered plugins and check the newly added plugin is not there
   107  	aswPlugins := asw.GetRegisteredPlugins()
   108  	if len(aswPlugins) != 0 {
   109  		t.Fatalf("Actual state of world length should be zero but it's %d", len(aswPlugins))
   110  	}
   111  
   112  	// Check PluginExistsWithCorrectTimestamp returns false
   113  	if asw.PluginExistsWithCorrectTimestamp(pluginInfo) {
   114  		t.Fatalf("PluginExistsWithCorrectTimestamp returns true for the removed plugin")
   115  	}
   116  }
   117  
   118  // Verifies PluginExistsWithCorrectTimestamp returns false for an existing
   119  // plugin with the wrong timestamp
   120  func Test_ASW_PluginExistsWithCorrectTimestamp_Negative_WrongTimestamp(t *testing.T) {
   121  	// Skip tests that fail on Windows, as discussed during the SIG Testing meeting from January 10, 2023
   122  	if runtime.GOOS == "windows" {
   123  		t.Skip("Skipping test that fails on Windows")
   124  	}
   125  
   126  	// First, add a plugin
   127  	asw := NewActualStateOfWorld()
   128  	pluginInfo := PluginInfo{
   129  		SocketPath: "/var/lib/kubelet/device-plugins/test-plugin.sock",
   130  		Timestamp:  time.Now(),
   131  		Handler:    nil,
   132  		Name:       "test",
   133  	}
   134  	err := asw.AddPlugin(pluginInfo)
   135  	// Assert
   136  	if err != nil {
   137  		t.Fatalf("AddPlugin failed. Expected: <no error> Actual: <%v>", err)
   138  	}
   139  
   140  	newerPlugin := PluginInfo{
   141  		SocketPath: "/var/lib/kubelet/device-plugins/test-plugin.sock",
   142  		Timestamp:  time.Now(),
   143  	}
   144  	// Check PluginExistsWithCorrectTimestamp returns false
   145  	if asw.PluginExistsWithCorrectTimestamp(newerPlugin) {
   146  		t.Fatalf("PluginExistsWithCorrectTimestamp returns true for a plugin with newer timestamp")
   147  	}
   148  }
   149  

View as plain text