github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/configuration/container/container_util_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package container 21 22 import ( 23 "io/fs" 24 "net" 25 "os" 26 "path/filepath" 27 "strings" 28 "testing" 29 30 "github.com/docker/docker/api/types" 31 "github.com/stretchr/testify/require" 32 33 testapps "github.com/1aal/kubeblocks/pkg/testutil/apps" 34 ) 35 36 func testContainer(service string, id string, state string) types.Container { 37 name := "/" + strings.TrimPrefix(id, "/") 38 workingDir, _ := filepath.Abs(".") 39 return types.Container{ 40 ID: id, 41 Names: []string{name}, 42 Labels: testapps.WithMap("service", service, 43 "working_dir", workingDir, 44 "project", "test_project"), 45 State: state, 46 } 47 } 48 49 func TestIsSocketFile(t *testing.T) { 50 tmpDir, err := os.MkdirTemp(os.TempDir(), "SocketFileTest-") 51 require.Nil(t, err) 52 defer os.RemoveAll(tmpDir) 53 54 var ( 55 testFile1 = filepath.Join(tmpDir, "file1") 56 testFile2 = filepath.Join(tmpDir, "file2") 57 testFile3 = filepath.Join(tmpDir, "file3.sock") 58 ) 59 60 if err := os.WriteFile(testFile2, []byte(``), fs.ModePerm); err != nil { 61 t.Errorf("failed to write file: %s", testFile2) 62 } 63 64 l, err := net.Listen("unix", testFile3) 65 if err != nil { 66 t.Errorf("failed to create socket file: %s", testFile3) 67 } 68 defer l.Close() 69 70 tests := []struct { 71 name string 72 args string 73 want bool 74 }{{ 75 name: "socketTest", 76 args: testFile1, 77 want: false, 78 }, { 79 name: "socketTest", 80 args: testFile2, 81 want: false, 82 }, { 83 name: "socketTest", 84 args: testFile3, 85 want: true, 86 }, { 87 name: "socketTest", 88 args: formatSocketPath(testFile3), 89 want: true, 90 }, { 91 name: "socketTest", 92 // for test formatSocketPath 93 args: formatSocketPath(formatSocketPath(testFile3)), 94 want: true, 95 }} 96 for _, tt := range tests { 97 t.Run(tt.name, func(t *testing.T) { 98 if got := isSocketFile(tt.args); got != tt.want { 99 t.Errorf("isSocketFile() = %v, want %v", got, tt.want) 100 } 101 }) 102 } 103 }