Skip to content

Commit

Permalink
Merge pull request #4 from vydrazde/kypo-23.12
Browse files Browse the repository at this point in the history
Support for Kypo 23.12
  • Loading branch information
vydrazde authored Dec 21, 2023
2 parents b519bf6 + 5c78c83 commit 5f90f01
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 73 deletions.
75 changes: 47 additions & 28 deletions pkg/kypo/sandbox_allocation_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ type SandboxRequest struct {
Stages []string `json:"stages" tfsdk:"stages"`
}

type Pagination[T any] struct {
Page int64 `json:"page" tfsdk:"page"`
PageSize int64 `json:"page_size" tfsdk:"page_size"`
PageCount int64 `json:"page_count" tfsdk:"page_count"`
Count int64 `json:"count" tfsdk:"line_count"`
TotalCount int64 `json:"total_count" tfsdk:"total_count"`
Results T `json:"results" tfsdk:"results"`
}

type SandboxRequestStageOutput struct {
Page int64 `json:"page" tfsdk:"page"`
PageSize int64 `json:"page_size" tfsdk:"page_size"`
Expand All @@ -38,15 +47,6 @@ type SandboxRequestStageOutput struct {
Result string `json:"result" tfsdk:"result"`
}

type sandboxRequestStageOutputRaw struct {
Page int64 `json:"page"`
PageSize int64 `json:"page_size"`
PageCount int64 `json:"page_count"`
Count int64 `json:"count"`
TotalCount int64 `json:"total_count"`
Results []outputLine `json:"results"`
}

type outputLine struct {
Content string `json:"content"`
}
Expand Down Expand Up @@ -80,18 +80,43 @@ func (c *Client) CreateSandboxAllocationUnits(ctx context.Context, poolId, count
return nil, err
}

body, _, err := c.doRequestWithRetry(req, http.StatusCreated, "sandbox allocation units", fmt.Sprintf("sandbox pool %d", poolId))
body, _, err := c.doRequestWithRetry(req, http.StatusOK, "sandbox allocation units", fmt.Sprintf("sandbox pool %d", poolId))
if err != nil {
return nil, err
}

var allocationUnit []SandboxAllocationUnit
var allocationUnit Pagination[[]SandboxAllocationUnit]
err = json.Unmarshal(body, &allocationUnit)
if err != nil {
return nil, err
}

return allocationUnit, nil
return allocationUnit.Results, nil
}

func (c *Client) AwaitAllocationRequestCreate(ctx context.Context, requestId int64, pollTime time.Duration) error {
ticker := time.NewTicker(pollTime)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/kypo-sandbox-service/api/v1/sandbox-allocation-units/%d/allocation-request", c.Endpoint, requestId), nil)
if err != nil {
return err
}

_, status, err := c.doRequest(req)
if err != nil {
return err
}

if status != http.StatusNotFound {
return nil
}
}
}
}

