Skip to content

Commit

Permalink
Adding machine interface
Browse files Browse the repository at this point in the history
This adds a machine interface that can be used to mock operations of
machine. This interface is subject to change as it reflects the Machine
structure.

Signed-off-by: xibz <[email protected]>
  • Loading branch information
xibz committed Apr 4, 2019
1 parent b704cc3 commit c8a9227
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 14 deletions.
13 changes: 13 additions & 0 deletions client_transports.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Copyright 2018-2019 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 firecracker

import (
Expand Down
13 changes: 13 additions & 0 deletions client_transports_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Copyright 2018-2019 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 firecracker

import (
Expand Down
8 changes: 7 additions & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ func NewSetMetadataHandler(metadata interface{}) Handler {
return Handler{
Name: SetMetadataHandlerName,
Fn: func(ctx context.Context, m *Machine) error {
return m.SetMetadata(ctx, metadata)
if _, err := m.client.PutMmds(ctx, metadata); err != nil {
m.logger.Errorf("Setting metadata: %s", err)
return err
}

m.logger.Printf("SetMetadata successful")
return nil
},
}
}
Expand Down
14 changes: 2 additions & 12 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,22 +588,12 @@ func (m *Machine) sendCtrlAltDel(ctx context.Context) error {
return err
}

// EnableMetadata will append or replace the metadata handler.
// EnableMetadata will append or replace the metadata handler and only will be
// called when calling the Machine Start operation.
func (m *Machine) EnableMetadata(metadata interface{}) {
m.Handlers.FcInit = m.Handlers.FcInit.Swappend(NewSetMetadataHandler(metadata))
}

// SetMetadata sets the machine's metadata for MDDS
func (m *Machine) SetMetadata(ctx context.Context, metadata interface{}) error {
if _, err := m.client.PutMmds(ctx, metadata); err != nil {
m.logger.Errorf("Setting metadata: %s", err)
return err
}

m.logger.Printf("SetMetadata successful")
return nil
}

// UpdateGuestDrive will modify the current guest drive of ID index with the new
// parameters of the partialDrive.
func (m *Machine) UpdateGuestDrive(ctx context.Context, driveID, pathOnHost string, opts ...PatchGuestDriveByIDOpt) error {
Expand Down
2 changes: 1 addition & 1 deletion machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ func TestWaitForSocket(t *testing.T) {

func testSetMetadata(ctx context.Context, t *testing.T, m *Machine) {
metadata := map[string]string{"key": "value"}
err := m.SetMetadata(ctx, metadata)
_, err := m.client.PutMmds(ctx, metadata)
if err != nil {
t.Errorf("failed to set metadata: %s", err)
}
Expand Down
29 changes: 29 additions & 0 deletions machineiface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2018-2019 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 firecracker

import (
"context"
)

// MachineIface can be used for mocking and testing of the Machine. The Machine
// is subject to change, meaning this interface would change.
type MachineIface interface {
Start(context.Context) error
StopVMM() error
Shutdown(context.Context) error
Wait(context.Context) error
EnableMetadata(interface{})
UpdateGuestDrive(context.Context, string, string, ...PatchGuestDriveByIDOpt) error
}
13 changes: 13 additions & 0 deletions rate_limiter.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Copyright 2018-2019 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 firecracker

import (
Expand Down
13 changes: 13 additions & 0 deletions rate_limiter_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Copyright 2018-2019 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 firecracker_test

import (
Expand Down

0 comments on commit c8a9227

Please sign in to comment.