-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: sbookworm <lisong.cruise@gmail.com>
- Loading branch information
Showing
2 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2020 The Prometheus 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 prometheus | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// NewPidFileFn create a function which could get pid from specified file | ||
func NewPidFileFn(pidFilePath string) func() (int, error) { | ||
return func() (int, error) { | ||
content, err := ioutil.ReadFile(pidFilePath) | ||
if err != nil { | ||
return 0, fmt.Errorf("can't read pid file %s with error: %+v", pidFilePath, err) | ||
} | ||
value, err := strconv.Atoi(strings.TrimSpace(string(content))) | ||
if err != nil { | ||
return 0, fmt.Errorf("can't parse pid file %s with error: %+v", pidFilePath, err) | ||
} | ||
|
||
return value, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2020 The Prometheus 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 prometheus | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func fileExist(path string) bool { | ||
_, err := os.Lstat(path) | ||
return !os.IsNotExist(err) | ||
} | ||
|
||
func cleanMockPidFile(pidFilePath string) { | ||
if fileExist(pidFilePath) { | ||
os.Remove(pidFilePath) | ||
} | ||
} | ||
|
||
// TestNewPidFileFn ... | ||
func TestNewPidFileFn(t *testing.T) { | ||
folderPath, err := os.Getwd() | ||
if err != nil { | ||
t.Error("failed to get current path") | ||
} | ||
mockPidFilePath := filepath.Join(folderPath, "mockPidFile") | ||
defer cleanMockPidFile(mockPidFilePath) | ||
|
||
testCases := []struct { | ||
mockPidFile func() | ||
expectedErrPrefix string | ||
expectedPid int | ||
desc string | ||
}{ | ||
{ | ||
mockPidFile: func() { | ||
cleanMockPidFile(mockPidFilePath) | ||
}, | ||
expectedErrPrefix: fmt.Sprintf("can't read pid file %s", mockPidFilePath), | ||
expectedPid: 0, | ||
desc: "no existed pid file", | ||
}, | ||
{ | ||
mockPidFile: func() { | ||
cleanMockPidFile(mockPidFilePath) | ||
f, _ := os.Create(mockPidFilePath) | ||
f.Write([]byte("abc")) | ||
f.Close() | ||
}, | ||
expectedErrPrefix: fmt.Sprintf("can't parse pid file %s", mockPidFilePath), | ||
expectedPid: 0, | ||
desc: "existed pid file, error pid number", | ||
}, | ||
{ | ||
mockPidFile: func() { | ||
cleanMockPidFile(mockPidFilePath) | ||
f, _ := os.Create(mockPidFilePath) | ||
f.Write([]byte("123")) | ||
f.Close() | ||
}, | ||
expectedErrPrefix: "", | ||
expectedPid: 123, | ||
desc: "existed pid file, correct pid number", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
fn := NewPidFileFn(mockPidFilePath) | ||
if fn == nil { | ||
t.Error("Should not get nil PidFileFn") | ||
} | ||
tc.mockPidFile() | ||
|
||
if pid, err := fn(); pid != tc.expectedPid || (err != nil && !strings.HasPrefix(err.Error(), tc.expectedErrPrefix)) { | ||
t.Error(tc.desc) | ||
} | ||
} | ||
} |