From 7a6293f8cdee584b8929efaff5e314d68475c723 Mon Sep 17 00:00:00 2001 From: jianli-97 Date: Wed, 3 Jul 2024 17:50:42 +0800 Subject: [PATCH] Add http and tftp test cases --- pkg/httpserver/httpserver_test.go | 126 +++++++++++++++++++++--------- pkg/tftpserver/tftpserver_test.go | 71 +++++++++++++++++ 2 files changed, 160 insertions(+), 37 deletions(-) create mode 100644 pkg/tftpserver/tftpserver_test.go diff --git a/pkg/httpserver/httpserver_test.go b/pkg/httpserver/httpserver_test.go index c63534a..8f843e3 100644 --- a/pkg/httpserver/httpserver_test.go +++ b/pkg/httpserver/httpserver_test.go @@ -1,5 +1,5 @@ /* -Copyright 2023 KylinSoft Co., Ltd. +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. @@ -14,46 +14,98 @@ See the License for the specific language governing permissions and limitations under the License. */ -package httpserver_test +package httpserver import ( - "io" - "nestos-kubernetes-deployer/pkg/httpserver" + "nestos-kubernetes-deployer/pkg/constants" "net/http" + "os" "testing" + "time" ) -func TestHTTPService(t *testing.T) { - // Create a new http service instance - httpService := &httpserver.HTTPService{ - Port: "9080", - } - - // Start the file service - if err := httpService.Start(); err != nil { - t.Fatalf("Error starting file service: %v", err) - } - defer httpService.Stop() - - // Add test file to the file service - testContent := []byte("Hello, world!") - httpService.AddFileToCache("test.txt", testContent) - - // Make an HTTP request to retrieve the test file content - resp, err := http.Get("http://localhost:9080/file/test.txt") - if err != nil { - t.Fatalf("Error making GET request: %v", err) - } - defer resp.Body.Close() - - // Read the response body content - respBody, err := io.ReadAll(resp.Body) - if err != nil { - t.Fatalf("Error reading response body: %v", err) - } - - // Check if the response body content matches the expected content - if string(respBody) != string(testContent) { - t.Errorf("Expected response body %s, got %s", string(testContent), string(respBody)) - } +func TestHTTPServer(t *testing.T) { + hs := NewHTTPService("9080") + + t.Run("TestAddFileToCache", func(t *testing.T) { + var content = []byte("test") + if err := hs.AddFileToCache("test", content); err != nil { + t.Error("test fail", err) + return + } + if cachedContent, ok := hs.FileCache["/test"]; !ok || string(cachedContent) != "test" { + t.Error("test fail: cached content mismatch") + return + } + }) + + t.Run("TestStartHTTPService", func(t *testing.T) { + go func() { + StartHTTPService(hs) + }() + time.Sleep(1 * time.Second) + if err := hs.Stop(); err != nil { + t.Error("test fail", err) + return + } + }) + + t.Run("TestStartStop", func(t *testing.T) { + hs.DirPath = "tmp" + go func() { + if err := hs.Stop(); err != nil { + t.Error("test fail", err) + return + } + if err := hs.Start(); err != nil { + t.Error("test fail", err) + return + } + }() + time.Sleep(1 * time.Second) + if err := hs.Stop(); err != nil { + t.Error("test fail", err) + return + } + }) + + t.Run("TestServer", func(t *testing.T) { + content := []byte("test content") + hs.AddFileToCache("/testfile", content) + + go func() { + if err := hs.Stop(); err != nil { + t.Error("test fail", err) + return + } + if err := hs.Start(); err != nil { + t.Error("test fail", err) + return + } + }() + time.Sleep(1 * time.Second) + + _, err := http.Get("http://localhost:9080/testfile") + if err != nil { + t.Error("test fail", err) + return + } + + _, err = http.Get("http://localhost:9080/dir" + os.TempDir()) + if err != nil { + t.Error("test fail", err) + return + } + + _, err = http.Get("http://localhost:9080" + constants.RpmPackageList) + if err != nil { + t.Error("test fail", err) + return + } + + if err := hs.Stop(); err != nil { + t.Error("test fail", err) + return + } + }) } diff --git a/pkg/tftpserver/tftpserver_test.go b/pkg/tftpserver/tftpserver_test.go new file mode 100644 index 0000000..1acd0d8 --- /dev/null +++ b/pkg/tftpserver/tftpserver_test.go @@ -0,0 +1,71 @@ +/* +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 tftpserver + +import ( + "io" + "testing" +) + +type RFWT struct { + RootDir string +} + +func (rf *RFWT) ReadFrom(r io.Reader) (n int64, err error) { + return 0, nil +} + +func (wt *RFWT) WriteTo(w io.Writer) (n int64, err error) { + return 0, nil +} + +func TestTFTPServer(t *testing.T) { + service := NewTFTPService("127.0.0.1", "69", "testDir") + + t.Run("TestStartStop", func(t *testing.T) { + go func() { + if err := service.Start(); err != nil { + t.Error("test fail", err) + return + } + }() + if err := service.Stop(); err != nil { + t.Error("test fail", err) + return + } + }) + + th := &TFTPHandler{ + RootDir: "testDir", + } + t.Run("TestReadHandler", func(t *testing.T) { + rf := &RFWT{} + + if err := th.ReadHandler("test", rf); err != nil { + t.Error("test fail", err) + return + } + }) + t.Run("TestWriteHandler", func(t *testing.T) { + wt := &RFWT{} + + if err := th.WriteHandler("test", wt); err != nil { + t.Error("test fail", err) + return + } + }) +} -- Gitee