// CreateSandboxAllocationUnitAwait creates a single sandbox allocation unit and waits until its allocation finishes.
Expand All @@ -105,6 +130,10 @@ func (c *Client) CreateSandboxAllocationUnitAwait(ctx context.Context, poolId in
return nil, fmt.Errorf("expected one allocation unit to be created, got %d instead", len(units))
}
unit := units[0]
err = c.AwaitAllocationRequestCreate(ctx, unit.Id, pollTime)
if err != nil {
return nil, err
}
request, err := c.PollRequestFinished(ctx, unit.Id, pollTime, "allocation")
if err != nil {
return nil, err
Expand All @@ -114,24 +143,14 @@ func (c *Client) CreateSandboxAllocationUnitAwait(ctx context.Context, poolId in
}

// CreateSandboxCleanupRequest starts a cleanup request for the specified sandbox allocation unit.
func (c *Client) CreateSandboxCleanupRequest(ctx context.Context, unitId int64) (*SandboxRequest, error) {
func (c *Client) CreateSandboxCleanupRequest(ctx context.Context, unitId int64) error {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/kypo-sandbox-service/api/v1/sandbox-allocation-units/%d/cleanup-request", c.Endpoint, unitId), nil)
if err != nil {
return nil, err
}

body, _, err := c.doRequestWithRetry(req, http.StatusCreated, "sandbox cleanup request", fmt.Sprintf("sandbox allocation unit %d", unitId))
if err != nil {
return nil, err
}

sandboxRequest := SandboxRequest{}
err = json.Unmarshal(body, &sandboxRequest)
if err != nil {
return nil, err
return err
}

return &sandboxRequest, nil
_, _, err = c.doRequestWithRetry(req, http.StatusCreated, "sandbox cleanup request", fmt.Sprintf("sandbox allocation unit %d", unitId))
return err
}

// PollRequestFinished periodically checks whether the specified request on given allocation unit has finished.
Expand Down Expand Up @@ -177,7 +196,7 @@ func (c *Client) PollRequestFinished(ctx context.Context, unitId int64, pollTime
// CreateSandboxCleanupRequestAwait starts the cleanup request for the given sandbox allocation unit and waits until it finishes.
// Once the cleanup is started, the status is checked once every `pollTime` elapses.
func (c *Client) CreateSandboxCleanupRequestAwait(ctx context.Context, unitId int64, pollTime time.Duration) error {
_, err := c.CreateSandboxCleanupRequest(ctx, unitId)
err := c.CreateSandboxCleanupRequest(ctx, unitId)
if err != nil {
return err
}
Expand Down Expand Up @@ -227,7 +246,7 @@ func (c *Client) GetSandboxRequestAnsibleOutputs(ctx context.Context, sandboxReq
return nil, err
}

outputRaw := sandboxRequestStageOutputRaw{}
outputRaw := Pagination[[]outputLine]{}

err = json.Unmarshal(body, &outputRaw)
if err != nil {
Expand Down
74 changes: 29 additions & 45 deletions pkg/kypo/sandbox_allocation_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ type SandboxAllocationUnit struct {
Locked bool `json:"locked"`
}

type Pagination struct {
Page int `json:"page"`
PageSize int `json:"page_size"`
PageCount int `json:"page_count"`
Count int `json:"count"`
TotalCount int `json:"total_count"`
Results any `json:"results"`
}

var (
sandboxAllocationUnitResponse = SandboxAllocationUnit{
Id: 1,
Expand All @@ -48,6 +57,16 @@ var (
},
Locked: false,
}
sandboxAllocationUnitResponsePagination = Pagination{
Page: 1,
PageSize: 50,
PageCount: 1,
Count: 1,
TotalCount: 1,
Results: []SandboxAllocationUnit{
sandboxAllocationUnitResponse,
},
}
expectedSandboxAllocationUnit = kypo.SandboxAllocationUnit{
Id: 1,
PoolId: 1,
Expand Down Expand Up @@ -160,8 +179,8 @@ func TestCreateSandboxAllocationUnitSuccessful(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
assertSandboxAllocationUnitCreate(t, request)

writer.WriteHeader(http.StatusCreated)
response, _ := json.Marshal([]SandboxAllocationUnit{sandboxAllocationUnitResponse})
writer.WriteHeader(http.StatusOK)
response, _ := json.Marshal(sandboxAllocationUnitResponsePagination)
_, _ = fmt.Fprint(writer, string(response))
}))
defer ts.Close()
Expand Down Expand Up @@ -241,8 +260,8 @@ func TestCreateSandboxAllocationUnitAwaitSuccessful(t *testing.T) {
if counter == 1 {
assertSandboxAllocationUnitCreate(t, request)

writer.WriteHeader(http.StatusCreated)
response, _ := json.Marshal([]SandboxAllocationUnit{sandboxAllocationUnitResponse})
writer.WriteHeader(http.StatusOK)
response, _ := json.Marshal(sandboxAllocationUnitResponsePagination)
_, _ = fmt.Fprint(writer, string(response))
return
}
Expand All @@ -268,7 +287,7 @@ func TestCreateSandboxAllocationUnitAwaitSuccessful(t *testing.T) {

assert.NoError(t, err)
assert.Equal(t, &expected, actual)
assert.Equal(t, 2, counter)
assert.Equal(t, 3, counter)
}

func TestCreateSandboxAllocationUnitAwaitSuccessfulWithDelay(t *testing.T) {
Expand All @@ -278,8 +297,8 @@ func TestCreateSandboxAllocationUnitAwaitSuccessfulWithDelay(t *testing.T) {
if counter == 1 {
assertSandboxAllocationUnitCreate(t, request)

writer.WriteHeader(http.StatusCreated)
response, _ := json.Marshal([]SandboxAllocationUnit{sandboxAllocationUnitResponse})
writer.WriteHeader(http.StatusOK)
response, _ := json.Marshal(sandboxAllocationUnitResponsePagination)
_, _ = fmt.Fprint(writer, string(response))
return
}
Expand Down Expand Up @@ -330,29 +349,14 @@ func TestCleanupSandboxAllocationUnitSuccessful(t *testing.T) {
assertSandboxAllocationUnitCleanup(t, request)

writer.WriteHeader(http.StatusCreated)
r := SandboxAllocationRequest{
Id: 1,
AllocationUnitId: 1,
Created: "2023-11-26T17:04:20.032500+01:00",
Stages: []string{"IN_QUEUE", "IN_QUEUE", "IN_QUEUE"},
}
response, _ := json.Marshal(r)
_, _ = fmt.Fprint(writer, string(response))
}))
defer ts.Close()

c := minimalClient(ts)

actual, err := c.CreateSandboxCleanupRequest(context.Background(), 1)
expected := kypo.SandboxRequest{
Id: 1,
AllocationUnitId: 1,
Created: "2023-11-26T17:04:20.032500+01:00",
Stages: []string{"IN_QUEUE", "IN_QUEUE", "IN_QUEUE"},
}
err := c.CreateSandboxCleanupRequest(context.Background(), 1)

assert.NoError(t, err)
assert.Equal(t, &expected, actual)
}

func TestCleanupSandboxAllocationUnitNotFound(t *testing.T) {
Expand All @@ -377,9 +381,8 @@ func TestCleanupSandboxAllocationUnitNotFound(t *testing.T) {
Err: kypo.ErrNotFound,
}

actual, err := c.CreateSandboxCleanupRequest(context.Background(), 1)
err := c.CreateSandboxCleanupRequest(context.Background(), 1)

assert.Nil(t, actual)
assert.Equal(t, expected, err)
}

Expand All @@ -397,9 +400,8 @@ func TestCleanupSandboxAllocationUnitServerError(t *testing.T) {
Identifier: "sandbox allocation unit 1",
Err: fmt.Errorf("status: 500, body: "),
}
actual, err := c.CreateSandboxCleanupRequest(context.Background(), 1)
err := c.CreateSandboxCleanupRequest(context.Background(), 1)

assert.Nil(t, actual)
assert.Equal(t, expected, err)
}

Expand All @@ -411,14 +413,6 @@ func TestCleanupSandboxAllocationUnitAwaitSuccessful(t *testing.T) {
assertSandboxAllocationUnitCleanup(t, request)

writer.WriteHeader(http.StatusCreated)
r := SandboxAllocationRequest{
Id: 1,
AllocationUnitId: 1,
Created: "2023-11-26T17:04:20.032500+01:00",
Stages: []string{"IN_QUEUE", "IN_QUEUE", "IN_QUEUE"},
}
response, _ := json.Marshal(r)
_, _ = fmt.Fprint(writer, string(response))
return
}

Expand Down Expand Up @@ -456,8 +450,6 @@ func TestCleanupSandboxAllocationUnitAwaitSuccessfulWithDelay(t *testing.T) {
assertSandboxAllocationUnitCleanup(t, request)

writer.WriteHeader(http.StatusCreated)
response, _ := json.Marshal(r)
_, _ = fmt.Fprint(writer, string(response))
return
}

Expand Down Expand Up @@ -493,14 +485,6 @@ func TestCleanupSandboxAllocationUnitAwaitFailed(t *testing.T) {
assertSandboxAllocationUnitCleanup(t, request)

writer.WriteHeader(http.StatusCreated)
r := SandboxAllocationRequest{
Id: 1,
AllocationUnitId: 1,
Created: "2023-11-26T17:04:20.032500+01:00",
Stages: []string{"IN_QUEUE", "IN_QUEUE", "IN_QUEUE"},
}
response, _ := json.Marshal(r)
_, _ = fmt.Fprint(writer, string(response))
return
}

Expand Down

0 comments on commit 5f90f01

Please sign in to comment.