diff --git a/pkg/unikontainers/utils.go b/pkg/unikontainers/utils.go index 4ec29cd7..77aad478 100644 --- a/pkg/unikontainers/utils.go +++ b/pkg/unikontainers/utils.go @@ -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 { diff --git a/pkg/unikontainers/utils_test.go b/pkg/unikontainers/utils_test.go index 7f88ca3b..a78522fa 100644 --- a/pkg/unikontainers/utils_test.go +++ b/pkg/unikontainers/utils_test.go @@ -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()