From fe978c8a26dd240e3ff0a56ac24b33e222789c0d Mon Sep 17 00:00:00 2001 From: Manoram Sharma Date: Tue, 28 May 2024 12:33:54 +0530 Subject: [PATCH 1/5] Added tests for File, List and Scheduler under runtime pkg Signed-off-by: Manoram Sharma --- pkg/kwokctl/runtime/file_test.go | 211 ++++++++++++++++++++++++++ pkg/kwokctl/runtime/list_test.go | 89 +++++++++++ pkg/kwokctl/runtime/scheduler_test.go | 74 +++++++++ 3 files changed, 374 insertions(+) create mode 100644 pkg/kwokctl/runtime/file_test.go create mode 100644 pkg/kwokctl/runtime/list_test.go create mode 100644 pkg/kwokctl/runtime/scheduler_test.go diff --git a/pkg/kwokctl/runtime/file_test.go b/pkg/kwokctl/runtime/file_test.go new file mode 100644 index 000000000..bf8219a0d --- /dev/null +++ b/pkg/kwokctl/runtime/file_test.go @@ -0,0 +1,211 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package runtime + +import ( + "os" + "path/filepath" + "testing" +) + +func TestCreateFile(t *testing.T) { + // Create a temporary directory + tmpDir := t.TempDir() + + // Define the path for the test file + testFilePath := filepath.Join(tmpDir, "test.txt") + + // Create a mock Cluster instance + cluster := &Cluster{} + + // Test CreateFile function + err := cluster.CreateFile(testFilePath) + if err != nil { + t.Fatalf("CreateFile returned an unexpected error: %v", err) + } + + // Check if the file exists + if _, err := os.Stat(testFilePath); os.IsNotExist(err) { + t.Errorf("CreateFile did not create the file as expected") + } +} + +func TestCopyFile(t *testing.T) { + + // Create a temporary directory + tmpDir := t.TempDir() + + // Define the path for the test files + srcFilePath := filepath.Join(tmpDir, "src.txt") + destFilePath := filepath.Join(tmpDir, "dest.txt") + + // Create a mock source file + if err := os.WriteFile(srcFilePath, []byte("source"), 0644); err != nil { + t.Fatalf("Failed to create source file: %v", err) + } + + // Create a mock Cluster instance + cluster := &Cluster{} + + // Test CopyFile function + err := cluster.CopyFile(srcFilePath, destFilePath) + if err != nil { + t.Fatalf("CopyFile returned an unexpected error: %v", err) + } + + // Check if the destination file exists and has the same content as the source file + destContent, err := os.ReadFile(destFilePath) + if err != nil { + t.Fatalf("Failed to read destination file: %v", err) + } + srcContent, err := os.ReadFile(srcFilePath) + if err != nil { + t.Fatalf("Failed to read source file: %v", err) + } + if string(destContent) != string(srcContent) { + t.Errorf("CopyFile did not copy the file content as expected") + } +} + +func TestRenameFile(t *testing.T) { + + // Create a temporary directory + tmpDir := t.TempDir() + + // Define the path for the test files + oldFilePath := filepath.Join(tmpDir, "old.txt") + newFilePath := filepath.Join(tmpDir, "new.txt") + + // Create a mock source file + if err := os.WriteFile(oldFilePath, []byte("content"), 0644); err != nil { + t.Fatalf("Failed to create source file: %v", err) + } + + // Create a mock Cluster instance + cluster := &Cluster{} + + // Test RenameFile function + err := cluster.RenameFile(oldFilePath, newFilePath) + if err != nil { + t.Fatalf("RenameFile returned an unexpected error: %v", err) + } + + // Check if the old file exists and the new file exists after renaming + if _, err := os.Stat(oldFilePath); !os.IsNotExist(err) { + t.Errorf("RenameFile did not remove the old file as expected") + } + if _, err := os.Stat(newFilePath); os.IsNotExist(err) { + t.Errorf("RenameFile did not create the new file as expected") + } +} + +func TestAppendToFile(t *testing.T) { + + // Create a temporary directory + tmpDir := t.TempDir() + + // Define the path for the test file + testFilePath := filepath.Join(tmpDir, "test.txt") + + // Create a mock Cluster instance + cluster := &Cluster{} + + // Write initial content to the file + initialContent := []byte("initial content") + if err := os.WriteFile(testFilePath, initialContent, 0644); err != nil { + t.Fatalf("Failed to create test file: %v", err) + } + + // Define content to append + appendContent := []byte("appended content") + + // Test AppendToFile function + err := cluster.AppendToFile(testFilePath, appendContent) + if err != nil { + t.Fatalf("AppendToFile returned an unexpected error: %v", err) + } + + // Read the file to verify the appended content + fileContent, err := os.ReadFile(testFilePath) + if err != nil { + t.Fatalf("Failed to read test file: %v", err) + } + + // Check if the content contains both initial and appended content + expectedContent := append(initialContent, appendContent...) + if string(fileContent) != string(expectedContent) { + t.Errorf("AppendToFile did not append the content to the file as expected") + } +} + +func TestRemove(t *testing.T) { + // Create a temporary directory + tmpDir := t.TempDir() + + // Define the path for the test file + testFilePath := filepath.Join(tmpDir, "test.txt") + + // Create a mock file + if _, err := os.Create(testFilePath); err != nil { + t.Fatalf("Failed to create test file: %v", err) + } + + // Create a mock Cluster instance + cluster := &Cluster{} + + // Test Remove function + err := cluster.Remove(testFilePath) + if err != nil { + t.Fatalf("Remove returned an unexpected error: %v", err) + } + + // Check if the file exists after removal + if _, err := os.Stat(testFilePath); !os.IsNotExist(err) { + t.Errorf("Remove did not remove the file as expected") + } +} + +func TestRemoveAll(t *testing.T) { + + // Create a temporary directory + tmpDir := t.TempDir() + + // Create a mock subdirectory and file + subDir := filepath.Join(tmpDir, "subdir") + testFilePath := filepath.Join(subDir, "test.txt") + + if err := os.Mkdir(subDir, 0755); err != nil { + t.Fatalf("Failed to create subdirectory: %v", err) + } + if _, err := os.Create(testFilePath); err != nil { + t.Fatalf("Failed to create test file: %v", err) + } + + // Create a mock Cluster instance + cluster := &Cluster{} + + // Test RemoveAll function + err := cluster.RemoveAll(tmpDir) + if err != nil { + t.Fatalf("RemoveAll returned an unexpected error: %v", err) + } + + // Check if the directory and its contents exist after removal + if _, err := os.Stat(subDir); !os.IsNotExist(err) { + t.Errorf("RemoveAll did not remove the directory as expected") + } +} \ No newline at end of file diff --git a/pkg/kwokctl/runtime/list_test.go b/pkg/kwokctl/runtime/list_test.go new file mode 100644 index 000000000..41091e64d --- /dev/null +++ b/pkg/kwokctl/runtime/list_test.go @@ -0,0 +1,89 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package runtime + +import ( + "context" + "os" + "path/filepath" + "reflect" + "testing" + + "sigs.k8s.io/kwok/pkg/config" + "sigs.k8s.io/kwok/pkg/consts" +) + +func TestListClusters(t *testing.T) { + ctx := context.TODO() + + // Create a temporary directory for testing clusters + tmpDir := t.TempDir() + + // Set the clusters directory to the temporary directory + config.ClustersDir = tmpDir + + // Create some temporary cluster directories and config files + clusterDirs := []string{"cluster1", "cluster2"} + for _, clusterDir := range clusterDirs { + clusterPath := filepath.Join(tmpDir, clusterDir) + if err := os.MkdirAll(clusterPath, 0755); err != nil { + t.Fatal(err) + } + + // Create a config file in each cluster directory + configFilePath := filepath.Join(clusterPath, consts.ConfigName) + if _, err := os.Create(configFilePath); err != nil { + t.Fatal(err) + } + } + + // Test ListClusters function + clusters, err := ListClusters(ctx) + if err != nil { + t.Fatalf("ListClusters returned an unexpected error: %v", err) + } + + // Define the expected list of clusters + expectedClusters := []string{"cluster1", "cluster2"} + + // Compare the actual clusters with the expected clusters + if !reflect.DeepEqual(clusters, expectedClusters) { + t.Errorf("ListClusters returned unexpected clusters: got %v, want %v", clusters, expectedClusters) + } +} + +func TestGetUsedPorts(t *testing.T) { + ctx := context.TODO() + + // Create a temporary directory for testing clusters + tmpDir := t.TempDir() + defer os.RemoveAll(tmpDir) + + // Create some temporary cluster directories + clusterDirs := []string{"cluster1", "cluster2"} + for _, clusterDir := range clusterDirs { + if err := os.MkdirAll(tmpDir+"/"+clusterDir, 0755); err != nil { + t.Fatal(err) + } + } + + // Test GetUsedPorts function + usedPorts := GetUsedPorts(ctx) + if len(usedPorts) != 0 { + t.Errorf("GetUsedPorts returned unexpected used ports: got %v, want empty set", usedPorts) + } +} \ No newline at end of file diff --git a/pkg/kwokctl/runtime/scheduler_test.go b/pkg/kwokctl/runtime/scheduler_test.go new file mode 100644 index 000000000..52364c037 --- /dev/null +++ b/pkg/kwokctl/runtime/scheduler_test.go @@ -0,0 +1,74 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package runtime + +import ( + "fmt" + "os" + "path/filepath" + "testing" +) + +func TestCopySchedulerConfig(t *testing.T) { + // Create a temporary directory for testing + tmpDir := t.TempDir() + + // Define paths for old and new scheduler configuration files + oldSchedulerConfig := filepath.Join(tmpDir, "old_scheduler_config.yaml") + newSchedulerConfig := filepath.Join(tmpDir, "new_scheduler_config.yaml") + kubeconfigPath := filepath.Join(tmpDir, "kubeconfig.yaml") + + // Create a temporary old scheduler configuration file + err := os.WriteFile(oldSchedulerConfig, []byte("old scheduler config"), 0644) + if err != nil { + t.Fatal(err) + } + + // Create a temporary kubeconfig file + err = os.WriteFile(kubeconfigPath, []byte("kubeconfig"), 0644) + if err != nil { + t.Fatal(err) + } + + // Create a mock Cluster instance + cluster := &Cluster{} + + // Test CopySchedulerConfig function + err = cluster.CopySchedulerConfig(oldSchedulerConfig, newSchedulerConfig, kubeconfigPath) + if err != nil { + t.Fatalf("CopySchedulerConfig returned an unexpected error: %v", err) + } + + // Verify that the new scheduler configuration file exists + _, err = os.Stat(newSchedulerConfig) + if err != nil { + t.Fatalf("New scheduler configuration file does not exist: %v", err) + } + + // Read the contents of the new scheduler configuration file + newConfigBytes, err := os.ReadFile(newSchedulerConfig) + if err != nil { + t.Fatalf("Failed to read new scheduler configuration file: %v", err) + } + + // Define the expected content of the new scheduler configuration file + expectedConfig := fmt.Sprintf("old scheduler config\nclientConnection:\n kubeconfig: %q\n", kubeconfigPath) + + // Compare the actual content with the expected content + if string(newConfigBytes) != expectedConfig { + t.Errorf("Unexpected content in the new scheduler configuration file: got %q, want %q", string(newConfigBytes), expectedConfig) + } +} From aa5271c787423cbe1f539671146472d2a4a9eef0 Mon Sep 17 00:00:00 2001 From: Manoram Sharma Date: Wed, 29 May 2024 18:11:47 +0530 Subject: [PATCH 2/5] Solved lint errors Signed-off-by: Manoram Sharma --- pkg/kwokctl/runtime/file_test.go | 24 +++++++++++------------- pkg/kwokctl/runtime/list_test.go | 11 ++++++++--- pkg/kwokctl/runtime/scheduler_test.go | 4 ++-- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/pkg/kwokctl/runtime/file_test.go b/pkg/kwokctl/runtime/file_test.go index bf8219a0d..f2cf843a4 100644 --- a/pkg/kwokctl/runtime/file_test.go +++ b/pkg/kwokctl/runtime/file_test.go @@ -45,8 +45,7 @@ func TestCreateFile(t *testing.T) { } func TestCopyFile(t *testing.T) { - - // Create a temporary directory + // Create a temporary directory tmpDir := t.TempDir() // Define the path for the test files @@ -54,7 +53,7 @@ func TestCopyFile(t *testing.T) { destFilePath := filepath.Join(tmpDir, "dest.txt") // Create a mock source file - if err := os.WriteFile(srcFilePath, []byte("source"), 0644); err != nil { + if err := os.WriteFile(srcFilePath, []byte("source"), 0640); err != nil { t.Fatalf("Failed to create source file: %v", err) } @@ -82,8 +81,7 @@ func TestCopyFile(t *testing.T) { } func TestRenameFile(t *testing.T) { - - // Create a temporary directory + // Create a temporary directory tmpDir := t.TempDir() // Define the path for the test files @@ -91,7 +89,7 @@ func TestRenameFile(t *testing.T) { newFilePath := filepath.Join(tmpDir, "new.txt") // Create a mock source file - if err := os.WriteFile(oldFilePath, []byte("content"), 0644); err != nil { + if err := os.WriteFile(oldFilePath, []byte("content"), 0640); err != nil { t.Fatalf("Failed to create source file: %v", err) } @@ -114,8 +112,7 @@ func TestRenameFile(t *testing.T) { } func TestAppendToFile(t *testing.T) { - - // Create a temporary directory + // Create a temporary directory tmpDir := t.TempDir() // Define the path for the test file @@ -126,7 +123,7 @@ func TestAppendToFile(t *testing.T) { // Write initial content to the file initialContent := []byte("initial content") - if err := os.WriteFile(testFilePath, initialContent, 0644); err != nil { + if err := os.WriteFile(testFilePath, initialContent, 0640); err != nil { t.Fatalf("Failed to create test file: %v", err) } @@ -146,7 +143,9 @@ func TestAppendToFile(t *testing.T) { } // Check if the content contains both initial and appended content - expectedContent := append(initialContent, appendContent...) + tempContent := append(initialContent, appendContent...) + expectedContent := append(initialContent[:0], tempContent...) + if string(fileContent) != string(expectedContent) { t.Errorf("AppendToFile did not append the content to the file as expected") } @@ -180,15 +179,14 @@ func TestRemove(t *testing.T) { } func TestRemoveAll(t *testing.T) { - - // Create a temporary directory + // Create a temporary directory tmpDir := t.TempDir() // Create a mock subdirectory and file subDir := filepath.Join(tmpDir, "subdir") testFilePath := filepath.Join(subDir, "test.txt") - if err := os.Mkdir(subDir, 0755); err != nil { + if err := os.Mkdir(subDir, 0750); err != nil { t.Fatalf("Failed to create subdirectory: %v", err) } if _, err := os.Create(testFilePath); err != nil { diff --git a/pkg/kwokctl/runtime/list_test.go b/pkg/kwokctl/runtime/list_test.go index 41091e64d..c2675902f 100644 --- a/pkg/kwokctl/runtime/list_test.go +++ b/pkg/kwokctl/runtime/list_test.go @@ -40,7 +40,7 @@ func TestListClusters(t *testing.T) { clusterDirs := []string{"cluster1", "cluster2"} for _, clusterDir := range clusterDirs { clusterPath := filepath.Join(tmpDir, clusterDir) - if err := os.MkdirAll(clusterPath, 0755); err != nil { + if err := os.MkdirAll(clusterPath, 0750); err != nil { t.Fatal(err) } @@ -71,12 +71,17 @@ func TestGetUsedPorts(t *testing.T) { // Create a temporary directory for testing clusters tmpDir := t.TempDir() - defer os.RemoveAll(tmpDir) + // Defer the removal of the temporary directory + defer func() { + if err := os.RemoveAll(tmpDir); err != nil { + t.Errorf("error removing temporary directory: %v", err) + } + }() // Create some temporary cluster directories clusterDirs := []string{"cluster1", "cluster2"} for _, clusterDir := range clusterDirs { - if err := os.MkdirAll(tmpDir+"/"+clusterDir, 0755); err != nil { + if err := os.MkdirAll(tmpDir+"/"+clusterDir, 0750); err != nil { t.Fatal(err) } } diff --git a/pkg/kwokctl/runtime/scheduler_test.go b/pkg/kwokctl/runtime/scheduler_test.go index 52364c037..88fb047b1 100644 --- a/pkg/kwokctl/runtime/scheduler_test.go +++ b/pkg/kwokctl/runtime/scheduler_test.go @@ -32,13 +32,13 @@ func TestCopySchedulerConfig(t *testing.T) { kubeconfigPath := filepath.Join(tmpDir, "kubeconfig.yaml") // Create a temporary old scheduler configuration file - err := os.WriteFile(oldSchedulerConfig, []byte("old scheduler config"), 0644) + err := os.WriteFile(oldSchedulerConfig, []byte("old scheduler config"), 0640) if err != nil { t.Fatal(err) } // Create a temporary kubeconfig file - err = os.WriteFile(kubeconfigPath, []byte("kubeconfig"), 0644) + err = os.WriteFile(kubeconfigPath, []byte("kubeconfig"), 0640) if err != nil { t.Fatal(err) } From 89dbd0c07b90b00ebe1c12391f4ddedcc7202891 Mon Sep 17 00:00:00 2001 From: Manoram Sharma Date: Wed, 29 May 2024 18:26:48 +0530 Subject: [PATCH 3/5] update lint Signed-off-by: Manoram Sharma --- pkg/kwokctl/runtime/file_test.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/kwokctl/runtime/file_test.go b/pkg/kwokctl/runtime/file_test.go index f2cf843a4..61d92fe0c 100644 --- a/pkg/kwokctl/runtime/file_test.go +++ b/pkg/kwokctl/runtime/file_test.go @@ -143,12 +143,14 @@ func TestAppendToFile(t *testing.T) { } // Check if the content contains both initial and appended content - tempContent := append(initialContent, appendContent...) - expectedContent := append(initialContent[:0], tempContent...) - - if string(fileContent) != string(expectedContent) { - t.Errorf("AppendToFile did not append the content to the file as expected") - } + compareFileContent(t, initialContent, appendContent, fileContent) +} + +func compareFileContent(t *testing.T, initialContent, appendContent, fileContent []byte) { + expectedContent := append(initialContent, appendContent...) + if string(fileContent) != string(expectedContent) { + t.Errorf("AppendToFile did not append the content to the file as expected") + } } func TestRemove(t *testing.T) { From eb5ab9b00caf49658934830218e17e3ebe3799d1 Mon Sep 17 00:00:00 2001 From: Manoram Sharma Date: Wed, 29 May 2024 19:02:24 +0530 Subject: [PATCH 4/5] update lint Signed-off-by: Manoram Sharma --- pkg/kwokctl/runtime/file_test.go | 18 +++++++++--------- pkg/kwokctl/runtime/list_test.go | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pkg/kwokctl/runtime/file_test.go b/pkg/kwokctl/runtime/file_test.go index 61d92fe0c..4d365f022 100644 --- a/pkg/kwokctl/runtime/file_test.go +++ b/pkg/kwokctl/runtime/file_test.go @@ -45,7 +45,7 @@ func TestCreateFile(t *testing.T) { } func TestCopyFile(t *testing.T) { - // Create a temporary directory + // Create a temporary directory tmpDir := t.TempDir() // Define the path for the test files @@ -81,7 +81,7 @@ func TestCopyFile(t *testing.T) { } func TestRenameFile(t *testing.T) { - // Create a temporary directory + // Create a temporary directory tmpDir := t.TempDir() // Define the path for the test files @@ -112,7 +112,7 @@ func TestRenameFile(t *testing.T) { } func TestAppendToFile(t *testing.T) { - // Create a temporary directory + // Create a temporary directory tmpDir := t.TempDir() // Define the path for the test file @@ -147,10 +147,10 @@ func TestAppendToFile(t *testing.T) { } func compareFileContent(t *testing.T, initialContent, appendContent, fileContent []byte) { - expectedContent := append(initialContent, appendContent...) - if string(fileContent) != string(expectedContent) { - t.Errorf("AppendToFile did not append the content to the file as expected") - } + expectedContent := append(initialContent, appendContent...) + if string(fileContent) != string(expectedContent) { + t.Errorf("AppendToFile did not append the content to the file as expected") + } } func TestRemove(t *testing.T) { @@ -181,7 +181,7 @@ func TestRemove(t *testing.T) { } func TestRemoveAll(t *testing.T) { - // Create a temporary directory + // Create a temporary directory tmpDir := t.TempDir() // Create a mock subdirectory and file @@ -208,4 +208,4 @@ func TestRemoveAll(t *testing.T) { if _, err := os.Stat(subDir); !os.IsNotExist(err) { t.Errorf("RemoveAll did not remove the directory as expected") } -} \ No newline at end of file +} diff --git a/pkg/kwokctl/runtime/list_test.go b/pkg/kwokctl/runtime/list_test.go index c2675902f..06dec9faa 100644 --- a/pkg/kwokctl/runtime/list_test.go +++ b/pkg/kwokctl/runtime/list_test.go @@ -73,9 +73,9 @@ func TestGetUsedPorts(t *testing.T) { tmpDir := t.TempDir() // Defer the removal of the temporary directory defer func() { - if err := os.RemoveAll(tmpDir); err != nil { - t.Errorf("error removing temporary directory: %v", err) - } + if err := os.RemoveAll(tmpDir); err != nil { + t.Errorf("error removing temporary directory: %v", err) + } }() // Create some temporary cluster directories @@ -91,4 +91,4 @@ func TestGetUsedPorts(t *testing.T) { if len(usedPorts) != 0 { t.Errorf("GetUsedPorts returned unexpected used ports: got %v, want empty set", usedPorts) } -} \ No newline at end of file +} From 7269d4a5d5636ea29f9c61962031b44917c47bb6 Mon Sep 17 00:00:00 2001 From: Manoram Sharma Date: Wed, 29 May 2024 19:20:07 +0530 Subject: [PATCH 5/5] solved lint Signed-off-by: Manoram Sharma --- pkg/kwokctl/runtime/file_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/kwokctl/runtime/file_test.go b/pkg/kwokctl/runtime/file_test.go index 4d365f022..0b501282c 100644 --- a/pkg/kwokctl/runtime/file_test.go +++ b/pkg/kwokctl/runtime/file_test.go @@ -143,11 +143,9 @@ func TestAppendToFile(t *testing.T) { } // Check if the content contains both initial and appended content - compareFileContent(t, initialContent, appendContent, fileContent) -} + expectedContent := initialContent + expectedContent = append(expectedContent, appendContent...) -func compareFileContent(t *testing.T, initialContent, appendContent, fileContent []byte) { - expectedContent := append(initialContent, appendContent...) if string(fileContent) != string(expectedContent) { t.Errorf("AppendToFile did not append the content to the file as expected") }