Skip to content

Commit

Permalink
Ensure image and kernel in Partition are downloadable (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored May 9, 2022
1 parent 7f87922 commit c8666dd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
12 changes: 12 additions & 0 deletions cmd/metal-api/internal/service/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"

Expand Down Expand Up @@ -188,6 +189,13 @@ func createTestEnvironment(t *testing.T) testEnv {

partitionName := "test-partition"
partitionDesc := "Test Partition"

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "I am a downloadable content")
}))
defer ts.Close()

downloadableFile := ts.URL
partition := v1.PartitionCreateRequest{
Common: v1.Common{
Identifiable: v1.Identifiable{
Expand All @@ -198,6 +206,10 @@ func createTestEnvironment(t *testing.T) testEnv {
Description: &partitionDesc,
},
},
PartitionBootConfiguration: v1.PartitionBootConfiguration{
ImageURL: &downloadableFile,
KernelURL: &downloadableFile,
},
}
var createdPartition v1.PartitionResponse
status = te.partitionCreate(t, partition, &createdPartition)
Expand Down
17 changes: 17 additions & 0 deletions cmd/metal-api/internal/service/partition-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,18 @@ func (r partitionResource) createPartition(request *restful.Request, response *r
if requestPayload.PartitionBootConfiguration.ImageURL != nil {
imageURL = *requestPayload.PartitionBootConfiguration.ImageURL
}
err = checkImageURL("image", imageURL)
if checkError(request, response, utils.CurrentFuncName(), err) {
return
}
var kernelURL string
if requestPayload.PartitionBootConfiguration.KernelURL != nil {
kernelURL = *requestPayload.PartitionBootConfiguration.KernelURL
}
err = checkImageURL("kernel", kernelURL)
if checkError(request, response, utils.CurrentFuncName(), err) {
return
}
var commandLine string
if requestPayload.PartitionBootConfiguration.CommandLine != nil {
commandLine = *requestPayload.PartitionBootConfiguration.CommandLine
Expand Down Expand Up @@ -276,9 +284,18 @@ func (r partitionResource) updatePartition(request *restful.Request, response *r
newPartition.MgmtServiceAddress = *requestPayload.MgmtServiceAddress
}
if requestPayload.PartitionBootConfiguration.ImageURL != nil {
err = checkImageURL("kernel", *requestPayload.PartitionBootConfiguration.ImageURL)
if checkError(request, response, utils.CurrentFuncName(), err) {
return
}
newPartition.BootConfiguration.ImageURL = *requestPayload.PartitionBootConfiguration.ImageURL
}

if requestPayload.PartitionBootConfiguration.KernelURL != nil {
err = checkImageURL("kernel", *requestPayload.PartitionBootConfiguration.KernelURL)
if checkError(request, response, utils.CurrentFuncName(), err) {
return
}
newPartition.BootConfiguration.KernelURL = *requestPayload.PartitionBootConfiguration.KernelURL
}
if requestPayload.PartitionBootConfiguration.CommandLine != nil {
Expand Down
22 changes: 19 additions & 3 deletions cmd/metal-api/internal/service/partition-service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -142,6 +143,13 @@ func TestCreatePartition(t *testing.T) {
service := NewPartition(ds, topicCreater)
container := restful.NewContainer().Add(service)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "I am a downloadable content")
}))
defer ts.Close()

downloadableFile := ts.URL

createRequest := v1.PartitionCreateRequest{
Common: v1.Common{
Identifiable: v1.Identifiable{
Expand All @@ -152,6 +160,10 @@ func TestCreatePartition(t *testing.T) {
Description: &testdata.Partition1.Description,
},
},
PartitionBootConfiguration: v1.PartitionBootConfiguration{
ImageURL: &downloadableFile,
KernelURL: &downloadableFile,
},
}
js, err := json.Marshal(createRequest)
require.NoError(t, err)
Expand Down Expand Up @@ -180,9 +192,13 @@ func TestUpdatePartition(t *testing.T) {

service := NewPartition(ds, &nopTopicCreater{})
container := restful.NewContainer().Add(service)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "I am a downloadable content")
}))
defer ts.Close()

mgmtService := "mgmt"
imageURL := "http://somewhere/image1.zip"
downloadableFile := ts.URL
updateRequest := v1.PartitionUpdateRequest{
Common: v1.Common{
Describable: v1.Describable{
Expand All @@ -195,7 +211,7 @@ func TestUpdatePartition(t *testing.T) {
},
MgmtServiceAddress: &mgmtService,
PartitionBootConfiguration: &v1.PartitionBootConfiguration{
ImageURL: &imageURL,
ImageURL: &downloadableFile,
},
}
js, err := json.Marshal(updateRequest)
Expand All @@ -218,7 +234,7 @@ func TestUpdatePartition(t *testing.T) {
require.Equal(t, testdata.Partition2.Name, *result.Name)
require.Equal(t, testdata.Partition2.Description, *result.Description)
require.Equal(t, mgmtService, *result.MgmtServiceAddress)
require.Equal(t, imageURL, *result.PartitionBootConfiguration.ImageURL)
require.Equal(t, downloadableFile, *result.PartitionBootConfiguration.ImageURL)
}

func TestPartitionCapacity(t *testing.T) {
Expand Down

0 comments on commit c8666dd

Please sign in to comment.