forked from GoogleCloudPlatform/guest-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test fixtures (GoogleCloudPlatform#100)
* add test fixtures * PR feedback
- Loading branch information
Showing
3 changed files
with
500 additions
and
11 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,122 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License 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 imagetest | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/GoogleCloudPlatform/compute-image-tools/daisy" | ||
) | ||
|
||
const ( | ||
createVMsStepName = "create-vms" | ||
createDisksStepName = "create-disks" | ||
successMatch = "FINISHED-TEST" | ||
) | ||
|
||
// TestVM is a test VM. | ||
type TestVM struct { | ||
name string | ||
testWorkflow *TestWorkflow | ||
instance *daisy.Instance | ||
} | ||
|
||
// AddMetadata adds the specified key:value pair to metadata during VM creation. | ||
func (t *TestVM) AddMetadata(key, value string) { | ||
createVMStep := t.testWorkflow.wf.Steps[createVMsStepName] | ||
for _, vm := range createVMStep.CreateInstances.Instances { | ||
if vm.Name == t.name { | ||
if vm.Metadata == nil { | ||
vm.Metadata = make(map[string]string) | ||
} | ||
vm.Metadata[key] = value | ||
return | ||
} | ||
} | ||
} | ||
|
||
// RunTests runs only the named tests on the testVM. | ||
// | ||
// From go help test: | ||
// -run regexp | ||
// Run only those tests and examples matching the regular expression. | ||
// For tests, the regular expression is split by unbracketed slash (/) | ||
// characters into a sequence of regular expressions, and each part | ||
// of a test's identifier must match the corresponding element in | ||
// the sequence, if any. Note that possible parents of matches are | ||
// run too, so that -run=X/Y matches and runs and reports the result | ||
// of all tests matching X, even those without sub-tests matching Y, | ||
// because it must run them to look for those sub-tests. | ||
func (t *TestVM) RunTests(runtest string) { | ||
t.AddMetadata("_test_run", runtest) | ||
} | ||
|
||
// SetShutdownScript sets the `shutdown-script` metadata key for a VM. | ||
func (t *TestVM) SetShutdownScript(script string) { | ||
t.AddMetadata("shutdown-script", script) | ||
} | ||
|
||
// SetStartupScript sets the `startup-script` metadata key for a VM. | ||
func (t *TestVM) SetStartupScript(script string) { | ||
t.AddMetadata("startup-script", script) | ||
} | ||
|
||
// Reboot stops the VM, waits for it to shutdown, then starts it again. Your | ||
// test package must handle being run twice. | ||
func (t *TestVM) Reboot() error { | ||
// Grab the wait step that was added with CreateTestVM. | ||
waitStep, ok := t.testWorkflow.wf.Steps["wait-"+t.name] | ||
if !ok { | ||
return fmt.Errorf("wait-%s step missing", t.name) | ||
} | ||
|
||
stopInstancesStep, err := t.testWorkflow.addStopStep(t.name, t.name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := t.testWorkflow.wf.AddDependency(stopInstancesStep, waitStep); err != nil { | ||
return err | ||
} | ||
|
||
waitStopStep, err := t.testWorkflow.addWaitStep("stopped-"+t.name, t.name, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := t.testWorkflow.wf.AddDependency(waitStopStep, stopInstancesStep); err != nil { | ||
return err | ||
} | ||
|
||
startInstancesStep, err := t.testWorkflow.addStartStep(t.name, t.name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := t.testWorkflow.wf.AddDependency(startInstancesStep, waitStopStep); err != nil { | ||
return err | ||
} | ||
|
||
waitStartedStep, err := t.testWorkflow.addWaitStep("started-"+t.name, t.name, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := t.testWorkflow.wf.AddDependency(waitStartedStep, startInstancesStep); err != nil { | ||
return err | ||
} | ||
|
||
return 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
Oops, something went wrong.