-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check native environment before starting (#25186)
Running Beats/Agent build for 32bits sometimes leads to problems on 64bit platforms. Some system metrics do have different sizing on 32 and 64bit architectures, which can trip up Agent/Beats wrongly interpreting the given data. On Windows/Linux it is possible to run 32bit binaries. Now a check will be run to fail on startup if the architecture does not match the binaries architecture. (cherry picked from commit 7253026)
- Loading branch information
Steffen Siering
authored and
mergify-bot
committed
Apr 21, 2021
1 parent
f73073c
commit 97d3fa9
Showing
6 changed files
with
150 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,48 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
||
// +build linux windows | ||
|
||
package platformcheck | ||
|
||
import ( | ||
"fmt" | ||
"math/bits" | ||
"strings" | ||
|
||
"github.com/shirou/gopsutil/host" | ||
) | ||
|
||
func CheckNativePlatformCompat() error { | ||
const compiledArchBits = bits.UintSize // 32 if the binary was compiled for 32 bit architecture. | ||
|
||
if compiledArchBits > 32 { | ||
// We assume that 64bit binaries can only be run on 64bit systems | ||
return nil | ||
} | ||
|
||
arch, err := host.KernelArch() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if strings.Contains(arch, "64") { | ||
return fmt.Errorf("trying to run %vBit binary on 64Bit system", compiledArchBits) | ||
} | ||
|
||
return 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,24 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
||
// +build !linux,!windows | ||
|
||
package platformcheck | ||
|
||
func CheckNativePlatformCompat() error { | ||
return 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,65 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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 platformcheck | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCheckPlatformCompat(t *testing.T) { | ||
if !(runtime.GOARCH == "amd64" && (runtime.GOOS == "linux" || | ||
runtime.GOOS == "windows")) { | ||
t.Skip("Test not support on current platform") | ||
} | ||
|
||
// compile test helper | ||
tmp := t.TempDir() | ||
helper := filepath.Join(tmp, "helper") | ||
|
||
cmd := exec.Command("go", "test", "-c", "-o", helper) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
cmd.Env = append(os.Environ(), "GOARCH=386") | ||
require.NoError(t, cmd.Run(), "failed to compile test helper") | ||
|
||
// run test helper | ||
cmd = exec.Command(helper, "-test.v", "-test.run", "TestHelper") | ||
cmd.Env = []string{"GO_USE_HELPER=1"} | ||
output, err := cmd.Output() | ||
if err != nil { | ||
t.Logf("32bit binary tester failed.\n Output: %s", output) | ||
} | ||
} | ||
|
||
func TestHelper(t *testing.T) { | ||
if os.Getenv("GO_USE_HELPER") != "1" { | ||
t.Log("ignore helper") | ||
return | ||
} | ||
|
||
err := CheckNativePlatformCompat() | ||
if err.Error() != "trying to run 32Bit binary on 64Bit system" { | ||
t.Error("expected the native platform check to fail") | ||
} | ||
} |
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
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
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