Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for File, List and Scheduler under runtime pkg #1117

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 209 additions & 0 deletions pkg/kwokctl/runtime/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*
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"), 0640); 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"), 0640); 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, 0640); 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 := initialContent
expectedContent = append(expectedContent, 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, 0750); 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")
}
}
94 changes: 94 additions & 0 deletions pkg/kwokctl/runtime/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
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, 0750); 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 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, 0750); 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)
}
}
74 changes: 74 additions & 0 deletions pkg/kwokctl/runtime/scheduler_test.go
Original file line number Diff line number Diff line change
@@ -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"), 0640)
if err != nil {
t.Fatal(err)
}

// Create a temporary kubeconfig file
err = os.WriteFile(kubeconfigPath, []byte("kubeconfig"), 0640)
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)
}
}
Loading