-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MGMT-16332: fix requirements base version
- Loading branch information
Showing
4 changed files
with
353 additions
and
28 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,62 @@ | ||
package lvm | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
type baseHardware struct { | ||
CPUCores int64 `json:"cpu_cores"` | ||
RAMMib int64 `json:"ram_mib"` | ||
} | ||
|
||
type hardwareRequirement struct { | ||
Version string `json:"version,omitempty"` | ||
Master baseHardware `json:"master"` | ||
Worker baseHardware `json:"worker"` | ||
Sno baseHardware `json:"sno"` | ||
} | ||
|
||
const ( | ||
envRequirementsKey string = "HW_VALIDATOR_REQUIREMENTS" | ||
urlRequirement string = "https://raw.githubusercontent.com/openshift/assisted-service/master/data/default_hw_requirements.json" | ||
) | ||
|
||
//TODO: move to common and use it for all operators | ||
|
||
type pageDownloader func(s string) (*http.Response, error) | ||
|
||
func downloadFromUrl(pageDownloadFunc pageDownloader) (*http.Response, error) { | ||
return pageDownloadFunc(urlRequirement) | ||
} | ||
|
||
func get_page(url string) (*http.Response, error) { | ||
return http.Get(url) | ||
} | ||
|
||
func getBaseRequiemnts(pageDownloadFunc pageDownloader) (hardwareRequirement, error) { | ||
var baseHardwareRequirement = make([]hardwareRequirement, 1) | ||
|
||
baseRequirement := os.Getenv(envRequirementsKey) | ||
if baseRequirement != "" { | ||
err := json.Unmarshal([]byte(baseRequirement), &baseHardwareRequirement) | ||
if err == nil { | ||
return baseHardwareRequirement[0], nil | ||
} | ||
} | ||
resp, err := downloadFromUrl(pageDownloadFunc) | ||
if err != nil { | ||
return baseHardwareRequirement[0], fmt.Errorf("Couldn't download requirement file") | ||
} | ||
data, _ := io.ReadAll(resp.Body) | ||
err = json.Unmarshal(data, &baseHardwareRequirement) | ||
if err != nil { | ||
return baseHardwareRequirement[0], fmt.Errorf("Couldn't marshal JSON from URL") | ||
|
||
} | ||
|
||
return baseHardwareRequirement[0], 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,95 @@ | ||
package lvm | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var hwRequirement = ` | ||
[ | ||
{ | ||
"version": "default", | ||
"master": { | ||
"cpu_cores": 1, | ||
"ram_mib": 2, | ||
"disk_size_gb": 3, | ||
"installation_disk_speed_threshold_ms": 10, | ||
"network_latency_threshold_ms": 100, | ||
"packet_loss_percentage": 0 | ||
}, | ||
"worker": { | ||
"cpu_cores": 2, | ||
"ram_mib": 2, | ||
"disk_size_gb": 3, | ||
"installation_disk_speed_threshold_ms": 10, | ||
"network_latency_threshold_ms": 1000, | ||
"packet_loss_percentage": 10 | ||
}, | ||
"sno": { | ||
"cpu_cores": 3, | ||
"ram_mib": 2, | ||
"disk_size_gb": 3, | ||
"installation_disk_speed_threshold_ms": 10, | ||
"network_latency_threshold_ms": 1000, | ||
"packet_loss_percentage": 10 | ||
} | ||
} | ||
]` | ||
|
||
func mock_get_page(url string) (*http.Response, error) { | ||
content := strings.NewReader(hwRequirement) | ||
return &http.Response{ | ||
Body: io.NopCloser(content), | ||
}, nil | ||
} | ||
|
||
func mock_failed_get_page(url string) (*http.Response, error) { | ||
return &http.Response{}, fmt.Errorf("mock failure") | ||
} | ||
|
||
var _ = Describe("Lvm common", func() { | ||
|
||
var testData []hardwareRequirement | ||
_ = json.Unmarshal([]byte(hwRequirement), &testData) | ||
|
||
Context("GetBaseRequirements", func() { | ||
|
||
It("Base Requirements var", func() { | ||
os.Setenv(envRequirementsKey, hwRequirement) | ||
hwHardware, err := getBaseRequiemnts(get_page) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
Expect(testData[0].Master.CPUCores).Should(Equal(hwHardware.Master.CPUCores)) | ||
Expect(testData[0].Worker.CPUCores).Should(Equal(hwHardware.Worker.CPUCores)) | ||
Expect(testData[0].Sno.CPUCores).Should(Equal(hwHardware.Sno.CPUCores)) | ||
os.Unsetenv(envRequirementsKey) | ||
|
||
}) | ||
It("Base Requirements URL", func() { | ||
hwHardware, err := getBaseRequiemnts(mock_get_page) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
Expect(testData[0].Master.CPUCores).Should(Equal(hwHardware.Master.CPUCores)) | ||
Expect(testData[0].Worker.CPUCores).Should(Equal(hwHardware.Worker.CPUCores)) | ||
Expect(testData[0].Sno.CPUCores).Should(Equal(hwHardware.Sno.CPUCores)) | ||
}) | ||
|
||
It("Base Requirements URL failed", func() { | ||
_, err := getBaseRequiemnts(mock_failed_get_page) | ||
Expect(err).Should(HaveOccurred()) | ||
|
||
// failed download | ||
failureMessage := fmt.Errorf("Couldn't download requirement file") | ||
Expect(err).Should(Equal(failureMessage)) | ||
|
||
}) | ||
}) | ||
}, | ||
) |
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
Oops, something went wrong.