From c8a92272906ad09e5100d7b86289d20d5f67b158 Mon Sep 17 00:00:00 2001 From: xibz Date: Thu, 4 Apr 2019 11:53:31 -0700 Subject: [PATCH] Adding machine interface 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 --- client_transports.go | 13 +++++++++++++ client_transports_test.go | 13 +++++++++++++ handlers.go | 8 +++++++- machine.go | 14 ++------------ machine_test.go | 2 +- machineiface.go | 29 +++++++++++++++++++++++++++++ rate_limiter.go | 13 +++++++++++++ rate_limiter_test.go | 13 +++++++++++++ 8 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 machineiface.go diff --git a/client_transports.go b/client_transports.go index 37f194b8..5d70f074 100644 --- a/client_transports.go +++ b/client_transports.go @@ -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 ( diff --git a/client_transports_test.go b/client_transports_test.go index 3f76c27c..234cf8b0 100644 --- a/client_transports_test.go +++ b/client_transports_test.go @@ -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 ( diff --git a/handlers.go b/handlers.go index 17f5bba7..7706abc7 100644 --- a/handlers.go +++ b/handlers.go @@ -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 }, } } diff --git a/machine.go b/machine.go index d9c15c60..ba50b6d9 100644 --- a/machine.go +++ b/machine.go @@ -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 { diff --git a/machine_test.go b/machine_test.go index f52c0d51..e3c30fd8 100644 --- a/machine_test.go +++ b/machine_test.go @@ -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) } diff --git a/machineiface.go b/machineiface.go new file mode 100644 index 00000000..c5526aec --- /dev/null +++ b/machineiface.go @@ -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 +} diff --git a/rate_limiter.go b/rate_limiter.go index f61d457a..bf3537c1 100644 --- a/rate_limiter.go +++ b/rate_limiter.go @@ -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 ( diff --git a/rate_limiter_test.go b/rate_limiter_test.go index ef24b780..1afe07d8 100644 --- a/rate_limiter_test.go +++ b/rate_limiter_test.go @@ -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 (