-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove CPU template for non-intel instances
Helps in testing of AMD instance, by removing CPUTemplate from tests and adding a test to check CPUTemplate support (Intel only). Signed-off-by: Vaishnavi Vejella <[email protected]>
- Loading branch information
Showing
5 changed files
with
145 additions
and
44 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file 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 internal | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"os" | ||
"regexp" | ||
"runtime" | ||
"sync" | ||
) | ||
|
||
var ( | ||
isIntel bool | ||
isIntelOnce sync.Once | ||
) | ||
|
||
// SupportCPUTemplate returns true if Firecracker supports CPU templates on | ||
// the current architecture. | ||
func SupportCPUTemplate() (bool, error) { | ||
if runtime.GOARCH != "amd64" { | ||
return false, nil | ||
} | ||
|
||
var err error | ||
isIntelOnce.Do(func() { | ||
isIntel, err = checkIsIntel() | ||
}) | ||
return isIntel, err | ||
} | ||
|
||
var vendorID = regexp.MustCompile(`^vendor_id\s*:\s*(.+)$`) | ||
|
||
func checkIsIntel() (bool, error) { | ||
f, err := os.Open("/proc/cpuinfo") | ||
if err != nil { | ||
return false, err | ||
} | ||
defer f.Close() | ||
|
||
id, err := findFirstVendorID(f) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return id == "GenuineIntel", nil | ||
} | ||
|
||
func findFirstVendorID(r io.Reader) (string, error) { | ||
s := bufio.NewScanner(r) | ||
for s.Scan() { | ||
line := s.Text() | ||
matches := vendorID.FindStringSubmatch(line) | ||
if len(matches) == 2 { | ||
return matches[1], nil | ||
} | ||
} | ||
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,41 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file 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 internal | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFindFirstVendorID(t *testing.T) { | ||
cases := []struct { | ||
input string | ||
vendorID string | ||
}{ | ||
{"vendor_id : GenuineIntel", "GenuineIntel"}, | ||
{"vendor_id : AuthenticAMD", "AuthenticAMD"}, | ||
|
||
// aarch64 doesn't have vendor IDs. | ||
{"", ""}, | ||
} | ||
for _, c := range cases { | ||
r := strings.NewReader(c.input) | ||
id, err := findFirstVendorID(r) | ||
require.NoError(t, err) | ||
assert.Equal(t, c.vendorID, id) | ||
} | ||
} |
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