From 9929b22afcc05367ae4217ef1a8d95dc9f645a51 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Mon, 21 Oct 2024 14:39:28 +0200 Subject: [PATCH] Replace suite tests with regular go tests (#2762) * Replace file suite tests with go tests * Replace file suite tests with go tests --- util/file_suite_test.go | 126 -------------------------------------- util/file_test.go | 130 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 126 deletions(-) delete mode 100644 util/file_suite_test.go diff --git a/util/file_suite_test.go b/util/file_suite_test.go deleted file mode 100644 index 3de7db49b..000000000 --- a/util/file_suite_test.go +++ /dev/null @@ -1,126 +0,0 @@ -package util_test - -import ( - "crypto/md5" - "encoding/hex" - "io" - "os" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - "github.com/netbirdio/netbird/util" -) - -var _ = Describe("Client", func() { - - var ( - tmpDir string - ) - - type TestConfig struct { - SomeMap map[string]string - SomeArray []string - SomeField int - } - - BeforeEach(func() { - var err error - tmpDir, err = os.MkdirTemp("", "wiretrustee_util_test_tmp_*") - Expect(err).NotTo(HaveOccurred()) - }) - - AfterEach(func() { - err := os.RemoveAll(tmpDir) - Expect(err).NotTo(HaveOccurred()) - }) - - Describe("Config", func() { - Context("in JSON format", func() { - It("should be written and read successfully", func() { - - m := make(map[string]string) - m["key1"] = "value1" - m["key2"] = "value2" - - arr := []string{"value1", "value2"} - - written := &TestConfig{ - SomeMap: m, - SomeArray: arr, - SomeField: 99, - } - - err := util.WriteJson(tmpDir+"/testconfig.json", written) - Expect(err).NotTo(HaveOccurred()) - - read, err := util.ReadJson(tmpDir+"/testconfig.json", &TestConfig{}) - Expect(err).NotTo(HaveOccurred()) - Expect(read).NotTo(BeNil()) - Expect(read.(*TestConfig).SomeMap["key1"]).To(BeEquivalentTo(written.SomeMap["key1"])) - Expect(read.(*TestConfig).SomeMap["key2"]).To(BeEquivalentTo(written.SomeMap["key2"])) - Expect(read.(*TestConfig).SomeArray).To(ContainElements(arr)) - Expect(read.(*TestConfig).SomeField).To(BeEquivalentTo(written.SomeField)) - - }) - }) - }) - - Describe("Copying file contents", func() { - Context("from one file to another", func() { - It("should be successful", func() { - - src := tmpDir + "/copytest_src" - dst := tmpDir + "/copytest_dst" - - err := util.WriteJson(src, []string{"1", "2", "3"}) - Expect(err).NotTo(HaveOccurred()) - - err = util.CopyFileContents(src, dst) - Expect(err).NotTo(HaveOccurred()) - - hashSrc := md5.New() - hashDst := md5.New() - - srcFile, err := os.Open(src) - Expect(err).NotTo(HaveOccurred()) - - dstFile, err := os.Open(dst) - Expect(err).NotTo(HaveOccurred()) - - _, err = io.Copy(hashSrc, srcFile) - Expect(err).NotTo(HaveOccurred()) - - _, err = io.Copy(hashDst, dstFile) - Expect(err).NotTo(HaveOccurred()) - - err = srcFile.Close() - Expect(err).NotTo(HaveOccurred()) - - err = dstFile.Close() - Expect(err).NotTo(HaveOccurred()) - - Expect(hex.EncodeToString(hashSrc.Sum(nil)[:16])).To(BeEquivalentTo(hex.EncodeToString(hashDst.Sum(nil)[:16]))) - }) - }) - }) - - Describe("Handle config file without full path", func() { - Context("config file handling", func() { - It("should be successful", func() { - written := &TestConfig{ - SomeField: 123, - } - cfgFile := "test_cfg.json" - defer os.Remove(cfgFile) - - err := util.WriteJson(cfgFile, written) - Expect(err).NotTo(HaveOccurred()) - - read, err := util.ReadJson(cfgFile, &TestConfig{}) - Expect(err).NotTo(HaveOccurred()) - Expect(read).NotTo(BeNil()) - }) - }) - }) -}) diff --git a/util/file_test.go b/util/file_test.go index 1330e738e..566d8eda6 100644 --- a/util/file_test.go +++ b/util/file_test.go @@ -1,12 +1,142 @@ package util import ( + "crypto/md5" + "encoding/hex" + "io" "os" "reflect" "strings" "testing" + + "github.com/stretchr/testify/require" ) +type TestConfig struct { + SomeMap map[string]string + SomeArray []string + SomeField int +} + +func TestConfigJSON(t *testing.T) { + tests := []struct { + name string + config *TestConfig + expectedError bool + }{ + { + name: "Valid JSON config", + config: &TestConfig{ + SomeMap: map[string]string{"key1": "value1", "key2": "value2"}, + SomeArray: []string{"value1", "value2"}, + SomeField: 99, + }, + expectedError: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tmpDir := t.TempDir() + + err := WriteJson(tmpDir+"/testconfig.json", tt.config) + require.NoError(t, err) + + read, err := ReadJson(tmpDir+"/testconfig.json", &TestConfig{}) + require.NoError(t, err) + require.NotNil(t, read) + require.Equal(t, tt.config.SomeMap["key1"], read.(*TestConfig).SomeMap["key1"]) + require.Equal(t, tt.config.SomeMap["key2"], read.(*TestConfig).SomeMap["key2"]) + require.ElementsMatch(t, tt.config.SomeArray, read.(*TestConfig).SomeArray) + require.Equal(t, tt.config.SomeField, read.(*TestConfig).SomeField) + }) + } +} + +func TestCopyFileContents(t *testing.T) { + tests := []struct { + name string + srcContent []string + expectedError bool + }{ + { + name: "Copy file contents successfully", + srcContent: []string{"1", "2", "3"}, + expectedError: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tmpDir := t.TempDir() + + src := tmpDir + "/copytest_src" + dst := tmpDir + "/copytest_dst" + + err := WriteJson(src, tt.srcContent) + require.NoError(t, err) + + err = CopyFileContents(src, dst) + require.NoError(t, err) + + hashSrc := md5.New() + hashDst := md5.New() + + srcFile, err := os.Open(src) + require.NoError(t, err) + defer func() { + _ = srcFile.Close() + }() + + dstFile, err := os.Open(dst) + require.NoError(t, err) + defer func() { + _ = dstFile.Close() + }() + + _, err = io.Copy(hashSrc, srcFile) + require.NoError(t, err) + + _, err = io.Copy(hashDst, dstFile) + require.NoError(t, err) + + require.Equal(t, hex.EncodeToString(hashSrc.Sum(nil)[:16]), hex.EncodeToString(hashDst.Sum(nil)[:16])) + }) + } +} + +func TestHandleConfigFileWithoutFullPath(t *testing.T) { + tests := []struct { + name string + config *TestConfig + expectedError bool + }{ + { + name: "Handle config file without full path", + config: &TestConfig{ + SomeField: 123, + }, + expectedError: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfgFile := "test_cfg.json" + defer func() { + _ = os.Remove(cfgFile) + }() + + err := WriteJson(cfgFile, tt.config) + require.NoError(t, err) + + read, err := ReadJson(cfgFile, &TestConfig{}) + require.NoError(t, err) + require.NotNil(t, read) + }) + } +} + func TestReadJsonWithEnvSub(t *testing.T) { type Config struct { CertFile string `json:"CertFile"`