diff --git a/pkg/utils/exec_test.go b/pkg/utils/exec_test.go index a573597d749002b84d0b8e6e1d395b7e488aaff0..a30dfd04942f044bc318678d1cb307cab4472412 100644 --- a/pkg/utils/exec_test.go +++ b/pkg/utils/exec_test.go @@ -13,10 +13,9 @@ 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 utils_test +package utils import ( - "nestos-kubernetes-deployer/pkg/utils" "testing" ) @@ -42,7 +41,7 @@ func TestRunCommand(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gotOutput, err := utils.RunCommand(tt.command) + gotOutput, err := RunCommand(tt.command) if (err != nil) != tt.wantErr { t.Errorf("RunCommand() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/pkg/utils/filehandler_test.go b/pkg/utils/filehandler_test.go new file mode 100644 index 0000000000000000000000000000000000000000..2ae7903681c996cfd7598ab7bfb177fce41f9279 --- /dev/null +++ b/pkg/utils/filehandler_test.go @@ -0,0 +1,74 @@ +/* +Copyright 2024 KylinSoft Co., Ltd. + +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 utils + +import ( + "nestos-kubernetes-deployer/data" + "nestos-kubernetes-deployer/pkg/constants" + "os" + "path/filepath" + "testing" + + "github.com/sirupsen/logrus" +) + +func TestOsmanager(t *testing.T) { + // Change to the data directory where the test files are located + err := os.Chdir("../../data") + if err != nil { + t.Fatal(err) + } + + url := filepath.Join("bootconfig/systemd", constants.InitClusterService) + tmplData := map[string]interface{}{ + "Key": "Value", // Adjust according to the template requirements + } + t.Run("FetchAndUnmarshalUrl Success", func(t *testing.T) { + _, err := FetchAndUnmarshalUrl(url, tmplData) + if err != nil { + t.Error("test fail", err) + return + } + + }) + + t.Run("FetchAndUnmarshalUrl Fail", func(t *testing.T) { + _, err := FetchAndUnmarshalUrl("", "") + if err == nil { + t.Error("expected failure, got success") + } + }) + + t.Run("GetCompleteFile Success", func(t *testing.T) { + file, err := data.Assets.Open(url) + if err != nil { + logrus.Errorf("Error opening file %s: %v\n", url, err) + return + } + defer file.Close() + GetCompleteFile(url, file, tmplData) + }) + + t.Run("GetCompleteFile Fail", func(t *testing.T) { + file, err := data.Assets.Open(url) + if err != nil { + logrus.Errorf("Error opening file %s: %v\n", url, err) + return + } + defer file.Close() + GetCompleteFile("", file, tmplData) + }) +} diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go new file mode 100644 index 0000000000000000000000000000000000000000..ae51919a39dcd3a24359e170e446a59cf89c0078 --- /dev/null +++ b/pkg/utils/utils_test.go @@ -0,0 +1,103 @@ +/* +Copyright 2024 KylinSoft Co., Ltd. + +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 utils + +import ( + "fmt" + "net" + "path/filepath" + "testing" +) + +func TestGetKubernetesApiVersion(t *testing.T) { + tests := []struct { + versionNumber uint + expected string + expectError bool + }{ + {1, "v1beta1", false}, + {2, "v1beta2", false}, + {3, "v1beta3", false}, + {0, "", false}, + {4, "", true}, + } + + for _, tt := range tests { + t.Run(fmt.Sprintf("versionNumber=%d", tt.versionNumber), func(t *testing.T) { + version, err := GetKubernetesApiVersion(tt.versionNumber) + if tt.expectError && err == nil { + t.Errorf("expected error, got nil") + } + if !tt.expectError && err != nil { + t.Errorf("unexpected error: %v", err) + } + if version != tt.expected { + t.Errorf("expected %s, got %s", tt.expected, version) + } + }) + } +} + +func TestGetDefaultPubKeyPath(t *testing.T) { + expected := filepath.Join(getSysHome(), ".ssh", "id_rsa.pub") + if result := GetDefaultPubKeyPath(); result != expected { + t.Errorf("expected %s, got %s", expected, result) + } +} + +func TestGetApiServerEndpoint(t *testing.T) { + ip := "127.0.0.1" + expected := fmt.Sprintf("%s:%s", ip, "6443") + if result := GetApiServerEndpoint(ip); result != expected { + t.Errorf("expected %s, got %s", expected, result) + } +} + +func TestGetLocalIP(t *testing.T) { + ip, err := GetLocalIP() + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if net.ParseIP(ip) == nil { + t.Errorf("expected a valid IP address, got %s", ip) + } +} + +func TestIsPortOpen(t *testing.T) { + port := "8080" + if !IsPortOpen(port) { + t.Errorf("expected port %s to be open", port) + } +} + +func TestConstructURL(t *testing.T) { + host := "localhost" + role := "worker" + expected := "http://localhost/worker" + if result := ConstructURL(host, role); result != expected { + t.Errorf("expected %s, got %s", expected, result) + } +} + +func TestGenerateWWN(t *testing.T) { + wwn, err := GenerateWWN() + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if len(wwn) != 16 { + t.Errorf("expected WWN length 16, got %d", len(wwn)) + } +}