Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ bundles/

# python-related directories
__pycache__/

# IDE/editor specific files
.idea/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an unrelated change. It would be better to leave it out of this PR.

24 changes: 0 additions & 24 deletions pkg/unikontainers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,6 @@ const (
rootfsDirName = "rootfs"
)

// getInitPid extracts "init_process_pid" value from the given JSON file
func getInitPid(filePath string) (float64, error) {
// Open the JSON file for reading
file, err := os.Open(filePath)
if err != nil {
return 0, nil
}
defer file.Close()

// Decode the JSON data into a map[string]interface{}
var jsonData map[string]interface{}
decoder := json.NewDecoder(file)
if err := decoder.Decode(&jsonData); err != nil {
return 0, nil
}

// Extract the specific value "init_process_pid"
initProcessPID, found := jsonData["init_process_pid"].(float64) // Assuming it's a numeric value
if !found {
return 0, nil
}
return initProcessPID, nil
}

// copy sourceFile to targetDir
// creates targetDir and all necessary parent directories
func copyFile(sourceFile string, targetPath string) error {
Expand Down
72 changes: 0 additions & 72 deletions pkg/unikontainers/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,78 +47,6 @@ func TestWritePidFile(t *testing.T) {
os.Remove(pidFilePath)
}

func TestGetInitPid(t *testing.T) {
t.Run("init PID found", func(t *testing.T) {
t.Parallel()

// Create a temporary file for testing
tmpDir := t.TempDir()
tmpFile, err := os.CreateTemp(tmpDir, "test*.json")
assert.NoError(t, err)

// Write test data to the file
testData := map[string]interface{}{
"init_process_pid": 12345.0,
}
jsonData, err := json.Marshal(testData)
assert.NoError(t, err)

_, err = tmpFile.Write(jsonData)
assert.NoError(t, err)
tmpFile.Close()

// Call the function and check the result
pid, err := getInitPid(tmpFile.Name())
assert.NoError(t, err, "Expected no error in getting init PID")
assert.Equal(t, 12345.0, pid, "Expected PID to be 12345")
})
t.Run("init PID file not found", func(t *testing.T) {
t.Parallel()
// Call the function with a non-existent file
pid, err := getInitPid("nonexistent.json")
assert.Equal(t, float64(0), pid, "Expected PID to be 0 for nonexistent file")
assert.NoError(t, err, "Expected no error for nonexistent file")
})

t.Run("init PID invalid JSON", func(t *testing.T) {
t.Parallel()
// Create a temporary file with invalid JSON
tmpDir := t.TempDir()
tmpFile, err := os.CreateTemp(tmpDir, "test*.json")
assert.NoError(t, err)
_, err = tmpFile.WriteString("{invalid json}")
assert.NoError(t, err)
tmpFile.Close()

// Call the function and check the result
pid, err := getInitPid(tmpFile.Name())
assert.Equal(t, float64(0), pid, "Expected PID to be 0 for invalid JSON")
assert.NoError(t, err, "Expected no error for invalid JSON")
})
t.Run("init PID missing key", func(t *testing.T) {
t.Parallel()
// Create a temporary file without "init_process_pid"
tmpDir := t.TempDir()
tmpFile, err := os.CreateTemp(tmpDir, "test*.json")
assert.NoError(t, err)

testData := map[string]interface{}{
"some_other_key": 12345.0,
}
jsonData, err := json.Marshal(testData)
assert.NoError(t, err)

_, err = tmpFile.Write(jsonData)
assert.NoError(t, err)
tmpFile.Close()

// Call the function and check the result
pid, err := getInitPid(tmpFile.Name())
assert.Equal(t, float64(0), pid, "Expected PID to be 0 for missing key")
assert.NoError(t, err, "Expected no error for missing key")
})
}

func TestCopyFile(t *testing.T) {
t.Run("copy file success", func(t *testing.T) {
t.Parallel()
Expand Down